From b1f579c7dc4b53cba73f872da2b69b6fd4383abd Mon Sep 17 00:00:00 2001 From: paoloricciuti Date: Wed, 18 Dec 2024 12:31:19 +0100 Subject: [PATCH] fix: properly transform invalid identifiers --- .../codemods/legacy-story.test.ts | 22 +++++++++++++++++++ .../pre-transform/codemods/legacy-story.ts | 5 ++--- .../codemods/template-to-snippet.test.ts | 21 ++++++++++++++++++ .../codemods/template-to-snippet.ts | 3 ++- src/utils/identifier-utils.ts | 20 +++++++++++++++++ 5 files changed, 67 insertions(+), 4 deletions(-) diff --git a/src/compiler/pre-transform/codemods/legacy-story.test.ts b/src/compiler/pre-transform/codemods/legacy-story.test.ts index 4ec8788a..d3ddf47d 100644 --- a/src/compiler/pre-transform/codemods/legacy-story.test.ts +++ b/src/compiler/pre-transform/codemods/legacy-story.test.ts @@ -188,6 +188,28 @@ describe(transformLegacyStory.name, () => { ).toMatchInlineSnapshot(`""`); }); + it("transforms 'template' prop to 'children' and text expression becomes expression tag with identifier to snippet (case with invalid identifier)", async ({ + expect, + }) => { + const code = ` + + + + `; + const component = await parseAndExtractSvelteNode(code, 'Component'); + + expect( + print( + transformLegacyStory({ + component, + state: { componentIdentifierName: {} }, + }) + ) + ).toMatchInlineSnapshot(`""`); + }); + it("when directive 'let:args' is used then it wraps Story fragment with 'children' snippet block", async ({ expect, }) => { diff --git a/src/compiler/pre-transform/codemods/legacy-story.ts b/src/compiler/pre-transform/codemods/legacy-story.ts index fcf77d62..523174af 100644 --- a/src/compiler/pre-transform/codemods/legacy-story.ts +++ b/src/compiler/pre-transform/codemods/legacy-story.ts @@ -1,5 +1,3 @@ -import { camelCase } from 'es-toolkit/string'; - import { createASTArrayExpression, createASTAttribute, @@ -11,6 +9,7 @@ import { } from '$lib/parser/ast.js'; import { InvalidTemplateAttribute } from '$lib/utils/error/legacy-api/index.js'; +import { hashTemplateName } from '#utils/identifier-utils'; import type { State } from '..'; interface Params { @@ -253,7 +252,7 @@ function templateToChildren( value: [ createASTExpressionTag({ type: 'Identifier', - name: camelCase( + name: hashTemplateName( value[0].type === 'Text' ? value[0].data : ((value[0].expression as ESTreeAST.Literal).value as string) diff --git a/src/compiler/pre-transform/codemods/template-to-snippet.test.ts b/src/compiler/pre-transform/codemods/template-to-snippet.test.ts index 4955122b..f2ea7dd6 100644 --- a/src/compiler/pre-transform/codemods/template-to-snippet.test.ts +++ b/src/compiler/pre-transform/codemods/template-to-snippet.test.ts @@ -46,6 +46,27 @@ describe(transformTemplateToSnippet.name, () => { `); }); + it("covers a case with provided prop 'id' and prop `id` not being a valid identifier", async ({ + expect, + }) => { + const code = ` + + + + `; + const component = await parseAndExtractSvelteNode(code, 'Component'); + + expect(print(transformTemplateToSnippet({ component }))).toMatchInlineSnapshot(` + "{#snippet template_haitqt(args)} +