From a7a94cfb29f6ea85cfafd1afa2c61363f72cb4b3 Mon Sep 17 00:00:00 2001 From: Tan Li Hau Date: Wed, 12 Jan 2022 15:39:33 +0800 Subject: [PATCH] fix: ts preprocessor - consider store suffixed with number (#461) Fixes sveltejs/svelte#7120 the original regex had +-=, which forgot to escape -, resulting ignoring any characters between + to = --- src/transformers/typescript.ts | 2 +- test/fixtures/TypeScriptImports.svelte | 3 ++- test/transformers/typescript.test.ts | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/transformers/typescript.ts b/src/transformers/typescript.ts index ad1ec836..8e128270 100644 --- a/src/transformers/typescript.ts +++ b/src/transformers/typescript.ts @@ -152,7 +152,7 @@ function injectVarsToCode({ // TODO investigate if it's possible to achieve this with a // TS transformer (previous attemps have failed) const codestores = Array.from( - contentForCodestores.match(/\$[^\s();:,[\]{}.?!+-=*/\\~|&%<>^`"'°§]+/g) || + contentForCodestores.match(/\$[^\s();:,[\]{}.?!+\-=*/\\~|&%<>^`"'°§]+/g) || [], (name) => name.slice(1), ).filter((name) => !JAVASCRIPT_RESERVED_KEYWORD_SET.has(name)); diff --git a/test/fixtures/TypeScriptImports.svelte b/test/fixtures/TypeScriptImports.svelte index 72e5ca81..76538343 100644 --- a/test/fixtures/TypeScriptImports.svelte +++ b/test/fixtures/TypeScriptImports.svelte @@ -10,7 +10,7 @@ import Nested from "./Nested.svelte"; import { hello } from "./script"; import { AValue, AType } from "./types"; - import { storeTemplateOnly, storeScriptOnly } from "./store"; + import { storeTemplateOnly, storeScriptOnly, store0 } from "./store"; import { onlyUsedInModuleScript } from "./modulescript"; const ui = { MyNested: Nested }; const val: AType = "test1"; @@ -39,6 +39,7 @@ } $storeScriptOnly; $storeModuleScriptOnly; + $store0; // These shouldn't count as store values: // $\\; $$; $§; $%; $°; $(; $); $[; $]; $<; $>; $ ; $^; $`; $"; $'; diff --git a/test/transformers/typescript.test.ts b/test/transformers/typescript.test.ts index ce09a4ba..21eb0b9e 100644 --- a/test/transformers/typescript.test.ts +++ b/test/transformers/typescript.test.ts @@ -129,7 +129,7 @@ describe('transformer - typescript', () => { expect(code).toContain(`import { hello } from "./script"`); expect(code).toContain(`import { AValue } from "./types"`); expect(code).toContain( - `import { storeTemplateOnly, storeScriptOnly } from "./store"`, + `import { storeTemplateOnly, storeScriptOnly, store0 } from "./store"`, ); expect(code).toContain( `import { onlyUsedInModuleScript } from "./modulescript";`,