BVB Source Codes

prettier Show util.js Source code

Return Download prettier: download util.js Source code - Download prettier Source code - Type:.js
  1. "use strict";
  2.  
  3. function isExportDeclaration(node) {
  4.   if (node) {
  5.     switch (node.type) {
  6.       case "ExportDeclaration":
  7.       case "ExportDefaultDeclaration":
  8.       case "ExportDefaultSpecifier":
  9.       case "DeclareExportDeclaration":
  10.       case "ExportNamedDeclaration":
  11.       case "ExportAllDeclaration":
  12.         return true;
  13.     }
  14.   }
  15.  
  16.   return false;
  17. }
  18.  
  19. function getParentExportDeclaration(path) {
  20.   const parentNode = path.getParentNode();
  21.   if (path.getName() === "declaration" && isExportDeclaration(parentNode)) {
  22.     return parentNode;
  23.   }
  24.  
  25.   return null;
  26. }
  27.  
  28. function getPenultimate(arr) {
  29.   if (arr.length > 1) {
  30.     return arr[arr.length - 2];
  31.   }
  32.   return null;
  33. }
  34.  
  35. function getLast(arr) {
  36.   if (arr.length > 0) {
  37.     return arr[arr.length - 1];
  38.   }
  39.   return null;
  40. }
  41.  
  42. function skip(chars) {
  43.   return (text, index, opts) => {
  44.     const backwards = opts && opts.backwards;
  45.  
  46.     // Allow `skip` functions to be threaded together without having
  47.     // to check for failures (did someone say monads?).
  48.     if (index === false) {
  49.       return false;
  50.     }
  51.  
  52.     const length = text.length;
  53.     let cursor = index;
  54.     while (cursor >= 0 && cursor < length) {
  55.       const c = text.charAt(cursor);
  56.       if (chars instanceof RegExp) {
  57.         if (!chars.test(c)) {
  58.           return cursor;
  59.         }
  60.       } else if (chars.indexOf(c) === -1) {
  61.         return cursor;
  62.       }
  63.  
  64.       backwards ? cursor-- : cursor++;
  65.     }
  66.  
  67.     if (cursor === -1 || cursor === length) {
  68.       // If we reached the beginning or end of the file, return the
  69.       // out-of-bounds cursor. It's up to the caller to handle this
  70.       // correctly. We don't want to indicate `false` though if it
  71.       // actually skipped valid characters.
  72.       return cursor;
  73.     }
  74.     return false;
  75.   };
  76. }
  77.  
  78. const skipWhitespace = skip(/\s/);
  79. const skipSpaces = skip(" \t");
  80. const skipToLineEnd = skip(",; \t");
  81. const skipEverythingButNewLine = skip(/[^\r\n]/);
  82.  
  83. function skipInlineComment(text, index) {
  84.   if (index === false) {
  85.     return false;
  86.   }
  87.  
  88.   if (text.charAt(index) === "/" && text.charAt(index + 1) === "*") {
  89.     for (let i = index + 2; i < text.length; ++i) {
  90.       if (text.charAt(i) === "*" && text.charAt(i + 1) === "/") {
  91.         return i + 2;
  92.       }
  93.     }
  94.   }
  95.   return index;
  96. }
  97.  
  98. function skipTrailingComment(text, index) {
  99.   if (index === false) {
  100.     return false;
  101.   }
  102.  
  103.   if (text.charAt(index) === "/" && text.charAt(index + 1) === "/") {
  104.     return skipEverythingButNewLine(text, index);
  105.   }
  106.   return index;
  107. }
  108.  
  109. // This one doesn't use the above helper function because it wants to
  110. // test \r\n in order and `skip` doesn't support ordering and we only
  111. // want to skip one newline. It's simple to implement.
  112. function skipNewline(text, index, opts) {
  113.   const backwards = opts && opts.backwards;
  114.   if (index === false) {
  115.     return false;
  116.   }
  117.  
  118.   const atIndex = text.charAt(index);
  119.   if (backwards) {
  120.     if (text.charAt(index - 1) === "\r" && atIndex === "\n") {
  121.       return index - 2;
  122.     }
  123.     if (
  124.       atIndex === "\n" ||
  125.       atIndex === "\r" ||
  126.       atIndex === "\u2028" ||
  127.       atIndex === "\u2029"
  128.     ) {
  129.       return index - 1;
  130.     }
  131.   } else {
  132.     if (atIndex === "\r" && text.charAt(index + 1) === "\n") {
  133.       return index + 2;
  134.     }
  135.     if (
  136.       atIndex === "\n" ||
  137.       atIndex === "\r" ||
  138.       atIndex === "\u2028" ||
  139.       atIndex === "\u2029"
  140.     ) {
  141.       return index + 1;
  142.     }
  143.   }
  144.  
  145.   return index;
  146. }
  147.  
  148. function hasNewline(text, index, opts) {
  149.   opts = opts || {};
  150.   const idx = skipSpaces(text, opts.backwards ? index - 1 : index, opts);
  151.   const idx2 = skipNewline(text, idx, opts);
  152.   return idx !== idx2;
  153. }
  154.  
  155. function hasNewlineInRange(text, start, end) {
  156.   for (let i = start; i < end; ++i) {
  157.     if (text.charAt(i) === "\n") {
  158.       return true;
  159.     }
  160.   }
  161.   return false;
  162. }
  163.  
  164. // Note: this function doesn't ignore leading comments unlike isNextLineEmpty
  165. function isPreviousLineEmpty(text, node) {
  166.   let idx = locStart(node) - 1;
  167.   idx = skipSpaces(text, idx, { backwards: true });
  168.   idx = skipNewline(text, idx, { backwards: true });
  169.   idx = skipSpaces(text, idx, { backwards: true });
  170.   const idx2 = skipNewline(text, idx, { backwards: true });
  171.   return idx !== idx2;
  172. }
  173.  
  174. function isNextLineEmpty(text, node) {
  175.   let oldIdx = null;
  176.   let idx = locEnd(node);
  177.   while (idx !== oldIdx) {
  178.     // We need to skip all the potential trailing inline comments
  179.     oldIdx = idx;
  180.     idx = skipToLineEnd(text, idx);
  181.     idx = skipInlineComment(text, idx);
  182.     idx = skipSpaces(text, idx);
  183.   }
  184.   idx = skipTrailingComment(text, idx);
  185.   idx = skipNewline(text, idx);
  186.   return hasNewline(text, idx);
  187. }
  188.  
  189. function getNextNonSpaceNonCommentCharacter(text, node) {
  190.   let oldIdx = null;
  191.   let idx = locEnd(node);
  192.   while (idx !== oldIdx) {
  193.     oldIdx = idx;
  194.     idx = skipSpaces(text, idx);
  195.     idx = skipInlineComment(text, idx);
  196.     idx = skipTrailingComment(text, idx);
  197.     idx = skipNewline(text, idx);
  198.   }
  199.   return text.charAt(idx);
  200. }
  201.  
  202. function hasSpaces(text, index, opts) {
  203.   opts = opts || {};
  204.   const idx = skipSpaces(text, opts.backwards ? index - 1 : index, opts);
  205.   return idx !== index;
  206. }
  207.  
  208. function locStart(node) {
  209.   if (node.decorators && node.decorators.length > 0) {
  210.     return locStart(node.decorators[0]);
  211.   }
  212.   if (node.range) {
  213.     return node.range[0];
  214.   }
  215.   if (typeof node.start === "number") {
  216.     return node.start;
  217.   }
  218.   if (node.source) {
  219.     return lineColumnToIndex(node.source.start, node.source.input.css) - 1;
  220.   }
  221. }
  222.  
  223. function locEnd(node) {
  224.   if (node.range) {
  225.     return node.range[1];
  226.   }
  227.   if (typeof node.end === "number") {
  228.     return node.end;
  229.   }
  230.   if (node.source) {
  231.     return lineColumnToIndex(node.source.end, node.source.input.css);
  232.   }
  233. }
  234.  
  235. // Super inefficient, needs to be cached.
  236. function lineColumnToIndex(lineColumn, text) {
  237.   let index = 0;
  238.   for (let i = 0; i < lineColumn.line - 1; ++i) {
  239.     index = text.indexOf("\n", index) + 1;
  240.     if (index === -1) {
  241.       return -1;
  242.     }
  243.   }
  244.   return index + lineColumn.column;
  245. }
  246.  
  247. function setLocStart(node, index) {
  248.   if (node.range) {
  249.     node.range[0] = index;
  250.   } else {
  251.     node.start = index;
  252.   }
  253. }
  254.  
  255. function setLocEnd(node, index) {
  256.   if (node.range) {
  257.     node.range[1] = index;
  258.   } else {
  259.     node.end = index;
  260.   }
  261. }
  262.  
  263. const PRECEDENCE = {};
  264. [
  265.   ["||"],
  266.   ["&&"],
  267.   ["|"],
  268.   ["^"],
  269.   ["&"],
  270.   ["==", "===", "!=", "!=="],
  271.   ["<", ">", "<=", ">=", "in", "instanceof"],
  272.   [">>", "<<", ">>>"],
  273.   ["+", "-"],
  274.   ["*", "/", "%"],
  275.   ["**"]
  276. ].forEach((tier, i) => {
  277.   tier.forEach(op => {
  278.     PRECEDENCE[op] = i;
  279.   });
  280. });
  281.  
  282. function getPrecedence(op) {
  283.   return PRECEDENCE[op];
  284. }
  285.  
  286. // Tests if an expression starts with `{`, or (if forbidFunctionAndClass holds) `function` or `class`.
  287. // Will be overzealous if there's already necessary grouping parentheses.
  288. function startsWithNoLookaheadToken(node, forbidFunctionAndClass) {
  289.   node = getLeftMost(node);
  290.   switch (node.type) {
  291.     case "FunctionExpression":
  292.     case "ClassExpression":
  293.       return forbidFunctionAndClass;
  294.     case "ObjectExpression":
  295.       return true;
  296.     case "MemberExpression":
  297.       return startsWithNoLookaheadToken(node.object, forbidFunctionAndClass);
  298.     case "TaggedTemplateExpression":
  299.       if (node.tag.type === "FunctionExpression") {
  300.         // IIFEs are always already parenthesized
  301.         return false;
  302.       }
  303.       return startsWithNoLookaheadToken(node.tag, forbidFunctionAndClass);
  304.     case "CallExpression":
  305.       if (node.callee.type === "FunctionExpression") {
  306.         // IIFEs are always already parenthesized
  307.         return false;
  308.       }
  309.       return startsWithNoLookaheadToken(node.callee, forbidFunctionAndClass);
  310.     case "ConditionalExpression":
  311.       return startsWithNoLookaheadToken(node.test, forbidFunctionAndClass);
  312.     case "UpdateExpression":
  313.       return (
  314.         !node.prefix &&
  315.         startsWithNoLookaheadToken(node.argument, forbidFunctionAndClass)
  316.       );
  317.     case "BindExpression":
  318.       return (
  319.         node.object &&
  320.         startsWithNoLookaheadToken(node.object, forbidFunctionAndClass)
  321.       );
  322.     case "SequenceExpression":
  323.       return startsWithNoLookaheadToken(
  324.         node.expressions[0],
  325.         forbidFunctionAndClass
  326.       );
  327.     case "TSAsExpression":
  328.       return startsWithNoLookaheadToken(
  329.         node.expression,
  330.         forbidFunctionAndClass
  331.       );
  332.     default:
  333.       return false;
  334.   }
  335. }
  336.  
  337. function getLeftMost(node) {
  338.   if (node.left) {
  339.     return getLeftMost(node.left);
  340.   } else {
  341.     return node;
  342.   }
  343. }
  344.  
  345. function hasBlockComments(node) {
  346.   return node.comments && node.comments.some(isBlockComment);
  347. }
  348.  
  349. function isBlockComment(comment) {
  350.   return comment.type === "Block" || comment.type === "CommentBlock";
  351. }
  352.  
  353. function getAlignmentSize(value, tabWidth, startIndex) {
  354.   startIndex = startIndex || 0;
  355.  
  356.   let size = 0;
  357.   for (let i = startIndex; i < value.length; ++i) {
  358.     if (value[i] === "\t") {
  359.       // Tabs behave in a way that they are aligned to the nearest
  360.       // multiple of tabWidth:
  361.       // 0 -> 4, 1 -> 4, 2 -> 4, 3 -> 4
  362.       // 4 -> 8, 5 -> 8, 6 -> 8, 7 -> 8 ...
  363.       size = size + tabWidth - size % tabWidth;
  364.     } else {
  365.       size++;
  366.     }
  367.   }
  368.  
  369.   return size;
  370. }
  371.  
  372. module.exports = {
  373.   getPrecedence,
  374.   isExportDeclaration,
  375.   getParentExportDeclaration,
  376.   getPenultimate,
  377.   getLast,
  378.   getNextNonSpaceNonCommentCharacter,
  379.   skipWhitespace,
  380.   skipSpaces,
  381.   skipNewline,
  382.   isNextLineEmpty,
  383.   isPreviousLineEmpty,
  384.   hasNewline,
  385.   hasNewlineInRange,
  386.   hasSpaces,
  387.   locStart,
  388.   locEnd,
  389.   setLocStart,
  390.   setLocEnd,
  391.   startsWithNoLookaheadToken,
  392.   hasBlockComments,
  393.   isBlockComment,
  394.   getAlignmentSize
  395. };
  396.  
downloadutil.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