Skip to content

Commit 4d8b2d0

Browse files
Make NodePath<T | U> distributive (#16439)
1 parent a8994f8 commit 4d8b2d0

35 files changed

Lines changed: 1113 additions & 1137 deletions

File tree

packages/babel-helper-create-class-features-plugin/src/decorators.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1876,10 +1876,8 @@ function transformClass(
18761876

18771877
// update originalClassPath according to the new AST
18781878
originalClassPath = (
1879-
newPath.get("callee").get("body") as NodePath<t.ClassBody>
1880-
)
1881-
.get("body")[0]
1882-
.get("key");
1879+
newPath.get("callee").get("body") as NodePath<t.Class>
1880+
).get("body.0.key");
18831881
}
18841882
}
18851883
if (!classInitInjected && classInitCall) {
@@ -2252,11 +2250,11 @@ function NamedEvaluationVisitoryFactory(
22522250
// the object properties under object patterns
22532251
ObjectExpression(path, state) {
22542252
for (const propertyPath of path.get("properties")) {
2253+
if (!propertyPath.isObjectProperty()) continue;
22552254
const { node } = propertyPath;
2256-
if (node.type !== "ObjectProperty") continue;
22572255
const id = node.key;
22582256
const initializer = skipTransparentExprWrappers(
2259-
propertyPath.get("value"),
2257+
propertyPath.get("value") as NodePath<t.Expression>,
22602258
);
22612259
if (isAnonymous(initializer)) {
22622260
if (!node.computed) {
@@ -2274,7 +2272,7 @@ function NamedEvaluationVisitoryFactory(
22742272
}
22752273
} else {
22762274
const ref = handleComputedProperty(
2277-
propertyPath as NodePath<t.ObjectProperty>,
2275+
propertyPath,
22782276
// The key of a computed object property must not be a private name
22792277
id as t.Expression,
22802278
state,

packages/babel-helper-create-class-features-plugin/src/fields.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1593,7 +1593,7 @@ export function buildFieldsInitNodes(
15931593
// this maybe a bug for ts
15941594
switch (true) {
15951595
case isStaticBlock: {
1596-
const blockBody = (prop.node as t.StaticBlock).body;
1596+
const blockBody = prop.node.body;
15971597
// We special-case the single expression case to avoid the iife, since
15981598
// it's common.
15991599
if (blockBody.length === 1 && t.isExpressionStatement(blockBody[0])) {
@@ -1640,16 +1640,13 @@ export function buildFieldsInitNodes(
16401640
// It might still be possible to a computed static fields whose resulting
16411641
// key is "name" or "length", but the assumption is telling us that it's
16421642
// not going to happen.
1643-
// @ts-expect-error checked in switch
16441643
if (!isNameOrLength(prop.node)) {
1645-
// @ts-expect-error checked in switch
16461644
staticNodes.push(buildPublicFieldInitLoose(t.cloneNode(ref), prop));
16471645
break;
16481646
}
16491647
// falls through
16501648
case isStatic && isPublic && isField && !setPublicClassFields:
16511649
staticNodes.push(
1652-
// @ts-expect-error checked in switch
16531650
buildPublicFieldInitSpec(t.cloneNode(ref), prop, file),
16541651
);
16551652
break;
@@ -1681,15 +1678,13 @@ export function buildFieldsInitNodes(
16811678
instanceNodes.unshift(
16821679
buildPrivateMethodInitLoose(
16831680
t.thisExpression(),
1684-
// @ts-expect-error checked in switch
16851681
prop,
16861682
privateNamesMap,
16871683
),
16881684
);
16891685
pureStaticNodes.push(
16901686
buildPrivateMethodDeclaration(
16911687
file,
1692-
// @ts-expect-error checked in switch
16931688
prop,
16941689
privateNamesMap,
16951690
privateFieldsAsSymbolsOrProperties,
@@ -1703,7 +1698,6 @@ export function buildFieldsInitNodes(
17031698
instanceNodes.unshift(
17041699
buildPrivateInstanceMethodInitSpec(
17051700
t.thisExpression(),
1706-
// @ts-expect-error checked in switch
17071701
prop,
17081702
privateNamesMap,
17091703
file,
@@ -1712,7 +1706,6 @@ export function buildFieldsInitNodes(
17121706
pureStaticNodes.push(
17131707
buildPrivateMethodDeclaration(
17141708
file,
1715-
// @ts-expect-error checked in switch
17161709
prop,
17171710
privateNamesMap,
17181711
privateFieldsAsSymbolsOrProperties,
@@ -1732,7 +1725,6 @@ export function buildFieldsInitNodes(
17321725
pureStaticNodes.push(
17331726
buildPrivateMethodDeclaration(
17341727
file,
1735-
// @ts-expect-error checked in switch
17361728
prop,
17371729
privateNamesMap,
17381730
privateFieldsAsSymbolsOrProperties,
@@ -1746,7 +1738,6 @@ export function buildFieldsInitNodes(
17461738
staticNodes.unshift(
17471739
buildPrivateStaticMethodInitLoose(
17481740
t.cloneNode(ref),
1749-
// @ts-expect-error checked in switch
17501741
prop,
17511742
file,
17521743
privateNamesMap,
@@ -1755,21 +1746,18 @@ export function buildFieldsInitNodes(
17551746
pureStaticNodes.push(
17561747
buildPrivateMethodDeclaration(
17571748
file,
1758-
// @ts-expect-error checked in switch
17591749
prop,
17601750
privateNamesMap,
17611751
privateFieldsAsSymbolsOrProperties,
17621752
),
17631753
);
17641754
break;
17651755
case isInstance && isPublic && isField && setPublicClassFields:
1766-
// @ts-expect-error checked in switch
17671756
instanceNodes.push(buildPublicFieldInitLoose(t.thisExpression(), prop));
17681757
break;
17691758
case isInstance && isPublic && isField && !setPublicClassFields:
17701759
lastInstanceNodeReturnsThis = true;
17711760
instanceNodes.push(
1772-
// @ts-expect-error checked in switch
17731761
buildPublicFieldInitSpec(t.thisExpression(), prop, file),
17741762
);
17751763
break;

packages/babel-helper-create-class-features-plugin/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ export function createClassFeaturePlugin({
233233
const innerBinding = path.node.id;
234234
let ref: t.Identifier | null;
235235
if (!innerBinding || !pathIsClassDeclaration) {
236-
nameFunction(path);
236+
nameFunction(path as NodePath<t.ClassExpression>);
237237
ref = path.scope.generateUidIdentifier(innerBinding?.name || "Class");
238238
}
239239
const classRefForDefine = ref ?? t.cloneNode(innerBinding);

packages/babel-helper-member-expression-to-functions/src/index.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -215,12 +215,9 @@ const handle = {
215215
);
216216
}
217217

218-
// @ts-expect-error isOptionalMemberExpression does not work with NodePath union
219218
const startingNode = startingOptional.isOptionalMemberExpression()
220-
? // @ts-expect-error isOptionalMemberExpression does not work with NodePath union
221-
startingOptional.node.object
222-
: // @ts-expect-error isOptionalMemberExpression does not work with NodePath union
223-
startingOptional.node.callee;
219+
? startingOptional.node.object
220+
: startingOptional.node.callee;
224221
const baseNeedsMemoised = scope.maybeGenerateMemoised(startingNode);
225222
const baseRef = baseNeedsMemoised ?? startingNode;
226223

packages/babel-helper-module-transforms/src/normalize-and-load-metadata.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,6 @@ function getLocalExportMetadata(
549549
declaration.isFunctionDeclaration() ||
550550
declaration.isClassDeclaration()
551551
) {
552-
// @ts-expect-error todo(flow->ts): improve babel-types
553552
getLocalMetadata(declaration.get("id")).names.push("default");
554553
} else {
555554
// These should have been removed by the nameAnonymousExports() call.
@@ -594,9 +593,7 @@ function removeImportExportDeclarations(programPath: NodePath<t.Program>) {
594593
) {
595594
// @ts-expect-error todo(flow->ts): avoid mutations
596595
declaration._blockHoist = child.node._blockHoist;
597-
child.replaceWith(
598-
declaration as NodePath<t.FunctionDeclaration | t.ClassDeclaration>,
599-
);
596+
child.replaceWith(declaration);
600597
} else {
601598
// These should have been removed by the nameAnonymousExports() call.
602599
throw declaration.buildCodeFrameError(

packages/babel-helper-module-transforms/src/rewrite-live-references.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ const rewriteReferencesVisitor: Visitor<RewriteReferencesVisitorState> = {
569569
}
570570

571571
path.ensureBlock();
572-
const bodyPath = path.get("body");
572+
const bodyPath = path.get("body") as NodePath<t.BlockStatement>;
573573

574574
const newLoopId = scope.generateUidIdentifierBasedOnNode(left);
575575
path

packages/babel-helper-split-export-declaration/src/index.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,7 @@ export default function splitExportDeclaration(
7474
}
7575

7676
return exportDeclaration;
77-
} else if (
78-
// @ts-expect-error TS can not narrow down to NodePath<t.ExportNamedDeclaration>
79-
exportDeclaration.get("specifiers").length > 0
80-
) {
77+
} else if (exportDeclaration.get("specifiers").length > 0) {
8178
throw new Error("It doesn't make sense to split exported specifiers.");
8279
}
8380

packages/babel-helper-wrap-function/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ function plainFunction(
117117
| t.FunctionExpression
118118
| t.CallExpression;
119119
} else {
120-
node = path.node as t.FunctionDeclaration | t.FunctionExpression;
120+
node = path.node;
121121
}
122122

123123
const isDeclaration = isFunctionDeclaration(node);

packages/babel-helpers/src/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ function getHelperMetadata(file: File): HelperMetadata {
5959
}
6060
if (
6161
child.get("specifiers").length !== 1 ||
62-
// @ts-expect-error isImportDefaultSpecifier does not work with NodePath union
6362
!child.get("specifiers.0").isImportDefaultSpecifier()
6463
) {
6564
throw child.buildCodeFrameError(
@@ -223,7 +222,7 @@ function permuteHelperAST(
223222
exp.replaceWith(decl);
224223
} else if (id.type === "MemberExpression") {
225224
exportBindingAssignments.forEach(assignPath => {
226-
const assign: NodePath<t.Expression> = path.get(assignPath);
225+
const assign = path.get(assignPath) as NodePath<t.Expression>;
227226
assign.replaceWith(assignmentExpression("=", id, assign.node));
228227
});
229228
exp.replaceWith(decl);

packages/babel-plugin-bugfix-firefox-class-in-computed-class-key/src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,10 @@ export default declare(({ types: t, traverse, assertVersion }) => {
9393
elem.node.computed &&
9494
containsClassExpression(elem.get("key"))
9595
) {
96-
wrap(elem.get("key"));
96+
wrap(
97+
// @ts-expect-error .key also includes t.PrivateName
98+
elem.get("key") satisfies NodePath<t.Expression>,
99+
);
97100
}
98101
}
99102
},

0 commit comments

Comments
 (0)