BVB Source Codes

prettier Show doc-builders.js Source code

Return Download prettier: download doc-builders.js Source code - Download prettier Source code - Type:.js
  1. "use strict";
  2.  
  3. function assertDoc(val) {
  4.   if (
  5.     !(typeof val === "string" || (val != null && typeof val.type === "string"))
  6.   ) {
  7.     throw new Error(
  8.       "Value " + JSON.stringify(val) + " is not a valid document"
  9.     );
  10.   }
  11. }
  12.  
  13. function concat(parts) {
  14.   parts.forEach(assertDoc);
  15.  
  16.   // We cannot do this until we change `printJSXElement` to not
  17.   // access the internals of a document directly.
  18.   // if(parts.length === 1) {
  19.   //   // If it's a single document, no need to concat it.
  20.   //   return parts[0];
  21.   // }
  22.   return { type: "concat", parts };
  23. }
  24.  
  25. function indent(contents) {
  26.   assertDoc(contents);
  27.  
  28.   return { type: "indent", contents };
  29. }
  30.  
  31. function align(n, contents) {
  32.   assertDoc(contents);
  33.  
  34.   return { type: "align", contents, n };
  35. }
  36.  
  37. function group(contents, opts) {
  38.   opts = opts || {};
  39.  
  40.   assertDoc(contents);
  41.  
  42.   return {
  43.     type: "group",
  44.     contents: contents,
  45.     break: !!opts.shouldBreak,
  46.     expandedStates: opts.expandedStates
  47.   };
  48. }
  49.  
  50. function conditionalGroup(states, opts) {
  51.   return group(
  52.     states[0],
  53.     Object.assign(opts || {}, { expandedStates: states })
  54.   );
  55. }
  56.  
  57. function fill(parts) {
  58.   parts.forEach(assertDoc);
  59.  
  60.   return { type: "fill", parts };
  61. }
  62.  
  63. function ifBreak(breakContents, flatContents) {
  64.   if (breakContents) {
  65.     assertDoc(breakContents);
  66.   }
  67.   if (flatContents) {
  68.     assertDoc(flatContents);
  69.   }
  70.  
  71.   return { type: "if-break", breakContents, flatContents };
  72. }
  73.  
  74. function lineSuffix(contents) {
  75.   assertDoc(contents);
  76.   return { type: "line-suffix", contents };
  77. }
  78.  
  79. const lineSuffixBoundary = { type: "line-suffix-boundary" };
  80. const breakParent = { type: "break-parent" };
  81. const line = { type: "line" };
  82. const softline = { type: "line", soft: true };
  83. const hardline = concat([{ type: "line", hard: true }, breakParent]);
  84. const literalline = concat([
  85.   { type: "line", hard: true, literal: true },
  86.   breakParent
  87. ]);
  88. const cursor = { type: "cursor", placeholder: Symbol() };
  89.  
  90. function join(sep, arr) {
  91.   const res = [];
  92.  
  93.   for (let i = 0; i < arr.length; i++) {
  94.     if (i !== 0) {
  95.       res.push(sep);
  96.     }
  97.  
  98.     res.push(arr[i]);
  99.   }
  100.  
  101.   return concat(res);
  102. }
  103.  
  104. function addAlignmentToDoc(doc, size, tabWidth) {
  105.   let aligned = doc;
  106.   if (size > 0) {
  107.     // Use indent to add tabs for all the levels of tabs we need
  108.     for (let i = 0; i < Math.floor(size / tabWidth); ++i) {
  109.       aligned = indent(aligned);
  110.     }
  111.     // Use align for all the spaces that are needed
  112.     aligned = align(size % tabWidth, aligned);
  113.     // size is absolute from 0 and not relative to the current
  114.     // indentation, so we use -Infinity to reset the indentation to 0
  115.     aligned = align(-Infinity, aligned);
  116.   }
  117.   return aligned;
  118. }
  119.  
  120. module.exports = {
  121.   concat,
  122.   join,
  123.   line,
  124.   softline,
  125.   hardline,
  126.   literalline,
  127.   group,
  128.   conditionalGroup,
  129.   fill,
  130.   lineSuffix,
  131.   lineSuffixBoundary,
  132.   cursor,
  133.   breakParent,
  134.   ifBreak,
  135.   indent,
  136.   align,
  137.   addAlignmentToDoc
  138. };
  139.  
downloaddoc-builders.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