Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: deep clone shared AST field definitions #14737

Merged
merged 1 commit into from Jul 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 19 additions & 19 deletions packages/babel-types/src/ast-types/generated/index.ts
Expand Up @@ -444,8 +444,8 @@ export interface FunctionDeclaration extends BaseNode {
id?: Identifier | null;
params: Array<Identifier | Pattern | RestElement>;
body: BlockStatement;
generator?: boolean;
async?: boolean;
generator: boolean;
async: boolean;
declare?: boolean | null;
predicate?: DeclaredPredicate | InferredPredicate | null;
returnType?: TypeAnnotation | TSTypeAnnotation | Noop | null;
Expand All @@ -461,8 +461,8 @@ export interface FunctionExpression extends BaseNode {
id?: Identifier | null;
params: Array<Identifier | Pattern | RestElement>;
body: BlockStatement;
generator?: boolean;
async?: boolean;
generator: boolean;
async: boolean;
predicate?: DeclaredPredicate | InferredPredicate | null;
returnType?: TypeAnnotation | TSTypeAnnotation | Noop | null;
typeParameters?:
Expand Down Expand Up @@ -582,8 +582,8 @@ export interface ObjectMethod extends BaseNode {
params: Array<Identifier | Pattern | RestElement>;
body: BlockStatement;
computed: boolean;
generator?: boolean;
async?: boolean;
generator: boolean;
async: boolean;
decorators?: Array<Decorator> | null;
returnType?: TypeAnnotation | TSTypeAnnotation | Noop | null;
typeParameters?:
Expand Down Expand Up @@ -738,7 +738,7 @@ export interface ArrowFunctionExpression extends BaseNode {
type: "ArrowFunctionExpression";
params: Array<Identifier | Pattern | RestElement>;
body: BlockStatement | Expression;
async?: boolean;
async: boolean;
expression: boolean;
generator?: boolean;
predicate?: DeclaredPredicate | InferredPredicate | null;
Expand Down Expand Up @@ -878,14 +878,14 @@ export interface MetaProperty extends BaseNode {

export interface ClassMethod extends BaseNode {
type: "ClassMethod";
kind?: "get" | "set" | "method" | "constructor";
kind: "get" | "set" | "method" | "constructor";
key: Identifier | StringLiteral | NumericLiteral | BigIntLiteral | Expression;
params: Array<Identifier | Pattern | RestElement | TSParameterProperty>;
body: BlockStatement;
computed?: boolean;
static?: boolean;
generator?: boolean;
async?: boolean;
computed: boolean;
static: boolean;
generator: boolean;
async: boolean;
abstract?: boolean | null;
access?: "public" | "private" | "protected" | null;
accessibility?: "public" | "private" | "protected" | null;
Expand Down Expand Up @@ -996,8 +996,8 @@ export interface ClassProperty extends BaseNode {
value?: Expression | null;
typeAnnotation?: TypeAnnotation | TSTypeAnnotation | Noop | null;
decorators?: Array<Decorator> | null;
computed?: boolean;
static?: boolean;
computed: boolean;
static: boolean;
abstract?: boolean | null;
accessibility?: "public" | "private" | "protected" | null;
declare?: boolean | null;
Expand All @@ -1020,8 +1020,8 @@ export interface ClassAccessorProperty extends BaseNode {
value?: Expression | null;
typeAnnotation?: TypeAnnotation | TSTypeAnnotation | Noop | null;
decorators?: Array<Decorator> | null;
computed?: boolean;
static?: boolean;
computed: boolean;
static: boolean;
abstract?: boolean | null;
accessibility?: "public" | "private" | "protected" | null;
declare?: boolean | null;
Expand Down Expand Up @@ -1050,7 +1050,7 @@ export interface ClassPrivateMethod extends BaseNode {
key: PrivateName;
params: Array<Identifier | Pattern | RestElement | TSParameterProperty>;
body: BlockStatement;
static?: boolean;
static: boolean;
abstract?: boolean | null;
access?: "public" | "private" | "protected" | null;
accessibility?: "public" | "private" | "protected" | null;
Expand Down Expand Up @@ -1725,7 +1725,7 @@ export interface TSPropertySignature extends BaseNode {
key: Expression;
typeAnnotation?: TSTypeAnnotation | null;
initializer?: Expression | null;
computed?: boolean | null;
computed?: boolean;
kind: "get" | "set";
optional?: boolean | null;
readonly?: boolean | null;
Expand All @@ -1737,7 +1737,7 @@ export interface TSMethodSignature extends BaseNode {
typeParameters?: TSTypeParameterDeclaration | null;
parameters: Array<Identifier | RestElement>;
typeAnnotation?: TSTypeAnnotation | null;
computed?: boolean | null;
computed?: boolean;
kind: "method" | "get" | "set";
optional?: boolean | null;
}
Expand Down
69 changes: 35 additions & 34 deletions packages/babel-types/src/definitions/core.ts
Expand Up @@ -362,7 +362,7 @@ defineType("ForStatement", {
},
});

export const functionCommon = {
export const functionCommon = () => ({
params: {
validate: chain(
assertValueType("array"),
Expand All @@ -375,9 +375,9 @@ export const functionCommon = {
async: {
default: false,
},
};
});

export const functionTypeAnnotationCommon = {
export const functionTypeAnnotationCommon = () => ({
returnType: {
validate: process.env.BABEL_8_BREAKING
? assertNodeType("TypeAnnotation", "TSTypeAnnotation")
Expand All @@ -400,10 +400,10 @@ export const functionTypeAnnotationCommon = {
),
optional: true,
},
};
});

export const functionDeclarationCommon = {
...functionCommon,
export const functionDeclarationCommon = () => ({
...functionCommon(),
declare: {
validate: assertValueType("boolean"),
optional: true,
Expand All @@ -412,14 +412,14 @@ export const functionDeclarationCommon = {
validate: assertNodeType("Identifier"),
optional: true, // May be null for `export default function`
},
};
});

defineType("FunctionDeclaration", {
builder: ["id", "params", "body", "generator", "async"],
visitor: ["id", "params", "body", "returnType", "typeParameters"],
fields: {
...functionDeclarationCommon,
...functionTypeAnnotationCommon,
...functionDeclarationCommon(),
...functionTypeAnnotationCommon(),
body: {
validate: assertNodeType("BlockStatement"),
},
Expand Down Expand Up @@ -461,8 +461,8 @@ defineType("FunctionExpression", {
"Pureish",
],
fields: {
...functionCommon,
...functionTypeAnnotationCommon,
...functionCommon(),
...functionTypeAnnotationCommon(),
id: {
validate: assertNodeType("Identifier"),
optional: true,
Expand All @@ -477,7 +477,7 @@ defineType("FunctionExpression", {
},
});

export const patternLikeCommon = {
export const patternLikeCommon = () => ({
typeAnnotation: {
validate: process.env.BABEL_8_BREAKING
? assertNodeType("TypeAnnotation", "TSTypeAnnotation")
Expand All @@ -494,15 +494,16 @@ export const patternLikeCommon = {
assertValueType("array"),
assertEach(assertNodeType("Decorator")),
),
optional: true,
},
};
});

defineType("Identifier", {
builder: ["name"],
visitor: ["typeAnnotation", "decorators" /* for legacy param decorators */],
aliases: ["Expression", "PatternLike", "LVal", "TSEntityName"],
fields: {
...patternLikeCommon,
...patternLikeCommon(),
name: {
validate: chain(
assertValueType("string"),
Expand Down Expand Up @@ -772,8 +773,8 @@ defineType("ObjectExpression", {
defineType("ObjectMethod", {
builder: ["kind", "key", "params", "body", "computed", "generator", "async"],
fields: {
...functionCommon,
...functionTypeAnnotationCommon,
...functionCommon(),
...functionTypeAnnotationCommon(),
kind: {
validate: assertOneOf("method", "get", "set"),
...(!process.env.BABEL_TYPES_8_BREAKING ? { default: "method" } : {}),
Expand Down Expand Up @@ -948,7 +949,7 @@ defineType("RestElement", {
aliases: ["LVal", "PatternLike"],
deprecatedAlias: "RestProperty",
fields: {
...patternLikeCommon,
...patternLikeCommon(),
argument: {
validate: !process.env.BABEL_TYPES_8_BREAKING
? assertNodeType("LVal")
Expand Down Expand Up @@ -1234,7 +1235,7 @@ defineType("AssignmentPattern", {
builder: ["left", "right"],
aliases: ["Pattern", "PatternLike", "LVal"],
fields: {
...patternLikeCommon,
...patternLikeCommon(),
left: {
validate: assertNodeType(
"Identifier",
Expand Down Expand Up @@ -1265,7 +1266,7 @@ defineType("ArrayPattern", {
builder: ["elements"],
aliases: ["Pattern", "PatternLike", "LVal"],
fields: {
...patternLikeCommon,
...patternLikeCommon(),
elements: {
validate: chain(
assertValueType("array"),
Expand Down Expand Up @@ -1299,8 +1300,8 @@ defineType("ArrowFunctionExpression", {
"Pureish",
],
fields: {
...functionCommon,
...functionTypeAnnotationCommon,
...functionCommon(),
...functionTypeAnnotationCommon(),
expression: {
// https://github.com/babel/babylon/issues/505
validate: assertValueType("boolean"),
Expand Down Expand Up @@ -1786,7 +1787,7 @@ defineType("MetaProperty", {
},
});

export const classMethodOrPropertyCommon = {
export const classMethodOrPropertyCommon = () => ({
abstract: {
validate: assertValueType("boolean"),
optional: true,
Expand Down Expand Up @@ -1832,11 +1833,11 @@ export const classMethodOrPropertyCommon = {
),
),
},
};
});

export const classMethodOrDeclareMethodCommon = {
...functionCommon,
...classMethodOrPropertyCommon,
export const classMethodOrDeclareMethodCommon = () => ({
...functionCommon(),
...classMethodOrPropertyCommon(),
params: {
validate: chain(
assertValueType("array"),
Expand Down Expand Up @@ -1868,7 +1869,7 @@ export const classMethodOrDeclareMethodCommon = {
),
optional: true,
},
};
});

defineType("ClassMethod", {
aliases: ["Function", "Scopable", "BlockParent", "FunctionParent", "Method"],
Expand All @@ -1891,8 +1892,8 @@ defineType("ClassMethod", {
"typeParameters",
],
fields: {
...classMethodOrDeclareMethodCommon,
...functionTypeAnnotationCommon,
...classMethodOrDeclareMethodCommon(),
...functionTypeAnnotationCommon(),
body: {
validate: assertNodeType("BlockStatement"),
},
Expand All @@ -1908,7 +1909,7 @@ defineType("ObjectPattern", {
builder: ["properties"],
aliases: ["Pattern", "PatternLike", "LVal"],
fields: {
...patternLikeCommon,
...patternLikeCommon(),
properties: {
validate: chain(
assertValueType("array"),
Expand Down Expand Up @@ -2163,7 +2164,7 @@ defineType("ClassProperty", {
],
aliases: ["Property"],
fields: {
...classMethodOrPropertyCommon,
...classMethodOrPropertyCommon(),
value: {
validate: assertNodeType("Expression"),
optional: true,
Expand Down Expand Up @@ -2217,7 +2218,7 @@ defineType("ClassAccessorProperty", {
],
aliases: ["Property", "Accessor"],
fields: {
...classMethodOrPropertyCommon,
...classMethodOrPropertyCommon(),
key: {
validate: chain(
(function () {
Expand Down Expand Up @@ -2354,8 +2355,8 @@ defineType("ClassPrivateMethod", {
"Private",
],
fields: {
...classMethodOrDeclareMethodCommon,
...functionTypeAnnotationCommon,
...classMethodOrDeclareMethodCommon(),
...functionTypeAnnotationCommon(),
kind: {
validate: assertOneOf("get", "set", "method"),
default: "method",
Expand Down