Skip to content

Commit bca362a

Browse files
authored
Deprecate ModuleDeclaration alias (#15266)
1 parent 6e8ce9d commit bca362a

14 files changed

Lines changed: 175 additions & 38 deletions

File tree

packages/babel-traverse/src/path/generated/asserts.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,9 @@ export interface NodePathAssertions {
258258
assertImportNamespaceSpecifier(
259259
opts?: object,
260260
): asserts this is NodePath<t.ImportNamespaceSpecifier>;
261+
assertImportOrExportDeclaration(
262+
opts?: object,
263+
): asserts this is NodePath<t.ImportOrExportDeclaration>;
261264
assertImportSpecifier(
262265
opts?: object,
263266
): asserts this is NodePath<t.ImportSpecifier>;

packages/babel-traverse/src/path/generated/validators.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,10 @@ interface BaseNodePathValidators {
431431
this: NodePath<T>,
432432
opts?: object,
433433
): this is NodePath<T & t.ImportNamespaceSpecifier>;
434+
isImportOrExportDeclaration<T extends t.Node>(
435+
this: NodePath<T>,
436+
opts?: object,
437+
): this is NodePath<T & t.ImportOrExportDeclaration>;
434438
isImportSpecifier<T extends t.Node>(
435439
this: NodePath<T>,
436440
opts?: object,

packages/babel-traverse/src/visitors.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import * as virtualTypes from "./path/lib/virtual-types";
2-
import { DEPRECATED_KEYS, FLIPPED_ALIAS_KEYS, TYPES } from "@babel/types";
2+
import {
3+
DEPRECATED_KEYS,
4+
DEPRECATED_ALIASES,
5+
FLIPPED_ALIAS_KEYS,
6+
TYPES,
7+
} from "@babel/types";
38
import type { NodePath, Visitor } from "./index";
49

510
type VIRTUAL_TYPES = keyof typeof virtualTypes;
@@ -93,20 +98,26 @@ export function explode(visitor: Visitor) {
9398
for (const nodeType of Object.keys(visitor) as (keyof Visitor)[]) {
9499
if (shouldIgnoreKey(nodeType)) continue;
95100

96-
const fns = visitor[nodeType];
97-
98101
let aliases = FLIPPED_ALIAS_KEYS[nodeType];
99102

100-
const deprecatedKey = DEPRECATED_KEYS[nodeType];
101-
if (deprecatedKey) {
103+
if (nodeType in DEPRECATED_KEYS) {
104+
const deprecatedKey = DEPRECATED_KEYS[nodeType];
102105
console.trace(
103106
`Visitor defined for ${nodeType} but it has been renamed to ${deprecatedKey}`,
104107
);
105108
aliases = [deprecatedKey];
109+
} else if (nodeType in DEPRECATED_ALIASES) {
110+
const deprecatedAlias =
111+
DEPRECATED_ALIASES[nodeType as keyof typeof DEPRECATED_ALIASES];
112+
console.trace(
113+
`Visitor defined for ${nodeType} but it has been renamed to ${deprecatedAlias}`,
114+
);
115+
aliases = FLIPPED_ALIAS_KEYS[deprecatedAlias];
106116
}
107117

108118
if (!aliases) continue;
109119

120+
const fns = visitor[nodeType];
110121
// clear it from the visitor
111122
delete visitor[nodeType];
112123

packages/babel-traverse/test/traverse.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,4 +337,40 @@ describe("traverse", function () {
337337
expect(programShouldStop).toBe(false);
338338
});
339339
});
340+
describe("traverse.explode", () => {
341+
describe("deprecated types and aliases", () => {
342+
beforeAll(() => {
343+
jest.spyOn(console, "trace").mockImplementation(() => {});
344+
});
345+
afterAll(() => {
346+
console.trace.mockClear();
347+
});
348+
it("should warn for deprecated node types", () => {
349+
const visitNumericLiteral = () => {};
350+
const visitor = {
351+
NumberLiteral: visitNumericLiteral,
352+
};
353+
traverse.explode(visitor);
354+
expect(console.trace).toHaveBeenCalledWith(
355+
"Visitor defined for NumberLiteral but it has been renamed to NumericLiteral",
356+
);
357+
expect(visitor).toHaveProperty("NumericLiteral.enter", [
358+
visitNumericLiteral,
359+
]);
360+
});
361+
it("should warn for deprecated aliases", () => {
362+
const visitImportOrExportDeclaration = () => {};
363+
const visitor = {
364+
ModuleDeclaration: visitImportOrExportDeclaration,
365+
};
366+
traverse.explode(visitor);
367+
expect(console.trace).toHaveBeenCalledWith(
368+
"Visitor defined for ModuleDeclaration but it has been renamed to ImportOrExportDeclaration",
369+
);
370+
expect(visitor).toHaveProperty("ImportDeclaration.enter", [
371+
visitImportOrExportDeclaration,
372+
]);
373+
});
374+
});
375+
});
340376
});

packages/babel-types/scripts/generators/asserts.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
DEPRECATED_KEYS,
3+
DEPRECATED_ALIASES,
34
FLIPPED_ALIAS_KEYS,
45
NODE_FIELDS,
56
VISITOR_KEYS,
@@ -39,14 +40,23 @@ function assert(type: string, node: any, opts?: any): void {
3940
output += addAssertHelper(type);
4041
});
4142

42-
Object.keys(FLIPPED_ALIAS_KEYS).forEach(type => {
43-
output += addAssertHelper(type);
44-
});
43+
Object.keys(FLIPPED_ALIAS_KEYS)
44+
.filter(
45+
type => !Object.prototype.hasOwnProperty.call(DEPRECATED_ALIASES, type)
46+
)
47+
.forEach(type => {
48+
output += addAssertHelper(type);
49+
});
50+
51+
const deprecatedNodeTypesAndAliases = {
52+
...DEPRECATED_KEYS,
53+
...DEPRECATED_ALIASES,
54+
};
4555

46-
Object.keys(DEPRECATED_KEYS).forEach(type => {
47-
const newType = DEPRECATED_KEYS[type];
56+
Object.keys(deprecatedNodeTypesAndAliases).forEach(type => {
57+
const newType = deprecatedNodeTypesAndAliases[type];
4858
output += `export function assert${type}(node: any, opts: any): void {
49-
console.trace("The node type ${type} has been renamed to ${newType}");
59+
console.trace("\`assert${type}\` has been deprecated, please migrate to \`assert${newType}\`.");
5060
assert("${type}", node, opts);
5161
}\n`;
5262
});

packages/babel-types/scripts/generators/constants.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { FLIPPED_ALIAS_KEYS } from "../../lib/index.js";
1+
import { DEPRECATED_ALIASES, FLIPPED_ALIAS_KEYS } from "../../lib/index.js";
22

33
export default function generateConstants() {
44
let output = `/*
@@ -7,8 +7,20 @@ export default function generateConstants() {
77
*/
88
import { FLIPPED_ALIAS_KEYS } from "../../definitions";\n\n`;
99

10-
Object.keys(FLIPPED_ALIAS_KEYS).forEach(type => {
11-
output += `export const ${type.toUpperCase()}_TYPES = FLIPPED_ALIAS_KEYS["${type}"];\n`;
10+
Object.keys(FLIPPED_ALIAS_KEYS)
11+
.filter(
12+
type => !Object.prototype.hasOwnProperty.call(DEPRECATED_ALIASES, type)
13+
)
14+
.forEach(type => {
15+
output += `export const ${type.toUpperCase()}_TYPES = FLIPPED_ALIAS_KEYS["${type}"];\n`;
16+
});
17+
18+
Object.keys(DEPRECATED_ALIASES).forEach(type => {
19+
const newType = `${DEPRECATED_ALIASES[type].toUpperCase()}_TYPES`;
20+
output += `/**
21+
* @deprecated migrate to ${newType}.
22+
*/
23+
export const ${type.toUpperCase()}_TYPES = ${newType}`;
1224
});
1325

1426
return output;

packages/babel-types/scripts/generators/validators.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
DEPRECATED_ALIASES,
23
DEPRECATED_KEYS,
34
FLIPPED_ALIAS_KEYS,
45
NODE_FIELDS,
@@ -77,15 +78,29 @@ import type * as t from "../..";\n\n`;
7778
output += addIsHelper(type);
7879
});
7980

80-
Object.keys(FLIPPED_ALIAS_KEYS).forEach(type => {
81-
output += addIsHelper(type, FLIPPED_ALIAS_KEYS[type]);
82-
});
81+
Object.keys(FLIPPED_ALIAS_KEYS)
82+
.filter(
83+
type => !Object.prototype.hasOwnProperty.call(DEPRECATED_ALIASES, type)
84+
)
85+
.forEach(type => {
86+
output += addIsHelper(type, FLIPPED_ALIAS_KEYS[type]);
87+
});
8388

8489
Object.keys(DEPRECATED_KEYS).forEach(type => {
8590
const newType = DEPRECATED_KEYS[type];
86-
const deprecated = `console.trace("The node type ${type} has been renamed to ${newType}");`;
91+
const deprecated = `console.trace("\`is${type}\` has been deprecated, please migrate to \`is${newType}\`.");`;
8792
output += addIsHelper(type, null, deprecated);
8893
});
8994

95+
Object.keys(DEPRECATED_ALIASES).forEach(type => {
96+
const newType = DEPRECATED_ALIASES[type];
97+
const deprecated = `console.trace("\`is${type}\` has been deprecated, please migrate to \`is${newType}\`.");`;
98+
output += `export function is${type}(node: object | null | undefined, opts?: object | null): node is t.${newType} {
99+
${deprecated}
100+
return is${newType}(node, opts);
101+
}
102+
`;
103+
});
104+
90105
return output;
91106
}

packages/babel-types/src/asserts/generated/index.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1694,11 +1694,11 @@ export function assertClass(
16941694
): asserts node is t.Class {
16951695
assert("Class", node, opts);
16961696
}
1697-
export function assertModuleDeclaration(
1697+
export function assertImportOrExportDeclaration(
16981698
node: object | null | undefined,
16991699
opts?: object | null,
1700-
): asserts node is t.ModuleDeclaration {
1701-
assert("ModuleDeclaration", node, opts);
1700+
): asserts node is t.ImportOrExportDeclaration {
1701+
assert("ImportOrExportDeclaration", node, opts);
17021702
}
17031703
export function assertExportDeclaration(
17041704
node: object | null | undefined,
@@ -1804,21 +1804,31 @@ export function assertTSBaseType(
18041804
}
18051805
export function assertNumberLiteral(node: any, opts: any): void {
18061806
console.trace(
1807-
"The node type NumberLiteral has been renamed to NumericLiteral",
1807+
"`assertNumberLiteral` has been deprecated, please migrate to `assertNumericLiteral`.",
18081808
);
18091809
assert("NumberLiteral", node, opts);
18101810
}
18111811
export function assertRegexLiteral(node: any, opts: any): void {
1812-
console.trace("The node type RegexLiteral has been renamed to RegExpLiteral");
1812+
console.trace(
1813+
"`assertRegexLiteral` has been deprecated, please migrate to `assertRegExpLiteral`.",
1814+
);
18131815
assert("RegexLiteral", node, opts);
18141816
}
18151817
export function assertRestProperty(node: any, opts: any): void {
1816-
console.trace("The node type RestProperty has been renamed to RestElement");
1818+
console.trace(
1819+
"`assertRestProperty` has been deprecated, please migrate to `assertRestElement`.",
1820+
);
18171821
assert("RestProperty", node, opts);
18181822
}
18191823
export function assertSpreadProperty(node: any, opts: any): void {
18201824
console.trace(
1821-
"The node type SpreadProperty has been renamed to SpreadElement",
1825+
"`assertSpreadProperty` has been deprecated, please migrate to `assertSpreadElement`.",
18221826
);
18231827
assert("SpreadProperty", node, opts);
18241828
}
1829+
export function assertModuleDeclaration(node: any, opts: any): void {
1830+
console.trace(
1831+
"`assertModuleDeclaration` has been deprecated, please migrate to `assertImportOrExportDeclaration`.",
1832+
);
1833+
assert("ModuleDeclaration", node, opts);
1834+
}

packages/babel-types/src/ast-types/generated/index.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2455,7 +2455,7 @@ export type Property =
24552455
export type UnaryLike = UnaryExpression | SpreadElement;
24562456
export type Pattern = AssignmentPattern | ArrayPattern | ObjectPattern;
24572457
export type Class = ClassExpression | ClassDeclaration;
2458-
export type ModuleDeclaration =
2458+
export type ImportOrExportDeclaration =
24592459
| ExportAllDeclaration
24602460
| ExportDefaultDeclaration
24612461
| ExportNamedDeclaration
@@ -2743,6 +2743,11 @@ export type TSBaseType =
27432743
| TSVoidKeyword
27442744
| TSThisType
27452745
| TSLiteralType;
2746+
export type ModuleDeclaration =
2747+
| ExportAllDeclaration
2748+
| ExportDefaultDeclaration
2749+
| ExportNamedDeclaration
2750+
| ImportDeclaration;
27462751

27472752
export interface Aliases {
27482753
Standardized: Standardized;
@@ -2776,7 +2781,7 @@ export interface Aliases {
27762781
UnaryLike: UnaryLike;
27772782
Pattern: Pattern;
27782783
Class: Class;
2779-
ModuleDeclaration: ModuleDeclaration;
2784+
ImportOrExportDeclaration: ImportOrExportDeclaration;
27802785
ExportDeclaration: ExportDeclaration;
27812786
ModuleSpecifier: ModuleSpecifier;
27822787
Accessor: Accessor;
@@ -2794,6 +2799,7 @@ export interface Aliases {
27942799
TSTypeElement: TSTypeElement;
27952800
TSType: TSType;
27962801
TSBaseType: TSBaseType;
2802+
ModuleDeclaration: ModuleDeclaration;
27972803
}
27982804

27992805
export type DeprecatedAliases =

packages/babel-types/src/constants/generated/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ export const PROPERTY_TYPES = FLIPPED_ALIAS_KEYS["Property"];
3636
export const UNARYLIKE_TYPES = FLIPPED_ALIAS_KEYS["UnaryLike"];
3737
export const PATTERN_TYPES = FLIPPED_ALIAS_KEYS["Pattern"];
3838
export const CLASS_TYPES = FLIPPED_ALIAS_KEYS["Class"];
39-
export const MODULEDECLARATION_TYPES = FLIPPED_ALIAS_KEYS["ModuleDeclaration"];
39+
export const IMPORTOREXPORTDECLARATION_TYPES =
40+
FLIPPED_ALIAS_KEYS["ImportOrExportDeclaration"];
4041
export const EXPORTDECLARATION_TYPES = FLIPPED_ALIAS_KEYS["ExportDeclaration"];
4142
export const MODULESPECIFIER_TYPES = FLIPPED_ALIAS_KEYS["ModuleSpecifier"];
4243
export const ACCESSOR_TYPES = FLIPPED_ALIAS_KEYS["Accessor"];
@@ -55,3 +56,7 @@ export const TYPESCRIPT_TYPES = FLIPPED_ALIAS_KEYS["TypeScript"];
5556
export const TSTYPEELEMENT_TYPES = FLIPPED_ALIAS_KEYS["TSTypeElement"];
5657
export const TSTYPE_TYPES = FLIPPED_ALIAS_KEYS["TSType"];
5758
export const TSBASETYPE_TYPES = FLIPPED_ALIAS_KEYS["TSBaseType"];
59+
/**
60+
* @deprecated migrate to IMPORTOREXPORTDECLARATION_TYPES.
61+
*/
62+
export const MODULEDECLARATION_TYPES = IMPORTOREXPORTDECLARATION_TYPES;

0 commit comments

Comments
 (0)