Skip to content

Conversation

@JLHwung
Copy link
Contributor

@JLHwung JLHwung commented Jan 16, 2025

Q                       A
Fixed Issues? #16679
Patch: Bug Fix?
Major: Breaking Change? Yes
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link babel/website#3042
Any Dependency Changes?
License MIT

In this PR, we generate the TSTemplateLiteralType AST node when there is at least one type element (${...}) within the template.

@JLHwung JLHwung added PR: Breaking Change 💥 A type of pull request used for our changelog categories for next major release pkg: parser PR: Needs Docs labels Jan 16, 2025
@babel-bot
Copy link
Collaborator

babel-bot commented Jan 16, 2025

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
Copy link
Contributor Author

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.

@JLHwung JLHwung marked this pull request as ready for review January 16, 2025 20:58
@JLHwung JLHwung force-pushed the ts-template-literal-type branch from 877427a to 5400a2d Compare January 17, 2025 18:30
export interface TsTemplateLiteralType extends TsTypeBase {
type: "TSTemplateLiteralType";
quasis: TemplateElement[];
expressions: TsType[];
Copy link
Contributor Author

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.

@JLHwung JLHwung force-pushed the ts-template-literal-type branch from a5010ee to cd7b11e Compare January 17, 2025 19:04
Since the generator requires the types information
Comment on lines 501 to 520
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);
}
Copy link
Member

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.

@babel babel deleted a comment from Patrikoska1 Jan 20, 2025
@JLHwung
Copy link
Contributor Author

JLHwung commented Jan 22, 2025

Pushed a ranges fix in the @babel/eslint-parser for the TSTemplateLiteralType.

@JLHwung JLHwung requested a review from liuxingbaoyu January 24, 2025 15:40
@liuxingbaoyu
Copy link
Member

image
Can TemplateLiteral.expressions still be a TSType?

@JLHwung
Copy link
Contributor Author

JLHwung commented Jan 24, 2025

image Can TemplateLiteral.expressions still be a TSType?

Good question. Yes it can still contain a TSType in certain rare situation: It is invalid syntax but we still parse it for the sake of error recovery.

"right": {
"type": "TemplateLiteral",
"start":16,"end":22,"loc":{"start":{"line":1,"column":16,"index":16},"end":{"line":1,"column":22,"index":22}},
"expressions": [
{
"type": "TSLiteralType",
"start":19,"end":20,"loc":{"start":{"line":1,"column":19,"index":19},"end":{"line":1,"column":20,"index":20}},
"literal": {
"type": "NumericLiteral",
"start":19,"end":20,"loc":{"start":{"line":1,"column":19,"index":19},"end":{"line":1,"column":20,"index":20}},
"extra": {
"rawValue": 0,
"raw": "0"
},
"value": 0
}
}
],

@JLHwung JLHwung merged commit e02b0ff into babel:main Jan 27, 2025
55 checks passed
@JLHwung JLHwung deleted the ts-template-literal-type branch January 27, 2025 15:00
laine-hallot pushed a commit to laine-hallot/uwu-parser that referenced this pull request Mar 31, 2025
* 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
@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Apr 29, 2025
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 29, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: parser PR: Breaking Change 💥 A type of pull request used for our changelog categories for next major release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants