BVB Source Codes

prettier Show doc-utils.js Source code

Return Download prettier: download doc-utils.js Source code - Download prettier Source code - Type:.js
  1. "use strict";
  2.  
  3. function traverseDoc(doc, onEnter, onExit, shouldTraverseConditionalGroups) {
  4.   function traverseDocRec(doc) {
  5.     let shouldRecurse = true;
  6.     if (onEnter) {
  7.       if (onEnter(doc) === false) {
  8.         shouldRecurse = false;
  9.       }
  10.     }
  11.  
  12.     if (shouldRecurse) {
  13.       if (doc.type === "concat" || doc.type === "fill") {
  14.         for (let i = 0; i < doc.parts.length; i++) {
  15.           traverseDocRec(doc.parts[i]);
  16.         }
  17.       } else if (doc.type === "if-break") {
  18.         if (doc.breakContents) {
  19.           traverseDocRec(doc.breakContents);
  20.         }
  21.         if (doc.flatContents) {
  22.           traverseDocRec(doc.flatContents);
  23.         }
  24.       } else if (doc.type === "group" && doc.expandedStates) {
  25.         if (shouldTraverseConditionalGroups) {
  26.           doc.expandedStates.forEach(traverseDocRec);
  27.         } else {
  28.           traverseDocRec(doc.contents);
  29.         }
  30.       } else if (doc.contents) {
  31.         traverseDocRec(doc.contents);
  32.       }
  33.     }
  34.  
  35.     if (onExit) {
  36.       onExit(doc);
  37.     }
  38.   }
  39.  
  40.   traverseDocRec(doc);
  41. }
  42.  
  43. function mapDoc(doc, func) {
  44.   doc = func(doc);
  45.  
  46.   if (doc.type === "concat" || doc.type === "fill") {
  47.     return Object.assign({}, doc, {
  48.       parts: doc.parts.map(d => mapDoc(d, func))
  49.     });
  50.   } else if (doc.type === "if-break") {
  51.     return Object.assign({}, doc, {
  52.       breakContents: doc.breakContents && mapDoc(doc.breakContents, func),
  53.       flatContents: doc.flatContents && mapDoc(doc.flatContents, func)
  54.     });
  55.   } else if (doc.contents) {
  56.     return Object.assign({}, doc, { contents: mapDoc(doc.contents, func) });
  57.   } else {
  58.     return doc;
  59.   }
  60. }
  61.  
  62. function findInDoc(doc, fn, defaultValue) {
  63.   let result = defaultValue;
  64.   let hasStopped = false;
  65.   traverseDoc(doc, doc => {
  66.     const maybeResult = fn(doc);
  67.     if (maybeResult !== undefined) {
  68.       hasStopped = true;
  69.       result = maybeResult;
  70.     }
  71.     if (hasStopped) {
  72.       return false;
  73.     }
  74.   });
  75.   return result;
  76. }
  77.  
  78. function isEmpty(n) {
  79.   return typeof n === "string" && n.length === 0;
  80. }
  81.  
  82. function isLineNext(doc) {
  83.   return findInDoc(
  84.     doc,
  85.     doc => {
  86.       if (typeof doc === "string") {
  87.         return false;
  88.       }
  89.       if (doc.type === "line") {
  90.         return true;
  91.       }
  92.     },
  93.     false
  94.   );
  95. }
  96.  
  97. function willBreak(doc) {
  98.   return findInDoc(
  99.     doc,
  100.     doc => {
  101.       if (doc.type === "group" && doc.break) {
  102.         return true;
  103.       }
  104.       if (doc.type === "line" && doc.hard) {
  105.         return true;
  106.       }
  107.       if (doc.type === "break-parent") {
  108.         return true;
  109.       }
  110.     },
  111.     false
  112.   );
  113. }
  114.  
  115. function breakParentGroup(groupStack) {
  116.   if (groupStack.length > 0) {
  117.     const parentGroup = groupStack[groupStack.length - 1];
  118.     // Breaks are not propagated through conditional groups because
  119.     // the user is expected to manually handle what breaks.
  120.     if (!parentGroup.expandedStates) {
  121.       parentGroup.break = true;
  122.     }
  123.   }
  124.   return null;
  125. }
  126.  
  127. function propagateBreaks(doc) {
  128.   const alreadyVisited = new Map();
  129.   const groupStack = [];
  130.   traverseDoc(
  131.     doc,
  132.     doc => {
  133.       if (doc.type === "break-parent") {
  134.         breakParentGroup(groupStack);
  135.       }
  136.       if (doc.type === "group") {
  137.         groupStack.push(doc);
  138.         if (alreadyVisited.has(doc)) {
  139.           return false;
  140.         }
  141.         alreadyVisited.set(doc, true);
  142.       }
  143.     },
  144.     doc => {
  145.       if (doc.type === "group") {
  146.         const group = groupStack.pop();
  147.         if (group.break) {
  148.           breakParentGroup(groupStack);
  149.         }
  150.       }
  151.     },
  152.     /* shouldTraverseConditionalGroups */ true
  153.   );
  154. }
  155.  
  156. module.exports = {
  157.   isEmpty,
  158.   willBreak,
  159.   isLineNext,
  160.   traverseDoc,
  161.   mapDoc,
  162.   propagateBreaks
  163. };
  164.  
downloaddoc-utils.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