@babel/traverse 7.18.5 → 7.18.9

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/context.js CHANGED
@@ -31,17 +31,19 @@ class TraversalContext {
31
31
  if (!(keys != null && keys.length)) return false;
32
32
 
33
33
  for (const key of keys) {
34
- if (node[key]) return true;
34
+ if (node[key]) {
35
+ return true;
36
+ }
35
37
  }
36
38
 
37
39
  return false;
38
40
  }
39
41
 
40
- create(node, obj, key, listKey) {
42
+ create(node, container, key, listKey) {
41
43
  return _path.default.get({
42
44
  parentPath: this.parentPath,
43
45
  parent: node,
44
- container: obj,
46
+ container,
45
47
  key: key,
46
48
  listKey
47
49
  });
@@ -181,7 +181,10 @@ function _resyncParent() {
181
181
 
182
182
  function _resyncKey() {
183
183
  if (!this.container) return;
184
- if (this.node === this.container[this.key]) return;
184
+
185
+ if (this.node === this.container[this.key]) {
186
+ return;
187
+ }
185
188
 
186
189
  if (Array.isArray(this.container)) {
187
190
  for (let i = 0; i < this.container.length; i++) {
@@ -251,10 +251,11 @@ function hoistFunctionEnvironment(fnPath, noNewArrows = true, allowInsertArrow =
251
251
  const flatSuperProps = superProps.reduce((acc, superProp) => acc.concat(standardizeSuperProperty(superProp)), []);
252
252
  flatSuperProps.forEach(superProp => {
253
253
  const key = superProp.node.computed ? "" : superProp.get("property").node.name;
254
- const isAssignment = superProp.parentPath.isAssignmentExpression({
254
+ const superParentPath = superProp.parentPath;
255
+ const isAssignment = superParentPath.isAssignmentExpression({
255
256
  left: superProp.node
256
257
  });
257
- const isCall = superProp.parentPath.isCallExpression({
258
+ const isCall = superParentPath.isCallExpression({
258
259
  callee: superProp.node
259
260
  });
260
261
  const superBinding = getSuperPropBinding(thisEnvFn, isAssignment, key);
@@ -265,18 +266,18 @@ function hoistFunctionEnvironment(fnPath, noNewArrows = true, allowInsertArrow =
265
266
  }
266
267
 
267
268
  if (isAssignment) {
268
- const value = superProp.parentPath.node.right;
269
+ const value = superParentPath.node.right;
269
270
  args.push(value);
270
271
  }
271
272
 
272
273
  const call = callExpression(identifier(superBinding), args);
273
274
 
274
275
  if (isCall) {
275
- superProp.parentPath.unshiftContainer("arguments", thisExpression());
276
+ superParentPath.unshiftContainer("arguments", thisExpression());
276
277
  superProp.replaceWith(memberExpression(call, identifier("call")));
277
- thisPaths.push(superProp.parentPath.get("arguments.0"));
278
+ thisPaths.push(superParentPath.get("arguments.0"));
278
279
  } else if (isAssignment) {
279
- superProp.parentPath.replaceWith(call);
280
+ superParentPath.replaceWith(call);
280
281
  } else {
281
282
  superProp.replaceWith(call);
282
283
  }
@@ -8,6 +8,14 @@ exports.evaluateTruthy = evaluateTruthy;
8
8
  const VALID_CALLEES = ["String", "Number", "Math"];
9
9
  const INVALID_METHODS = ["random"];
10
10
 
11
+ function isValidCallee(val) {
12
+ return VALID_CALLEES.includes(val);
13
+ }
14
+
15
+ function isInvalidMethod(val) {
16
+ return INVALID_METHODS.includes(val);
17
+ }
18
+
11
19
  function evaluateTruthy() {
12
20
  const res = this.evaluate();
13
21
  if (res.confident) return !!res.value;
@@ -336,7 +344,7 @@ function _evaluate(path, state) {
336
344
  let context;
337
345
  let func;
338
346
 
339
- if (callee.isIdentifier() && !path.scope.getBinding(callee.node.name) && VALID_CALLEES.indexOf(callee.node.name) >= 0) {
347
+ if (callee.isIdentifier() && !path.scope.getBinding(callee.node.name) && isValidCallee(callee.node.name)) {
340
348
  func = global[callee.node.name];
341
349
  }
342
350
 
@@ -344,7 +352,7 @@ function _evaluate(path, state) {
344
352
  const object = callee.get("object");
345
353
  const property = callee.get("property");
346
354
 
347
- if (object.isIdentifier() && property.isIdentifier() && VALID_CALLEES.indexOf(object.node.name) >= 0 && INVALID_METHODS.indexOf(property.node.name) < 0) {
355
+ if (object.isIdentifier() && property.isIdentifier() && isValidCallee(object.node.name) && !isInvalidMethod(property.node.name)) {
348
356
  context = global[object.node.name];
349
357
  func = context[property.node.name];
350
358
  }
@@ -17,6 +17,7 @@ var _t = require("@babel/types");
17
17
  const {
18
18
  anyTypeAnnotation,
19
19
  isAnyTypeAnnotation,
20
+ isArrayTypeAnnotation,
20
21
  isBooleanTypeAnnotation,
21
22
  isEmptyTypeAnnotation,
22
23
  isFlowBaseAnnotation,
@@ -25,6 +26,10 @@ const {
25
26
  isMixedTypeAnnotation,
26
27
  isNumberTypeAnnotation,
27
28
  isStringTypeAnnotation,
29
+ isTSArrayType,
30
+ isTSTypeAnnotation,
31
+ isTSTypeReference,
32
+ isTupleTypeAnnotation,
28
33
  isTypeAnnotation,
29
34
  isUnionTypeAnnotation,
30
35
  isVoidTypeAnnotation,
@@ -40,7 +45,11 @@ function getTypeAnnotation() {
40
45
  }
41
46
 
42
47
  type = this._getTypeAnnotation() || anyTypeAnnotation();
43
- if (isTypeAnnotation(type)) type = type.typeAnnotation;
48
+
49
+ if (isTypeAnnotation(type) || isTSTypeAnnotation(type)) {
50
+ type = type.typeAnnotation;
51
+ }
52
+
44
53
  this.setData("typeAnnotation", type);
45
54
  return type;
46
55
  }
@@ -156,7 +165,16 @@ function baseTypeStrictlyMatches(rightArg) {
156
165
 
157
166
  function isGenericType(genericName) {
158
167
  const type = this.getTypeAnnotation();
168
+
169
+ if (genericName === "Array") {
170
+ if (isTSArrayType(type) || isArrayTypeAnnotation(type) || isTupleTypeAnnotation(type)) {
171
+ return true;
172
+ }
173
+ }
174
+
159
175
  return isGenericTypeAnnotation(type) && isIdentifier(type.id, {
160
176
  name: genericName
177
+ }) || isTSTypeReference(type) && isIdentifier(type.typeName, {
178
+ name: genericName
161
179
  });
162
180
  }
@@ -7,13 +7,11 @@ exports.default = _default;
7
7
 
8
8
  var _t = require("@babel/types");
9
9
 
10
+ var _util = require("./util");
11
+
10
12
  const {
11
13
  BOOLEAN_NUMBER_BINARY_OPERATORS,
12
- createFlowUnionType,
13
- createTSUnionType,
14
14
  createTypeAnnotationBasedOnTypeof,
15
- createUnionTypeAnnotation,
16
- isTSTypeAnnotation,
17
15
  numberTypeAnnotation,
18
16
  voidTypeAnnotation
19
17
  } = _t;
@@ -61,15 +59,7 @@ function getTypeAnnotationBindingConstantViolations(binding, path, name) {
61
59
  return;
62
60
  }
63
61
 
64
- if (isTSTypeAnnotation(types[0]) && createTSUnionType) {
65
- return createTSUnionType(types);
66
- }
67
-
68
- if (createFlowUnionType) {
69
- return createFlowUnionType(types);
70
- }
71
-
72
- return createUnionTypeAnnotation(types);
62
+ return (0, _util.createUnionType)(types);
73
63
  }
74
64
 
75
65
  function getConstantViolationsBefore(binding, path, functions) {
@@ -182,25 +172,11 @@ function getConditionalAnnotation(binding, path, name) {
182
172
  }
183
173
 
184
174
  if (types.length) {
185
- if (isTSTypeAnnotation(types[0]) && createTSUnionType) {
186
- return {
187
- typeAnnotation: createTSUnionType(types),
188
- ifStatement
189
- };
190
- }
191
-
192
- if (createFlowUnionType) {
193
- return {
194
- typeAnnotation: createFlowUnionType(types),
195
- ifStatement
196
- };
197
- }
198
-
199
175
  return {
200
- typeAnnotation: createUnionTypeAnnotation(types),
176
+ typeAnnotation: (0, _util.createUnionType)(types),
201
177
  ifStatement
202
178
  };
203
179
  }
204
180
 
205
- return getConditionalAnnotation(ifStatement, name);
181
+ return getConditionalAnnotation(binding, ifStatement, name);
206
182
  }
@@ -26,6 +26,8 @@ exports.RegExpLiteral = RegExpLiteral;
26
26
  exports.RestElement = RestElement;
27
27
  exports.SequenceExpression = SequenceExpression;
28
28
  exports.StringLiteral = StringLiteral;
29
+ exports.TSAsExpression = TSAsExpression;
30
+ exports.TSNonNullExpression = TSNonNullExpression;
29
31
  exports.TaggedTemplateExpression = TaggedTemplateExpression;
30
32
  exports.TemplateLiteral = TemplateLiteral;
31
33
  exports.TypeCastExpression = TypeCastExpression;
@@ -37,6 +39,8 @@ var _t = require("@babel/types");
37
39
 
38
40
  var _infererReference = require("./inferer-reference");
39
41
 
42
+ var _util = require("./util");
43
+
40
44
  const {
41
45
  BOOLEAN_BINARY_OPERATORS,
42
46
  BOOLEAN_UNARY_OPERATORS,
@@ -47,37 +51,20 @@ const {
47
51
  arrayTypeAnnotation,
48
52
  booleanTypeAnnotation,
49
53
  buildMatchMemberExpression,
50
- createFlowUnionType,
51
- createTSUnionType,
52
- createUnionTypeAnnotation,
53
54
  genericTypeAnnotation,
54
55
  identifier,
55
- isTSTypeAnnotation,
56
56
  nullLiteralTypeAnnotation,
57
57
  numberTypeAnnotation,
58
58
  stringTypeAnnotation,
59
59
  tupleTypeAnnotation,
60
60
  unionTypeAnnotation,
61
- voidTypeAnnotation
61
+ voidTypeAnnotation,
62
+ isIdentifier
62
63
  } = _t;
63
64
 
64
65
  function VariableDeclarator() {
65
- var _type;
66
-
67
- const id = this.get("id");
68
- if (!id.isIdentifier()) return;
69
- const init = this.get("init");
70
- let type = init.getTypeAnnotation();
71
-
72
- if (((_type = type) == null ? void 0 : _type.type) === "AnyTypeAnnotation") {
73
- if (init.isCallExpression() && init.get("callee").isIdentifier({
74
- name: "Array"
75
- }) && !init.scope.hasBinding("Array", true)) {
76
- type = ArrayExpression();
77
- }
78
- }
79
-
80
- return type;
66
+ if (!this.get("id").isIdentifier()) return;
67
+ return this.get("init").getTypeAnnotation();
81
68
  }
82
69
 
83
70
  function TypeCastExpression(node) {
@@ -86,8 +73,18 @@ function TypeCastExpression(node) {
86
73
 
87
74
  TypeCastExpression.validParent = true;
88
75
 
76
+ function TSAsExpression(node) {
77
+ return node.typeAnnotation;
78
+ }
79
+
80
+ TSAsExpression.validParent = true;
81
+
82
+ function TSNonNullExpression() {
83
+ return this.get("expression").getTypeAnnotation();
84
+ }
85
+
89
86
  function NewExpression(node) {
90
- if (this.get("callee").isIdentifier()) {
87
+ if (node.callee.type === "Identifier") {
91
88
  return genericTypeAnnotation(node.callee);
92
89
  }
93
90
  }
@@ -133,30 +130,12 @@ function BinaryExpression(node) {
133
130
 
134
131
  function LogicalExpression() {
135
132
  const argumentTypes = [this.get("left").getTypeAnnotation(), this.get("right").getTypeAnnotation()];
136
-
137
- if (isTSTypeAnnotation(argumentTypes[0]) && createTSUnionType) {
138
- return createTSUnionType(argumentTypes);
139
- }
140
-
141
- if (createFlowUnionType) {
142
- return createFlowUnionType(argumentTypes);
143
- }
144
-
145
- return createUnionTypeAnnotation(argumentTypes);
133
+ return (0, _util.createUnionType)(argumentTypes);
146
134
  }
147
135
 
148
136
  function ConditionalExpression() {
149
137
  const argumentTypes = [this.get("consequent").getTypeAnnotation(), this.get("alternate").getTypeAnnotation()];
150
-
151
- if (isTSTypeAnnotation(argumentTypes[0]) && createTSUnionType) {
152
- return createTSUnionType(argumentTypes);
153
- }
154
-
155
- if (createFlowUnionType) {
156
- return createFlowUnionType(argumentTypes);
157
- }
158
-
159
- return createUnionTypeAnnotation(argumentTypes);
138
+ return (0, _util.createUnionType)(argumentTypes);
160
139
  }
161
140
 
162
141
  function SequenceExpression() {
@@ -229,7 +208,9 @@ function CallExpression() {
229
208
 
230
209
  if (isObjectKeys(callee)) {
231
210
  return arrayTypeAnnotation(stringTypeAnnotation());
232
- } else if (isArrayFrom(callee) || isObjectValues(callee)) {
211
+ } else if (isArrayFrom(callee) || isObjectValues(callee) || isIdentifier(callee, {
212
+ name: "Array"
213
+ })) {
233
214
  return arrayTypeAnnotation(anyTypeAnnotation());
234
215
  } else if (isObjectEntries(callee)) {
235
216
  return arrayTypeAnnotation(tupleTypeAnnotation([stringTypeAnnotation(), anyTypeAnnotation()]));
@@ -246,14 +227,20 @@ function resolveCall(callee) {
246
227
  callee = callee.resolve();
247
228
 
248
229
  if (callee.isFunction()) {
249
- if (callee.is("async")) {
250
- if (callee.is("generator")) {
230
+ const {
231
+ node
232
+ } = callee;
233
+
234
+ if (node.async) {
235
+ if (node.generator) {
251
236
  return genericTypeAnnotation(identifier("AsyncIterator"));
252
237
  } else {
253
238
  return genericTypeAnnotation(identifier("Promise"));
254
239
  }
255
240
  } else {
256
- if (callee.node.returnType) {
241
+ if (node.generator) {
242
+ return genericTypeAnnotation(identifier("Iterator"));
243
+ } else if (callee.node.returnType) {
257
244
  return callee.node.returnType;
258
245
  } else {}
259
246
  }
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.createUnionType = createUnionType;
7
+
8
+ var _t = require("@babel/types");
9
+
10
+ const {
11
+ createFlowUnionType,
12
+ createTSUnionType,
13
+ createUnionTypeAnnotation,
14
+ isFlowType,
15
+ isTSType
16
+ } = _t;
17
+
18
+ function createUnionType(types) {
19
+ {
20
+ if (isFlowType(types[0])) {
21
+ if (createFlowUnionType) {
22
+ return createFlowUnionType(types);
23
+ }
24
+
25
+ return createUnionTypeAnnotation(types);
26
+ } else {
27
+ if (createTSUnionType) {
28
+ return createTSUnionType(types);
29
+ }
30
+ }
31
+ }
32
+ }
@@ -441,13 +441,20 @@ function isInStrictMode() {
441
441
  sourceType: "module"
442
442
  })) return true;
443
443
  if (path.isClass()) return true;
444
- if (!path.isProgram() && !path.isFunction()) return false;
445
444
 
446
445
  if (path.isArrowFunctionExpression() && !path.get("body").isBlockStatement()) {
447
446
  return false;
448
447
  }
449
448
 
450
- const body = path.isFunction() ? path.node.body : path.node;
449
+ let body;
450
+
451
+ if (path.isFunction()) {
452
+ body = path.node.body;
453
+ } else if (path.isProgram()) {
454
+ body = path.node;
455
+ } else {
456
+ return false;
457
+ }
451
458
 
452
459
  for (const directive of body.directives) {
453
460
  if (directive.value.value === "use strict") {
@@ -4,6 +4,9 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.hooks = void 0;
7
+
8
+ var _ = require("..");
9
+
7
10
  const hooks = [function (self, parent) {
8
11
  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
12
 
@@ -69,10 +69,11 @@ function replaceWithMultiple(nodes) {
69
69
 
70
70
  function replaceWithSourceString(replacement) {
71
71
  this.resync();
72
+ let ast;
72
73
 
73
74
  try {
74
75
  replacement = `(${replacement})`;
75
- replacement = (0, _parser.parse)(replacement);
76
+ ast = (0, _parser.parse)(replacement);
76
77
  } catch (err) {
77
78
  const loc = err.loc;
78
79
 
@@ -89,23 +90,21 @@ function replaceWithSourceString(replacement) {
89
90
  throw err;
90
91
  }
91
92
 
92
- replacement = replacement.program.body[0].expression;
93
+ const expressionAST = ast.program.body[0].expression;
93
94
 
94
- _index.default.removeProperties(replacement);
95
+ _index.default.removeProperties(expressionAST);
95
96
 
96
- return this.replaceWith(replacement);
97
+ return this.replaceWith(expressionAST);
97
98
  }
98
99
 
99
- function replaceWith(replacement) {
100
+ function replaceWith(replacementPath) {
100
101
  this.resync();
101
102
 
102
103
  if (this.removed) {
103
104
  throw new Error("You can't replace this node, we've already removed it");
104
105
  }
105
106
 
106
- if (replacement instanceof _index2.default) {
107
- replacement = replacement.node;
108
- }
107
+ let replacement = replacementPath instanceof _index2.default ? replacementPath.node : replacementPath;
109
108
 
110
109
  if (!replacement) {
111
110
  throw new Error("You passed `path.replaceWith()` a falsy node, use `path.remove()` instead");
@@ -38,9 +38,11 @@ const {
38
38
  isMethod,
39
39
  isModuleDeclaration,
40
40
  isModuleSpecifier,
41
+ isNullLiteral,
41
42
  isObjectExpression,
42
43
  isProperty,
43
44
  isPureish,
45
+ isRegExpLiteral,
44
46
  isSuper,
45
47
  isTaggedTemplateExpression,
46
48
  isTemplateLiteral,
@@ -75,7 +77,7 @@ function gatherNodeParts(node, parts) {
75
77
  }
76
78
  } else if (isModuleSpecifier(node)) {
77
79
  gatherNodeParts(node.local, parts);
78
- } else if (isLiteral(node)) {
80
+ } else if (isLiteral(node) && !isNullLiteral(node) && !isRegExpLiteral(node) && !isTemplateLiteral(node)) {
79
81
  parts.push(node.value);
80
82
  }
81
83
 
@@ -185,7 +187,7 @@ function gatherNodeParts(node, parts) {
185
187
  break;
186
188
 
187
189
  case "JSXOpeningElement":
188
- parts.push(node.name);
190
+ gatherNodeParts(node.name, parts);
189
191
  break;
190
192
 
191
193
  case "JSXFragment":
@@ -9,17 +9,10 @@ var _binding = require("../binding");
9
9
 
10
10
  var _helperSplitExportDeclaration = require("@babel/helper-split-export-declaration");
11
11
 
12
- var _t = require("@babel/types");
12
+ var t = require("@babel/types");
13
13
 
14
14
  var _helperEnvironmentVisitor = require("@babel/helper-environment-visitor");
15
15
 
16
- const {
17
- assignmentExpression,
18
- identifier,
19
- toExpression,
20
- variableDeclaration,
21
- variableDeclarator
22
- } = _t;
23
16
  const renameVisitor = {
24
17
  ReferencedIdentifier({
25
18
  node
@@ -64,7 +57,17 @@ class Renamer {
64
57
  return;
65
58
  }
66
59
 
67
- if (maybeExportDeclar.isExportDefaultDeclaration() && !maybeExportDeclar.get("declaration").node.id) {
60
+ if (maybeExportDeclar.isExportDefaultDeclaration()) {
61
+ const {
62
+ declaration
63
+ } = maybeExportDeclar.node;
64
+
65
+ if (t.isDeclaration(declaration) && !declaration.id) {
66
+ return;
67
+ }
68
+ }
69
+
70
+ if (maybeExportDeclar.isExportAllDeclaration()) {
68
71
  return;
69
72
  }
70
73
 
@@ -72,23 +75,11 @@ class Renamer {
72
75
  }
73
76
 
74
77
  maybeConvertFromClassFunctionDeclaration(path) {
75
- return;
76
- if (!path.isFunctionDeclaration() && !path.isClassDeclaration()) return;
77
- if (this.binding.kind !== "hoisted") return;
78
- path.node.id = identifier(this.oldName);
79
- path.node._blockHoist = 3;
80
- path.replaceWith(variableDeclaration("let", [variableDeclarator(identifier(this.newName), toExpression(path.node))]));
78
+ return path;
81
79
  }
82
80
 
83
81
  maybeConvertFromClassFunctionExpression(path) {
84
- return;
85
- if (!path.isFunctionExpression() && !path.isClassExpression()) return;
86
- if (this.binding.kind !== "local") return;
87
- path.node.id = identifier(this.oldName);
88
- this.binding.scope.parent.push({
89
- id: identifier(this.newName)
90
- });
91
- path.replaceWith(assignmentExpression("=", identifier(this.newName), path.node));
82
+ return path;
92
83
  }
93
84
 
94
85
  rename(block) {
@@ -128,8 +119,8 @@ class Renamer {
128
119
  }
129
120
 
130
121
  if (parentDeclar) {
131
- this.maybeConvertFromClassFunctionDeclaration(parentDeclar);
132
- this.maybeConvertFromClassFunctionExpression(parentDeclar);
122
+ this.maybeConvertFromClassFunctionDeclaration(path);
123
+ this.maybeConvertFromClassFunctionExpression(path);
133
124
  }
134
125
  }
135
126
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@babel/traverse",
3
- "version": "7.18.5",
3
+ "version": "7.18.9",
4
4
  "description": "The Babel Traverse module maintains the overall tree state, and is responsible for replacing, removing, and adding nodes",
5
5
  "author": "The Babel Team (https://babel.dev/team)",
6
6
  "homepage": "https://babel.dev/docs/en/next/babel-traverse",
@@ -16,21 +16,22 @@
16
16
  },
17
17
  "main": "./lib/index.js",
18
18
  "dependencies": {
19
- "@babel/code-frame": "^7.16.7",
20
- "@babel/generator": "^7.18.2",
21
- "@babel/helper-environment-visitor": "^7.18.2",
22
- "@babel/helper-function-name": "^7.17.9",
23
- "@babel/helper-hoist-variables": "^7.16.7",
24
- "@babel/helper-split-export-declaration": "^7.16.7",
25
- "@babel/parser": "^7.18.5",
26
- "@babel/types": "^7.18.4",
19
+ "@babel/code-frame": "^7.18.6",
20
+ "@babel/generator": "^7.18.9",
21
+ "@babel/helper-environment-visitor": "^7.18.9",
22
+ "@babel/helper-function-name": "^7.18.9",
23
+ "@babel/helper-hoist-variables": "^7.18.6",
24
+ "@babel/helper-split-export-declaration": "^7.18.6",
25
+ "@babel/parser": "^7.18.9",
26
+ "@babel/types": "^7.18.9",
27
27
  "debug": "^4.1.0",
28
28
  "globals": "^11.1.0"
29
29
  },
30
30
  "devDependencies": {
31
- "@babel/helper-plugin-test-runner": "^7.16.7"
31
+ "@babel/helper-plugin-test-runner": "^7.18.6"
32
32
  },
33
33
  "engines": {
34
34
  "node": ">=6.9.0"
35
- }
35
+ },
36
+ "type": "commonjs"
36
37
  }
@@ -1,4 +1,4 @@
1
- import t from "@babel/types";
1
+ import * as t from "@babel/types";
2
2
 
3
3
  export default function generateAsserts() {
4
4
  let output = `/*
@@ -1,5 +1,5 @@
1
- import t from "@babel/types";
2
- import virtualTypes from "../../lib/path/lib/virtual-types.js";
1
+ import * as t from "@babel/types";
2
+ import * as virtualTypes from "../../lib/path/lib/virtual-types.js";
3
3
 
4
4
  export default function generateValidators() {
5
5
  let output = `/*
@@ -14,16 +14,19 @@ export interface NodePathValidators {
14
14
  `;
15
15
 
16
16
  for (const type of [...t.TYPES].sort()) {
17
- output += `is${type}(opts?: object): this is NodePath<t.${type}>;`;
17
+ output += `is${type}<T extends t.Node>(this: NodePath<T>, opts?: object): this is NodePath<T & t.${type}>;`;
18
18
  }
19
19
 
20
20
  for (const type of Object.keys(virtualTypes)) {
21
+ // TODO: Remove this check once we stop compiling to CJS
22
+ if (type === "default" || type === "__esModule") continue;
23
+
21
24
  const { types } = virtualTypes[type];
22
25
  if (type[0] === "_") continue;
23
26
  if (t.NODE_FIELDS[type] || t.FLIPPED_ALIAS_KEYS[type]) {
24
- output += `is${type}(opts?: object): this is NodePath<t.${type}>;`;
27
+ output += `is${type}<T extends t.Node>(this: NodePath<T>, opts?: object): this is NodePath<T & t.${type}>;`;
25
28
  } else if (types /* in VirtualTypeAliases */) {
26
- output += `is${type}(opts?: object): this is NodePath<VirtualTypeAliases["${type}"]>;`;
29
+ output += `is${type}<T extends t.Node>(this: NodePath<T>, opts?: object): this is NodePath<T & VirtualTypeAliases["${type}"]>;`;
27
30
  } else if (type === "Pure") {
28
31
  output += `isPure(constantsOnly?: boolean): boolean;`;
29
32
  } else {
@@ -1,4 +1,4 @@
1
- import virtualTypes from "../../lib/path/lib/virtual-types.js";
1
+ import * as virtualTypes from "../../lib/path/lib/virtual-types.js";
2
2
 
3
3
  export default function generateValidators() {
4
4
  let output = `/*
@@ -11,6 +11,9 @@ export interface VirtualTypeAliases {
11
11
  `;
12
12
 
13
13
  for (const type of Object.keys(virtualTypes)) {
14
+ // TODO: Remove this check once we stop compiling to CJS
15
+ if (type === "default" || type === "__esModule") continue;
16
+
14
17
  output += ` ${type}: ${(virtualTypes[type].types || ["Node"])
15
18
  .map(t => `t.${t}`)
16
19
  .join(" | ")};`;