From f00a024b7c289ae5543442c3c6cd4d1d0373e386 Mon Sep 17 00:00:00 2001 From: Jacopo Patroclo Martinelli <42748150+JacopoPatroclo@users.noreply.github.com> Date: Mon, 25 Mar 2024 21:17:05 +0900 Subject: [PATCH] Correctly handle self closing jsx elements (#158) * fix(ts-html-plugin): handle self closing elements as safe avoinding unecessary errors, impr debug expiriance * Create odd-panthers-occur.md Signed-off-by: Arthur Fiorette <47537704+arthurfiorette@users.noreply.github.com> * run ci again Signed-off-by: Arthur Fiorette <47537704+arthurfiorette@users.noreply.github.com> --------- Signed-off-by: Arthur Fiorette <47537704+arthurfiorette@users.noreply.github.com> Co-authored-by: Arthur Fiorette <47537704+arthurfiorette@users.noreply.github.com> --- .changeset/odd-panthers-occur.md | 5 ++ packages/ts-html-plugin/src/util.ts | 14 +++-- .../ts-html-plugin/test/operators.test.ts | 51 +++++++++++-------- .../ts-html-plugin/test/util/lang-server.ts | 9 +++- 4 files changed, 55 insertions(+), 24 deletions(-) create mode 100644 .changeset/odd-panthers-occur.md diff --git a/.changeset/odd-panthers-occur.md b/.changeset/odd-panthers-occur.md new file mode 100644 index 00000000..49898b9a --- /dev/null +++ b/.changeset/odd-panthers-occur.md @@ -0,0 +1,5 @@ +--- +"@kitajs/ts-html-plugin": patch +--- + +Correctly handle self closing jsx elements diff --git a/packages/ts-html-plugin/src/util.ts b/packages/ts-html-plugin/src/util.ts index 893f2367..8af99590 100644 --- a/packages/ts-html-plugin/src/util.ts +++ b/packages/ts-html-plugin/src/util.ts @@ -15,7 +15,9 @@ const ESCAPE_HTML_REGEX = /^(\w+\.)?(escapeHtml|e|escape)/i; /** If the node is a JSX element or fragment */ export function isJsx(ts: typeof TS, node: TS.Node): node is JsxElement | JsxFragment { - return ts.isJsxElement(node) || ts.isJsxFragment(node); + return ( + ts.isJsxElement(node) || ts.isJsxFragment(node) || ts.isJsxSelfClosingElement(node) + ); } export function recursiveDiagnoseJsxElements( @@ -71,7 +73,7 @@ export function diagnoseJsxElement( ): void { const file = node.getSourceFile(); - // Validations that does not applies to fragments + // Validations that does not applies to fragments or serlf closing elements if (ts.isJsxElement(node)) { // Script tags should be ignored if (node.openingElement.tagName.getText() === 'script') { @@ -81,7 +83,7 @@ export function diagnoseJsxElement( const safeAttribute = getSafeAttribute(node.openingElement); // Safe mode warnings - if (safeAttribute) { + if (safeAttribute && node.children) { if ( // Empty element node.children.length === 0 || @@ -129,6 +131,12 @@ export function diagnoseJsxElement( } } + // If this expression does not have children, we can ignore it + // for example it could be a self closing element + if (!node.children) { + return; + } + // Look for expressions for (const exp of node.children) { if (!ts.isJsxExpression(exp)) { diff --git a/packages/ts-html-plugin/test/operators.test.ts b/packages/ts-html-plugin/test/operators.test.ts index 7f5255b1..256dd5c1 100644 --- a/packages/ts-html-plugin/test/operators.test.ts +++ b/packages/ts-html-plugin/test/operators.test.ts @@ -7,6 +7,11 @@ it('Operators are evaluated normally', async () => { await using server = new TSLangServer(__dirname); const diagnostics = await server.openWithDiagnostics/* tsx */ ` + + function CustomComponent(props: { name: string }) { + return ${'