@babel/traverse 7.20.0 → 7.20.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of @babel/traverse might be problematic. Click here for more details.
- package/lib/cache.js +0 -3
- package/lib/cache.js.map +1 -1
- package/lib/context.js +7 -21
- package/lib/context.js.map +1 -1
- package/lib/hub.js +0 -6
- package/lib/hub.js.map +1 -1
- package/lib/index.js +3 -21
- package/lib/index.js.map +1 -1
- package/lib/path/ancestry.js +3 -23
- package/lib/path/ancestry.js.map +1 -1
- package/lib/path/comments.js +0 -4
- package/lib/path/comments.js.map +1 -1
- package/lib/path/context.js +11 -52
- package/lib/path/context.js.map +1 -1
- package/lib/path/conversion.js +16 -75
- package/lib/path/conversion.js.map +1 -1
- package/lib/path/evaluation.js +9 -85
- package/lib/path/evaluation.js.map +1 -1
- package/lib/path/family.js +6 -73
- package/lib/path/family.js.map +1 -1
- package/lib/path/index.js +2 -61
- package/lib/path/index.js.map +1 -1
- package/lib/path/inference/index.js +2 -27
- package/lib/path/inference/index.js.map +1 -1
- package/lib/path/inference/inferer-reference.js +10 -32
- package/lib/path/inference/inferer-reference.js.map +1 -1
- package/lib/path/inference/inferers.js +4 -44
- package/lib/path/inference/inferers.js.map +1 -1
- package/lib/path/inference/util.js +0 -4
- package/lib/path/inference/util.js.map +1 -1
- package/lib/path/introspection.js +17 -59
- package/lib/path/introspection.js.map +1 -1
- package/lib/path/lib/hoister.js +7 -23
- package/lib/path/lib/hoister.js.map +1 -1
- package/lib/path/lib/removal-hooks.js +7 -3
- package/lib/path/lib/removal-hooks.js.map +1 -1
- package/lib/path/lib/virtual-types-validator.js +0 -23
- package/lib/path/lib/virtual-types-validator.js.map +1 -1
- package/lib/path/lib/virtual-types.js +1 -0
- package/lib/path/lib/virtual-types.js.map +1 -1
- package/lib/path/modification.js +12 -47
- package/lib/path/modification.js.map +1 -1
- package/lib/path/removal.js +0 -16
- package/lib/path/removal.js.map +1 -1
- package/lib/path/replacement.js +8 -50
- package/lib/path/replacement.js.map +1 -1
- package/lib/scope/binding.js +2 -14
- package/lib/scope/binding.js.map +1 -1
- package/lib/scope/index.js +18 -183
- package/lib/scope/index.js.map +1 -1
- package/lib/scope/lib/renamer.js +2 -25
- package/lib/scope/lib/renamer.js.map +1 -1
- package/lib/traverse-node.js +0 -7
- package/lib/traverse-node.js.map +1 -1
- package/lib/visitors.js +13 -44
- package/lib/visitors.js.map +1 -1
- package/package.json +3 -3
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"names":["react","cloneNode","jsxExpressionContainer","variableDeclaration","variableDeclarator","referenceVisitor","ReferencedIdentifier","path","state","isJSXIdentifier","isCompatTag","node","name","parentPath","isJSXMemberExpression","scope","isFunction","isArrowFunctionExpression","parent","breakOnScopePaths","push","binding","getBinding","violation","constantViolations","mutableBinding","stop","bindings","PathHoister","constructor","scopes","attachAfter","isCompatibleScope","key","Object","keys","bindingIdentifierEquals","identifier","getCompatibleScopes","indexOf","getAttachmentPath","_getAttachmentPath","targetScope","isProgram","hasOwnBinding","kind","parentKey","bindingParentPath","getAttachmentParentForPath","violationPath","pop","hasOwnParamBindings","bodies","get","i","length","_blockHoist","getNextScopeAttachmentParent","Array","isArray","container","isStatement","constant","run","traverse","attachTo","getFunctionParent","uid","generateUidIdentifier","declarator","insertFn","attached","isVariableDeclarator","isJSXElement","children","replaceWith"],"sources":["../../../src/path/lib/hoister.ts"],"sourcesContent":["import { react } from \"@babel/types\";\nimport {\n cloneNode,\n jsxExpressionContainer,\n variableDeclaration,\n variableDeclarator,\n} from \"@babel/types\";\nimport type * as t from \"@babel/types\";\nimport type Scope from \"../../scope\";\nimport type NodePath from \"../index\";\nimport type Binding from \"../../scope/binding\";\nimport type { Visitor } from \"../../types\";\n\nconst referenceVisitor: Visitor<PathHoister> = {\n // This visitor looks for bindings to establish a topmost scope for hoisting.\n ReferencedIdentifier(path, state) {\n // Don't hoist regular JSX identifiers ('div', 'span', etc).\n // We do have to consider member expressions for hoisting (e.g. `this.component`)\n if (\n path.isJSXIdentifier() &&\n react.isCompatTag(path.node.name) &&\n !path.parentPath.isJSXMemberExpression()\n ) {\n return;\n }\n\n // If the identifier refers to `this`, we need to break on the closest non-arrow scope.\n if (path.node.name === \"this\") {\n let scope = path.scope;\n do {\n if (\n scope.path.isFunction() &&\n !scope.path.isArrowFunctionExpression()\n ) {\n break;\n }\n } while ((scope = scope.parent));\n if (scope) state.breakOnScopePaths.push(scope.path);\n }\n\n // direct references that we need to track to hoist this to the highest scope we can\n const binding = path.scope.getBinding(path.node.name);\n if (!binding) return;\n\n // we can handle reassignments only if they happen in the same scope as the declaration\n for (const violation of binding.constantViolations) {\n if (violation.scope !== binding.path.scope) {\n state.mutableBinding = true;\n path.stop();\n return;\n }\n }\n\n // this binding isn't accessible from the parent scope so we can safely ignore it\n // eg. it's in a closure etc\n if (binding !== state.scope.getBinding(path.node.name)) return;\n\n state.bindings[path.node.name] = binding;\n },\n};\n\nexport default class PathHoister<T extends t.Node = t.Node> {\n breakOnScopePaths: NodePath[];\n bindings: { [k: string]: Binding };\n mutableBinding: boolean;\n private scopes: Scope[];\n scope: Scope;\n private path: NodePath<T>;\n private attachAfter: boolean;\n\n constructor(path: NodePath<T>, scope: Scope) {\n // Storage for scopes we can't hoist above.\n this.breakOnScopePaths = [];\n // Storage for bindings that may affect what path we can hoist to.\n this.bindings = {};\n // \"true\" if the current path contains a reference to a binding whose\n // value can change and thus can't be safely hoisted.\n this.mutableBinding = false;\n // Storage for eligible scopes.\n this.scopes = [];\n // Our original scope and path.\n this.scope = scope;\n this.path = path;\n // By default, we attach as far up as we can; but if we're trying\n // to avoid referencing a binding, we may have to go after.\n this.attachAfter = false;\n }\n\n // A scope is compatible if all required bindings are reachable.\n isCompatibleScope(scope: Scope) {\n for (const key of Object.keys(this.bindings)) {\n const binding = this.bindings[key];\n if (!scope.bindingIdentifierEquals(key, binding.identifier)) {\n return false;\n }\n }\n\n return true;\n }\n\n // Look through all scopes and push compatible ones.\n getCompatibleScopes() {\n let scope = this.path.scope;\n do {\n if (this.isCompatibleScope(scope)) {\n this.scopes.push(scope);\n } else {\n break;\n }\n\n // deopt: These scopes are set in the visitor on const violations\n if (this.breakOnScopePaths.indexOf(scope.path) >= 0) {\n break;\n }\n } while ((scope = scope.parent));\n }\n\n getAttachmentPath() {\n let path = this._getAttachmentPath();\n if (!path) return;\n\n let targetScope = path.scope;\n\n // don't allow paths that have their own lexical environments to pollute\n if (targetScope.path === path) {\n targetScope = path.scope.parent;\n }\n\n // avoid hoisting to a scope that contains bindings that are executed after our attachment path\n if (targetScope.path.isProgram() || targetScope.path.isFunction()) {\n for (const name of Object.keys(this.bindings)) {\n // check binding is a direct child of this paths scope\n if (!targetScope.hasOwnBinding(name)) continue;\n\n const binding = this.bindings[name];\n\n // allow parameter references and expressions in params (like destructuring rest)\n if (binding.kind === \"param\" || binding.path.parentKey === \"params\") {\n continue;\n }\n\n // For each binding, get its attachment parent. This gives us an idea of where we might\n // introduce conflicts.\n const bindingParentPath = this.getAttachmentParentForPath(binding.path);\n\n // If the binding's attachment appears at or after our attachment point, then we move after it.\n if (bindingParentPath.key >= path.key) {\n this.attachAfter = true;\n path = binding.path;\n\n // We also move past any constant violations.\n for (const violationPath of binding.constantViolations) {\n if (this.getAttachmentParentForPath(violationPath).key > path.key) {\n path = violationPath;\n }\n }\n }\n }\n }\n\n return path;\n }\n\n _getAttachmentPath() {\n const scopes = this.scopes;\n\n const scope = scopes.pop();\n // deopt: no compatible scopes\n if (!scope) return;\n\n if (scope.path.isFunction()) {\n if (this.hasOwnParamBindings(scope)) {\n // deopt: should ignore this scope since it's ourselves\n if (this.scope === scope) return;\n\n // needs to be attached to the body\n const bodies = scope.path.get(\"body\").get(\"body\") as NodePath[];\n for (let i = 0; i < bodies.length; i++) {\n // Don't attach to something that's going to get hoisted,\n // like a default parameter\n // @ts-expect-error todo(flow->ts): avoid mutating the node, introducing new fields\n if (bodies[i].node._blockHoist) continue;\n return bodies[i];\n }\n // deopt: If here, no attachment path found\n } else {\n // doesn't need to be be attached to this scope\n return this.getNextScopeAttachmentParent();\n }\n } else if (scope.path.isProgram()) {\n return this.getNextScopeAttachmentParent();\n }\n }\n\n getNextScopeAttachmentParent() {\n const scope = this.scopes.pop();\n if (scope) return this.getAttachmentParentForPath(scope.path);\n }\n\n // Find an attachment for this path.\n getAttachmentParentForPath(path: NodePath) {\n do {\n if (\n // Beginning of the scope\n !path.parentPath ||\n // Has siblings and is a statement\n (Array.isArray(path.container) && path.isStatement())\n ) {\n return path;\n }\n } while ((path = path.parentPath));\n }\n\n // Returns true if a scope has param bindings.\n hasOwnParamBindings(scope: Scope) {\n for (const name of Object.keys(this.bindings)) {\n if (!scope.hasOwnBinding(name)) continue;\n\n const binding = this.bindings[name];\n // Ensure constant; without it we could place behind a reassignment\n if (binding.kind === \"param\" && binding.constant) return true;\n }\n return false;\n }\n\n run() {\n this.path.traverse(referenceVisitor, this);\n\n if (this.mutableBinding) return;\n\n this.getCompatibleScopes();\n\n const attachTo = this.getAttachmentPath();\n if (!attachTo) return;\n\n // don't bother hoisting to the same function as this will cause multiple branches to be\n // evaluated more than once leading to a bad optimisation\n if (attachTo.getFunctionParent() === this.path.getFunctionParent()) return;\n\n // generate declaration and insert it to our point\n let uid: t.Identifier | t.JSXExpressionContainer =\n attachTo.scope.generateUidIdentifier(\"ref\");\n\n // @ts-expect-error todo(flow->ts): more specific type for this.path\n const declarator = variableDeclarator(uid, this.path.node);\n\n const insertFn = this.attachAfter ? \"insertAfter\" : \"insertBefore\";\n const [attached] = attachTo[insertFn]([\n attachTo.isVariableDeclarator()\n ? declarator\n : variableDeclaration(\"var\", [declarator]),\n ]);\n\n const parent = this.path.parentPath;\n if (parent.isJSXElement() && this.path.container === parent.node.children) {\n // turning the `span` in `<div><span /></div>` to an expression so we need to wrap it with\n // an expression container\n uid = jsxExpressionContainer(uid);\n }\n\n this.path.replaceWith(cloneNode(uid));\n\n return attachTo.isVariableDeclarator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ACuD,YAAP,MAAyB,KAAKlE,IAAL,CAAUqD,SAAV,KAAwB1C,MAAM,CAACP,IAAP,CAAY+D,QAAjE,EAA2E;MAGzEP,GAAG,GAAGjE,sBAAsB,CAACiE,GAAD,CAA5B;IACD;;IAED,KAAK5D,IAAL,CAAUoE,WAAV,CAAsB1E,SAAS,CAACkE,GAAD,CAA/B;IAEA,OAAOF,QAAQ,CAACO,oBAAT,KACHD,QAAQ,CAAClB,GAAT,CAAa,MAAb,CADG,GAEHkB,QAAQ,CAAClB,GAAT,CAAa,qBAAb,CAFJ;EAGD;;AA5MyD"}
|
1
|
+
{"version":3,"names":["react","cloneNode","jsxExpressionContainer","variableDeclaration","variableDeclarator","referenceVisitor","ReferencedIdentifier","path","state","isJSXIdentifier","isCompatTag","node","name","parentPath","isJSXMemberExpression","scope","isFunction","isArrowFunctionExpression","parent","breakOnScopePaths","push","binding","getBinding","violation","constantViolations","mutableBinding","stop","bindings","PathHoister","constructor","scopes","attachAfter","isCompatibleScope","key","Object","keys","bindingIdentifierEquals","identifier","getCompatibleScopes","indexOf","getAttachmentPath","_getAttachmentPath","targetScope","isProgram","hasOwnBinding","kind","parentKey","bindingParentPath","getAttachmentParentForPath","violationPath","pop","hasOwnParamBindings","bodies","get","i","length","_blockHoist","getNextScopeAttachmentParent","Array","isArray","container","isStatement","constant","run","traverse","attachTo","getFunctionParent","uid","generateUidIdentifier","declarator","insertFn","attached","isVariableDeclarator","isJSXElement","children","replaceWith"],"sources":["../../../src/path/lib/hoister.ts"],"sourcesContent":["import { react } from \"@babel/types\";\nimport {\n cloneNode,\n jsxExpressionContainer,\n variableDeclaration,\n variableDeclarator,\n} from \"@babel/types\";\nimport type * as t from \"@babel/types\";\nimport type Scope from \"../../scope\";\nimport type NodePath from \"../index\";\nimport type Binding from \"../../scope/binding\";\nimport type { Visitor } from \"../../types\";\n\nconst referenceVisitor: Visitor<PathHoister> = {\n // This visitor looks for bindings to establish a topmost scope for hoisting.\n ReferencedIdentifier(path, state) {\n // Don't hoist regular JSX identifiers ('div', 'span', etc).\n // We do have to consider member expressions for hoisting (e.g. `this.component`)\n if (\n path.isJSXIdentifier() &&\n react.isCompatTag(path.node.name) &&\n !path.parentPath.isJSXMemberExpression()\n ) {\n return;\n }\n\n // If the identifier refers to `this`, we need to break on the closest non-arrow scope.\n if (path.node.name === \"this\") {\n let scope = path.scope;\n do {\n if (\n scope.path.isFunction() &&\n !scope.path.isArrowFunctionExpression()\n ) {\n break;\n }\n } while ((scope = scope.parent));\n if (scope) state.breakOnScopePaths.push(scope.path);\n }\n\n // direct references that we need to track to hoist this to the highest scope we can\n const binding = path.scope.getBinding(path.node.name);\n if (!binding) return;\n\n // we can handle reassignments only if they happen in the same scope as the declaration\n for (const violation of binding.constantViolations) {\n if (violation.scope !== binding.path.scope) {\n state.mutableBinding = true;\n path.stop();\n return;\n }\n }\n\n // this binding isn't accessible from the parent scope so we can safely ignore it\n // eg. it's in a closure etc\n if (binding !== state.scope.getBinding(path.node.name)) return;\n\n state.bindings[path.node.name] = binding;\n },\n};\n\nexport default class PathHoister<T extends t.Node = t.Node> {\n breakOnScopePaths: NodePath[];\n bindings: { [k: string]: Binding };\n mutableBinding: boolean;\n private scopes: Scope[];\n scope: Scope;\n private path: NodePath<T>;\n private attachAfter: boolean;\n\n constructor(path: NodePath<T>, scope: Scope) {\n // Storage for scopes we can't hoist above.\n this.breakOnScopePaths = [];\n // Storage for bindings that may affect what path we can hoist to.\n this.bindings = {};\n // \"true\" if the current path contains a reference to a binding whose\n // value can change and thus can't be safely hoisted.\n this.mutableBinding = false;\n // Storage for eligible scopes.\n this.scopes = [];\n // Our original scope and path.\n this.scope = scope;\n this.path = path;\n // By default, we attach as far up as we can; but if we're trying\n // to avoid referencing a binding, we may have to go after.\n this.attachAfter = false;\n }\n\n // A scope is compatible if all required bindings are reachable.\n isCompatibleScope(scope: Scope) {\n for (const key of Object.keys(this.bindings)) {\n const binding = this.bindings[key];\n if (!scope.bindingIdentifierEquals(key, binding.identifier)) {\n return false;\n }\n }\n\n return true;\n }\n\n // Look through all scopes and push compatible ones.\n getCompatibleScopes() {\n let scope = this.path.scope;\n do {\n if (this.isCompatibleScope(scope)) {\n this.scopes.push(scope);\n } else {\n break;\n }\n\n // deopt: These scopes are set in the visitor on const violations\n if (this.breakOnScopePaths.indexOf(scope.path) >= 0) {\n break;\n }\n } while ((scope = scope.parent));\n }\n\n getAttachmentPath() {\n let path = this._getAttachmentPath();\n if (!path) return;\n\n let targetScope = path.scope;\n\n // don't allow paths that have their own lexical environments to pollute\n if (targetScope.path === path) {\n targetScope = path.scope.parent;\n }\n\n // avoid hoisting to a scope that contains bindings that are executed after our attachment path\n if (targetScope.path.isProgram() || targetScope.path.isFunction()) {\n for (const name of Object.keys(this.bindings)) {\n // check binding is a direct child of this paths scope\n if (!targetScope.hasOwnBinding(name)) continue;\n\n const binding = this.bindings[name];\n\n // allow parameter references and expressions in params (like destructuring rest)\n if (binding.kind === \"param\" || binding.path.parentKey === \"params\") {\n continue;\n }\n\n // For each binding, get its attachment parent. This gives us an idea of where we might\n // introduce conflicts.\n const bindingParentPath = this.getAttachmentParentForPath(binding.path);\n\n // If the binding's attachment appears at or after our attachment point, then we move after it.\n if (bindingParentPath.key >= path.key) {\n this.attachAfter = true;\n path = binding.path;\n\n // We also move past any constant violations.\n for (const violationPath of binding.constantViolations) {\n if (this.getAttachmentParentForPath(violationPath).key > path.key) {\n path = violationPath;\n }\n }\n }\n }\n }\n\n return path;\n }\n\n _getAttachmentPath() {\n const scopes = this.scopes;\n\n const scope = scopes.pop();\n // deopt: no compatible scopes\n if (!scope) return;\n\n if (scope.path.isFunction()) {\n if (this.hasOwnParamBindings(scope)) {\n // deopt: should ignore this scope since it's ourselves\n if (this.scope === scope) return;\n\n // needs to be attached to the body\n const bodies = scope.path.get(\"body\").get(\"body\") as NodePath[];\n for (let i = 0; i < bodies.length; i++) {\n // Don't attach to something that's going to get hoisted,\n // like a default parameter\n // @ts-expect-error todo(flow->ts): avoid mutating the node, introducing new fields\n if (bodies[i].node._blockHoist) continue;\n return bodies[i];\n }\n // deopt: If here, no attachment path found\n } else {\n // doesn't need to be be attached to this scope\n return this.getNextScopeAttachmentParent();\n }\n } else if (scope.path.isProgram()) {\n return this.getNextScopeAttachmentParent();\n }\n }\n\n getNextScopeAttachmentParent() {\n const scope = this.scopes.pop();\n if (scope) return this.getAttachmentParentForPath(scope.path);\n }\n\n // Find an attachment for this path.\n getAttachmentParentForPath(path: NodePath) {\n do {\n if (\n // Beginning of the scope\n !path.parentPath ||\n // Has siblings and is a statement\n (Array.isArray(path.container) && path.isStatement())\n ) {\n return path;\n }\n } while ((path = path.parentPath));\n }\n\n // Returns true if a scope has param bindings.\n hasOwnParamBindings(scope: Scope) {\n for (const name of Object.keys(this.bindings)) {\n if (!scope.hasOwnBinding(name)) continue;\n\n const binding = this.bindings[name];\n // Ensure constant; without it we could place behind a reassignment\n if (binding.kind === \"param\" && binding.constant) return true;\n }\n return false;\n }\n\n run() {\n this.path.traverse(referenceVisitor, this);\n\n if (this.mutableBinding) return;\n\n this.getCompatibleScopes();\n\n const attachTo = this.getAttachmentPath();\n if (!attachTo) return;\n\n // don't bother hoisting to the same function as this will cause multiple branches to be\n // evaluated more than once leading to a bad optimisation\n if (attachTo.getFunctionParent() === this.path.getFunctionParent()) return;\n\n // generate declaration and insert it to our point\n let uid: t.Identifier | t.JSXExpressionContainer =\n attachTo.scope.generateUidIdentifier(\"ref\");\n\n // @ts-expect-error todo(flow->ts): more specific type for this.path\n const declarator = variableDeclarator(uid, this.path.node);\n\n const insertFn = this.attachAfter ? \"insertAfter\" : \"insertBefore\";\n const [attached] = attachTo[insertFn]([\n attachTo.isVariableDeclarator()\n ? declarator\n : variableDeclaration(\"var\", [declarator]),\n ]);\n\n const parent = this.path.parentPath;\n if (parent.isJSXElement() && this.path.container === parent.node.children) {\n // turning the `span` in `<div><span /></div>` to an expression so we need to wrap it with\n // an expression container\n uid = jsxExpressionContainer(uid);\n }\n\n this.path.replaceWith(cloneNode(uid));\n\n return attachTo.isVariableDeclarator()\n ? attached.get(\"init\")\n : attached.get(\"declarations.0.init\");\n }\n}\n"],"mappings":";;;;;;AAAA;AAAqC;AAAA;EAA5BA;AAAK;AAAA;EAEZC,SAAS;EACTC,sBAAsB;EACtBC,mBAAmB;EACnBC;AAAkB;AAQpB,MAAMC,gBAAsC,GAAG;EAE7CC,oBAAoB,CAACC,IAAI,EAAEC,KAAK,EAAE;IAGhC,IACED,IAAI,CAACE,eAAe,EAAE,IACtBT,KAAK,CAACU,WAAW,CAACH,IAAI,CAACI,IAAI,CAACC,IAAI,CAAC,IACjC,CAACL,IAAI,CAACM,UAAU,CAACC,qBAAqB,EAAE,EACxC;MACA;IACF;;IAGA,IAAIP,IAAI,CAACI,IAAI,CAACC,IAAI,KAAK,MAAM,EAAE;MAC7B,IAAIG,KAAK,GAAGR,IAAI,CAACQ,KAAK;MACtB,GAAG;QACD,IACEA,KAAK,CAACR,IAAI,CAACS,UAAU,EAAE,IACvB,CAACD,KAAK,CAACR,IAAI,CAACU,yBAAyB,EAAE,EACvC;UACA;QACF;MACF,CAAC,QAASF,KAAK,GAAGA,KAAK,CAACG,MAAM;MAC9B,IAAIH,KAAK,EAAEP,KAAK,CAACW,iBAAiB,CAACC,IAAI,CAACL,KAAK,CAACR,IAAI,CAAC;IACrD;;IAGA,MAAMc,OAAO,GAAGd,IAAI,CAACQ,KAAK,CAACO,UAAU,CAACf,IAAI,CAACI,IAAI,CAACC,IAAI,CAAC;IACrD,IAAI,CAACS,OAAO,EAAE;;IAGd,KAAK,MAAME,SAAS,IAAIF,OAAO,CAACG,kBAAkB,EAAE;MAClD,IAAID,SAAS,CAACR,KAAK,KAAKM,OAAO,CAACd,IAAI,CAACQ,KAAK,EAAE;QAC1CP,KAAK,CAACiB,cAAc,GAAG,IAAI;QAC3BlB,IAAI,CAACmB,IAAI,EAAE;QACX;MACF;IACF;;IAIA,IAAIL,OAAO,KAAKb,KAAK,CAACO,KAAK,CA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AE,GAC3BH,UAAU,GACVlE,mBAAmB,CAAC,KAAK,EAAE,CAACkE,UAAU,CAAC,CAAC,CAC7C,CAAC;IAEF,MAAMnD,MAAM,GAAG,IAAI,CAACX,IAAI,CAACM,UAAU;IACnC,IAAIK,MAAM,CAACuD,YAAY,EAAE,IAAI,IAAI,CAAClE,IAAI,CAACqD,SAAS,KAAK1C,MAAM,CAACP,IAAI,CAAC+D,QAAQ,EAAE;MAGzEP,GAAG,GAAGjE,sBAAsB,CAACiE,GAAG,CAAC;IACnC;IAEA,IAAI,CAAC5D,IAAI,CAACoE,WAAW,CAAC1E,SAAS,CAACkE,GAAG,CAAC,CAAC;IAErC,OAAOF,QAAQ,CAACO,oBAAoB,EAAE,GAClCD,QAAQ,CAAClB,GAAG,CAAC,MAAM,CAAC,GACpBkB,QAAQ,CAAClB,GAAG,CAAC,qBAAqB,CAAC;EACzC;AACF;AAAC"}
|
@@ -4,9 +4,14 @@ Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
5
5
|
});
|
6
6
|
exports.hooks = void 0;
|
7
|
-
const hooks = [function (self, parent) {
|
8
|
-
const removeParent = self.key === "test" && (parent.isWhile() || parent.isSwitchCase()) || self.key === "declaration" && parent.isExportDeclaration() || self.key === "body" && parent.isLabeledStatement() || self.listKey === "declarations" && parent.isVariableDeclaration() && parent.node.declarations.length === 1 || self.key === "expression" && parent.isExpressionStatement();
|
9
7
|
|
8
|
+
const hooks = [function (self, parent) {
|
9
|
+
const removeParent =
|
10
|
+
self.key === "test" && (parent.isWhile() || parent.isSwitchCase()) ||
|
11
|
+
self.key === "declaration" && parent.isExportDeclaration() ||
|
12
|
+
self.key === "body" && parent.isLabeledStatement() ||
|
13
|
+
self.listKey === "declarations" && parent.isVariableDeclaration() && parent.node.declarations.length === 1 ||
|
14
|
+
self.key === "expression" && parent.isExpressionStatement();
|
10
15
|
if (removeParent) {
|
11
16
|
parent.remove();
|
12
17
|
return true;
|
@@ -23,7 +28,6 @@ const hooks = [function (self, parent) {
|
|
23
28
|
} else {
|
24
29
|
parent.replaceWith(parent.node.left);
|
25
30
|
}
|
26
|
-
|
27
31
|
return true;
|
28
32
|
}
|
29
33
|
}, function (self, parent) {
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"names":["hooks","self","parent","removeParent","key","isWhile","isSwitchCase","isExportDeclaration","isLabeledStatement","listKey","isVariableDeclaration","node","declarations","length","isExpressionStatement","remove","isSequenceExpression","expressions","replaceWith","isBinary","right","left","isIfStatement","isLoop","isArrowFunctionExpression","type","body"],"sources":["../../../src/path/lib/removal-hooks.ts"],"sourcesContent":["// this file contains hooks that handle ancestry cleanup of parent nodes when removing children\n\nimport type NodePath from \"..\";\nimport type * as t from \"@babel/types\";\n/**\n * Pre hooks should be used for either rejecting removal or delegating removal\n */\n\nexport const hooks = [\n function (self: NodePath, parent: NodePath) {\n const removeParent =\n // while (NODE);\n // removing the test of a while/switch, we can either just remove it entirely *or* turn the\n // `test` into `true` unlikely that the latter will ever be what's wanted so we just remove\n // the loop to avoid infinite recursion\n (self.key === \"test\" && (parent.isWhile() || parent.isSwitchCase())) ||\n // export NODE;\n // just remove a declaration for an export as this is no longer valid\n (self.key === \"declaration\" && parent.isExportDeclaration()) ||\n // label: NODE\n // stray labeled statement with no body\n (self.key === \"body\" && parent.isLabeledStatement()) ||\n // let NODE;\n // remove an entire declaration if there are no declarators left\n (self.listKey === \"declarations\" &&\n parent.isVariableDeclaration() &&\n parent.node.declarations.length === 1) ||\n // NODE;\n // remove the entire expression statement if there's no expression\n (self.key === \"expression\" && parent.isExpressionStatement());\n\n if (removeParent) {\n parent.remove();\n return true;\n }\n },\n\n function (self: NodePath, parent: NodePath) {\n if (parent.isSequenceExpression() && parent.node.expressions.length === 1) {\n // (node, NODE);\n // we've just removed the second element of a sequence expression so let's turn that sequence\n // expression into a regular expression\n parent.replaceWith(parent.node.expressions[0]);\n return true;\n }\n },\n\n function (self: NodePath, parent: NodePath) {\n if (parent.isBinary()) {\n // left + NODE;\n // NODE + right;\n // we're in a binary expression, better remove it and replace it with the last expression\n if (self.key === \"left\") {\n parent.replaceWith(parent.node.right);\n } else {\n // key === \"right\"\n parent.replaceWith(parent.node.left);\n }\n return true;\n }\n },\n\n function (self: NodePath, parent: NodePath) {\n if (\n (parent.isIfStatement() && self.key === \"consequent\") ||\n (self.key === \"body\" &&\n (parent.isLoop() || parent.isArrowFunctionExpression()))\n ) {\n self.replaceWith({\n type: \"BlockStatement\",\n body: [],\n } as t.BlockStatement);\n return true;\n }\n },\n];\n"],"mappings":"
|
1
|
+
{"version":3,"names":["hooks","self","parent","removeParent","key","isWhile","isSwitchCase","isExportDeclaration","isLabeledStatement","listKey","isVariableDeclaration","node","declarations","length","isExpressionStatement","remove","isSequenceExpression","expressions","replaceWith","isBinary","right","left","isIfStatement","isLoop","isArrowFunctionExpression","type","body"],"sources":["../../../src/path/lib/removal-hooks.ts"],"sourcesContent":["// this file contains hooks that handle ancestry cleanup of parent nodes when removing children\n\nimport type NodePath from \"..\";\nimport type * as t from \"@babel/types\";\n/**\n * Pre hooks should be used for either rejecting removal or delegating removal\n */\n\nexport const hooks = [\n function (self: NodePath, parent: NodePath) {\n const removeParent =\n // while (NODE);\n // removing the test of a while/switch, we can either just remove it entirely *or* turn the\n // `test` into `true` unlikely that the latter will ever be what's wanted so we just remove\n // the loop to avoid infinite recursion\n (self.key === \"test\" && (parent.isWhile() || parent.isSwitchCase())) ||\n // export NODE;\n // just remove a declaration for an export as this is no longer valid\n (self.key === \"declaration\" && parent.isExportDeclaration()) ||\n // label: NODE\n // stray labeled statement with no body\n (self.key === \"body\" && parent.isLabeledStatement()) ||\n // let NODE;\n // remove an entire declaration if there are no declarators left\n (self.listKey === \"declarations\" &&\n parent.isVariableDeclaration() &&\n parent.node.declarations.length === 1) ||\n // NODE;\n // remove the entire expression statement if there's no expression\n (self.key === \"expression\" && parent.isExpressionStatement());\n\n if (removeParent) {\n parent.remove();\n return true;\n }\n },\n\n function (self: NodePath, parent: NodePath) {\n if (parent.isSequenceExpression() && parent.node.expressions.length === 1) {\n // (node, NODE);\n // we've just removed the second element of a sequence expression so let's turn that sequence\n // expression into a regular expression\n parent.replaceWith(parent.node.expressions[0]);\n return true;\n }\n },\n\n function (self: NodePath, parent: NodePath) {\n if (parent.isBinary()) {\n // left + NODE;\n // NODE + right;\n // we're in a binary expression, better remove it and replace it with the last expression\n if (self.key === \"left\") {\n parent.replaceWith(parent.node.right);\n } else {\n // key === \"right\"\n parent.replaceWith(parent.node.left);\n }\n return true;\n }\n },\n\n function (self: NodePath, parent: NodePath) {\n if (\n (parent.isIfStatement() && self.key === \"consequent\") ||\n (self.key === \"body\" &&\n (parent.isLoop() || parent.isArrowFunctionExpression()))\n ) {\n self.replaceWith({\n type: \"BlockStatement\",\n body: [],\n } as t.BlockStatement);\n return true;\n }\n },\n];\n"],"mappings":";;;;;;;AAQO,MAAMA,KAAK,GAAG,CACnB,UAAUC,IAAc,EAAEC,MAAgB,EAAE;EAC1C,MAAMC,YAAY;EAKfF,IAAI,CAACG,GAAG,KAAK,MAAM,KAAKF,MAAM,CAACG,OAAO,EAAE,IAAIH,MAAM,CAACI,YAAY,EAAE,CAAC;EAGlEL,IAAI,CAACG,GAAG,KAAK,aAAa,IAAIF,MAAM,CAACK,mBAAmB,EAAG;EAG3DN,IAAI,CAACG,GAAG,KAAK,MAAM,IAAIF,MAAM,CAACM,kBAAkB,EAAG;EAGnDP,IAAI,CAACQ,OAAO,KAAK,cAAc,IAC9BP,MAAM,CAACQ,qBAAqB,EAAE,IAC9BR,MAAM,CAACS,IAAI,CAACC,YAAY,CAACC,MAAM,KAAK,CAAE;EAGvCZ,IAAI,CAACG,GAAG,KAAK,YAAY,IAAIF,MAAM,CAACY,qBAAqB,EAAG;EAE/D,IAAIX,YAAY,EAAE;IAChBD,MAAM,CAACa,MAAM,EAAE;IACf,OAAO,IAAI;EACb;AACF,CAAC,EAED,UAAUd,IAAc,EAAEC,MAAgB,EAAE;EAC1C,IAAIA,MAAM,CAACc,oBAAoB,EAAE,IAAId,MAAM,CAACS,IAAI,CAACM,WAAW,CAACJ,MAAM,KAAK,CAAC,EAAE;IAIzEX,MAAM,CAACgB,WAAW,CAAChB,MAAM,CAACS,IAAI,CAACM,WAAW,CAAC,CAAC,CAAC,CAAC;IAC9C,OAAO,IAAI;EACb;AACF,CAAC,EAED,UAAUhB,IAAc,EAAEC,MAAgB,EAAE;EAC1C,IAAIA,MAAM,CAACiB,QAAQ,EAAE,EAAE;IAIrB,IAAIlB,IAAI,CAACG,GAAG,KAAK,MAAM,EAAE;MACvBF,MAAM,CAACgB,WAAW,CAAChB,MAAM,CAACS,IAAI,CAACS,KAAK,CAAC;IACvC,CAAC,MAAM;MAELlB,MAAM,CAACgB,WAAW,CAAChB,MAAM,CAACS,IAAI,CAACU,IAAI,CAAC;IACtC;IACA,OAAO,IAAI;EACb;AACF,CAAC,EAED,UAAUpB,IAAc,EAAEC,MAAgB,EAAE;EAC1C,IACGA,MAAM,CAACoB,aAAa,EAAE,IAAIrB,IAAI,CAACG,GAAG,KAAK,YAAY,IACnDH,IAAI,CAACG,GAAG,KAAK,MAAM,KACjBF,MAAM,CAACqB,MAAM,EAAE,IAAIrB,MAAM,CAACsB,yBAAyB,EAAE,CAAE,EAC1D;IACAvB,IAAI,CAACiB,WAAW,CAAC;MACfO,IAAI,EAAE,gBAAgB;MACtBC,IAAI,EAAE;IACR,CAAC,CAAqB;IACtB,OAAO,IAAI;EACb;AACF,CAAC,CACF;AAAC"}
|
@@ -21,9 +21,7 @@ exports.isSpreadProperty = isSpreadProperty;
|
|
21
21
|
exports.isStatement = isStatement;
|
22
22
|
exports.isUser = isUser;
|
23
23
|
exports.isVar = isVar;
|
24
|
-
|
25
24
|
var _t = require("@babel/types");
|
26
|
-
|
27
25
|
const {
|
28
26
|
isBinding,
|
29
27
|
isBlockScoped: nodeIsBlockScoped,
|
@@ -50,13 +48,11 @@ const {
|
|
50
48
|
const {
|
51
49
|
isCompatTag
|
52
50
|
} = react;
|
53
|
-
|
54
51
|
function isReferencedIdentifier(opts) {
|
55
52
|
const {
|
56
53
|
node,
|
57
54
|
parent
|
58
55
|
} = this;
|
59
|
-
|
60
56
|
if (!isIdentifier(node, opts) && !isJSXMemberExpression(parent, opts)) {
|
61
57
|
if (isJSXIdentifier(node, opts)) {
|
62
58
|
if (isCompatTag(node.name)) return false;
|
@@ -67,7 +63,6 @@ function isReferencedIdentifier(opts) {
|
|
67
63
|
|
68
64
|
return nodeIsReferenced(node, parent, this.parentPath.parent);
|
69
65
|
}
|
70
|
-
|
71
66
|
function isReferencedMemberExpression() {
|
72
67
|
const {
|
73
68
|
node,
|
@@ -75,7 +70,6 @@ function isReferencedMemberExpression() {
|
|
75
70
|
} = this;
|
76
71
|
return isMemberExpression(node) && nodeIsReferenced(node, parent);
|
77
72
|
}
|
78
|
-
|
79
73
|
function isBindingIdentifier() {
|
80
74
|
const {
|
81
75
|
node,
|
@@ -84,13 +78,11 @@ function isBindingIdentifier() {
|
|
84
78
|
const grandparent = this.parentPath.parent;
|
85
79
|
return isIdentifier(node) && isBinding(node, parent, grandparent);
|
86
80
|
}
|
87
|
-
|
88
81
|
function isStatement() {
|
89
82
|
const {
|
90
83
|
node,
|
91
84
|
parent
|
92
85
|
} = this;
|
93
|
-
|
94
86
|
if (nodeIsStatement(node)) {
|
95
87
|
if (isVariableDeclaration(node)) {
|
96
88
|
if (isForXStatement(parent, {
|
@@ -100,13 +92,11 @@ function isStatement() {
|
|
100
92
|
init: node
|
101
93
|
})) return false;
|
102
94
|
}
|
103
|
-
|
104
95
|
return true;
|
105
96
|
} else {
|
106
97
|
return false;
|
107
98
|
}
|
108
99
|
}
|
109
|
-
|
110
100
|
function isExpression() {
|
111
101
|
if (this.isIdentifier()) {
|
112
102
|
return this.isReferencedIdentifier();
|
@@ -114,40 +104,31 @@ function isExpression() {
|
|
114
104
|
return nodeIsExpression(this.node);
|
115
105
|
}
|
116
106
|
}
|
117
|
-
|
118
107
|
function isScope() {
|
119
108
|
return nodeIsScope(this.node, this.parent);
|
120
109
|
}
|
121
|
-
|
122
110
|
function isReferenced() {
|
123
111
|
return nodeIsReferenced(this.node, this.parent);
|
124
112
|
}
|
125
|
-
|
126
113
|
function isBlockScoped() {
|
127
114
|
return nodeIsBlockScoped(this.node);
|
128
115
|
}
|
129
|
-
|
130
116
|
function isVar() {
|
131
117
|
return nodeIsVar(this.node);
|
132
118
|
}
|
133
|
-
|
134
119
|
function isUser() {
|
135
120
|
return this.node && !!this.node.loc;
|
136
121
|
}
|
137
|
-
|
138
122
|
function isGenerated() {
|
139
123
|
return !this.isUser();
|
140
124
|
}
|
141
|
-
|
142
125
|
function isPure(constantsOnly) {
|
143
126
|
return this.scope.isPure(this.node, constantsOnly);
|
144
127
|
}
|
145
|
-
|
146
128
|
function isFlow() {
|
147
129
|
const {
|
148
130
|
node
|
149
131
|
} = this;
|
150
|
-
|
151
132
|
if (nodeIsFlow(node)) {
|
152
133
|
return true;
|
153
134
|
} else if (isImportDeclaration(node)) {
|
@@ -164,21 +145,17 @@ function isFlow() {
|
|
164
145
|
function isRestProperty() {
|
165
146
|
return nodeIsRestElement(this.node) && this.parentPath && this.parentPath.isObjectPattern();
|
166
147
|
}
|
167
|
-
|
168
148
|
function isSpreadProperty() {
|
169
149
|
return nodeIsRestElement(this.node) && this.parentPath && this.parentPath.isObjectExpression();
|
170
150
|
}
|
171
|
-
|
172
151
|
function isForAwaitStatement() {
|
173
152
|
return isForOfStatement(this.node, {
|
174
153
|
await: true
|
175
154
|
});
|
176
155
|
}
|
177
|
-
|
178
156
|
function isExistentialTypeParam() {
|
179
157
|
throw new Error("`path.isExistentialTypeParam` has been renamed to `path.isExistsTypeAnnotation()` in Babel 7.");
|
180
158
|
}
|
181
|
-
|
182
159
|
function isNumericLiteralTypeAnnotation() {
|
183
160
|
throw new Error("`path.isNumericLiteralTypeAnnotation()` has been renamed to `path.isNumberLiteralTypeAnnotation()` in Babel 7.");
|
184
161
|
}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"names":["isBinding","isBlockScoped","nodeIsBlockScoped","isExportDeclaration","isExpression","nodeIsExpression","isFlow","nodeIsFlow","isForStatement","isForXStatement","isIdentifier","isImportDeclaration","isImportSpecifier","isJSXIdentifier","isJSXMemberExpression","isMemberExpression","isRestElement","nodeIsRestElement","isReferenced","nodeIsReferenced","isScope","nodeIsScope","isStatement","nodeIsStatement","isVar","nodeIsVar","isVariableDeclaration","react","isForOfStatement","isCompatTag","isReferencedIdentifier","opts","node","parent","name","parentPath","isReferencedMemberExpression","isBindingIdentifier","grandparent","left","init","isUser","loc","isGenerated","isPure","constantsOnly","scope","importKind","exportKind","isRestProperty","isObjectPattern","isSpreadProperty","isObjectExpression","isForAwaitStatement","await","isExistentialTypeParam","Error","isNumericLiteralTypeAnnotation"],"sources":["../../../src/path/lib/virtual-types-validator.ts"],"sourcesContent":["import type NodePath from \"../index\";\nimport {\n isBinding,\n isBlockScoped as nodeIsBlockScoped,\n isExportDeclaration,\n isExpression as nodeIsExpression,\n isFlow as nodeIsFlow,\n isForStatement,\n isForXStatement,\n isIdentifier,\n isImportDeclaration,\n isImportSpecifier,\n isJSXIdentifier,\n isJSXMemberExpression,\n isMemberExpression,\n isRestElement as nodeIsRestElement,\n isReferenced as nodeIsReferenced,\n isScope as nodeIsScope,\n isStatement as nodeIsStatement,\n isVar as nodeIsVar,\n isVariableDeclaration,\n react,\n isForOfStatement,\n} from \"@babel/types\";\nimport type * as t from \"@babel/types\";\nconst { isCompatTag } = react;\nimport type { VirtualTypeAliases } from \"./virtual-types\";\n\nexport interface VirtualTypeNodePathValidators {\n isBindingIdentifier<T extends t.Node>(\n this: NodePath<T>,\n opts?: object,\n ): this is NodePath<T & VirtualTypeAliases[\"BindingIdentifier\"]>;\n isBlockScoped(opts?: object): boolean;\n /**\n * @deprecated\n */\n isExistentialTypeParam<T extends t.Node>(\n this: NodePath<T>,\n opts?: object,\n ): this is NodePath<T & VirtualTypeAliases[\"ExistentialTypeParam\"]>;\n isExpression<T extends t.Node>(\n this: NodePath<T>,\n opts?: object,\n ): this is NodePath<T & t.Expression>;\n isFlow<T extends t.Node>(\n this: NodePath<T>,\n opts?: object,\n ): this is NodePath<T & t.Flow>;\n isForAwaitStatement<T extends t.Node>(\n this: NodePath<T>,\n opts?: object,\n ): this is NodePath<T & VirtualTypeAliases[\"ForAwaitStatement\"]>;\n isGenerated(opts?: object): boolean;\n /**\n * @deprecated\n */\n isNumericLiteralTypeAnnotation(opts?: object): void;\n isPure(opts?: object): boolean;\n isReferenced(opts?: object): boolean;\n isReferencedIdentifier<T extends t.Node>(\n this: NodePath<T>,\n opts?: object,\n ): this is NodePath<T & VirtualTypeAliases[\"ReferencedIdentifier\"]>;\n isReferencedMemberExpression<T extends t.Node>(\n this: NodePath<T>,\n opts?: object,\n ): this is NodePath<T & VirtualTypeAliases[\"ReferencedMemberExpression\"]>;\n isRestProperty<T extends t.Node>(\n this: NodePath<T>,\n opts?: object,\n ): this is NodePath<T & t.RestProperty>;\n isScope<T extends t.Node>(\n this: NodePath<T>,\n opts?: object,\n ): this is NodePath<T & VirtualTypeAliases[\"Scope\"]>;\n isSpreadProperty<T extends t.Node>(\n this: NodePath<T>,\n opts?: object,\n ): this is NodePath<T & t.SpreadProperty>;\n isStatement<T extends t.Node>(\n this: NodePath<T>,\n opts?: object,\n ): this is NodePath<T & t.Statement>;\n isUser(opts?: object): boolean;\n isVar<T extends t.Node>(\n this: NodePath<T>,\n opts?: object,\n ): this is NodePath<T & VirtualTypeAliases[\"Var\"]>;\n}\n\nexport function isReferencedIdentifier(this: NodePath, opts?: any): boolean {\n const { node, parent } = this;\n if (!isIdentifier(node, opts) && !isJSXMemberExpression(parent, opts)) {\n if (isJSXIdentifier(node, opts)) {\n if (isCompatTag(node.name)) return false;\n } else {\n // not a JSXIdentifier or an Identifier\n return false;\n }\n }\n\n // check if node is referenced\n return nodeIsReferenced(node, parent, this.parentPath.parent);\n}\n\nexport function isReferencedMemberExpression(this: NodePath): boolean {\n const { node, parent } = this;\n return isMemberExpression(node) && nodeIsReferenced(node, parent);\n}\n\nexport function isBindingIdentifier(this: NodePath): boolean {\n const { node, parent } = this;\n const grandparent = this.parentPath.parent;\n return isIdentifier(node) && isBinding(node, parent, grandparent);\n}\n\nexport function isStatement(this: NodePath): boolean {\n const { node, parent } = this;\n if (nodeIsStatement(node)) {\n if (isVariableDeclaration(node)) {\n if (isForXStatement(parent, { left: node })) return false;\n if (isForStatement(parent, { init: node })) return false;\n }\n\n return true;\n } else {\n return false;\n }\n}\n\nexport function isExpression(this: NodePath): boolean {\n if (this.isIdentifier()) {\n return this.isReferencedIdentifier();\n } else {\n return nodeIsExpression(this.node);\n }\n}\n\nexport function isScope(this: NodePath): boolean {\n return nodeIsScope(this.node, this.parent);\n}\n\nexport function isReferenced(this: NodePath): boolean {\n return nodeIsReferenced(this.node, this.parent);\n}\n\nexport function isBlockScoped(this: NodePath): boolean {\n return nodeIsBlockScoped(this.node);\n}\n\nexport function isVar(this: NodePath): boolean {\n return nodeIsVar(this.node);\n}\n\nexport function isUser(this: NodePath): boolean {\n return this.node && !!this.node.loc;\n}\n\nexport function isGenerated(this: NodePath): boolean {\n return !this.isUser();\n}\n\nexport function isPure(this: NodePath, constantsOnly?: boolean): boolean {\n return this.scope.isPure(this.node, constantsOnly);\n}\n\nexport function isFlow(this: NodePath): boolean {\n const { node } = this;\n if (nodeIsFlow(node)) {\n return true;\n } else if (isImportDeclaration(node)) {\n return node.importKind === \"type\" || node.importKind === \"typeof\";\n } else if (isExportDeclaration(node)) {\n return node.exportKind === \"type\";\n } else if (isImportSpecifier(node)) {\n return node.importKind === \"type\" || node.importKind === \"typeof\";\n } else {\n return false;\n }\n}\n\n// TODO: 7.0 Backwards Compat\nexport function isRestProperty(this: NodePath): boolean {\n return (\n nodeIsRestElement(this.node) &&\n this.parentPath &&\n this.parentPath.isObjectPattern()\n );\n}\n\nexport function isSpreadProperty(this: NodePath): boolean {\n return (\n nodeIsRestElement(this.node) &&\n this.parentPath &&\n this.parentPath.isObjectExpression()\n );\n}\n\nexport function isForAwaitStatement(this: NodePath): boolean {\n return isForOfStatement(this.node, { await: true });\n}\n\nexport function isExistentialTypeParam(this: NodePath): void {\n throw new Error(\n \"`path.isExistentialTypeParam` has been renamed to `path.isExistsTypeAnnotation()` in Babel 7.\",\n );\n}\n\nexport function isNumericLiteralTypeAnnotation(this: NodePath): void {\n throw new Error(\n \"`path.isNumericLiteralTypeAnnotation()` has been renamed to `path.isNumberLiteralTypeAnnotation()` in Babel 7.\",\n );\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AACA;;;EACEA,S;EACAC,a,EAAiBC,iB;EACjBC,mB;EACAC,Y,EAAgBC,gB;EAChBC,M,EAAUC,U;EACVC,c;EACAC,e;EACAC,Y;EACAC,mB;EACAC,iB;EACAC,e;EACAC,qB;EACAC,kB;EACAC,a,EAAiBC,iB;EACjBC,Y,EAAgBC,gB;EAChBC,O,EAAWC,W;EACXC,W,EAAeC,e;EACfC,K,EAASC,S;EACTC,qB;EACAC,K;EACAC;;AAGF,MAAM;EAAEC;AAAF,IAAkBF,KAAxB;;AAkEO,SAASG,sBAAT,CAAgDC,IAAhD,EAAqE;EAC1E,MAAM;IAAEC,IAAF;IAAQC;EAAR,IAAmB,IAAzB;;EACA,IAAI,CAACvB,YAAY,CAACsB,IAAD,EAAOD,IAAP,CAAb,IAA6B,CAACjB,qBAAqB,CAACmB,MAAD,EAASF,IAAT,CAAvD,EAAuE;IACrE,IAAIlB,eAAe,CAACmB,IAAD,EAAOD,IAAP,CAAnB,EAAiC;MAC/B,IAAIF,WAAW,CAACG,IAAI,CAACE,IAAN,CAAf,EAA4B,OAAO,KAAP;IAC7B,CAFD,MAEO;MAEL,OAAO,KAAP;IACD;EACF;;EAGD,OAAOf,gBAAgB,CAACa,IAAD,EAAOC,MAAP,EAAe,KAAKE,UAAL,CAAgBF,MAA/B,CAAvB;AACD;;AAEM,SAASG,4BAAT,GAA+D;EACpE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IACA,KAAKG,UADL,IAEA,KAAKA,UAAL,CAAgBiB,kBAAhB,EAHF;AAKD;;AAEM,SAASC,mBAAT,GAAsD;EAC3D,OAAOzB,gBAAgB,CAAC,KAAKI,IAAN,EAAY;IAAEsB,KAAK,EAAE;EAAT,CAAZ,CAAvB;AACD;;AAEM,SAASC,sBAAT,GAAsD;EAC3D,MAAM,IAAIC,KAAJ,CACJ,+FADI,CAAN;AAGD;;AAEM,SAASC,8BAAT,GAA8D;EACnE,MAAM,IAAID,KAAJ,CACJ,gHADI,CAAN;AAGD"}
|
1
|
+
{"version":3,"names":["isBinding","isBlockScoped","nodeIsBlockScoped","isExportDeclaration","isExpression","nodeIsExpression","isFlow","nodeIsFlow","isForStatement","isForXStatement","isIdentifier","isImportDeclaration","isImportSpecifier","isJSXIdentifier","isJSXMemberExpression","isMemberExpression","isRestElement","nodeIsRestElement","isReferenced","nodeIsReferenced","isScope","nodeIsScope","isStatement","nodeIsStatement","isVar","nodeIsVar","isVariableDeclaration","react","isForOfStatement","isCompatTag","isReferencedIdentifier","opts","node","parent","name","parentPath","isReferencedMemberExpression","isBindingIdentifier","grandparent","left","init","isUser","loc","isGenerated","isPure","constantsOnly","scope","importKind","exportKind","isRestProperty","isObjectPattern","isSpreadProperty","isObjectExpression","isForAwaitStatement","await","isExistentialTypeParam","Error","isNumericLiteralTypeAnnotation"],"sources":["../../../src/path/lib/virtual-types-validator.ts"],"sourcesContent":["import type NodePath from \"../index\";\nimport {\n isBinding,\n isBlockScoped as nodeIsBlockScoped,\n isExportDeclaration,\n isExpression as nodeIsExpression,\n isFlow as nodeIsFlow,\n isForStatement,\n isForXStatement,\n isIdentifier,\n isImportDeclaration,\n isImportSpecifier,\n isJSXIdentifier,\n isJSXMemberExpression,\n isMemberExpression,\n isRestElement as nodeIsRestElement,\n isReferenced as nodeIsReferenced,\n isScope as nodeIsScope,\n isStatement as nodeIsStatement,\n isVar as nodeIsVar,\n isVariableDeclaration,\n react,\n isForOfStatement,\n} from \"@babel/types\";\nimport type * as t from \"@babel/types\";\nconst { isCompatTag } = react;\nimport type { VirtualTypeAliases } from \"./virtual-types\";\n\nexport interface VirtualTypeNodePathValidators {\n isBindingIdentifier<T extends t.Node>(\n this: NodePath<T>,\n opts?: object,\n ): this is NodePath<T & VirtualTypeAliases[\"BindingIdentifier\"]>;\n isBlockScoped(opts?: object): boolean;\n /**\n * @deprecated\n */\n isExistentialTypeParam<T extends t.Node>(\n this: NodePath<T>,\n opts?: object,\n ): this is NodePath<T & VirtualTypeAliases[\"ExistentialTypeParam\"]>;\n isExpression<T extends t.Node>(\n this: NodePath<T>,\n opts?: object,\n ): this is NodePath<T & t.Expression>;\n isFlow<T extends t.Node>(\n this: NodePath<T>,\n opts?: object,\n ): this is NodePath<T & t.Flow>;\n isForAwaitStatement<T extends t.Node>(\n this: NodePath<T>,\n opts?: object,\n ): this is NodePath<T & VirtualTypeAliases[\"ForAwaitStatement\"]>;\n isGenerated(opts?: object): boolean;\n /**\n * @deprecated\n */\n isNumericLiteralTypeAnnotation(opts?: object): void;\n isPure(opts?: object): boolean;\n isReferenced(opts?: object): boolean;\n isReferencedIdentifier<T extends t.Node>(\n this: NodePath<T>,\n opts?: object,\n ): this is NodePath<T & VirtualTypeAliases[\"ReferencedIdentifier\"]>;\n isReferencedMemberExpression<T extends t.Node>(\n this: NodePath<T>,\n opts?: object,\n ): this is NodePath<T & VirtualTypeAliases[\"ReferencedMemberExpression\"]>;\n isRestProperty<T extends t.Node>(\n this: NodePath<T>,\n opts?: object,\n ): this is NodePath<T & t.RestProperty>;\n isScope<T extends t.Node>(\n this: NodePath<T>,\n opts?: object,\n ): this is NodePath<T & VirtualTypeAliases[\"Scope\"]>;\n isSpreadProperty<T extends t.Node>(\n this: NodePath<T>,\n opts?: object,\n ): this is NodePath<T & t.SpreadProperty>;\n isStatement<T extends t.Node>(\n this: NodePath<T>,\n opts?: object,\n ): this is NodePath<T & t.Statement>;\n isUser(opts?: object): boolean;\n isVar<T extends t.Node>(\n this: NodePath<T>,\n opts?: object,\n ): this is NodePath<T & VirtualTypeAliases[\"Var\"]>;\n}\n\nexport function isReferencedIdentifier(this: NodePath, opts?: any): boolean {\n const { node, parent } = this;\n if (!isIdentifier(node, opts) && !isJSXMemberExpression(parent, opts)) {\n if (isJSXIdentifier(node, opts)) {\n if (isCompatTag(node.name)) return false;\n } else {\n // not a JSXIdentifier or an Identifier\n return false;\n }\n }\n\n // check if node is referenced\n return nodeIsReferenced(node, parent, this.parentPath.parent);\n}\n\nexport function isReferencedMemberExpression(this: NodePath): boolean {\n const { node, parent } = this;\n return isMemberExpression(node) && nodeIsReferenced(node, parent);\n}\n\nexport function isBindingIdentifier(this: NodePath): boolean {\n const { node, parent } = this;\n const grandparent = this.parentPath.parent;\n return isIdentifier(node) && isBinding(node, parent, grandparent);\n}\n\nexport function isStatement(this: NodePath): boolean {\n const { node, parent } = this;\n if (nodeIsStatement(node)) {\n if (isVariableDeclaration(node)) {\n if (isForXStatement(parent, { left: node })) return false;\n if (isForStatement(parent, { init: node })) return false;\n }\n\n return true;\n } else {\n return false;\n }\n}\n\nexport function isExpression(this: NodePath): boolean {\n if (this.isIdentifier()) {\n return this.isReferencedIdentifier();\n } else {\n return nodeIsExpression(this.node);\n }\n}\n\nexport function isScope(this: NodePath): boolean {\n return nodeIsScope(this.node, this.parent);\n}\n\nexport function isReferenced(this: NodePath): boolean {\n return nodeIsReferenced(this.node, this.parent);\n}\n\nexport function isBlockScoped(this: NodePath): boolean {\n return nodeIsBlockScoped(this.node);\n}\n\nexport function isVar(this: NodePath): boolean {\n return nodeIsVar(this.node);\n}\n\nexport function isUser(this: NodePath): boolean {\n return this.node && !!this.node.loc;\n}\n\nexport function isGenerated(this: NodePath): boolean {\n return !this.isUser();\n}\n\nexport function isPure(this: NodePath, constantsOnly?: boolean): boolean {\n return this.scope.isPure(this.node, constantsOnly);\n}\n\nexport function isFlow(this: NodePath): boolean {\n const { node } = this;\n if (nodeIsFlow(node)) {\n return true;\n } else if (isImportDeclaration(node)) {\n return node.importKind === \"type\" || node.importKind === \"typeof\";\n } else if (isExportDeclaration(node)) {\n return node.exportKind === \"type\";\n } else if (isImportSpecifier(node)) {\n return node.importKind === \"type\" || node.importKind === \"typeof\";\n } else {\n return false;\n }\n}\n\n// TODO: 7.0 Backwards Compat\nexport function isRestProperty(this: NodePath): boolean {\n return (\n nodeIsRestElement(this.node) &&\n this.parentPath &&\n this.parentPath.isObjectPattern()\n );\n}\n\nexport function isSpreadProperty(this: NodePath): boolean {\n return (\n nodeIsRestElement(this.node) &&\n this.parentPath &&\n this.parentPath.isObjectExpression()\n );\n}\n\nexport function isForAwaitStatement(this: NodePath): boolean {\n return isForOfStatement(this.node, { await: true });\n}\n\nexport function isExistentialTypeParam(this: NodePath): void {\n throw new Error(\n \"`path.isExistentialTypeParam` has been renamed to `path.isExistsTypeAnnotation()` in Babel 7.\",\n );\n}\n\nexport function isNumericLiteralTypeAnnotation(this: NodePath): void {\n throw new Error(\n \"`path.isNumericLiteralTypeAnnotation()` has been renamed to `path.isNumberLiteralTypeAnnotation()` in Babel 7.\",\n );\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AACA;AAsBsB;EArBpBA,SAAS;EACTC,aAAa,EAAIC,iBAAiB;EAClCC,mBAAmB;EACnBC,YAAY,EAAIC,gBAAgB;EAChCC,MAAM,EAAIC,UAAU;EACpBC,cAAc;EACdC,eAAe;EACfC,YAAY;EACZC,mBAAmB;EACnBC,iBAAiB;EACjBC,eAAe;EACfC,qBAAqB;EACrBC,kBAAkB;EAClBC,aAAa,EAAIC,iBAAiB;EAClCC,YAAY,EAAIC,gBAAgB;EAChCC,OAAO,EAAIC,WAAW;EACtBC,WAAW,EAAIC,eAAe;EAC9BC,KAAK,EAAIC,SAAS;EAClBC,qBAAqB;EACrBC,KAAK;EACLC;AAAgB;AAGlB,MAAM;EAAEC;AAAY,CAAC,GAAGF,KAAK;AAkEtB,SAASG,sBAAsB,CAAiBC,IAAU,EAAW;EA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U,KAAK,QAAQ;EACnE,CAAC,MAAM,IAAI5C,mBAAmB,CAAC6B,IAAI,CAAC,EAAE;IACpC,OAAOA,IAAI,CAACgB,UAAU,KAAK,MAAM;EACnC,CAAC,MAAM,IAAIpC,iBAAiB,CAACoB,IAAI,CAAC,EAAE;IAClC,OAAOA,IAAI,CAACe,UAAU,KAAK,MAAM,IAAIf,IAAI,CAACe,UAAU,KAAK,QAAQ;EACnE,CAAC,MAAM;IACL,OAAO,KAAK;EACd;AACF;;AAGO,SAASE,cAAc,GAA0B;EACtD,OACEhC,iBAAiB,CAAC,IAAI,CAACe,IAAI,CAAC,IAC5B,IAAI,CAACG,UAAU,IACf,IAAI,CAACA,UAAU,CAACe,eAAe,EAAE;AAErC;AAEO,SAASC,gBAAgB,GAA0B;EACxD,OACElC,iBAAiB,CAAC,IAAI,CAACe,IAAI,CAAC,IAC5B,IAAI,CAACG,UAAU,IACf,IAAI,CAACA,UAAU,CAACiB,kBAAkB,EAAE;AAExC;AAEO,SAASC,mBAAmB,GAA0B;EAC3D,OAAOzB,gBAAgB,CAAC,IAAI,CAACI,IAAI,EAAE;IAAEsB,KAAK,EAAE;EAAK,CAAC,CAAC;AACrD;AAEO,SAASC,sBAAsB,GAAuB;EAC3D,MAAM,IAAIC,KAAK,CACb,+FAA+F,CAChG;AACH;AAEO,SAASC,8BAA8B,GAAuB;EACnE,MAAM,IAAID,KAAK,CACb,gHAAgH,CACjH;AACH"}
|
@@ -29,6 +29,7 @@ exports.Generated = Generated;
|
|
29
29
|
const Pure = null;
|
30
30
|
exports.Pure = Pure;
|
31
31
|
const Flow = ["Flow", "ImportDeclaration", "ExportDeclaration", "ImportSpecifier"];
|
32
|
+
|
32
33
|
exports.Flow = Flow;
|
33
34
|
const RestProperty = ["RestElement"];
|
34
35
|
exports.RestProperty = RestProperty;
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"names":["ReferencedIdentifier","ReferencedMemberExpression","BindingIdentifier","Statement","Expression","Scope","Referenced","BlockScoped","Var","User","Generated","Pure","Flow","RestProperty","SpreadProperty","ExistentialTypeParam","NumericLiteralTypeAnnotation","ForAwaitStatement"],"sources":["../../../src/path/lib/virtual-types.ts"],"sourcesContent":["import type * as t from \"@babel/types\";\n\nexport interface VirtualTypeAliases {\n BindingIdentifier: t.Identifier;\n BlockScoped: t.Node;\n ExistentialTypeParam: t.ExistsTypeAnnotation;\n Expression: t.Expression;\n Flow: t.Flow | t.ImportDeclaration | t.ExportDeclaration | t.ImportSpecifier;\n ForAwaitStatement: t.ForOfStatement;\n Generated: t.Node;\n NumericLiteralTypeAnnotation: t.NumberLiteralTypeAnnotation;\n Pure: t.Node;\n Referenced: t.Node;\n ReferencedIdentifier: t.Identifier | t.JSXIdentifier;\n ReferencedMemberExpression: t.MemberExpression;\n RestProperty: t.RestElement;\n Scope: t.Scopable | t.Pattern;\n SpreadProperty: t.RestElement;\n Statement: t.Statement;\n User: t.Node;\n Var: t.VariableDeclaration;\n}\n\ntype VirtualTypeMapping = readonly (t.Node[\"type\"] | keyof t.Aliases)[] | null;\n\nexport const ReferencedIdentifier: VirtualTypeMapping = [\n \"Identifier\",\n \"JSXIdentifier\",\n] as const;\n\nexport const ReferencedMemberExpression: VirtualTypeMapping = [\n \"MemberExpression\",\n] as const;\n\nexport const BindingIdentifier: VirtualTypeMapping = [\"Identifier\"] as const;\n\nexport const Statement: VirtualTypeMapping = [\"Statement\"] as const;\n\nexport const Expression: VirtualTypeMapping = [\"Expression\"] as const;\n\nexport const Scope: VirtualTypeMapping = [\"Scopable\", \"Pattern\"] as const;\n\nexport const Referenced: VirtualTypeMapping = null as null;\n\nexport const BlockScoped: VirtualTypeMapping = null as null;\n\nexport const Var: VirtualTypeMapping = [\"VariableDeclaration\"];\n\nexport const User: VirtualTypeMapping = null as null;\n\nexport const Generated: VirtualTypeMapping = null as null;\n\nexport const Pure: VirtualTypeMapping = null as null;\n\nexport const Flow: VirtualTypeMapping = [\n \"Flow\",\n \"ImportDeclaration\",\n \"ExportDeclaration\",\n \"ImportSpecifier\",\n] as const;\n\n// TODO: 7.0 Backwards Compat\nexport const RestProperty: VirtualTypeMapping = [\"RestElement\"] as const;\n\nexport const SpreadProperty: VirtualTypeMapping = [\"RestElement\"] as const;\n\nexport const ExistentialTypeParam: VirtualTypeMapping = [\n \"ExistsTypeAnnotation\",\n] as const;\n\nexport const NumericLiteralTypeAnnotation: VirtualTypeMapping = [\n \"NumberLiteralTypeAnnotation\",\n] as const;\n\nexport const ForAwaitStatement: VirtualTypeMapping = [\n \"ForOfStatement\",\n] as const;\n"],"mappings":";;;;;;AAyBO,MAAMA,oBAAwC,GAAG,CACtD,
|
1
|
+
{"version":3,"names":["ReferencedIdentifier","ReferencedMemberExpression","BindingIdentifier","Statement","Expression","Scope","Referenced","BlockScoped","Var","User","Generated","Pure","Flow","RestProperty","SpreadProperty","ExistentialTypeParam","NumericLiteralTypeAnnotation","ForAwaitStatement"],"sources":["../../../src/path/lib/virtual-types.ts"],"sourcesContent":["import type * as t from \"@babel/types\";\n\nexport interface VirtualTypeAliases {\n BindingIdentifier: t.Identifier;\n BlockScoped: t.Node;\n ExistentialTypeParam: t.ExistsTypeAnnotation;\n Expression: t.Expression;\n Flow: t.Flow | t.ImportDeclaration | t.ExportDeclaration | t.ImportSpecifier;\n ForAwaitStatement: t.ForOfStatement;\n Generated: t.Node;\n NumericLiteralTypeAnnotation: t.NumberLiteralTypeAnnotation;\n Pure: t.Node;\n Referenced: t.Node;\n ReferencedIdentifier: t.Identifier | t.JSXIdentifier;\n ReferencedMemberExpression: t.MemberExpression;\n RestProperty: t.RestElement;\n Scope: t.Scopable | t.Pattern;\n SpreadProperty: t.RestElement;\n Statement: t.Statement;\n User: t.Node;\n Var: t.VariableDeclaration;\n}\n\ntype VirtualTypeMapping = readonly (t.Node[\"type\"] | keyof t.Aliases)[] | null;\n\nexport const ReferencedIdentifier: VirtualTypeMapping = [\n \"Identifier\",\n \"JSXIdentifier\",\n] as const;\n\nexport const ReferencedMemberExpression: VirtualTypeMapping = [\n \"MemberExpression\",\n] as const;\n\nexport const BindingIdentifier: VirtualTypeMapping = [\"Identifier\"] as const;\n\nexport const Statement: VirtualTypeMapping = [\"Statement\"] as const;\n\nexport const Expression: VirtualTypeMapping = [\"Expression\"] as const;\n\nexport const Scope: VirtualTypeMapping = [\"Scopable\", \"Pattern\"] as const;\n\nexport const Referenced: VirtualTypeMapping = null as null;\n\nexport const BlockScoped: VirtualTypeMapping = null as null;\n\nexport const Var: VirtualTypeMapping = [\"VariableDeclaration\"];\n\nexport const User: VirtualTypeMapping = null as null;\n\nexport const Generated: VirtualTypeMapping = null as null;\n\nexport const Pure: VirtualTypeMapping = null as null;\n\nexport const Flow: VirtualTypeMapping = [\n \"Flow\",\n \"ImportDeclaration\",\n \"ExportDeclaration\",\n \"ImportSpecifier\",\n] as const;\n\n// TODO: 7.0 Backwards Compat\nexport const RestProperty: VirtualTypeMapping = [\"RestElement\"] as const;\n\nexport const SpreadProperty: VirtualTypeMapping = [\"RestElement\"] as const;\n\nexport const ExistentialTypeParam: VirtualTypeMapping = [\n \"ExistsTypeAnnotation\",\n] as const;\n\nexport const NumericLiteralTypeAnnotation: VirtualTypeMapping = [\n \"NumberLiteralTypeAnnotation\",\n] as const;\n\nexport const ForAwaitStatement: VirtualTypeMapping = [\n \"ForOfStatement\",\n] as const;\n"],"mappings":";;;;;;AAyBO,MAAMA,oBAAwC,GAAG,CACtD,YAAY,EACZ,eAAe,CACP;AAAC;AAEJ,MAAMC,0BAA8C,GAAG,CAC5D,kBAAkB,CACV;AAAC;AAEJ,MAAMC,iBAAqC,GAAG,CAAC,YAAY,CAAU;AAAC;AAEtE,MAAMC,SAA6B,GAAG,CAAC,WAAW,CAAU;AAAC;AAE7D,MAAMC,UAA8B,GAAG,CAAC,YAAY,CAAU;AAAC;AAE/D,MAAMC,KAAyB,GAAG,CAAC,UAAU,EAAE,SAAS,CAAU;AAAC;AAEnE,MAAMC,UAA8B,GAAG,IAAY;AAAC;AAEpD,MAAMC,WAA+B,GAAG,IAAY;AAAC;AAErD,MAAMC,GAAuB,GAAG,CAAC,qBAAqB,CAAC;AAAC;AAExD,MAAMC,IAAwB,GAAG,IAAY;AAAC;AAE9C,MAAMC,SAA6B,GAAG,IAAY;AAAC;AAEnD,MAAMC,IAAwB,GAAG,IAAY;AAAC;AAE9C,MAAMC,IAAwB,GAAG,CACtC,MAAM,EACN,mBAAmB,EACnB,mBAAmB,EACnB,iBAAiB,CACT;;AAAC;AAGJ,MAAMC,YAAgC,GAAG,CAAC,aAAa,CAAU;AAAC;AAElE,MAAMC,cAAkC,GAAG,CAAC,aAAa,CAAU;AAAC;AAEpE,MAAMC,oBAAwC,GAAG,CACtD,sBAAsB,CACd;AAAC;AAEJ,MAAMC,4BAAgD,GAAG,CAC9D,6BAA6B,CACrB;AAAC;AAEJ,MAAMC,iBAAqC,GAAG,CACnD,gBAAgB,CACR;AAAC"}
|
package/lib/path/modification.js
CHANGED
@@ -13,15 +13,10 @@ exports.insertBefore = insertBefore;
|
|
13
13
|
exports.pushContainer = pushContainer;
|
14
14
|
exports.unshiftContainer = unshiftContainer;
|
15
15
|
exports.updateSiblingKeys = updateSiblingKeys;
|
16
|
-
|
17
16
|
var _cache = require("../cache");
|
18
|
-
|
19
17
|
var _hoister = require("./lib/hoister");
|
20
|
-
|
21
18
|
var _index = require("./index");
|
22
|
-
|
23
19
|
var _t = require("@babel/types");
|
24
|
-
|
25
20
|
const {
|
26
21
|
arrowFunctionExpression,
|
27
22
|
assertExpression,
|
@@ -41,13 +36,10 @@ const {
|
|
41
36
|
|
42
37
|
function insertBefore(nodes_) {
|
43
38
|
this._assertUnremoved();
|
44
|
-
|
45
39
|
const nodes = this._verifyNodeList(nodes_);
|
46
|
-
|
47
40
|
const {
|
48
41
|
parentPath
|
49
42
|
} = this;
|
50
|
-
|
51
43
|
if (parentPath.isExpressionStatement() || parentPath.isLabeledStatement() || parentPath.isExportNamedDeclaration() || parentPath.isExportDefaultDeclaration() && this.isDeclaration()) {
|
52
44
|
return parentPath.insertBefore(nodes);
|
53
45
|
} else if (this.isNodeType("Expression") && !this.isJSXElement() || parentPath.isForStatement() && this.key === "init") {
|
@@ -59,77 +51,64 @@ function insertBefore(nodes_) {
|
|
59
51
|
const node = this.node;
|
60
52
|
const shouldInsertCurrentNode = node && (!this.isExpressionStatement() || node.expression != null);
|
61
53
|
this.replaceWith(blockStatement(shouldInsertCurrentNode ? [node] : []));
|
62
|
-
return this.unshiftContainer("body",
|
54
|
+
return this.unshiftContainer("body",
|
55
|
+
nodes);
|
63
56
|
} else {
|
64
57
|
throw new Error("We don't know what to do with this node type. " + "We were previously a Statement but we can't fit in here?");
|
65
58
|
}
|
66
59
|
}
|
67
|
-
|
68
60
|
function _containerInsert(from, nodes) {
|
69
61
|
this.updateSiblingKeys(from, nodes.length);
|
70
62
|
const paths = [];
|
71
|
-
this.container.splice(from, 0, ...nodes);
|
72
63
|
|
64
|
+
this.container.splice(from, 0, ...nodes);
|
73
65
|
for (let i = 0; i < nodes.length; i++) {
|
74
66
|
const to = from + i;
|
75
67
|
const path = this.getSibling(to);
|
76
68
|
paths.push(path);
|
77
|
-
|
78
69
|
if (this.context && this.context.queue) {
|
79
70
|
path.pushContext(this.context);
|
80
71
|
}
|
81
72
|
}
|
82
|
-
|
83
73
|
const contexts = this._getQueueContexts();
|
84
|
-
|
85
74
|
for (const path of paths) {
|
86
75
|
path.setScope();
|
87
76
|
path.debug("Inserted.");
|
88
|
-
|
89
77
|
for (const context of contexts) {
|
90
78
|
context.maybeQueue(path, true);
|
91
79
|
}
|
92
80
|
}
|
93
|
-
|
94
81
|
return paths;
|
95
82
|
}
|
96
|
-
|
97
83
|
function _containerInsertBefore(nodes) {
|
98
84
|
return this._containerInsert(this.key, nodes);
|
99
85
|
}
|
100
|
-
|
101
86
|
function _containerInsertAfter(nodes) {
|
102
87
|
return this._containerInsert(this.key + 1, nodes);
|
103
88
|
}
|
104
|
-
|
105
89
|
const last = arr => arr[arr.length - 1];
|
106
|
-
|
107
90
|
function isHiddenInSequenceExpression(path) {
|
108
91
|
return isSequenceExpression(path.parent) && (last(path.parent.expressions) !== path.node || isHiddenInSequenceExpression(path.parentPath));
|
109
92
|
}
|
110
|
-
|
111
93
|
function isAlmostConstantAssignment(node, scope) {
|
112
94
|
if (!isAssignmentExpression(node) || !isIdentifier(node.left)) {
|
113
95
|
return false;
|
114
96
|
}
|
115
97
|
|
116
98
|
const blockScope = scope.getBlockParent();
|
99
|
+
|
117
100
|
return blockScope.hasOwnBinding(node.left.name) && blockScope.getOwnBinding(node.left.name).constantViolations.length <= 1;
|
118
101
|
}
|
119
102
|
|
120
103
|
function insertAfter(nodes_) {
|
121
104
|
this._assertUnremoved();
|
122
|
-
|
123
105
|
if (this.isSequenceExpression()) {
|
124
106
|
return last(this.get("expressions")).insertAfter(nodes_);
|
125
107
|
}
|
126
|
-
|
127
108
|
const nodes = this._verifyNodeList(nodes_);
|
128
|
-
|
129
109
|
const {
|
130
110
|
parentPath
|
131
111
|
} = this;
|
132
|
-
|
133
112
|
if (parentPath.isExpressionStatement() || parentPath.isLabeledStatement() || parentPath.isExportNamedDeclaration() || parentPath.isExportDefaultDeclaration() && this.isDeclaration()) {
|
134
113
|
return parentPath.insertAfter(nodes.map(node => {
|
135
114
|
return isExpression(node) ? expressionStatement(node) : node;
|
@@ -140,17 +119,16 @@ function insertAfter(nodes_) {
|
|
140
119
|
let {
|
141
120
|
scope
|
142
121
|
} = this;
|
143
|
-
|
144
122
|
if (scope.path.isPattern()) {
|
145
123
|
assertExpression(node);
|
146
124
|
this.replaceWith(callExpression(arrowFunctionExpression([], node), []));
|
147
125
|
this.get("callee.body").insertAfter(nodes);
|
148
126
|
return [this];
|
149
127
|
}
|
150
|
-
|
151
128
|
if (isHiddenInSequenceExpression(this)) {
|
152
129
|
nodes.unshift(node);
|
153
|
-
}
|
130
|
+
}
|
131
|
+
else if (isCallExpression(node) && isSuper(node.callee)) {
|
154
132
|
nodes.unshift(node);
|
155
133
|
nodes.push(thisExpression());
|
156
134
|
} else if (isAlmostConstantAssignment(node, scope)) {
|
@@ -165,13 +143,12 @@ function insertAfter(nodes_) {
|
|
165
143
|
})) {
|
166
144
|
scope = scope.parent;
|
167
145
|
}
|
168
|
-
|
169
146
|
const temp = scope.generateDeclaredUidIdentifier();
|
170
|
-
nodes.unshift(expressionStatement(
|
147
|
+
nodes.unshift(expressionStatement(
|
148
|
+
assignmentExpression("=", cloneNode(temp), node)));
|
171
149
|
nodes.push(expressionStatement(cloneNode(temp)));
|
172
150
|
}
|
173
151
|
}
|
174
|
-
|
175
152
|
return this.replaceExpressionWithStatements(nodes);
|
176
153
|
} else if (Array.isArray(this.container)) {
|
177
154
|
return this._containerInsertAfter(nodes);
|
@@ -187,29 +164,23 @@ function insertAfter(nodes_) {
|
|
187
164
|
|
188
165
|
function updateSiblingKeys(fromIndex, incrementBy) {
|
189
166
|
if (!this.parent) return;
|
190
|
-
|
191
167
|
const paths = _cache.path.get(this.parent);
|
192
|
-
|
193
168
|
for (const [, path] of paths) {
|
194
169
|
if (path.key >= fromIndex) {
|
195
170
|
path.key += incrementBy;
|
196
171
|
}
|
197
172
|
}
|
198
173
|
}
|
199
|
-
|
200
174
|
function _verifyNodeList(nodes) {
|
201
175
|
if (!nodes) {
|
202
176
|
return [];
|
203
177
|
}
|
204
|
-
|
205
178
|
if (!Array.isArray(nodes)) {
|
206
179
|
nodes = [nodes];
|
207
180
|
}
|
208
|
-
|
209
181
|
for (let i = 0; i < nodes.length; i++) {
|
210
182
|
const node = nodes[i];
|
211
183
|
let msg;
|
212
|
-
|
213
184
|
if (!node) {
|
214
185
|
msg = "has falsy node";
|
215
186
|
} else if (typeof node !== "object") {
|
@@ -219,16 +190,13 @@ function _verifyNodeList(nodes) {
|
|
219
190
|
} else if (node instanceof _index.default) {
|
220
191
|
msg = "has a NodePath when it expected a raw object";
|
221
192
|
}
|
222
|
-
|
223
193
|
if (msg) {
|
224
194
|
const type = Array.isArray(node) ? "array" : typeof node;
|
225
195
|
throw new Error(`Node list ${msg} with the index of ${i} and type of ${type}`);
|
226
196
|
}
|
227
197
|
}
|
228
|
-
|
229
198
|
return nodes;
|
230
199
|
}
|
231
|
-
|
232
200
|
function unshiftContainer(listKey, nodes) {
|
233
201
|
this._assertUnremoved();
|
234
202
|
|
@@ -241,17 +209,15 @@ function unshiftContainer(listKey, nodes) {
|
|
241
209
|
listKey,
|
242
210
|
key: 0
|
243
211
|
}).setContext(this.context);
|
244
|
-
|
245
|
-
|
212
|
+
return path._containerInsertBefore(
|
213
|
+
nodes);
|
246
214
|
}
|
247
|
-
|
248
215
|
function pushContainer(listKey, nodes) {
|
249
216
|
this._assertUnremoved();
|
250
|
-
|
251
|
-
|
217
|
+
const verifiedNodes = this._verifyNodeList(
|
218
|
+
nodes);
|
252
219
|
|
253
220
|
const container = this.node[listKey];
|
254
|
-
|
255
221
|
const path = _index.default.get({
|
256
222
|
parentPath: this,
|
257
223
|
parent: this.node,
|
@@ -259,7 +225,6 @@ function pushContainer(listKey, nodes) {
|
|
259
225
|
listKey,
|
260
226
|
key: container.length
|
261
227
|
}).setContext(this.context);
|
262
|
-
|
263
228
|
return path.replaceWithMultiple(verifiedNodes);
|
264
229
|
}
|
265
230
|
|