BVB Source Codes

prettier Show clean-ast.js Source code

Return Download prettier: download clean-ast.js Source code - Download prettier Source code - Type:.js
  1. "use strict";
  2.  
  3. function cleanAST(ast) {
  4.   return JSON.stringify(massageAST(ast), null, 2);
  5. }
  6.  
  7. function massageAST(ast) {
  8.   if (Array.isArray(ast)) {
  9.     return ast.map(e => massageAST(e)).filter(e => e);
  10.   }
  11.   if (ast && typeof ast === "object") {
  12.     // We remove extra `;` and add them when needed
  13.     if (ast.type === "EmptyStatement") {
  14.       return undefined;
  15.     }
  16.  
  17.     // We move text around, including whitespaces and add {" "}
  18.     if (ast.type === "JSXText") {
  19.       return undefined;
  20.     }
  21.     if (
  22.       ast.type === "JSXExpressionContainer" &&
  23.       ast.expression.type === "Literal" &&
  24.       ast.expression.value === " "
  25.     ) {
  26.       return undefined;
  27.     }
  28.  
  29.     const newObj = {};
  30.     for (const key in ast) {
  31.       if (typeof ast[key] !== "function") {
  32.         newObj[key] = massageAST(ast[key]);
  33.       }
  34.     }
  35.  
  36.     [
  37.       "loc",
  38.       "range",
  39.       "raw",
  40.       "comments",
  41.       "leadingComments",
  42.       "trailingComments",
  43.       "extra",
  44.       "start",
  45.       "end",
  46.       "tokens",
  47.       "flags",
  48.       "raws",
  49.       "sourceIndex",
  50.       "id",
  51.       "source",
  52.       "before",
  53.       "after",
  54.       "trailingComma"
  55.     ].forEach(name => {
  56.       delete newObj[name];
  57.     });
  58.  
  59.     if (
  60.       ast.type === "media-query" ||
  61.       ast.type === "media-query-list" ||
  62.       ast.type === "media-feature-expression"
  63.     ) {
  64.       delete newObj.value;
  65.     }
  66.  
  67.     if (ast.type === "css-rule") {
  68.       delete newObj.params;
  69.     }
  70.  
  71.     if (ast.type === "media-feature") {
  72.       newObj.value = newObj.value.replace(/ /g, "");
  73.     }
  74.  
  75.     // (TypeScript) Ignore `static` in `constructor(static p) {}`
  76.     // and `export` in `constructor(export p) {}`
  77.     if (
  78.       ast.type === "TSParameterProperty" &&
  79.       ast.accessibility === null &&
  80.       !ast.readonly
  81.     ) {
  82.       return {
  83.         type: "Identifier",
  84.         name: ast.parameter.name,
  85.         typeAnnotation: newObj.parameter.typeAnnotation,
  86.         decorators: newObj.decorators
  87.       };
  88.     }
  89.  
  90.     // (TypeScript) ignore empty `specifiers` array
  91.     if (
  92.       ast.type === "TSNamespaceExportDeclaration" &&
  93.       ast.specifiers &&
  94.       ast.specifiers.length === 0
  95.     ) {
  96.       delete newObj.specifiers;
  97.     }
  98.  
  99.     // (TypeScript) allow parenthesization of TSFunctionType
  100.     if (
  101.       ast.type === "TSParenthesizedType" &&
  102.       ast.typeAnnotation.type === "TypeAnnotation" &&
  103.       ast.typeAnnotation.typeAnnotation.type === "TSFunctionType"
  104.     ) {
  105.       return newObj.typeAnnotation.typeAnnotation;
  106.     }
  107.  
  108.     // We convert <div></div> to <div />
  109.     if (ast.type === "JSXOpeningElement") {
  110.       delete newObj.selfClosing;
  111.     }
  112.     if (ast.type === "JSXElement") {
  113.       delete newObj.closingElement;
  114.     }
  115.  
  116.     // We change {'key': value} into {key: value}
  117.     if (
  118.       (ast.type === "Property" ||
  119.         ast.type === "MethodDefinition" ||
  120.         ast.type === "ClassProperty") &&
  121.       typeof ast.key === "object" &&
  122.       ast.key &&
  123.       (ast.key.type === "Literal" || ast.key.type === "Identifier")
  124.     ) {
  125.       delete newObj.key;
  126.     }
  127.  
  128.     return newObj;
  129.   }
  130.   return ast;
  131. }
  132.  
  133. module.exports = { cleanAST, massageAST };
  134.  
downloadclean-ast.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