-
-
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.
Transform TypeScript "declare" fields (#10546)
* Transform TypeScript "declare" fields * Remove multiple spaces * declareFields -> allowDeclareFields
- Loading branch information
1 parent
d31cab5
commit a0f0efd
Showing
21 changed files
with
191 additions
and
80 deletions.
There are no files selected for viewing
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
19 changes: 19 additions & 0 deletions
19
packages/babel-helper-create-class-features-plugin/src/typescript.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,19 @@ | ||
// @flow | ||
|
||
import type { NodePath } from "@babel/traverse"; | ||
|
||
export function assertFieldTransformed(path: NodePath) { | ||
// TODO (Babel 8): Also check path.node.definite | ||
|
||
if (path.node.declare) { | ||
throw path.buildCodeFrameError( | ||
`TypeScript 'declare' fields must first be transformed by ` + | ||
`@babel/plugin-transform-typescript.\n` + | ||
`If you have already enabled that plugin (or '@babel/preset-typescript'), make sure ` + | ||
`that it runs before any plugin related to additional class features:\n` + | ||
` - @babel/plugin-proposal-class-properties\n` + | ||
` - @babel/plugin-proposal-private-methods\n` + | ||
` - @babel/plugin-proposal-decorators`, | ||
); | ||
} | ||
} |
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
3 changes: 3 additions & 0 deletions
3
packages/babel-plugin-transform-typescript/test/fixtures/class/declare-not-enabled/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 @@ | ||
class A { | ||
declare x; | ||
} |
4 changes: 4 additions & 0 deletions
4
...es/babel-plugin-transform-typescript/test/fixtures/class/declare-not-enabled/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"], | ||
"throws": "The 'declare' modifier is only allowed when the 'allowDeclareFields' option of @babel/plugin-transform-typescript or @babel/preset-typescript is enabled." | ||
} |
3 changes: 3 additions & 0 deletions
3
...es/babel-plugin-transform-typescript/test/fixtures/class/declare-not-initialized/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 @@ | ||
class A { | ||
x; | ||
} |
3 changes: 3 additions & 0 deletions
3
...abel-plugin-transform-typescript/test/fixtures/class/declare-not-initialized/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 @@ | ||
{ | ||
"plugins": [["transform-typescript", { "allowDeclareFields": true }]] | ||
} |
3 changes: 3 additions & 0 deletions
3
...s/babel-plugin-transform-typescript/test/fixtures/class/declare-not-initialized/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,3 @@ | ||
class A { | ||
x; | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/babel-plugin-transform-typescript/test/fixtures/class/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 @@ | ||
class A { | ||
declare x; | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/babel-plugin-transform-typescript/test/fixtures/class/declare/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 @@ | ||
{ | ||
"plugins": [["transform-typescript", { "allowDeclareFields": true }]] | ||
} |
1 change: 1 addition & 0 deletions
1
packages/babel-plugin-transform-typescript/test/fixtures/class/declare/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 @@ | ||
class A {} |
3 changes: 2 additions & 1 deletion
3
packages/babel-plugin-transform-typescript/test/fixtures/class/properties/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,7 +1,8 @@ | ||
class C { | ||
public a?: number; | ||
private b: number = 0; | ||
readonly c!: number = 1; | ||
readonly c: number = 1; | ||
@foo d: number; | ||
@foo e: number = 3; | ||
f!: number; | ||
} |
4 changes: 4 additions & 0 deletions
4
...ransform-typescript/test/fixtures/class/transform-properties-declare-wrong-order/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,4 @@ | ||
class A { | ||
declare x; | ||
y; | ||
} |
7 changes: 7 additions & 0 deletions
7
...form-typescript/test/fixtures/class/transform-properties-declare-wrong-order/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,7 @@ | ||
{ | ||
"plugins": [ | ||
"proposal-class-properties", | ||
["transform-typescript", { "allowDeclareFields": true }] | ||
], | ||
"throws": "TypeScript 'declare' fields must first be transformed by @babel/plugin-transform-typescript.\nIf you have already enabled that plugin (or '@babel/preset-typescript'), make sure that it runs before any plugin related to additional class features:\n - @babel/plugin-proposal-class-properties\n - @babel/plugin-proposal-private-methods\n - @babel/plugin-proposal-decorators" | ||
} |
4 changes: 4 additions & 0 deletions
4
...bel-plugin-transform-typescript/test/fixtures/class/transform-properties-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,4 @@ | ||
class A { | ||
declare x; | ||
y; | ||
} |
6 changes: 6 additions & 0 deletions
6
...plugin-transform-typescript/test/fixtures/class/transform-properties-declare/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,6 @@ | ||
{ | ||
"plugins": [ | ||
["transform-typescript", { "allowDeclareFields": true }], | ||
"proposal-class-properties" | ||
] | ||
} |
8 changes: 8 additions & 0 deletions
8
...el-plugin-transform-typescript/test/fixtures/class/transform-properties-declare/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,8 @@ | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
|
||
class A { | ||
constructor() { | ||
_defineProperty(this, "y", void 0); | ||
} | ||
|
||
} |
3 changes: 3 additions & 0 deletions
3
...sform-typescript/test/fixtures/class/uninitialized-definite-with-declare-enabled/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 @@ | ||
class A { | ||
x!; | ||
} |
3 changes: 3 additions & 0 deletions
3
...m-typescript/test/fixtures/class/uninitialized-definite-with-declare-enabled/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 @@ | ||
{ | ||
"plugins": [["transform-typescript", { "allowDeclareFields": true }]] | ||
} |
1 change: 1 addition & 0 deletions
1
...form-typescript/test/fixtures/class/uninitialized-definite-with-declare-enabled/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 @@ | ||
class A {} |
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