-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Transform TypeScript "declare" fields #10546
Merged
nicolo-ribaudo
merged 3 commits into
babel:feat-7.7/ts-declare-fields
from
nicolo-ribaudo:ts-transform-declare-field
Nov 5, 2019
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,11 +44,94 @@ function registerGlobalType(programScope, name) { | |
} | ||
|
||
export default declare( | ||
(api, { jsxPragma = "React", allowNamespaces = false }) => { | ||
( | ||
api, | ||
{ | ||
jsxPragma = "React", | ||
allowNamespaces = false, | ||
allowDeclareFields = false, | ||
}, | ||
) => { | ||
api.assertVersion(7); | ||
|
||
const JSX_ANNOTATION_REGEX = /\*?\s*@jsx\s+([^\s]+)/; | ||
|
||
const classMemberVisitors = { | ||
field(path) { | ||
const { node } = path; | ||
|
||
if (!allowDeclareFields && node.declare) { | ||
throw path.buildCodeFrameError( | ||
`The 'declare' modifier is only allowed when the 'allowDeclareFields' option of ` + | ||
`@babel/plugin-transform-typescript or @babel/preset-typescript is enabled.`, | ||
); | ||
} | ||
if (node.definite || node.declare) { | ||
if (node.value) { | ||
throw path.buildCodeFrameError( | ||
`Definietly assigned fields and fields with the 'declare' modifier cannot` + | ||
` be initialized here, but only in the constructor`, | ||
); | ||
} | ||
|
||
path.remove(); | ||
} else if (!allowDeclareFields && !node.value && !node.decorators) { | ||
path.remove(); | ||
} | ||
|
||
if (node.accessibility) node.accessibility = null; | ||
if (node.abstract) node.abstract = null; | ||
if (node.readonly) node.readonly = null; | ||
if (node.optional) node.optional = null; | ||
if (node.typeAnnotation) node.typeAnnotation = null; | ||
}, | ||
method({ node }) { | ||
if (node.accessibility) node.accessibility = null; | ||
if (node.abstract) node.abstract = null; | ||
if (node.optional) node.optional = null; | ||
|
||
// Rest handled by Function visitor | ||
}, | ||
constructor(path, classPath) { | ||
// Collects parameter properties so that we can add an assignment | ||
// for each of them in the constructor body | ||
// | ||
// We use a WeakSet to ensure an assignment for a parameter | ||
// property is only added once. This is necessary for cases like | ||
// using `transform-classes`, which causes this visitor to run | ||
// twice. | ||
const parameterProperties = []; | ||
for (const param of path.node.params) { | ||
if ( | ||
param.type === "TSParameterProperty" && | ||
!PARSED_PARAMS.has(param.parameter) | ||
) { | ||
PARSED_PARAMS.add(param.parameter); | ||
parameterProperties.push(param.parameter); | ||
} | ||
} | ||
|
||
if (parameterProperties.length) { | ||
const assigns = parameterProperties.map(p => { | ||
let id; | ||
if (t.isIdentifier(p)) { | ||
id = p; | ||
} else if (t.isAssignmentPattern(p) && t.isIdentifier(p.left)) { | ||
id = p.left; | ||
} else { | ||
throw path.buildCodeFrameError( | ||
"Parameter properties can not be destructuring patterns.", | ||
); | ||
} | ||
|
||
return template.statement.ast`this.${id} = ${id}`; | ||
}); | ||
|
||
injectInitialization(classPath, path, assigns); | ||
} | ||
}, | ||
Comment on lines
+88
to
+132
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The code for |
||
}; | ||
|
||
return { | ||
name: "transform-typescript", | ||
inherits: syntaxTypeScript, | ||
|
@@ -192,27 +275,6 @@ export default declare( | |
if (node.definite) node.definite = null; | ||
}, | ||
|
||
ClassMethod(path) { | ||
const { node } = path; | ||
|
||
if (node.accessibility) node.accessibility = null; | ||
if (node.abstract) node.abstract = null; | ||
if (node.optional) node.optional = null; | ||
|
||
// Rest handled by Function visitor | ||
}, | ||
|
||
ClassProperty(path) { | ||
const { node } = path; | ||
|
||
if (node.accessibility) node.accessibility = null; | ||
if (node.abstract) node.abstract = null; | ||
if (node.readonly) node.readonly = null; | ||
if (node.optional) node.optional = null; | ||
if (node.definite) node.definite = null; | ||
if (node.typeAnnotation) node.typeAnnotation = null; | ||
}, | ||
|
||
TSIndexSignature(path) { | ||
path.remove(); | ||
}, | ||
|
@@ -238,54 +300,14 @@ export default declare( | |
// class transform would transform the class, causing more specific | ||
// visitors to not run. | ||
path.get("body.body").forEach(child => { | ||
const childNode = child.node; | ||
|
||
if (t.isClassMethod(childNode, { kind: "constructor" })) { | ||
// Collects parameter properties so that we can add an assignment | ||
// for each of them in the constructor body | ||
// | ||
// We use a WeakSet to ensure an assignment for a parameter | ||
// property is only added once. This is necessary for cases like | ||
// using `transform-classes`, which causes this visitor to run | ||
// twice. | ||
const parameterProperties = []; | ||
for (const param of childNode.params) { | ||
if ( | ||
param.type === "TSParameterProperty" && | ||
!PARSED_PARAMS.has(param.parameter) | ||
) { | ||
PARSED_PARAMS.add(param.parameter); | ||
parameterProperties.push(param.parameter); | ||
} | ||
} | ||
|
||
if (parameterProperties.length) { | ||
const assigns = parameterProperties.map(p => { | ||
let id; | ||
if (t.isIdentifier(p)) { | ||
id = p; | ||
} else if ( | ||
t.isAssignmentPattern(p) && | ||
t.isIdentifier(p.left) | ||
) { | ||
id = p.left; | ||
} else { | ||
throw path.buildCodeFrameError( | ||
"Parameter properties can not be destructuring patterns.", | ||
); | ||
} | ||
|
||
return template.statement.ast`this.${id} = ${id}`; | ||
}); | ||
|
||
injectInitialization(path, child, assigns); | ||
if (child.isClassMethod()) { | ||
if (child.node.kind === "constructor") { | ||
classMemberVisitors.constructor(child, path); | ||
} else { | ||
classMemberVisitors.method(child, path); | ||
} | ||
} else if (child.isClassProperty()) { | ||
childNode.typeAnnotation = null; | ||
|
||
if (!childNode.value && !childNode.decorators) { | ||
child.remove(); | ||
} | ||
classMemberVisitors.field(child, path); | ||
} | ||
}); | ||
}, | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't use the
ClassProperty
visitor because the class fields transform transforms them using theClass
visitor, so they would never be visited in practice.