-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support TypeScript const enums (#13324)
- Loading branch information
1 parent
0542f0d
commit b707842
Showing
35 changed files
with
280 additions
and
21 deletions.
There are no files selected for viewing
89 changes: 89 additions & 0 deletions
89
packages/babel-plugin-transform-typescript/src/const-enum.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import type * as t from "@babel/types"; | ||
import type { NodePath } from "@babel/traverse"; | ||
|
||
import { translateEnumValues } from "./enum"; | ||
|
||
export default function transpileConstEnum( | ||
path: NodePath<t.TSEnumDeclaration & { const: true }>, | ||
t: typeof import("@babel/types"), | ||
) { | ||
const { name } = path.node.id; | ||
|
||
const parentIsExport = path.parentPath.isExportNamedDeclaration(); | ||
let isExported = parentIsExport; | ||
if (!isExported && t.isProgram(path.parent)) { | ||
isExported = path.parent.body.some( | ||
stmt => | ||
t.isExportNamedDeclaration(stmt) && | ||
!stmt.source && | ||
stmt.specifiers.some( | ||
spec => t.isExportSpecifier(spec) && spec.local.name === name, | ||
), | ||
); | ||
} | ||
|
||
const entries = translateEnumValues(path, t); | ||
|
||
if (isExported) { | ||
const obj = t.objectExpression( | ||
entries.map(([name, value]) => | ||
t.objectProperty( | ||
t.isValidIdentifier(name) | ||
? t.identifier(name) | ||
: t.stringLiteral(name), | ||
value, | ||
), | ||
), | ||
); | ||
|
||
if (path.scope.hasOwnBinding(name)) { | ||
(parentIsExport ? path.parentPath : path).replaceWith( | ||
t.expressionStatement( | ||
t.callExpression( | ||
t.memberExpression(t.identifier("Object"), t.identifier("assign")), | ||
[path.node.id, obj], | ||
), | ||
), | ||
); | ||
} else { | ||
path.replaceWith( | ||
t.variableDeclaration("var", [t.variableDeclarator(path.node.id, obj)]), | ||
); | ||
path.scope.registerDeclaration(path); | ||
} | ||
|
||
return; | ||
} | ||
|
||
const entriesMap = new Map(entries); | ||
|
||
// TODO: After fixing https://github.com/babel/babel/pull/11065, we can | ||
// use path.scope.getBinding(name).referencePaths rather than doing | ||
// a full traversal. | ||
path.scope.path.traverse({ | ||
Scope(path) { | ||
if (path.scope.hasOwnBinding(name)) path.skip(); | ||
}, | ||
MemberExpression(path) { | ||
if (!t.isIdentifier(path.node.object, { name })) return; | ||
|
||
let key: string; | ||
if (path.node.computed) { | ||
if (t.isStringLiteral(path.node.property)) { | ||
key = path.node.property.value; | ||
} else { | ||
return; | ||
} | ||
} else if (t.isIdentifier(path.node.property)) { | ||
key = path.node.property.name; | ||
} else { | ||
return; | ||
} | ||
if (!entriesMap.has(key)) return; | ||
|
||
path.replaceWith(t.cloneNode(entriesMap.get(key))); | ||
}, | ||
}); | ||
|
||
path.remove(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 0 additions & 1 deletion
1
...ages/babel-plugin-transform-typescript/test/fixtures/declarations/const-enum/options.json
This file was deleted.
Oops, something went wrong.
3 changes: 3 additions & 0 deletions
3
...babel-plugin-transform-typescript/test/fixtures/declarations/export-declare-enum/input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export declare enum A {} | ||
|
||
; |
3 changes: 3 additions & 0 deletions
3
...l-plugin-transform-typescript/test/fixtures/declarations/export-declare-enum/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"sourceType": "module" | ||
} |
1 change: 1 addition & 0 deletions
1
...bel-plugin-transform-typescript/test/fixtures/declarations/export-declare-enum/output.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
; |
3 changes: 2 additions & 1 deletion
3
packages/babel-plugin-transform-typescript/test/fixtures/enum/const/input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
const enum E {} | ||
// With --isolatedModules, TSC ignores the "const" modifier when compiling enums | ||
const enum E {} |
1 change: 0 additions & 1 deletion
1
packages/babel-plugin-transform-typescript/test/fixtures/enum/const/options.json
This file was deleted.
Oops, something went wrong.
4 changes: 4 additions & 0 deletions
4
packages/babel-plugin-transform-typescript/test/fixtures/enum/const/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// With --isolatedModules, TSC ignores the "const" modifier when compiling enums | ||
var E; | ||
|
||
(function (E) {})(E || (E = {})); |
11 changes: 11 additions & 0 deletions
11
...n-transform-typescript/test/fixtures/optimize-const-enums/custom-values-exported/input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export const enum A { | ||
x = 3, | ||
y = "f", | ||
z = 4 << 2, | ||
w = y | ||
} | ||
|
||
A.x; | ||
A.y; | ||
A.z; | ||
A.w; |
10 changes: 10 additions & 0 deletions
10
...transform-typescript/test/fixtures/optimize-const-enums/custom-values-exported/output.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export var A = { | ||
x: 3, | ||
y: "f", | ||
z: 16, | ||
w: "f" | ||
}; | ||
A.x; | ||
A.y; | ||
A.z; | ||
A.w; |
11 changes: 11 additions & 0 deletions
11
...bel-plugin-transform-typescript/test/fixtures/optimize-const-enums/custom-values/input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
const enum A { | ||
x = 3, | ||
y = "f", | ||
z = 4 << 2, | ||
w = y | ||
} | ||
|
||
A.x; | ||
A.y; | ||
A.z; | ||
A.w; |
4 changes: 4 additions & 0 deletions
4
...l-plugin-transform-typescript/test/fixtures/optimize-const-enums/custom-values/output.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
3; | ||
"f"; | ||
16; | ||
"f"; |
3 changes: 3 additions & 0 deletions
3
...ges/babel-plugin-transform-typescript/test/fixtures/optimize-const-enums/declare/input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
declare const enum A { x } | ||
|
||
A.x; |
1 change: 1 addition & 0 deletions
1
...s/babel-plugin-transform-typescript/test/fixtures/optimize-const-enums/declare/output.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
0; |
3 changes: 3 additions & 0 deletions
3
...plugin-transform-typescript/test/fixtures/optimize-const-enums/export-const-enum/input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export const enum A { y } | ||
|
||
let x = A.y; |
4 changes: 4 additions & 0 deletions
4
...ugin-transform-typescript/test/fixtures/optimize-const-enums/export-const-enum/output.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export var A = { | ||
y: 0 | ||
}; | ||
let x = A.y; |
5 changes: 5 additions & 0 deletions
5
...es/babel-plugin-transform-typescript/test/fixtures/optimize-const-enums/exported/input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const enum A { y } | ||
|
||
let x = A.y; | ||
|
||
export { A }; |
5 changes: 5 additions & 0 deletions
5
.../babel-plugin-transform-typescript/test/fixtures/optimize-const-enums/exported/output.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
var A = { | ||
y: 0 | ||
}; | ||
let x = A.y; | ||
export { A }; |
8 changes: 8 additions & 0 deletions
8
...el-plugin-transform-typescript/test/fixtures/optimize-const-enums/local-shadowed/input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
const enum A { x } | ||
|
||
{ | ||
let A = {}; | ||
A.x; | ||
} | ||
|
||
A.x; |
5 changes: 5 additions & 0 deletions
5
...-plugin-transform-typescript/test/fixtures/optimize-const-enums/local-shadowed/output.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
let A = {}; | ||
A.x; | ||
} | ||
0; |
8 changes: 8 additions & 0 deletions
8
packages/babel-plugin-transform-typescript/test/fixtures/optimize-const-enums/local/input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
const enum A { | ||
x, y | ||
} | ||
|
||
A.x; | ||
A["y"]; | ||
A.z; | ||
A; |
4 changes: 4 additions & 0 deletions
4
...ges/babel-plugin-transform-typescript/test/fixtures/optimize-const-enums/local/output.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
0; | ||
1; | ||
A.z; | ||
A; |
13 changes: 13 additions & 0 deletions
13
...l-plugin-transform-typescript/test/fixtures/optimize-const-enums/merged-exported/input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export const enum A { | ||
x, y | ||
} | ||
|
||
export const enum A { | ||
z | ||
} | ||
|
||
A.x; | ||
A["y"]; | ||
A.z; | ||
A.w; | ||
A; |
12 changes: 12 additions & 0 deletions
12
...plugin-transform-typescript/test/fixtures/optimize-const-enums/merged-exported/output.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export var A = { | ||
x: 0, | ||
y: 1 | ||
}; | ||
Object.assign(A, { | ||
z: 0 | ||
}); | ||
A.x; | ||
A["y"]; | ||
A.z; | ||
A.w; | ||
A; |
13 changes: 13 additions & 0 deletions
13
...ages/babel-plugin-transform-typescript/test/fixtures/optimize-const-enums/merged/input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
const enum A { | ||
x, y | ||
} | ||
|
||
const enum A { | ||
z | ||
} | ||
|
||
A.x; | ||
A["y"]; | ||
A.z; | ||
A.w; | ||
A; |
5 changes: 5 additions & 0 deletions
5
...es/babel-plugin-transform-typescript/test/fixtures/optimize-const-enums/merged/output.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
0; | ||
1; | ||
0; | ||
A.w; | ||
A; |
4 changes: 4 additions & 0 deletions
4
packages/babel-plugin-transform-typescript/test/fixtures/optimize-const-enums/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"plugins": [["transform-typescript", { "optimizeConstEnums": true }]], | ||
"sourceType": "module" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.