@babel/traverse 7.17.9 → 7.23.2
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.
- package/README.md +1 -1
- package/lib/cache.js +27 -7
- package/lib/cache.js.map +1 -0
- package/lib/context.js +9 -31
- package/lib/context.js.map +1 -0
- package/lib/hub.js +2 -6
- package/lib/hub.js.map +1 -0
- package/lib/index.js +17 -33
- package/lib/index.js.map +1 -0
- package/lib/path/ancestry.js +3 -42
- package/lib/path/ancestry.js.map +1 -0
- package/lib/path/comments.js +23 -11
- package/lib/path/comments.js.map +1 -0
- package/lib/path/context.js +24 -65
- package/lib/path/context.js.map +1 -0
- package/lib/path/conversion.js +37 -100
- package/lib/path/conversion.js.map +1 -0
- package/lib/path/evaluation.js +69 -123
- package/lib/path/evaluation.js.map +1 -0
- package/lib/path/family.js +5 -76
- package/lib/path/family.js.map +1 -0
- package/lib/path/index.js +26 -90
- package/lib/path/index.js.map +1 -0
- package/lib/path/inference/index.js +26 -33
- package/lib/path/inference/index.js.map +1 -0
- package/lib/path/inference/inferer-reference.js +6 -61
- package/lib/path/inference/inferer-reference.js.map +1 -0
- package/lib/path/inference/inferers.js +32 -86
- package/lib/path/inference/inferers.js.map +1 -0
- package/lib/path/inference/util.js +30 -0
- package/lib/path/inference/util.js.map +1 -0
- package/lib/path/introspection.js +54 -105
- package/lib/path/introspection.js.map +1 -0
- package/lib/path/lib/hoister.js +2 -37
- package/lib/path/lib/hoister.js.map +1 -0
- package/lib/path/lib/removal-hooks.js +4 -4
- package/lib/path/lib/removal-hooks.js.map +1 -0
- package/lib/path/lib/virtual-types-validator.js +161 -0
- package/lib/path/lib/virtual-types-validator.js.map +1 -0
- package/lib/path/lib/virtual-types.js +20 -206
- package/lib/path/lib/virtual-types.js.map +1 -0
- package/lib/path/modification.js +17 -61
- package/lib/path/modification.js.map +1 -0
- package/lib/path/removal.js +9 -22
- package/lib/path/removal.js.map +1 -0
- package/lib/path/replacement.js +17 -72
- package/lib/path/replacement.js.map +1 -0
- package/lib/scope/binding.js +20 -12
- package/lib/scope/binding.js.map +1 -0
- package/lib/scope/index.js +115 -245
- package/lib/scope/index.js.map +1 -0
- package/lib/scope/lib/renamer.js +41 -74
- package/lib/scope/lib/renamer.js.map +1 -0
- package/lib/traverse-node.js +10 -11
- package/lib/traverse-node.js.map +1 -0
- package/lib/types.js +1 -3
- package/lib/types.js.map +1 -0
- package/lib/visitors.js +59 -83
- package/lib/visitors.js.map +1 -0
- package/package.json +13 -11
- package/lib/path/generated/asserts.js +0 -5
- package/lib/path/generated/validators.js +0 -5
- package/lib/path/generated/virtual-types.js +0 -3
- package/scripts/generators/asserts.js +0 -25
- package/scripts/generators/validators.js +0 -42
- package/scripts/generators/virtual-types.js +0 -24
- package/scripts/package.json +0 -1
package/lib/path/lib/hoister.js
CHANGED
@@ -4,9 +4,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
5
5
|
});
|
6
6
|
exports.default = void 0;
|
7
|
-
|
8
7
|
var _t = require("@babel/types");
|
9
|
-
|
10
8
|
var _t2 = _t;
|
11
9
|
const {
|
12
10
|
react
|
@@ -22,22 +20,17 @@ const referenceVisitor = {
|
|
22
20
|
if (path.isJSXIdentifier() && react.isCompatTag(path.node.name) && !path.parentPath.isJSXMemberExpression()) {
|
23
21
|
return;
|
24
22
|
}
|
25
|
-
|
26
23
|
if (path.node.name === "this") {
|
27
24
|
let scope = path.scope;
|
28
|
-
|
29
25
|
do {
|
30
26
|
if (scope.path.isFunction() && !scope.path.isArrowFunctionExpression()) {
|
31
27
|
break;
|
32
28
|
}
|
33
29
|
} while (scope = scope.parent);
|
34
|
-
|
35
30
|
if (scope) state.breakOnScopePaths.push(scope.path);
|
36
31
|
}
|
37
|
-
|
38
32
|
const binding = path.scope.getBinding(path.node.name);
|
39
33
|
if (!binding) return;
|
40
|
-
|
41
34
|
for (const violation of binding.constantViolations) {
|
42
35
|
if (violation.scope !== binding.path.scope) {
|
43
36
|
state.mutableBinding = true;
|
@@ -45,13 +38,10 @@ const referenceVisitor = {
|
|
45
38
|
return;
|
46
39
|
}
|
47
40
|
}
|
48
|
-
|
49
41
|
if (binding !== state.scope.getBinding(path.node.name)) return;
|
50
42
|
state.bindings[path.node.name] = binding;
|
51
43
|
}
|
52
|
-
|
53
44
|
};
|
54
|
-
|
55
45
|
class PathHoister {
|
56
46
|
constructor(path, scope) {
|
57
47
|
this.breakOnScopePaths = void 0;
|
@@ -69,60 +59,46 @@ class PathHoister {
|
|
69
59
|
this.path = path;
|
70
60
|
this.attachAfter = false;
|
71
61
|
}
|
72
|
-
|
73
62
|
isCompatibleScope(scope) {
|
74
63
|
for (const key of Object.keys(this.bindings)) {
|
75
64
|
const binding = this.bindings[key];
|
76
|
-
|
77
65
|
if (!scope.bindingIdentifierEquals(key, binding.identifier)) {
|
78
66
|
return false;
|
79
67
|
}
|
80
68
|
}
|
81
|
-
|
82
69
|
return true;
|
83
70
|
}
|
84
|
-
|
85
71
|
getCompatibleScopes() {
|
86
72
|
let scope = this.path.scope;
|
87
|
-
|
88
73
|
do {
|
89
74
|
if (this.isCompatibleScope(scope)) {
|
90
75
|
this.scopes.push(scope);
|
91
76
|
} else {
|
92
77
|
break;
|
93
78
|
}
|
94
|
-
|
95
79
|
if (this.breakOnScopePaths.indexOf(scope.path) >= 0) {
|
96
80
|
break;
|
97
81
|
}
|
98
82
|
} while (scope = scope.parent);
|
99
83
|
}
|
100
|
-
|
101
84
|
getAttachmentPath() {
|
102
85
|
let path = this._getAttachmentPath();
|
103
|
-
|
104
86
|
if (!path) return;
|
105
87
|
let targetScope = path.scope;
|
106
|
-
|
107
88
|
if (targetScope.path === path) {
|
108
89
|
targetScope = path.scope.parent;
|
109
90
|
}
|
110
|
-
|
111
91
|
if (targetScope.path.isProgram() || targetScope.path.isFunction()) {
|
112
92
|
for (const name of Object.keys(this.bindings)) {
|
113
93
|
if (!targetScope.hasOwnBinding(name)) continue;
|
114
94
|
const binding = this.bindings[name];
|
115
|
-
|
116
95
|
if (binding.kind === "param" || binding.path.parentKey === "params") {
|
117
96
|
continue;
|
118
97
|
}
|
119
|
-
|
120
98
|
const bindingParentPath = this.getAttachmentParentForPath(binding.path);
|
121
|
-
|
122
99
|
if (bindingParentPath.key >= path.key) {
|
123
100
|
this.attachAfter = true;
|
124
101
|
path = binding.path;
|
125
|
-
|
126
102
|
for (const violationPath of binding.constantViolations) {
|
127
103
|
if (this.getAttachmentParentForPath(violationPath).key > path.key) {
|
128
104
|
path = violationPath;
|
@@ -131,20 +107,16 @@ class PathHoister {
|
|
131
107
|
}
|
132
108
|
}
|
133
109
|
}
|
134
|
-
|
135
110
|
return path;
|
136
111
|
}
|
137
|
-
|
138
112
|
_getAttachmentPath() {
|
139
113
|
const scopes = this.scopes;
|
140
114
|
const scope = scopes.pop();
|
141
115
|
if (!scope) return;
|
142
|
-
|
143
116
|
if (scope.path.isFunction()) {
|
144
117
|
if (this.hasOwnParamBindings(scope)) {
|
145
118
|
if (this.scope === scope) return;
|
146
119
|
const bodies = scope.path.get("body").get("body");
|
147
|
-
|
148
120
|
for (let i = 0; i < bodies.length; i++) {
|
149
121
|
if (bodies[i].node._blockHoist) continue;
|
150
122
|
return bodies[i];
|
@@ -156,12 +128,10 @@ class PathHoister {
|
|
156
128
|
return this.getNextScopeAttachmentParent();
|
157
129
|
}
|
158
130
|
}
|
159
|
-
|
160
131
|
getNextScopeAttachmentParent() {
|
161
132
|
const scope = this.scopes.pop();
|
162
133
|
if (scope) return this.getAttachmentParentForPath(scope.path);
|
163
134
|
}
|
164
|
-
|
165
135
|
getAttachmentParentForPath(path) {
|
166
136
|
do {
|
167
137
|
if (!path.parentPath || Array.isArray(path.container) && path.isStatement()) {
|
@@ -169,17 +139,14 @@ class PathHoister {
|
|
169
139
|
}
|
170
140
|
} while (path = path.parentPath);
|
171
141
|
}
|
172
|
-
|
173
142
|
hasOwnParamBindings(scope) {
|
174
143
|
for (const name of Object.keys(this.bindings)) {
|
175
144
|
if (!scope.hasOwnBinding(name)) continue;
|
176
145
|
const binding = this.bindings[name];
|
177
146
|
if (binding.kind === "param" && binding.constant) return true;
|
178
147
|
}
|
179
|
-
|
180
148
|
return false;
|
181
149
|
}
|
182
|
-
|
183
150
|
run() {
|
184
151
|
this.path.traverse(referenceVisitor, this);
|
185
152
|
if (this.mutableBinding) return;
|
@@ -192,15 +159,13 @@ class PathHoister {
|
|
192
159
|
const insertFn = this.attachAfter ? "insertAfter" : "insertBefore";
|
193
160
|
const [attached] = attachTo[insertFn]([attachTo.isVariableDeclarator() ? declarator : variableDeclaration("var", [declarator])]);
|
194
161
|
const parent = this.path.parentPath;
|
195
|
-
|
196
162
|
if (parent.isJSXElement() && this.path.container === parent.node.children) {
|
197
163
|
uid = jsxExpressionContainer(uid);
|
198
164
|
}
|
199
|
-
|
200
165
|
this.path.replaceWith(cloneNode(uid));
|
201
166
|
return attachTo.isVariableDeclarator() ? attached.get("init") : attached.get("declarations.0.init");
|
202
167
|
}
|
203
|
-
|
204
168
|
}
|
169
|
+
exports.default = PathHoister;
|
205
170
|
|
206
|
-
|
171
|
+
//# sourceMappingURL=hoister.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"names":["_t","require","_t2","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","exports","default"],"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/index.ts\";\nimport type NodePath from \"../index.ts\";\nimport type Binding from \"../../scope/binding.ts\";\nimport type { Visitor } from \"../../types.ts\";\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(): NodePath<t.Expression> | undefined {\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,IAAAA,EAAA,GAAAC,OAAA;AAAqC,IAAAC,GAAA,GAAAF,EAAA;AAAA;EAA5BG;AAAK,IAAAH,EAAA;AAAA;EAEZI,SAAS;EACTC,sBAAsB;EACtBC,mBAAmB;EACnBC;AAAkB,IAAAL,GAAA;AAQpB,MAAMM,gBAAsC,GAAG;EAE7CC,oBAAoBA,CAACC,IAAI,EAAEC,KAAK,EAAE;IAGhC,IACED,IAAI,CAACE,eAAe,CAAC,CAAC,IACtBT,KAAK,CAACU,WAAW,CAACH,IAAI,CAACI,IAAI,CAACC,IAAI,CAAC,IACjC,CAACL,IAAI,CAACM,UAAU,CAACC,qBAAqB,CAAC,CAAC,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,CAAC,CAAC,IACvB,CAACD,KAAK,CAACR,IAAI,CAACU,yBAAyB,CAAC,CAAC,EACvC;UACA;QACF;MACF,CAAC,QAASF,KAAK,GA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AAAjD,WAAA"}
|
@@ -6,7 +6,6 @@ Object.defineProperty(exports, "__esModule", {
|
|
6
6
|
exports.hooks = void 0;
|
7
7
|
const hooks = [function (self, parent) {
|
8
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
|
-
|
10
9
|
if (removeParent) {
|
11
10
|
parent.remove();
|
12
11
|
return true;
|
@@ -23,11 +22,10 @@ const hooks = [function (self, parent) {
|
|
23
22
|
} else {
|
24
23
|
parent.replaceWith(parent.node.left);
|
25
24
|
}
|
26
|
-
|
27
25
|
return true;
|
28
26
|
}
|
29
27
|
}, function (self, parent) {
|
30
|
-
if (parent.isIfStatement() &&
|
28
|
+
if (parent.isIfStatement() && self.key === "consequent" || self.key === "body" && (parent.isLoop() || parent.isArrowFunctionExpression())) {
|
31
29
|
self.replaceWith({
|
32
30
|
type: "BlockStatement",
|
33
31
|
body: []
|
@@ -35,4 +33,6 @@ const hooks = [function (self, parent) {
|
|
35
33
|
return true;
|
36
34
|
}
|
37
35
|
}];
|
38
|
-
exports.hooks = hooks;
|
36
|
+
exports.hooks = hooks;
|
37
|
+
|
38
|
+
//# sourceMappingURL=removal-hooks.js.map
|
@@ -0,0 +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","exports"],"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 \"../index.ts\";\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,GAKfF,IAAI,CAACG,GAAG,KAAK,MAAM,KAAKF,MAAM,CAACG,OA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AAC,IAAIrB,IAAI,CAACG,GAAG,KAAK,YAAY,IACnDH,IAAI,CAACG,GAAG,KAAK,MAAM,KACjBF,MAAM,CAACqB,MAAM,CAAC,CAAC,IAAIrB,MAAM,CAACsB,yBAAyB,CAAC,CAAC,CAAE,EAC1D;IACAvB,IAAI,CAACiB,WAAW,CAAC;MACfO,IAAI,EAAE,gBAAgB;MACtBC,IAAI,EAAE;IACR,CAAqB,CAAC;IACtB,OAAO,IAAI;EACb;AACF,CAAC,CACF;AAACC,OAAA,CAAA3B,KAAA,GAAAA,KAAA"}
|
@@ -0,0 +1,161 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports.isBindingIdentifier = isBindingIdentifier;
|
7
|
+
exports.isBlockScoped = isBlockScoped;
|
8
|
+
exports.isExpression = isExpression;
|
9
|
+
exports.isFlow = isFlow;
|
10
|
+
exports.isForAwaitStatement = isForAwaitStatement;
|
11
|
+
exports.isGenerated = isGenerated;
|
12
|
+
exports.isPure = isPure;
|
13
|
+
exports.isReferenced = isReferenced;
|
14
|
+
exports.isReferencedIdentifier = isReferencedIdentifier;
|
15
|
+
exports.isReferencedMemberExpression = isReferencedMemberExpression;
|
16
|
+
exports.isRestProperty = isRestProperty;
|
17
|
+
exports.isScope = isScope;
|
18
|
+
exports.isSpreadProperty = isSpreadProperty;
|
19
|
+
exports.isStatement = isStatement;
|
20
|
+
exports.isUser = isUser;
|
21
|
+
exports.isVar = isVar;
|
22
|
+
var _t = require("@babel/types");
|
23
|
+
const {
|
24
|
+
isBinding,
|
25
|
+
isBlockScoped: nodeIsBlockScoped,
|
26
|
+
isExportDeclaration,
|
27
|
+
isExpression: nodeIsExpression,
|
28
|
+
isFlow: nodeIsFlow,
|
29
|
+
isForStatement,
|
30
|
+
isForXStatement,
|
31
|
+
isIdentifier,
|
32
|
+
isImportDeclaration,
|
33
|
+
isImportSpecifier,
|
34
|
+
isJSXIdentifier,
|
35
|
+
isJSXMemberExpression,
|
36
|
+
isMemberExpression,
|
37
|
+
isRestElement: nodeIsRestElement,
|
38
|
+
isReferenced: nodeIsReferenced,
|
39
|
+
isScope: nodeIsScope,
|
40
|
+
isStatement: nodeIsStatement,
|
41
|
+
isVar: nodeIsVar,
|
42
|
+
isVariableDeclaration,
|
43
|
+
react,
|
44
|
+
isForOfStatement
|
45
|
+
} = _t;
|
46
|
+
const {
|
47
|
+
isCompatTag
|
48
|
+
} = react;
|
49
|
+
function isReferencedIdentifier(opts) {
|
50
|
+
const {
|
51
|
+
node,
|
52
|
+
parent
|
53
|
+
} = this;
|
54
|
+
if (!isIdentifier(node, opts) && !isJSXMemberExpression(parent, opts)) {
|
55
|
+
if (isJSXIdentifier(node, opts)) {
|
56
|
+
if (isCompatTag(node.name)) return false;
|
57
|
+
} else {
|
58
|
+
return false;
|
59
|
+
}
|
60
|
+
}
|
61
|
+
return nodeIsReferenced(node, parent, this.parentPath.parent);
|
62
|
+
}
|
63
|
+
function isReferencedMemberExpression() {
|
64
|
+
const {
|
65
|
+
node,
|
66
|
+
parent
|
67
|
+
} = this;
|
68
|
+
return isMemberExpression(node) && nodeIsReferenced(node, parent);
|
69
|
+
}
|
70
|
+
function isBindingIdentifier() {
|
71
|
+
const {
|
72
|
+
node,
|
73
|
+
parent
|
74
|
+
} = this;
|
75
|
+
const grandparent = this.parentPath.parent;
|
76
|
+
return isIdentifier(node) && isBinding(node, parent, grandparent);
|
77
|
+
}
|
78
|
+
function isStatement() {
|
79
|
+
const {
|
80
|
+
node,
|
81
|
+
parent
|
82
|
+
} = this;
|
83
|
+
if (nodeIsStatement(node)) {
|
84
|
+
if (isVariableDeclaration(node)) {
|
85
|
+
if (isForXStatement(parent, {
|
86
|
+
left: node
|
87
|
+
})) return false;
|
88
|
+
if (isForStatement(parent, {
|
89
|
+
init: node
|
90
|
+
})) return false;
|
91
|
+
}
|
92
|
+
return true;
|
93
|
+
} else {
|
94
|
+
return false;
|
95
|
+
}
|
96
|
+
}
|
97
|
+
function isExpression() {
|
98
|
+
if (this.isIdentifier()) {
|
99
|
+
return this.isReferencedIdentifier();
|
100
|
+
} else {
|
101
|
+
return nodeIsExpression(this.node);
|
102
|
+
}
|
103
|
+
}
|
104
|
+
function isScope() {
|
105
|
+
return nodeIsScope(this.node, this.parent);
|
106
|
+
}
|
107
|
+
function isReferenced() {
|
108
|
+
return nodeIsReferenced(this.node, this.parent);
|
109
|
+
}
|
110
|
+
function isBlockScoped() {
|
111
|
+
return nodeIsBlockScoped(this.node);
|
112
|
+
}
|
113
|
+
function isVar() {
|
114
|
+
return nodeIsVar(this.node);
|
115
|
+
}
|
116
|
+
function isUser() {
|
117
|
+
return this.node && !!this.node.loc;
|
118
|
+
}
|
119
|
+
function isGenerated() {
|
120
|
+
return !this.isUser();
|
121
|
+
}
|
122
|
+
function isPure(constantsOnly) {
|
123
|
+
return this.scope.isPure(this.node, constantsOnly);
|
124
|
+
}
|
125
|
+
function isFlow() {
|
126
|
+
const {
|
127
|
+
node
|
128
|
+
} = this;
|
129
|
+
if (nodeIsFlow(node)) {
|
130
|
+
return true;
|
131
|
+
} else if (isImportDeclaration(node)) {
|
132
|
+
return node.importKind === "type" || node.importKind === "typeof";
|
133
|
+
} else if (isExportDeclaration(node)) {
|
134
|
+
return node.exportKind === "type";
|
135
|
+
} else if (isImportSpecifier(node)) {
|
136
|
+
return node.importKind === "type" || node.importKind === "typeof";
|
137
|
+
} else {
|
138
|
+
return false;
|
139
|
+
}
|
140
|
+
}
|
141
|
+
function isRestProperty() {
|
142
|
+
return nodeIsRestElement(this.node) && this.parentPath && this.parentPath.isObjectPattern();
|
143
|
+
}
|
144
|
+
function isSpreadProperty() {
|
145
|
+
return nodeIsRestElement(this.node) && this.parentPath && this.parentPath.isObjectExpression();
|
146
|
+
}
|
147
|
+
function isForAwaitStatement() {
|
148
|
+
return isForOfStatement(this.node, {
|
149
|
+
await: true
|
150
|
+
});
|
151
|
+
}
|
152
|
+
{
|
153
|
+
exports.isExistentialTypeParam = function isExistentialTypeParam() {
|
154
|
+
throw new Error("`path.isExistentialTypeParam` has been renamed to `path.isExistsTypeAnnotation()` in Babel 7.");
|
155
|
+
};
|
156
|
+
exports.isNumericLiteralTypeAnnotation = function isNumericLiteralTypeAnnotation() {
|
157
|
+
throw new Error("`path.isNumericLiteralTypeAnnotation()` has been renamed to `path.isNumberLiteralTypeAnnotation()` in Babel 7.");
|
158
|
+
};
|
159
|
+
}
|
160
|
+
|
161
|
+
//# sourceMappingURL=virtual-types-validator.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"names":["_t","require","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","exports","isExistentialTypeParam","Error","isNumericLiteralTypeAnnotation"],"sources":["../../../src/path/lib/virtual-types-validator.ts"],"sourcesContent":["import type NodePath from \"../index.ts\";\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.ts\";\n\ntype Opts<Obj> = Partial<{\n [Prop in keyof Obj]: Obj[Prop] extends t.Node\n ? t.Node\n : Obj[Prop] extends t.Node[]\n ? t.Node[]\n : Obj[Prop];\n}>;\n\nexport interface VirtualTypeNodePathValidators {\n isBindingIdentifier<T extends t.Node>(\n this: NodePath<T>,\n opts?: Opts<VirtualTypeAliases[\"BindingIdentifier\"]>,\n ): this is NodePath<T & VirtualTypeAliases[\"BindingIdentifier\"]>;\n isBlockScoped(opts?: Opts<VirtualTypeAliases[\"BlockScoped\"]>): boolean;\n /**\n * @deprecated\n */\n isExistentialTypeParam<T extends t.Node>(\n this: NodePath<T>,\n opts?: Opts<VirtualTypeAliases[\"ExistentialTypeParam\"]>,\n ): this is NodePath<T & VirtualTypeAliases[\"ExistentialTypeParam\"]>;\n isExpression<T extends t.Node>(\n this: NodePath<T>,\n opts?: Opts<VirtualTypeAliases[\"Expression\"]>,\n ): this is NodePath<T & t.Expression>;\n isFlow<T extends t.Node>(\n this: NodePath<T>,\n opts?: Opts<VirtualTypeAliases[\"Flow\"]>,\n ): this is NodePath<T & t.Flow>;\n isForAwaitStatement<T extends t.Node>(\n this: NodePath<T>,\n opts?: Opts<VirtualTypeAliases[\"ForAwaitStatement\"]>,\n ): this is NodePath<T & VirtualTypeAliases[\"ForAwaitStatement\"]>;\n isGenerated(opts?: VirtualTypeAliases[\"Generated\"]): boolean;\n /**\n * @deprecated\n */\n isNumericLiteralTypeAnnotation(\n opts?: VirtualTypeAliases[\"NumericLiteralTypeAnnotation\"],\n ): void;\n isPure(opts?: VirtualTypeAliases[\"Pure\"]): boolean;\n isReferenced(opts?: VirtualTypeAliases[\"Referenced\"]): boolean;\n isReferencedIdentifier<T extends t.Node>(\n this: NodePath<T>,\n opts?: Opts<VirtualTypeAliases[\"ReferencedIdentifier\"]>,\n ): this is NodePath<T & VirtualTypeAliases[\"ReferencedIdentifier\"]>;\n isReferencedMemberExpression<T extends t.Node>(\n this: NodePath<T>,\n opts?: Opts<VirtualTypeAliases[\"ReferencedMemberExpression\"]>,\n ): this is NodePath<T & VirtualTypeAliases[\"ReferencedMemberExpression\"]>;\n isRestProperty<T extends t.Node>(\n this: NodePath<T>,\n opts?: Opts<VirtualTypeAliases[\"RestProperty\"]>,\n ): this is NodePath<T & t.RestProperty>;\n isScope<T extends t.Node>(\n this: NodePath<T>,\n opts?: Opts<VirtualTypeAliases[\"Scope\"]>,\n ): this is NodePath<T & VirtualTypeAliases[\"Scope\"]>;\n isSpreadProperty<T extends t.Node>(\n this: NodePath<T>,\n opts?: Opts<VirtualTypeAliases[\"SpreadProperty\"]>,\n ): this is NodePath<T & t.SpreadProperty>;\n isStatement<T extends t.Node>(\n this: NodePath<T>,\n opts?: Opts<VirtualTypeAliases[\"Statement\"]>,\n ): this is NodePath<T & t.Statement>;\n isUser(opts?: VirtualTypeAliases[\"User\"]): boolean;\n isVar<T extends t.Node>(\n this: NodePath<T>,\n opts?: Opts<VirtualTypeAliases[\"Var\"]>,\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\nif (!process.env.BABEL_8_BREAKING && !USE_ESM) {\n // eslint-disable-next-line no-restricted-globals\n exports.isExistentialTypeParam = function isExistentialTypeParam(\n this: NodePath,\n ): void {\n throw new Error(\n \"`path.isExistentialTypeParam` has been renamed to `path.isExistsTypeAnnotation()` in Babel 7.\",\n );\n };\n\n // eslint-disable-next-line no-restricted-globals\n exports.isNumericLiteralTypeAnnotation =\n function isNumericLiteralTypeAnnotation(this: NodePath): void {\n throw new Error(\n \"`path.isNumericLiteralTypeAnnotation()` has been renamed to `path.isNumberLiteralTypeAnnotation()` in Babel 7.\",\n );\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AACA,IAAAA,EAAA,GAAAC,OAAA;AAsBsB;EArBpBC,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,IAAA9B,EAAA;AAGlB,MAAM;EAAE+B;AAAY,CAAC,GAAGF,KAAK;AA4EtB,SAASG,sBAAsBA,CAAiBC,IAAU,EAAW;EAC1E,MAAM;IAAEC,IAAI;IAAEC;EAAO,CAAC,GAAG,IAAI;EAC7B,IAAI,CAACvB,YAAY,CAACsB,IAAI,EAAED,IAAI,CAAC,IAAI,CAACjB,qBAAqB,CAACmB,MAAM,EAAEF,IAAI,CAAC,EAAE;IACrE,IAAIlB,eAAe,CAACmB,IAAI,EAAED,IAAI,CAAC,EAAE;MAC/B,IAAIF,WAAW,CAACG,IAAI,CAACE,IAAI,CAAC,EAAE,OAAO,KAAK;IAC1C,CAAC,MAAM;MAEL,OAAO,KAAK;IACd;EACF;EAGA,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