Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/six-pianos-know.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'esrap': patch
---

fix: preserve type arguments on tagged templates, array pattern annotations, and template literal type quasis
13 changes: 12 additions & 1 deletion src/languages/ts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,7 @@ export default (options = {}) => {
false
);
context.write(']', token_before(node.loc?.end));
if ('typeAnnotation' in node && node.typeAnnotation) context.visit(node.typeAnnotation);
},

/**
Expand Down Expand Up @@ -1779,6 +1780,7 @@ export default (options = {}) => {
/** @type {string} */ (node.tag.type) === 'ChainExpression' ||
EXPRESSIONS_PRECEDENCE[node.tag.type] < EXPRESSIONS_PRECEDENCE.CallExpression;
maybe_wrap(context, node.tag, wrap);
if (node.typeArguments) context.visit(node.typeArguments);
context.visit(node.quasi);
},

Expand Down Expand Up @@ -2065,7 +2067,16 @@ export default (options = {}) => {

if (/\n/.test(raw)) context.multiline = true;
}
context.write('`');
const raw = quasis[quasis.length - 1].value.raw;
context.write(raw + '`');
if (/\n/.test(raw)) context.multiline = true;
Comment thread
manuel3108 marked this conversation as resolved.
},

// @ts-expect-error not in TSESTree types
TSJSDocNullableType(node, context) {
if (!node.postfix) context.write('?');
context.visit(node.typeAnnotation);
if (node.postfix) context.write('?');
},

TSParameterProperty(node, context) {
Expand Down
31 changes: 31 additions & 0 deletions test/compat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,34 @@ test('acorn TS printer preserves module and mapped-type keywords', () => {

expect(code).toBe('declare module "svelte" {\n}\n\ntype M = {[K in keyof JSON]: K};');
});

test('TSJSDocNullableType prefix and postfix', () => {
/** @type {any} */
const ast = {
type: 'Program',
body: [
{
type: 'TSTypeAliasDeclaration',
id: { type: 'Identifier', name: 'A' },
typeAnnotation: {
type: 'TSJSDocNullableType',
typeAnnotation: { type: 'TSNumberKeyword' },
postfix: false
}
},
{
type: 'TSTypeAliasDeclaration',
id: { type: 'Identifier', name: 'B' },
typeAnnotation: {
type: 'TSJSDocNullableType',
typeAnnotation: { type: 'TSNumberKeyword' },
postfix: true
}
}
],
sourceType: 'module'
};

const { code } = print(ast, ts());
expect(code).toBe('type A = ?number;\ntype B = number?;');
});
5 changes: 5 additions & 0 deletions test/samples/ts-array-patterns/expected.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const [a, b]: string[] = ['foo', 'bar'];

function fn([first, second]: number[]) {
return first + second;
}
11 changes: 11 additions & 0 deletions test/samples/ts-array-patterns/expected.ts.map
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"version": 3,
"names": [],
"sources": [
"input.js"
],
"sourcesContent": [
"const [a, b]: string[] = ['foo', 'bar'];\n\nfunction fn([first, second]: number[]) {\n\treturn first + second;\n}\n"
],
"mappings": "AAAA,MAAM,CAAC,CAAC,EAAE,CAAC,GAAG,MAAM,MAAM,KAAK,EAAE,KAAK;;AAEtC,QAAQ,CAAC,EAAE,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,IAAI,CAAC;CACvC,MAAM,CAAC,KAAK,GAAG,MAAM;AACtB,CAAC"
}
5 changes: 5 additions & 0 deletions test/samples/ts-array-patterns/input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const [a, b]: string[] = ['foo', 'bar'];

function fn([first, second]: number[]) {
return first + second;
}
3 changes: 3 additions & 0 deletions test/samples/ts-tagged-template/expected.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
sql<string[]>`SELECT * FROM table`;

const res = tag<A, B>`hello ${world}`;
11 changes: 11 additions & 0 deletions test/samples/ts-tagged-template/expected.ts.map
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"version": 3,
"names": [],
"sources": [
"input.js"
],
"sourcesContent": [
"sql<string[]>`SELECT * FROM table`;\nconst res = tag<A, B>`hello ${world}`;\n"
],
"mappings": "AAAA,GAAG,CAAC,MAAM;;AACV,MAAM,AAAA,GAAG,GAAG,GAAG,CAAC,CAAC,EAAE,CAAC,UAAU,KAAK"
}
2 changes: 2 additions & 0 deletions test/samples/ts-tagged-template/input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
sql<string[]>`SELECT * FROM table`;
const res = tag<A, B>`hello ${world}`;
4 changes: 4 additions & 0 deletions test/samples/ts-template-literal-types/expected.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
type Event = `on${string}`;
type Path<T extends string> = `/${T}/index.html`;
type Plain = `static_string`;
type Multi<A extends string, B extends string> = `prefix_${A}_middle_${B}_suffix`;
11 changes: 11 additions & 0 deletions test/samples/ts-template-literal-types/expected.ts.map
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"version": 3,
"names": [],
"sources": [
"input.js"
],
"sourcesContent": [
"type Event = `on${string}`;\ntype Path<T extends string> = `/${T}/index.html`;\ntype Plain = `static_string`;\ntype Multi<A extends string, B extends string> = `prefix_${A}_middle_${B}_suffix`;\n"
],
"mappings": "KAAK,KAAK,QAAQ,MAAM;KACnB,IAAI,CAAC,CAAgB,SAAN,MAAM,QAAQ,CAAC;KAC9B,KAAK;KACL,KAAK,CAAC,CAAgB,SAAN,MAAM,EAAE,CAAgB,SAAN,MAAM,cAAc,CAAC,WAAW,CAAC"
}
4 changes: 4 additions & 0 deletions test/samples/ts-template-literal-types/input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
type Event = `on${string}`;
type Path<T extends string> = `/${T}/index.html`;
type Plain = `static_string`;
type Multi<A extends string, B extends string> = `prefix_${A}_middle_${B}_suffix`;
Loading