From dd8cea468ef6e6b67a6336d92a8f6eb8974164f6 Mon Sep 17 00:00:00 2001 From: Parth Mane Date: Mon, 8 Apr 2024 13:02:00 +0530 Subject: [PATCH 1/2] chore: Support type parameters on JSX components in TSX ( />) --- lib/printer.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/printer.ts b/lib/printer.ts index 5b3f9618..df020115 100644 --- a/lib/printer.ts +++ b/lib/printer.ts @@ -1331,6 +1331,8 @@ function genericPrintNoParens(path: any, options: any, print: any) { case "JSXOpeningElement": { parts.push("<", path.call(print, "name")); + const typeDefPart = path.call(print, "typeParameters"); + if (typeDefPart.length) parts.push(typeDefPart); const attrParts: any[] = []; path.each(function (attrPath: any) { From ec2d873c6afe1525b29bef0b2cf16d562bb30219 Mon Sep 17 00:00:00 2001 From: Parth Mane Date: Thu, 2 May 2024 17:44:10 +0530 Subject: [PATCH 2/2] tests: Add TSX tests --- test/run.ts | 1 + test/tsx.ts | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 test/tsx.ts diff --git a/test/run.ts b/test/run.ts index 39534ed8..293ea586 100644 --- a/test/run.ts +++ b/test/run.ts @@ -14,4 +14,5 @@ import "./printer"; import "./syntax"; import "./flow"; import "./typescript"; +import "./tsx"; import "./visit"; diff --git a/test/tsx.ts b/test/tsx.ts new file mode 100644 index 00000000..5f288fe8 --- /dev/null +++ b/test/tsx.ts @@ -0,0 +1,23 @@ +"use strict"; + +const nodeMajorVersion = parseInt(process.versions.node, 10); +import * as parser from "../parsers/babel-ts"; +import { EOL as eol } from "os"; +import * as recast from "../main"; +import assert from "assert"; + +(nodeMajorVersion >= 6 ? describe : xdescribe)( + "Babel TSX Compatibility", + function () { + function check(lines: string[]) { + const code = lines.join(eol); + const ast = recast.parse(code, { parser }); + const output = recast.prettyPrint(ast, { tabWidth: 2 }).code; + assert.strictEqual(code, output); + } + + it("should parse and print typed JSX elements", function () { + check([" />;"]); + }); + }, +);