diff options
Diffstat (limited to 'node_modules/@babel/generator/lib/node')
| -rw-r--r-- | node_modules/@babel/generator/lib/node/index.js | 111 | ||||
| -rw-r--r-- | node_modules/@babel/generator/lib/node/parentheses.js | 342 | ||||
| -rw-r--r-- | node_modules/@babel/generator/lib/node/whitespace.js | 214 | 
3 files changed, 667 insertions, 0 deletions
diff --git a/node_modules/@babel/generator/lib/node/index.js b/node_modules/@babel/generator/lib/node/index.js new file mode 100644 index 0000000..b594ae4 --- /dev/null +++ b/node_modules/@babel/generator/lib/node/index.js @@ -0,0 +1,111 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { +  value: true +}); +exports.needsParens = needsParens; +exports.needsWhitespace = needsWhitespace; +exports.needsWhitespaceAfter = needsWhitespaceAfter; +exports.needsWhitespaceBefore = needsWhitespaceBefore; + +var whitespace = require("./whitespace"); + +var parens = require("./parentheses"); + +var _t = require("@babel/types"); + +const { +  FLIPPED_ALIAS_KEYS, +  isCallExpression, +  isExpressionStatement, +  isMemberExpression, +  isNewExpression +} = _t; + +function expandAliases(obj) { +  const newObj = {}; + +  function add(type, func) { +    const fn = newObj[type]; +    newObj[type] = fn ? function (node, parent, stack) { +      const result = fn(node, parent, stack); +      return result == null ? func(node, parent, stack) : result; +    } : func; +  } + +  for (const type of Object.keys(obj)) { +    const aliases = FLIPPED_ALIAS_KEYS[type]; + +    if (aliases) { +      for (const alias of aliases) { +        add(alias, obj[type]); +      } +    } else { +      add(type, obj[type]); +    } +  } + +  return newObj; +} + +const expandedParens = expandAliases(parens); +const expandedWhitespaceNodes = expandAliases(whitespace.nodes); +const expandedWhitespaceList = expandAliases(whitespace.list); + +function find(obj, node, parent, printStack) { +  const fn = obj[node.type]; +  return fn ? fn(node, parent, printStack) : null; +} + +function isOrHasCallExpression(node) { +  if (isCallExpression(node)) { +    return true; +  } + +  return isMemberExpression(node) && isOrHasCallExpression(node.object); +} + +function needsWhitespace(node, parent, type) { +  if (!node) return 0; + +  if (isExpressionStatement(node)) { +    node = node.expression; +  } + +  let linesInfo = find(expandedWhitespaceNodes, node, parent); + +  if (!linesInfo) { +    const items = find(expandedWhitespaceList, node, parent); + +    if (items) { +      for (let i = 0; i < items.length; i++) { +        linesInfo = needsWhitespace(items[i], node, type); +        if (linesInfo) break; +      } +    } +  } + +  if (typeof linesInfo === "object" && linesInfo !== null) { +    return linesInfo[type] || 0; +  } + +  return 0; +} + +function needsWhitespaceBefore(node, parent) { +  return needsWhitespace(node, parent, "before"); +} + +function needsWhitespaceAfter(node, parent) { +  return needsWhitespace(node, parent, "after"); +} + +function needsParens(node, parent, printStack) { +  if (!parent) return false; + +  if (isNewExpression(parent) && parent.callee === node) { +    if (isOrHasCallExpression(node)) return true; +  } + +  return find(expandedParens, node, parent, printStack); +}
\ No newline at end of file diff --git a/node_modules/@babel/generator/lib/node/parentheses.js b/node_modules/@babel/generator/lib/node/parentheses.js new file mode 100644 index 0000000..5761a58 --- /dev/null +++ b/node_modules/@babel/generator/lib/node/parentheses.js @@ -0,0 +1,342 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { +  value: true +}); +exports.ArrowFunctionExpression = ArrowFunctionExpression; +exports.AssignmentExpression = AssignmentExpression; +exports.Binary = Binary; +exports.BinaryExpression = BinaryExpression; +exports.ClassExpression = ClassExpression; +exports.ConditionalExpression = ConditionalExpression; +exports.DoExpression = DoExpression; +exports.FunctionExpression = FunctionExpression; +exports.FunctionTypeAnnotation = FunctionTypeAnnotation; +exports.Identifier = Identifier; +exports.LogicalExpression = LogicalExpression; +exports.NullableTypeAnnotation = NullableTypeAnnotation; +exports.ObjectExpression = ObjectExpression; +exports.OptionalIndexedAccessType = OptionalIndexedAccessType; +exports.OptionalCallExpression = exports.OptionalMemberExpression = OptionalMemberExpression; +exports.SequenceExpression = SequenceExpression; +exports.TSAsExpression = TSAsExpression; +exports.TSInferType = TSInferType; +exports.TSTypeAssertion = TSTypeAssertion; +exports.TSIntersectionType = exports.TSUnionType = TSUnionType; +exports.UnaryLike = UnaryLike; +exports.IntersectionTypeAnnotation = exports.UnionTypeAnnotation = UnionTypeAnnotation; +exports.UpdateExpression = UpdateExpression; +exports.AwaitExpression = exports.YieldExpression = YieldExpression; + +var _t = require("@babel/types"); + +const { +  isArrayTypeAnnotation, +  isArrowFunctionExpression, +  isAssignmentExpression, +  isAwaitExpression, +  isBinary, +  isBinaryExpression, +  isCallExpression, +  isClassDeclaration, +  isClassExpression, +  isConditional, +  isConditionalExpression, +  isExportDeclaration, +  isExportDefaultDeclaration, +  isExpressionStatement, +  isFor, +  isForInStatement, +  isForOfStatement, +  isForStatement, +  isIfStatement, +  isIndexedAccessType, +  isIntersectionTypeAnnotation, +  isLogicalExpression, +  isMemberExpression, +  isNewExpression, +  isNullableTypeAnnotation, +  isObjectPattern, +  isOptionalCallExpression, +  isOptionalMemberExpression, +  isReturnStatement, +  isSequenceExpression, +  isSwitchStatement, +  isTSArrayType, +  isTSAsExpression, +  isTSIntersectionType, +  isTSNonNullExpression, +  isTSOptionalType, +  isTSRestType, +  isTSTypeAssertion, +  isTSUnionType, +  isTaggedTemplateExpression, +  isThrowStatement, +  isTypeAnnotation, +  isUnaryLike, +  isUnionTypeAnnotation, +  isVariableDeclarator, +  isWhileStatement, +  isYieldExpression +} = _t; +const PRECEDENCE = { +  "||": 0, +  "??": 0, +  "&&": 1, +  "|": 2, +  "^": 3, +  "&": 4, +  "==": 5, +  "===": 5, +  "!=": 5, +  "!==": 5, +  "<": 6, +  ">": 6, +  "<=": 6, +  ">=": 6, +  in: 6, +  instanceof: 6, +  ">>": 7, +  "<<": 7, +  ">>>": 7, +  "+": 8, +  "-": 8, +  "*": 9, +  "/": 9, +  "%": 9, +  "**": 10 +}; + +const isClassExtendsClause = (node, parent) => (isClassDeclaration(parent) || isClassExpression(parent)) && parent.superClass === node; + +const hasPostfixPart = (node, parent) => (isMemberExpression(parent) || isOptionalMemberExpression(parent)) && parent.object === node || (isCallExpression(parent) || isOptionalCallExpression(parent) || isNewExpression(parent)) && parent.callee === node || isTaggedTemplateExpression(parent) && parent.tag === node || isTSNonNullExpression(parent); + +function NullableTypeAnnotation(node, parent) { +  return isArrayTypeAnnotation(parent); +} + +function FunctionTypeAnnotation(node, parent, printStack) { +  return isUnionTypeAnnotation(parent) || isIntersectionTypeAnnotation(parent) || isArrayTypeAnnotation(parent) || isTypeAnnotation(parent) && isArrowFunctionExpression(printStack[printStack.length - 3]); +} + +function UpdateExpression(node, parent) { +  return hasPostfixPart(node, parent) || isClassExtendsClause(node, parent); +} + +function ObjectExpression(node, parent, printStack) { +  return isFirstInContext(printStack, { +    expressionStatement: true, +    arrowBody: true +  }); +} + +function DoExpression(node, parent, printStack) { +  return !node.async && isFirstInContext(printStack, { +    expressionStatement: true +  }); +} + +function Binary(node, parent) { +  if (node.operator === "**" && isBinaryExpression(parent, { +    operator: "**" +  })) { +    return parent.left === node; +  } + +  if (isClassExtendsClause(node, parent)) { +    return true; +  } + +  if (hasPostfixPart(node, parent) || isUnaryLike(parent) || isAwaitExpression(parent)) { +    return true; +  } + +  if (isBinary(parent)) { +    const parentOp = parent.operator; +    const parentPos = PRECEDENCE[parentOp]; +    const nodeOp = node.operator; +    const nodePos = PRECEDENCE[nodeOp]; + +    if (parentPos === nodePos && parent.right === node && !isLogicalExpression(parent) || parentPos > nodePos) { +      return true; +    } +  } +} + +function UnionTypeAnnotation(node, parent) { +  return isArrayTypeAnnotation(parent) || isNullableTypeAnnotation(parent) || isIntersectionTypeAnnotation(parent) || isUnionTypeAnnotation(parent); +} + +function OptionalIndexedAccessType(node, parent) { +  return isIndexedAccessType(parent, { +    objectType: node +  }); +} + +function TSAsExpression() { +  return true; +} + +function TSTypeAssertion() { +  return true; +} + +function TSUnionType(node, parent) { +  return isTSArrayType(parent) || isTSOptionalType(parent) || isTSIntersectionType(parent) || isTSUnionType(parent) || isTSRestType(parent); +} + +function TSInferType(node, parent) { +  return isTSArrayType(parent) || isTSOptionalType(parent); +} + +function BinaryExpression(node, parent) { +  return node.operator === "in" && (isVariableDeclarator(parent) || isFor(parent)); +} + +function SequenceExpression(node, parent) { +  if (isForStatement(parent) || isThrowStatement(parent) || isReturnStatement(parent) || isIfStatement(parent) && parent.test === node || isWhileStatement(parent) && parent.test === node || isForInStatement(parent) && parent.right === node || isSwitchStatement(parent) && parent.discriminant === node || isExpressionStatement(parent) && parent.expression === node) { +    return false; +  } + +  return true; +} + +function YieldExpression(node, parent) { +  return isBinary(parent) || isUnaryLike(parent) || hasPostfixPart(node, parent) || isAwaitExpression(parent) && isYieldExpression(node) || isConditionalExpression(parent) && node === parent.test || isClassExtendsClause(node, parent); +} + +function ClassExpression(node, parent, printStack) { +  return isFirstInContext(printStack, { +    expressionStatement: true, +    exportDefault: true +  }); +} + +function UnaryLike(node, parent) { +  return hasPostfixPart(node, parent) || isBinaryExpression(parent, { +    operator: "**", +    left: node +  }) || isClassExtendsClause(node, parent); +} + +function FunctionExpression(node, parent, printStack) { +  return isFirstInContext(printStack, { +    expressionStatement: true, +    exportDefault: true +  }); +} + +function ArrowFunctionExpression(node, parent) { +  return isExportDeclaration(parent) || ConditionalExpression(node, parent); +} + +function ConditionalExpression(node, parent) { +  if (isUnaryLike(parent) || isBinary(parent) || isConditionalExpression(parent, { +    test: node +  }) || isAwaitExpression(parent) || isTSTypeAssertion(parent) || isTSAsExpression(parent)) { +    return true; +  } + +  return UnaryLike(node, parent); +} + +function OptionalMemberExpression(node, parent) { +  return isCallExpression(parent, { +    callee: node +  }) || isMemberExpression(parent, { +    object: node +  }); +} + +function AssignmentExpression(node, parent) { +  if (isObjectPattern(node.left)) { +    return true; +  } else { +    return ConditionalExpression(node, parent); +  } +} + +function LogicalExpression(node, parent) { +  switch (node.operator) { +    case "||": +      if (!isLogicalExpression(parent)) return false; +      return parent.operator === "??" || parent.operator === "&&"; + +    case "&&": +      return isLogicalExpression(parent, { +        operator: "??" +      }); + +    case "??": +      return isLogicalExpression(parent) && parent.operator !== "??"; +  } +} + +function Identifier(node, parent, printStack) { +  if (node.name === "let") { +    const isFollowedByBracket = isMemberExpression(parent, { +      object: node, +      computed: true +    }) || isOptionalMemberExpression(parent, { +      object: node, +      computed: true, +      optional: false +    }); +    return isFirstInContext(printStack, { +      expressionStatement: isFollowedByBracket, +      forHead: isFollowedByBracket, +      forInHead: isFollowedByBracket, +      forOfHead: true +    }); +  } + +  return node.name === "async" && isForOfStatement(parent) && node === parent.left; +} + +function isFirstInContext(printStack, { +  expressionStatement = false, +  arrowBody = false, +  exportDefault = false, +  forHead = false, +  forInHead = false, +  forOfHead = false +}) { +  let i = printStack.length - 1; +  let node = printStack[i]; +  i--; +  let parent = printStack[i]; + +  while (i >= 0) { +    if (expressionStatement && isExpressionStatement(parent, { +      expression: node +    }) || exportDefault && isExportDefaultDeclaration(parent, { +      declaration: node +    }) || arrowBody && isArrowFunctionExpression(parent, { +      body: node +    }) || forHead && isForStatement(parent, { +      init: node +    }) || forInHead && isForInStatement(parent, { +      left: node +    }) || forOfHead && isForOfStatement(parent, { +      left: node +    })) { +      return true; +    } + +    if (hasPostfixPart(node, parent) && !isNewExpression(parent) || isSequenceExpression(parent) && parent.expressions[0] === node || isConditional(parent, { +      test: node +    }) || isBinary(parent, { +      left: node +    }) || isAssignmentExpression(parent, { +      left: node +    })) { +      node = parent; +      i--; +      parent = printStack[i]; +    } else { +      return false; +    } +  } + +  return false; +}
\ No newline at end of file diff --git a/node_modules/@babel/generator/lib/node/whitespace.js b/node_modules/@babel/generator/lib/node/whitespace.js new file mode 100644 index 0000000..80e2da9 --- /dev/null +++ b/node_modules/@babel/generator/lib/node/whitespace.js @@ -0,0 +1,214 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { +  value: true +}); +exports.nodes = exports.list = void 0; + +var _t = require("@babel/types"); + +const { +  FLIPPED_ALIAS_KEYS, +  isArrayExpression, +  isAssignmentExpression, +  isBinary, +  isBlockStatement, +  isCallExpression, +  isFunction, +  isIdentifier, +  isLiteral, +  isMemberExpression, +  isObjectExpression, +  isOptionalCallExpression, +  isOptionalMemberExpression, +  isStringLiteral +} = _t; + +function crawl(node, state = {}) { +  if (isMemberExpression(node) || isOptionalMemberExpression(node)) { +    crawl(node.object, state); +    if (node.computed) crawl(node.property, state); +  } else if (isBinary(node) || isAssignmentExpression(node)) { +    crawl(node.left, state); +    crawl(node.right, state); +  } else if (isCallExpression(node) || isOptionalCallExpression(node)) { +    state.hasCall = true; +    crawl(node.callee, state); +  } else if (isFunction(node)) { +    state.hasFunction = true; +  } else if (isIdentifier(node)) { +    state.hasHelper = state.hasHelper || isHelper(node.callee); +  } + +  return state; +} + +function isHelper(node) { +  if (isMemberExpression(node)) { +    return isHelper(node.object) || isHelper(node.property); +  } else if (isIdentifier(node)) { +    return node.name === "require" || node.name[0] === "_"; +  } else if (isCallExpression(node)) { +    return isHelper(node.callee); +  } else if (isBinary(node) || isAssignmentExpression(node)) { +    return isIdentifier(node.left) && isHelper(node.left) || isHelper(node.right); +  } else { +    return false; +  } +} + +function isType(node) { +  return isLiteral(node) || isObjectExpression(node) || isArrayExpression(node) || isIdentifier(node) || isMemberExpression(node); +} + +const nodes = { +  AssignmentExpression(node) { +    const state = crawl(node.right); + +    if (state.hasCall && state.hasHelper || state.hasFunction) { +      return { +        before: state.hasFunction, +        after: true +      }; +    } +  }, + +  SwitchCase(node, parent) { +    return { +      before: !!node.consequent.length || parent.cases[0] === node, +      after: !node.consequent.length && parent.cases[parent.cases.length - 1] === node +    }; +  }, + +  LogicalExpression(node) { +    if (isFunction(node.left) || isFunction(node.right)) { +      return { +        after: true +      }; +    } +  }, + +  Literal(node) { +    if (isStringLiteral(node) && node.value === "use strict") { +      return { +        after: true +      }; +    } +  }, + +  CallExpression(node) { +    if (isFunction(node.callee) || isHelper(node)) { +      return { +        before: true, +        after: true +      }; +    } +  }, + +  OptionalCallExpression(node) { +    if (isFunction(node.callee)) { +      return { +        before: true, +        after: true +      }; +    } +  }, + +  VariableDeclaration(node) { +    for (let i = 0; i < node.declarations.length; i++) { +      const declar = node.declarations[i]; +      let enabled = isHelper(declar.id) && !isType(declar.init); + +      if (!enabled) { +        const state = crawl(declar.init); +        enabled = isHelper(declar.init) && state.hasCall || state.hasFunction; +      } + +      if (enabled) { +        return { +          before: true, +          after: true +        }; +      } +    } +  }, + +  IfStatement(node) { +    if (isBlockStatement(node.consequent)) { +      return { +        before: true, +        after: true +      }; +    } +  } + +}; +exports.nodes = nodes; + +nodes.ObjectProperty = nodes.ObjectTypeProperty = nodes.ObjectMethod = function (node, parent) { +  if (parent.properties[0] === node) { +    return { +      before: true +    }; +  } +}; + +nodes.ObjectTypeCallProperty = function (node, parent) { +  var _parent$properties; + +  if (parent.callProperties[0] === node && !((_parent$properties = parent.properties) != null && _parent$properties.length)) { +    return { +      before: true +    }; +  } +}; + +nodes.ObjectTypeIndexer = function (node, parent) { +  var _parent$properties2, _parent$callPropertie; + +  if (parent.indexers[0] === node && !((_parent$properties2 = parent.properties) != null && _parent$properties2.length) && !((_parent$callPropertie = parent.callProperties) != null && _parent$callPropertie.length)) { +    return { +      before: true +    }; +  } +}; + +nodes.ObjectTypeInternalSlot = function (node, parent) { +  var _parent$properties3, _parent$callPropertie2, _parent$indexers; + +  if (parent.internalSlots[0] === node && !((_parent$properties3 = parent.properties) != null && _parent$properties3.length) && !((_parent$callPropertie2 = parent.callProperties) != null && _parent$callPropertie2.length) && !((_parent$indexers = parent.indexers) != null && _parent$indexers.length)) { +    return { +      before: true +    }; +  } +}; + +const list = { +  VariableDeclaration(node) { +    return node.declarations.map(decl => decl.init); +  }, + +  ArrayExpression(node) { +    return node.elements; +  }, + +  ObjectExpression(node) { +    return node.properties; +  } + +}; +exports.list = list; +[["Function", true], ["Class", true], ["Loop", true], ["LabeledStatement", true], ["SwitchStatement", true], ["TryStatement", true]].forEach(function ([type, amounts]) { +  if (typeof amounts === "boolean") { +    amounts = { +      after: amounts, +      before: amounts +    }; +  } + +  [type].concat(FLIPPED_ALIAS_KEYS[type] || []).forEach(function (type) { +    nodes[type] = function () { +      return amounts; +    }; +  }); +});
\ No newline at end of file  | 
