Skip to content

Printer introduces syntax error into .tsx files #59587

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

Closed
maxpatiiuk opened this issue Aug 10, 2024 · 0 comments · Fixed by #59612
Closed

Printer introduces syntax error into .tsx files #59587

maxpatiiuk opened this issue Aug 10, 2024 · 0 comments · Fixed by #59612
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@maxpatiiuk
Copy link

maxpatiiuk commented Aug 10, 2024

🔎 Search Terms

tsx printer printing syntax error generic type parameter

🕗 Version & Regression Information

  • This is the behavior in every version I tried.
  • Bug occurs here in the TypeScript source code:
    function emitTypeParameters(parentNode: SignatureDeclaration | InterfaceDeclaration | TypeAliasDeclaration | ClassDeclaration | ClassExpression, typeParameters: NodeArray<TypeParameterDeclaration> | undefined) {
    if (isFunctionLike(parentNode) && parentNode.typeArguments) { // Quick info uses type arguments in place of type parameters on instantiated signatures
    return emitTypeArguments(parentNode, parentNode.typeArguments);
    }
    emitList(parentNode, typeParameters, ListFormat.TypeParameters);
    }

⏯ Playground Link

https://github.com/maxpatiiuk/typescript-printer-tsx-syntax-error

💻 Code

import ts from 'typescript';

const sourceCode = 'export const id = <T,>(id: T): T => id';
const sourceFile = ts.createSourceFile(
  'index.tsx',
  sourceCode,
  ts.ScriptTarget.Latest,
  undefined,
  ts.ScriptKind.TSX
);

const printer = ts.createPrinter();
const printed = printer.printFile(sourceFile);

console.log('Original:');
console.log(sourceCode); // 'export const id = <T,>(id: T): T => id;'
console.log('Printed:');
console.log(printed); // 'export const id = <T>(id: T): T => id;'

🙁 Actual behavior

The printer removes trailing comma from the generic type parameters list.
That trailing comma is crucial to correctly parse the .tsx files. See how without it TypeScript (and ESBuilt and others) are reporting a syntax error: https://www.typescriptlang.org/play/?target=99#code/KYDwDg9gTgLgBAYwgOwM7wEbAGbWABSgEtl4BeOAHgBUAaAPgAoiATALjmoEoPq4z6cVgChQkWIhTo4AQ2wxgUQiXJVqTVrx6d+g1kA

🙂 Expected behavior

When generic type parameters are used in .tsx files, the list must end with , to disambiguate the syntax from jsx opening tag

Additional information about the issue

See also #15713

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue
Projects
None yet
4 participants