-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
[Babel 8] Create TSTemplateLiteralType #17066
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
Conversation
|
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/58621 |
| rm tests/format/typescript/typeparams/consistent/format.test.js | ||
| rm tests/format/typescript/template-literal-types/format.test.js | ||
| rm tests/format/typescript/method/format.test.js | ||
| rm tests/format/typescript/argument-expansion/format.test.js |
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.
/cc @fisker These tests are affected by this breaking change.
877427a to
5400a2d
Compare
packages/babel-parser/src/types.ts
Outdated
| export interface TsTemplateLiteralType extends TsTypeBase { | ||
| type: "TSTemplateLiteralType"; | ||
| quasis: TemplateElement[]; | ||
| expressions: TsType[]; |
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 overlooked that TSTemplateLiteralType uses types here. I will update the AST and the generator methods.
a5010ee to
cd7b11e
Compare
Since the generator requires the types information
| export function TSTemplateLiteralType( | ||
| this: Printer, | ||
| node: t.TSTemplateLiteralType, | ||
| ) { | ||
| const quasis = node.quasis; | ||
|
|
||
| let partRaw = "`"; | ||
|
|
||
| for (let i = 0; i < quasis.length; i++) { | ||
| partRaw += quasis[i].value.raw; | ||
|
|
||
| if (i + 1 < quasis.length) { | ||
| this.token(partRaw + "${", true); | ||
| this.print(node.types[i]); | ||
| partRaw = "}"; | ||
| } | ||
| } | ||
|
|
||
| this.token(partRaw + "`", true); | ||
| } |
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.
We can extract this to a function _printTemplate(quasis, substitutions), so that we don't have to keep the same logic in two different printers.
|
Pushed a ranges fix in the |
|
Good question. Yes it can still contain a Line 1 in 616f887
Lines 45 to 62 in 616f887
|
* breaking: create TSTemplateLiteralType * update test fixtures * ignore TSTemplateLiteralType in printer test * purge failing prettier tests * rename expressions to types * fix: generate TSLiteralType when the template does not contain types * restore template literals transform * update test fixtures * define TSTemplateLiteralType for Babel 7 Since the generator requires the types information * refactor: extract _printTemplate helper * Apply template literal range fixes to TSTemplateLiteralType


In this PR, we generate the
TSTemplateLiteralTypeAST node when there is at least onetypeelement (${...}) within the template.