Skip to content

Commit

Permalink
Use union for N.Node in parser
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Apr 12, 2024
1 parent 3e92630 commit 1de8c37
Show file tree
Hide file tree
Showing 10 changed files with 426 additions and 141 deletions.
1 change: 0 additions & 1 deletion packages/babel-parser/src/parser/expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ export default abstract class ExpressionParser extends LValParser {
prop.type === "SpreadElement" ||
this.isObjectMethod(prop) ||
prop.computed ||
// @ts-expect-error prop must be an ObjectProperty
prop.shorthand
) {
return;
Expand Down
29 changes: 13 additions & 16 deletions packages/babel-parser/src/parser/lval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import type {
PrivateName,
ObjectExpression,
ObjectPattern,
ArrayExpression,
ArrayPattern,
AssignmentProperty,
Assignable,
Expand Down Expand Up @@ -138,7 +137,7 @@ export default abstract class LValParser extends NodeUtils {
break;

case "ObjectExpression":
node.type = "ObjectPattern";
(node as Node).type = "ObjectPattern";
for (
let i = 0, length = node.properties.length, last = length - 1;
i < length;
Expand All @@ -150,7 +149,7 @@ export default abstract class LValParser extends NodeUtils {

if (
isLast &&
prop.type === "RestElement" &&
(prop as Node).type === "RestElement" &&
node.extra?.trailingCommaLoc
) {
this.raise(Errors.RestTrailingComma, node.extra.trailingCommaLoc);
Expand Down Expand Up @@ -178,7 +177,7 @@ export default abstract class LValParser extends NodeUtils {
}

case "ArrayExpression":
node.type = "ArrayPattern";
(node as Node).type = "ArrayPattern";
this.toAssignableList(
node.elements,
node.extra?.trailingCommaLoc,
Expand All @@ -191,7 +190,7 @@ export default abstract class LValParser extends NodeUtils {
this.raise(Errors.MissingEqInAssignment, node.left.loc.end);
}

node.type = "AssignmentPattern";
(node as Node).type = "AssignmentPattern";
delete node.operator;
this.toAssignable(node.left, isLHS);
break;
Expand Down Expand Up @@ -220,7 +219,7 @@ export default abstract class LValParser extends NodeUtils {
prop.key,
);
} else if (prop.type === "SpreadElement") {
prop.type = "RestElement";
(prop as Node).type = "RestElement";
const arg = prop.argument;
this.checkToRestConversion(arg, /* allowPattern */ false);
this.toAssignable(arg, isLHS);
Expand Down Expand Up @@ -276,15 +275,13 @@ export default abstract class LValParser extends NodeUtils {

case "ObjectExpression": {
const last = node.properties.length - 1;
return (node.properties as ObjectExpression["properties"]).every(
(prop, i) => {
return (
prop.type !== "ObjectMethod" &&
(i === last || prop.type !== "SpreadElement") &&
this.isAssignable(prop)
);
},
);
return node.properties.every((prop, i) => {
return (
prop.type !== "ObjectMethod" &&
(i === last || prop.type !== "SpreadElement") &&
this.isAssignable(prop)
);
});
}

case "ObjectProperty":
Expand All @@ -294,7 +291,7 @@ export default abstract class LValParser extends NodeUtils {
return this.isAssignable(node.argument);

case "ArrayExpression":
return (node as ArrayExpression).elements.every(
return node.elements.every(
element => element === null || this.isAssignable(element),
);

Expand Down
4 changes: 1 addition & 3 deletions packages/babel-parser/src/parser/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,7 @@ export abstract class NodeUtils extends UtilParser {
" Instead use resetEndLocation() or change type directly.",
);
}
// @ts-expect-error migrate to Babel types AST typings
node.type = type;
// @ts-expect-error migrate to Babel types AST typings
(node as T).type = type;
node.end = endLoc.index;
node.loc.end = endLoc;
if (this.options.ranges) node.range[1] = endLoc.index;
Expand Down
83 changes: 42 additions & 41 deletions packages/babel-parser/src/parser/statement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2158,7 +2158,7 @@ export default abstract class StatementParser extends ExpressionParser {

if (isPrivate) {
this.classScope.declarePrivateName(
this.getPrivateNameSV(node.key),
this.getPrivateNameSV(node.key as N.PrivateName),
ClassElementType.OTHER,
node.key.loc.start,
);
Expand Down Expand Up @@ -2219,13 +2219,11 @@ export default abstract class StatementParser extends ExpressionParser {
}

declareClassPrivateMethodInScope(
node: Undone<
N.ClassPrivateMethod | N.EstreeMethodDefinition | N.TSDeclareMethod
>,
node: Undone<N.ClassPrivateMethod | N.TSDeclareMethod>,
kind: number,
) {
this.classScope.declarePrivateName(
this.getPrivateNameSV(node.key),
this.getPrivateNameSV(node.key as N.PrivateName),
kind,
node.key.loc.start,
);
Expand Down Expand Up @@ -2325,18 +2323,9 @@ export default abstract class StatementParser extends ExpressionParser {
maybeDefaultIdentifier,
);
const parseAfterDefault = !hasDefault || this.eat(tt.comma);
const hasStar =
parseAfterDefault &&
this.eatExportStar(
// @ts-expect-error todo(flow->ts)
node,
);
const hasStar = parseAfterDefault && this.eatExportStar(node);
const hasNamespace =
hasStar &&
this.maybeParseExportNamespaceSpecifier(
// @ts-expect-error todo(flow->ts)
node,
);
hasStar && this.maybeParseExportNamespaceSpecifier(node);
const parseAfterNamespace =
parseAfterDefault && (!hasNamespace || this.eat(tt.comma));
const isFromRequired = hasDefault || hasStar;
Expand All @@ -2346,15 +2335,12 @@ export default abstract class StatementParser extends ExpressionParser {
if (decorators) {
throw this.raise(Errors.UnsupportedDecoratorExport, node);
}
this.parseExportFrom(node as Undone<N.ExportNamedDeclaration>, true);
this.parseExportFrom(node, true);

return this.finishNode(node, "ExportAllDeclaration");
}

const hasSpecifiers = this.maybeParseExportNamedSpecifiers(
// @ts-expect-error todo(flow->ts)
node,
);
const hasSpecifiers = this.maybeParseExportNamedSpecifiers(node);

if (hasDefault && parseAfterDefault && !hasStar && !hasSpecifiers) {
this.unexpected(null, tt.braceL);
Expand Down Expand Up @@ -2412,7 +2398,9 @@ export default abstract class StatementParser extends ExpressionParser {
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
eatExportStar(node: N.Node): boolean {
eatExportStar(
node: Undone<N.Node>,
): node is Undone<N.ExportNamedDeclaration | N.ExportAllDeclaration> {
return this.eat(tt.star);
}

Expand All @@ -2438,9 +2426,11 @@ export default abstract class StatementParser extends ExpressionParser {
return false;
}

maybeParseExportNamespaceSpecifier(node: N.Node): boolean {
maybeParseExportNamespaceSpecifier(
node: Undone<N.ExportNamedDeclaration | N.ExportAllDeclaration>,
): node is Undone<N.ExportNamedDeclaration> {
if (this.isContextual(tt._as)) {
if (!node.specifiers) node.specifiers = [];
(node as Undone<N.ExportNamedDeclaration>).specifiers ??= [];

const specifier = this.startNodeAt<N.ExportNamespaceSpecifier>(
this.state.lastTokStartLoc,
Expand All @@ -2449,24 +2439,28 @@ export default abstract class StatementParser extends ExpressionParser {
this.next();

specifier.exported = this.parseModuleExportName();
node.specifiers.push(
(node as Undone<N.ExportNamedDeclaration>).specifiers.push(
this.finishNode(specifier, "ExportNamespaceSpecifier"),
);
return true;
}
return false;
}

maybeParseExportNamedSpecifiers(node: N.Node): boolean {
maybeParseExportNamedSpecifiers(
node: Undone<N.Node>,
): node is Undone<N.ExportNamedDeclaration> {
if (this.match(tt.braceL)) {
if (!node.specifiers) node.specifiers = [];
const isTypeExport = node.exportKind === "type";
node.specifiers.push(...this.parseExportSpecifiers(isTypeExport));
const node2 = node as Undone<N.ExportNamedDeclaration>;

node.source = null;
node.declaration = null;
if (!node2.specifiers) node2.specifiers = [];
const isTypeExport = node2.exportKind === "type";
node2.specifiers.push(...this.parseExportSpecifiers(isTypeExport));

node2.source = null;
node2.declaration = null;
if (this.hasPlugin("importAssertions")) {
node.assertions = [];
node2.assertions = [];
}

return true;
Expand Down Expand Up @@ -2615,7 +2609,7 @@ export default abstract class StatementParser extends ExpressionParser {

parseExportFrom(
this: Parser,
node: Undone<N.ExportNamedDeclaration>,
node: Undone<N.ExportNamedDeclaration | N.ExportAllDeclaration>,
expect?: boolean,
): void {
if (this.eatContextual(tt._from)) {
Expand Down Expand Up @@ -2666,7 +2660,11 @@ export default abstract class StatementParser extends ExpressionParser {
}

checkExport(
node: Undone<N.ExportNamedDeclaration | N.ExportDefaultDeclaration>,
node: Undone<
| N.ExportNamedDeclaration
| N.ExportAllDeclaration
| N.ExportDefaultDeclaration
>,
checkNames?: boolean,
isDefault?: boolean,
isFrom?: boolean,
Expand Down Expand Up @@ -2712,18 +2710,19 @@ export default abstract class StatementParser extends ExpressionParser {
}
}
}
} else if (node.declaration) {
} else if ((node as Undone<N.ExportNamedDeclaration>).declaration) {
const decl = (node as Undone<N.ExportNamedDeclaration>).declaration;
// Exported declarations
if (
node.declaration.type === "FunctionDeclaration" ||
node.declaration.type === "ClassDeclaration"
decl.type === "FunctionDeclaration" ||
decl.type === "ClassDeclaration"
) {
const id = node.declaration.id;
const { id } = decl;
if (!id) throw new Error("Assertion failure");

this.checkDuplicateExports(node, id.name);
} else if (node.declaration.type === "VariableDeclaration") {
for (const declaration of node.declaration.declarations) {
} else if (decl.type === "VariableDeclaration") {
for (const declaration of decl.declarations) {
this.checkDeclaration(declaration.id);
}
}
Expand Down Expand Up @@ -3245,7 +3244,9 @@ export default abstract class StatementParser extends ExpressionParser {
}

maybeParseImportAttributes(
node: Undone<N.ImportDeclaration | N.ExportNamedDeclaration>,
node: Undone<
N.ImportDeclaration | N.ExportNamedDeclaration | N.ExportAllDeclaration
>,
) {
let attributes: N.ImportAttribute[];
let useWith = false;
Expand Down
10 changes: 6 additions & 4 deletions packages/babel-parser/src/parser/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import type State from "../tokenizer/state.ts";
import type {
EstreePropertyDefinition,
Node,
ObjectMethod,
ObjectProperty,
} from "../types.ts";
PrivateName,
} from "../types.d.ts";
import { lineBreak, skipWhiteSpaceToLineBreak } from "../util/whitespace.ts";
import { isIdentifierChar } from "../util/identifier.ts";
import ClassScopeHandler from "../util/class-scope.ts";
Expand Down Expand Up @@ -265,7 +267,7 @@ export default abstract class UtilParser extends Tokenizer {
* Test if given node is a PrivateName
* will be overridden in ESTree plugin
*/
isPrivateName(node: Node): boolean {
isPrivateName(node: Node): node is PrivateName {
return node.type === "PrivateName";
}

Expand All @@ -274,7 +276,7 @@ export default abstract class UtilParser extends Tokenizer {
* WITHOUT `#`
* @see {@link https://tc39.es/ecma262/#sec-static-semantics-stringvalue}
*/
getPrivateNameSV(node: Node): string {
getPrivateNameSV(node: PrivateName): string {
return node.id.name;
}

Expand All @@ -297,7 +299,7 @@ export default abstract class UtilParser extends Tokenizer {
return node.type === "ObjectProperty";
}

isObjectMethod(node: Node): boolean {
isObjectMethod(node: Node): node is ObjectMethod {
return node.type === "ObjectMethod";
}

Expand Down

0 comments on commit 1de8c37

Please sign in to comment.