Transform TypeScript "declare" fields#10546
Transform TypeScript "declare" fields#10546nicolo-ribaudo merged 3 commits intobabel:feat-7.7/ts-declare-fieldsfrom
Conversation
9cf1244 to
6faf79a
Compare
41eefc2 to
b2a6971
Compare
| 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); | ||
| } | ||
| }, |
There was a problem hiding this comment.
The code for method and constructor has just been moved here from the lines before, so that all the class members "visitors" are together.
| const JSX_ANNOTATION_REGEX = /\*?\s*@jsx\s+([^\s]+)/; | ||
|
|
||
| const classMemberVisitors = { | ||
| field(path) { |
There was a problem hiding this comment.
I didn't use the ClassProperty visitor because the class fields transform transforms them using the Class visitor, so they would never be visited in practice.
|
cc @babel/typescript |
| `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` + |
There was a problem hiding this comment.
nit: s/ / /g
(Even though I could comment on multiple lines, I couldn't submit a multiple line suggestion, else I would have created one.)
| (api, { jsxPragma = "React", allowNamespaces = false }) => { | ||
| ( | ||
| api, | ||
| { jsxPragma = "React", allowNamespaces = false, declareFields = false }, |
There was a problem hiding this comment.
Should we name this allowDeclareFields (or allowDeclare) to match allowNamespaces?
There was a problem hiding this comment.
👍 allowDeclareFields
6faf79a to
16e5870
Compare
127754a to
c188fc4
Compare
|
Merged to #10545 (not in master yet) |
* Transform TypeScript "declare" fields * Remove multiple spaces * declareFields -> allowDeclareFields
* Transform TypeScript "declare" fields * Remove multiple spaces * declareFields -> allowDeclareFields
* [parser] Add support for TS declare modifier on fields (#10484) * [parser] Add support for TS declare modifier on fields * Use Object.create(null) * Comment * Add support for TS declare types to types and generator (#10544) * Transform TypeScript "declare" fields (#10546) * Transform TypeScript "declare" fields * Remove multiple spaces * declareFields -> allowDeclareFields * Update after rebase
Since there is a breaking change (uninitialized fields are now initialized to undefined), I enabled it behind an option (
allowDeclareFields). This option is similar touseDefineForClassFields(microsoft/TypeScript#27644), except that [[Set]] vs [[Define]] is set by thelooseoption of the properties transform plugin.Incidentally, this PR fixes #10311 when the plugins are used in the correct order.