BVB Source Codes

prettier Show parser-postcss.js Source code

Return Download prettier: download parser-postcss.js Source code - Download prettier Source code - Type:.js
  1. "use strict";
  2.  
  3. const createError = require("./parser-create-error");
  4.  
  5. function parseSelector(selector) {
  6.   const selectorParser = require("postcss-selector-parser");
  7.   let result;
  8.   selectorParser(result_ => {
  9.     result = result_;
  10.   }).process(selector);
  11.   return addTypePrefix(result, "selector-");
  12. }
  13.  
  14. function parseValueNodes(nodes) {
  15.   let parenGroup = {
  16.     open: null,
  17.     close: null,
  18.     groups: [],
  19.     type: "paren_group"
  20.   };
  21.   const parenGroupStack = [parenGroup];
  22.   const rootParenGroup = parenGroup;
  23.   let commaGroup = {
  24.     groups: [],
  25.     type: "comma_group"
  26.   };
  27.   const commaGroupStack = [commaGroup];
  28.  
  29.   for (let i = 0; i < nodes.length; ++i) {
  30.     const node = nodes[i];
  31.     if (node.type === "paren" && node.value === "(") {
  32.       parenGroup = {
  33.         open: node,
  34.         close: null,
  35.         groups: [],
  36.         type: "paren_group"
  37.       };
  38.       parenGroupStack.push(parenGroup);
  39.  
  40.       commaGroup = {
  41.         groups: [],
  42.         type: "comma_group"
  43.       };
  44.       commaGroupStack.push(commaGroup);
  45.     } else if (node.type === "paren" && node.value === ")") {
  46.       if (commaGroup.groups.length) {
  47.         parenGroup.groups.push(commaGroup);
  48.       }
  49.       parenGroup.close = node;
  50.  
  51.       if (commaGroupStack.length === 1) {
  52.         throw new Error("Unbalanced parenthesis");
  53.       }
  54.  
  55.       commaGroupStack.pop();
  56.       commaGroup = commaGroupStack[commaGroupStack.length - 1];
  57.       commaGroup.groups.push(parenGroup);
  58.  
  59.       parenGroupStack.pop();
  60.       parenGroup = parenGroupStack[parenGroupStack.length - 1];
  61.     } else if (node.type === "comma") {
  62.       parenGroup.groups.push(commaGroup);
  63.       commaGroup = {
  64.         groups: [],
  65.         type: "comma_group"
  66.       };
  67.       commaGroupStack[commaGroupStack.length - 1] = commaGroup;
  68.     } else {
  69.       commaGroup.groups.push(node);
  70.     }
  71.   }
  72.   if (commaGroup.groups.length > 0) {
  73.     parenGroup.groups.push(commaGroup);
  74.   }
  75.   return rootParenGroup;
  76. }
  77.  
  78. function flattenGroups(node) {
  79.   if (
  80.     node.type === "paren_group" &&
  81.     !node.open &&
  82.     !node.close &&
  83.     node.groups.length === 1
  84.   ) {
  85.     return flattenGroups(node.groups[0]);
  86.   }
  87.  
  88.   if (node.type === "comma_group" && node.groups.length === 1) {
  89.     return flattenGroups(node.groups[0]);
  90.   }
  91.  
  92.   if (node.type === "paren_group" || node.type === "comma_group") {
  93.     return Object.assign({}, node, { groups: node.groups.map(flattenGroups) });
  94.   }
  95.  
  96.   return node;
  97. }
  98.  
  99. function addTypePrefix(node, prefix) {
  100.   if (node && typeof node === "object") {
  101.     delete node.parent;
  102.     for (const key in node) {
  103.       addTypePrefix(node[key], prefix);
  104.       if (key === "type" && typeof node[key] === "string") {
  105.         if (!node[key].startsWith(prefix)) {
  106.           node[key] = prefix + node[key];
  107.         }
  108.       }
  109.     }
  110.   }
  111.   return node;
  112. }
  113.  
  114. function addMissingType(node) {
  115.   if (node && typeof node === "object") {
  116.     delete node.parent;
  117.     for (const key in node) {
  118.       addMissingType(node[key]);
  119.     }
  120.     if (!Array.isArray(node) && node.value && !node.type) {
  121.       node.type = "unknown";
  122.     }
  123.   }
  124.   return node;
  125. }
  126.  
  127. function parseNestedValue(node) {
  128.   if (node && typeof node === "object") {
  129.     delete node.parent;
  130.     for (const key in node) {
  131.       parseNestedValue(node[key]);
  132.       if (key === "nodes") {
  133.         node.group = flattenGroups(parseValueNodes(node[key]));
  134.         delete node[key];
  135.       }
  136.     }
  137.   }
  138.   return node;
  139. }
  140.  
  141. function parseValue(value) {
  142.   const valueParser = require("postcss-values-parser");
  143.   const result = valueParser(value, { loose: true }).parse();
  144.   const parsedResult = parseNestedValue(result);
  145.   return addTypePrefix(parsedResult, "value-");
  146. }
  147.  
  148. function parseMediaQuery(value) {
  149.   const mediaParser = require("postcss-media-query-parser").default;
  150.   const result = addMissingType(mediaParser(value));
  151.   return addTypePrefix(result, "media-");
  152. }
  153.  
  154. function parseNestedCSS(node) {
  155.   if (node && typeof node === "object") {
  156.     delete node.parent;
  157.     for (const key in node) {
  158.       parseNestedCSS(node[key]);
  159.     }
  160.     if (typeof node.selector === "string") {
  161.       const selector = node.raws.selector
  162.         ? node.raws.selector.raw
  163.         : node.selector;
  164.  
  165.       try {
  166.         node.selector = parseSelector(selector);
  167.       } catch (e) {
  168.         // Fail silently. It's better to print it as is than to try and parse it
  169.         node.selector = selector;
  170.       }
  171.     }
  172.     if (node.type && typeof node.value === "string") {
  173.       try {
  174.         node.value = parseValue(node.value);
  175.       } catch (e) {
  176.         throw createError(
  177.           "(postcss-values-parser) " + e.toString(),
  178.           node.source
  179.         );
  180.       }
  181.     }
  182.     if (node.type === "css-atrule" && typeof node.params === "string") {
  183.       node.params = parseMediaQuery(node.params);
  184.     }
  185.   }
  186.   return node;
  187. }
  188.  
  189. function parseWithParser(parser, text) {
  190.   let result;
  191.   try {
  192.     result = parser.parse(text);
  193.   } catch (e) {
  194.     if (typeof e.line !== "number") {
  195.       throw e;
  196.     }
  197.     throw createError("(postcss) " + e.name + " " + e.reason, { start: e });
  198.   }
  199.   const prefixedResult = addTypePrefix(result, "css-");
  200.   const parsedResult = parseNestedCSS(prefixedResult);
  201.   return parsedResult;
  202. }
  203.  
  204. function requireParser(isSCSS) {
  205.   if (isSCSS) {
  206.     return require("postcss-scss");
  207.   } else {
  208.     return require("postcss-less");
  209.   }
  210. }
  211.  
  212. function parse(text) {
  213.   const isLikelySCSS = !!text.match(/(\w\s*: [^}:]+|#){|\@import url/);
  214.   try {
  215.     return parseWithParser(requireParser(isLikelySCSS), text);
  216.   } catch (e) {
  217.     try {
  218.       return parseWithParser(requireParser(!isLikelySCSS), text);
  219.     } catch (e2) {
  220.       throw e;
  221.     }
  222.   }
  223. }
  224.  
  225. module.exports = parse;
  226.  
downloadparser-postcss.js Source code - Download prettier Source code
Related Source Codes/Software:
storybook - 2017-06-07
ionicons - The premium icon font for Ionic ... 2017-06-07
AsyncDisplayKit - Smooth asynchronous user interfaces for iOS apps. ... 2017-06-07
lottie-android - Render After Effects animations natively on Androi... 2017-06-07
parse-server - Parse-compatible API server module for Node/Expres... 2017-06-07
inferno - An extremely fast, React-like JavaScript library f... 2017-06-08
guetzli - Perceptual JPEG encoder 2017-06-08
cs-video-courses - List of Computer Science courses with video lectur... 2017-06-08
interviews - Everything you need to know to get the job. 2017-06-08
prepack - Prepack is a partial evaluator for JavaScript. Pre... 2017-06-08
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top