diff --git a/.github/workflows/NodeCI.yml b/.github/workflows/NodeCI.yml index d2b0ac80..11c0e80a 100644 --- a/.github/workflows/NodeCI.yml +++ b/.github/workflows/NodeCI.yml @@ -52,10 +52,8 @@ jobs: steps: - uses: actions/checkout@v4 - uses: pnpm/action-setup@v2 - - name: Use Node.js ${{ matrix.node-version }} + - name: Use Node.js uses: actions/setup-node@v4 - with: - node-version: ${{ matrix.node-version }} - name: Install Svelte v4 run: |+ pnpm install -D svelte@4 @@ -153,17 +151,12 @@ jobs: - uses: actions/setup-node@v4 with: node-version: 18 - - name: Install Svelte v4 - run: |+ - pnpm install -D svelte@4 - rm -rf node_modules - name: Install Packages run: pnpm install - name: Update fixtures run: pnpm run update-fixtures - name: Check changes run: | - git checkout package.json && \ git add --all && \ git diff-index --cached HEAD --stat --exit-code test-and-coverage: diff --git a/package.json b/package.json index 0012f31c..954e77d7 100644 --- a/package.json +++ b/package.json @@ -41,7 +41,8 @@ "release": "changeset publish", "test": "pnpm run mocha \"tests/src/**/*.ts\" --reporter dot --timeout 60000", "ts": "node -r esbuild-register", - "update-fixtures": "pnpm run ts ./tools/update-fixtures.ts", + "update-fixtures": "pnpm i -D svelte@4 && pnpm run run-update-fixtures && git checkout package.json && pnpm i && pnpm run run-update-fixtures", + "run-update-fixtures": "pnpm run ts ./tools/update-fixtures.ts", "version:ci": "env-cmd -e version-ci pnpm run build:meta && changeset version" }, "peerDependencies": { diff --git a/tests/fixtures/parser/ast/$$slots-scope-output-svelte5.json b/tests/fixtures/parser/ast/$$slots-scope-output-svelte5.json index 1d1fbe75..77384a87 100644 --- a/tests/fixtures/parser/ast/$$slots-scope-output-svelte5.json +++ b/tests/fixtures/parser/ast/$$slots-scope-output-svelte5.json @@ -9,7 +9,10 @@ "identifier": { "type": "Identifier", "name": "$$slots", - "range": [5, 12], + "range": [ + 5, + 12 + ], "loc": { "start": { "line": 1, @@ -64,4 +67,4 @@ "references": [] } ] -} +} \ No newline at end of file diff --git a/tests/fixtures/parser/ast/docs/template-syntax/02-attributes-and-props/10-scope-output-svelte5.json b/tests/fixtures/parser/ast/docs/template-syntax/02-attributes-and-props/10-scope-output-svelte5.json index ecd15da3..d12c3a83 100644 --- a/tests/fixtures/parser/ast/docs/template-syntax/02-attributes-and-props/10-scope-output-svelte5.json +++ b/tests/fixtures/parser/ast/docs/template-syntax/02-attributes-and-props/10-scope-output-svelte5.json @@ -15,7 +15,10 @@ "identifier": { "type": "Identifier", "name": "$$props", - "range": [12, 19], + "range": [ + 12, + 19 + ], "loc": { "start": { "line": 1, @@ -64,4 +67,4 @@ "references": [] } ] -} +} \ No newline at end of file diff --git a/tests/fixtures/parser/ast/docs/template-syntax/02-attributes-and-props/11-scope-output-svelte5.json b/tests/fixtures/parser/ast/docs/template-syntax/02-attributes-and-props/11-scope-output-svelte5.json index 21617057..f5fdeda0 100644 --- a/tests/fixtures/parser/ast/docs/template-syntax/02-attributes-and-props/11-scope-output-svelte5.json +++ b/tests/fixtures/parser/ast/docs/template-syntax/02-attributes-and-props/11-scope-output-svelte5.json @@ -21,7 +21,10 @@ "identifier": { "type": "Identifier", "name": "$$restProps", - "range": [11, 22], + "range": [ + 11, + 22 + ], "loc": { "start": { "line": 1, @@ -64,4 +67,4 @@ "references": [] } ] -} +} \ No newline at end of file diff --git a/tests/fixtures/parser/ast/docs/template-syntax/13-slot/02-$$slots/01-scope-output-svelte5.json b/tests/fixtures/parser/ast/docs/template-syntax/13-slot/02-$$slots/01-scope-output-svelte5.json index f152869a..2f75bbb3 100644 --- a/tests/fixtures/parser/ast/docs/template-syntax/13-slot/02-$$slots/01-scope-output-svelte5.json +++ b/tests/fixtures/parser/ast/docs/template-syntax/13-slot/02-$$slots/01-scope-output-svelte5.json @@ -9,7 +9,10 @@ "identifier": { "type": "Identifier", "name": "$$slots", - "range": [61, 68], + "range": [ + 61, + 68 + ], "loc": { "start": { "line": 4, @@ -64,4 +67,4 @@ "references": [] } ] -} +} \ No newline at end of file diff --git a/tests/fixtures/parser/ast/svelte5/ts-$effect01-prefer-const-result.json b/tests/fixtures/parser/ast/svelte5/ts-$effect01-prefer-const-result.json index 8739e810..84e4d144 100644 --- a/tests/fixtures/parser/ast/svelte5/ts-$effect01-prefer-const-result.json +++ b/tests/fixtures/parser/ast/svelte5/ts-$effect01-prefer-const-result.json @@ -5,4 +5,4 @@ "line": 3, "column": 7 } -] +] \ No newline at end of file diff --git a/tests/fixtures/parser/ast/svelte5/ts-$effect01-scope-output.json b/tests/fixtures/parser/ast/svelte5/ts-$effect01-scope-output.json index 179d5270..f3f9dbc1 100644 --- a/tests/fixtures/parser/ast/svelte5/ts-$effect01-scope-output.json +++ b/tests/fixtures/parser/ast/svelte5/ts-$effect01-scope-output.json @@ -59,17 +59,17 @@ "type": "Identifier", "name": "$derived", "range": [ - 62, - 70 + 60, + 68 ], "loc": { "start": { "line": 3, - "column": 18 + "column": 16 }, "end": { "line": 3, - "column": 26 + "column": 24 } } }, @@ -89,8 +89,8 @@ "type": "Identifier", "name": "$effect", "range": [ - 86, - 93 + 84, + 91 ], "loc": { "start": { diff --git a/tests/fixtures/parser/ast/svelte5/ts-$effect01-type-output.svelte b/tests/fixtures/parser/ast/svelte5/ts-$effect01-type-output.svelte index 88e645ef..288e9417 100644 --- a/tests/fixtures/parser/ast/svelte5/ts-$effect01-type-output.svelte +++ b/tests/fixtures/parser/ast/svelte5/ts-$effect01-type-output.svelte @@ -2,8 +2,7 @@ let count = $state(0); // count: number, $state(0): 0 let doubled = $derived(count * 2); // doubled: number, $derived(count * 2): number - $effect(() => { - // $effect(() => { // runs when the component is mounted, and again // whenever `count` or `doubled` change, // after the DOM has been updated console.log({ count, doubled }); return () => { // if a callback is provided, it will run // a) immediately before the effect re-runs // b) when the component is destroyed console.log("cleanup"); }; }): void + $effect(() => { // $effect(() => { // runs when the component is mounted, and again // whenever `count` or `doubled` change, // after the DOM has been updated console.log({ count, doubled }); return () => { // if a callback is provided, it will run // a) immediately before the effect re-runs // b) when the component is destroyed console.log("cleanup"); }; }): void // runs when the component is mounted, and again // whenever `count` or `doubled` change, // after the DOM has been updated @@ -18,11 +17,8 @@ }); - -

{count} doubled is {doubled}

- +

{count} doubled is {doubled}

diff --git a/tests/fixtures/parser/ast/svelte5/ts-$effectpre01-type-output.svelte b/tests/fixtures/parser/ast/svelte5/ts-$effectpre01-type-output.svelte index 445adde7..e34d8140 100644 --- a/tests/fixtures/parser/ast/svelte5/ts-$effectpre01-type-output.svelte +++ b/tests/fixtures/parser/ast/svelte5/ts-$effectpre01-type-output.svelte @@ -2,8 +2,7 @@ let count = $state(0); // count: number, $state(0): 0 const doubled = $derived(count * 2); // doubled: number, $derived(count * 2): number - $effect(() => { - // $effect(() => { // runs when the component is mounted, and again // whenever `count` or `doubled` change, // after the DOM has been updated console.log({ count, doubled }); return () => { // if a callback is provided, it will run // a) immediately before the effect re-runs // b) when the component is destroyed console.log("cleanup"); }; }): void + $effect(() => { // $effect(() => { // runs when the component is mounted, and again // whenever `count` or `doubled` change, // after the DOM has been updated console.log({ count, doubled }); return () => { // if a callback is provided, it will run // a) immediately before the effect re-runs // b) when the component is destroyed console.log("cleanup"); }; }): void // runs when the component is mounted, and again // whenever `count` or `doubled` change, // after the DOM has been updated @@ -18,11 +17,8 @@ }); - -

{count} doubled is {doubled}

- +

{count} doubled is {doubled}

diff --git a/tests/fixtures/parser/ast/svelte5/ts-$props01-prefer-const-result.json b/tests/fixtures/parser/ast/svelte5/ts-$props01-prefer-const-result.json index edf06348..785a3f5f 100644 --- a/tests/fixtures/parser/ast/svelte5/ts-$props01-prefer-const-result.json +++ b/tests/fixtures/parser/ast/svelte5/ts-$props01-prefer-const-result.json @@ -23,4 +23,4 @@ "line": 8, "column": 21 } -] +] \ No newline at end of file diff --git a/tests/fixtures/parser/ast/svelte5/ts-$props01-type-output.svelte b/tests/fixtures/parser/ast/svelte5/ts-$props01-type-output.svelte index 39d5f8a3..feba88d5 100644 --- a/tests/fixtures/parser/ast/svelte5/ts-$props01-type-output.svelte +++ b/tests/fixtures/parser/ast/svelte5/ts-$props01-type-output.svelte @@ -1,6 +1,5 @@ -{a} - -{b} - -{c} - -{everythingElse} - +{a} +{b} +{c} +{everythingElse} diff --git a/tests/fixtures/parser/ast/svelte5/ts-$state01-scope-output.json b/tests/fixtures/parser/ast/svelte5/ts-$state01-scope-output.json index 0524a95f..38c277a5 100644 --- a/tests/fixtures/parser/ast/svelte5/ts-$state01-scope-output.json +++ b/tests/fixtures/parser/ast/svelte5/ts-$state01-scope-output.json @@ -79,13 +79,13 @@ "references": [] }, { - "name": "$props", + "name": "$effect", "identifiers": [], "defs": [], "references": [] }, { - "name": "$effect", + "name": "$props", "identifiers": [], "defs": [], "references": [] diff --git a/tests/fixtures/parser/ast/ts-$$props01-scope-output-svelte5.json b/tests/fixtures/parser/ast/ts-$$props01-scope-output-svelte5.json index a09d9d2b..d1c0d303 100644 --- a/tests/fixtures/parser/ast/ts-$$props01-scope-output-svelte5.json +++ b/tests/fixtures/parser/ast/ts-$$props01-scope-output-svelte5.json @@ -79,16 +79,16 @@ "references": [] }, { - "name": "$props", + "name": "$effect", "identifiers": [], "defs": [], "references": [] }, { - "name": "$effect", + "name": "$props", "identifiers": [], "defs": [], "references": [] } ] -} +} \ No newline at end of file diff --git a/tests/fixtures/parser/ast/ts-$$slots01-scope-output-svelte5.json b/tests/fixtures/parser/ast/ts-$$slots01-scope-output-svelte5.json index 917b33b3..9d05138e 100644 --- a/tests/fixtures/parser/ast/ts-$$slots01-scope-output-svelte5.json +++ b/tests/fixtures/parser/ast/ts-$$slots01-scope-output-svelte5.json @@ -55,16 +55,16 @@ "references": [] }, { - "name": "$props", + "name": "$effect", "identifiers": [], "defs": [], "references": [] }, { - "name": "$effect", + "name": "$props", "identifiers": [], "defs": [], "references": [] } ] -} +} \ No newline at end of file diff --git a/tests/fixtures/parser/ast/ts-$$slots02-no-slot-scope-output-svelte5.json b/tests/fixtures/parser/ast/ts-$$slots02-no-slot-scope-output-svelte5.json index 917b33b3..9d05138e 100644 --- a/tests/fixtures/parser/ast/ts-$$slots02-no-slot-scope-output-svelte5.json +++ b/tests/fixtures/parser/ast/ts-$$slots02-no-slot-scope-output-svelte5.json @@ -55,16 +55,16 @@ "references": [] }, { - "name": "$props", + "name": "$effect", "identifiers": [], "defs": [], "references": [] }, { - "name": "$effect", + "name": "$props", "identifiers": [], "defs": [], "references": [] } ] -} +} \ No newline at end of file diff --git a/tests/fixtures/parser/ast/ts-$$slots03-named-scope-output-svelte5.json b/tests/fixtures/parser/ast/ts-$$slots03-named-scope-output-svelte5.json index 917b33b3..9d05138e 100644 --- a/tests/fixtures/parser/ast/ts-$$slots03-named-scope-output-svelte5.json +++ b/tests/fixtures/parser/ast/ts-$$slots03-named-scope-output-svelte5.json @@ -55,16 +55,16 @@ "references": [] }, { - "name": "$props", + "name": "$effect", "identifiers": [], "defs": [], "references": [] }, { - "name": "$effect", + "name": "$props", "identifiers": [], "defs": [], "references": [] } ] -} +} \ No newline at end of file diff --git a/tests/fixtures/parser/ast/ts-$$slots04-named-scope-output-svelte5.json b/tests/fixtures/parser/ast/ts-$$slots04-named-scope-output-svelte5.json index 917b33b3..9d05138e 100644 --- a/tests/fixtures/parser/ast/ts-$$slots04-named-scope-output-svelte5.json +++ b/tests/fixtures/parser/ast/ts-$$slots04-named-scope-output-svelte5.json @@ -55,16 +55,16 @@ "references": [] }, { - "name": "$props", + "name": "$effect", "identifiers": [], "defs": [], "references": [] }, { - "name": "$effect", + "name": "$props", "identifiers": [], "defs": [], "references": [] } ] -} +} \ No newline at end of file diff --git a/tests/fixtures/parser/ast/tutorial/optional-slots03-scope-output-svelte5.json b/tests/fixtures/parser/ast/tutorial/optional-slots03-scope-output-svelte5.json index 89a3a719..a1cb3347 100644 --- a/tests/fixtures/parser/ast/tutorial/optional-slots03-scope-output-svelte5.json +++ b/tests/fixtures/parser/ast/tutorial/optional-slots03-scope-output-svelte5.json @@ -9,7 +9,10 @@ "identifier": { "type": "Identifier", "name": "$$slots", - "range": [758, 765], + "range": [ + 758, + 765 + ], "loc": { "start": { "line": 53, @@ -29,7 +32,10 @@ "identifier": { "type": "Identifier", "name": "$$slots", - "range": [872, 879], + "range": [ + 872, + 879 + ], "loc": { "start": { "line": 58, @@ -84,4 +90,4 @@ "references": [] } ] -} +} \ No newline at end of file diff --git a/tests/src/parser/test-utils.ts b/tests/src/parser/test-utils.ts index 1ef0a910..3cb6cc24 100644 --- a/tests/src/parser/test-utils.ts +++ b/tests/src/parser/test-utils.ts @@ -80,13 +80,13 @@ export function* listupFixtures(dir?: string): Iterable<{ input: string; inputFileName: string; outputFileName: string; - scopeFileName: string; typeFileName: string | null; config: Linter.ParserOptions; requirements: { scope?: Record; }; getScopeFile: () => string | null; + writeScopeFile: (json: string) => void; getRuleOutputFileName: (ruleName: string) => string; meetRequirements: (key: "test" | "scope" | "parse") => boolean; }> { @@ -95,7 +95,7 @@ export function* listupFixtures(dir?: string): Iterable<{ function getScopeFile(inputFileName: string, isSvelte5Only: boolean) { const scopeFileName = inputFileName.replace( - /input\.svelte$/u, + /input\.svelte(?:\.[jt]s)?$/u, "scope-output.json", ); if (!fs.existsSync(scopeFileName)) return null; @@ -106,7 +106,7 @@ function getScopeFile(inputFileName: string, isSvelte5Only: boolean) { const scopeFileJson = JSON.parse(scopeFile); const scopeFileNameSvelte5 = inputFileName.replace( - /input\.svelte$/u, + /input\.svelte(?:\.[jt]s)?$/u, "scope-output-svelte5.json", ); if (!fs.existsSync(scopeFileNameSvelte5)) { @@ -131,17 +131,72 @@ function getScopeFile(inputFileName: string, isSvelte5Only: boolean) { return JSON.stringify(scopeFileJson, null, 2); } +function writeScopeFile( + inputFileName: string, + json: string, + isSvelte5Only: boolean, +) { + const scopeFileName = inputFileName.replace( + /input\.svelte(?:\.[jt]s)?$/u, + "scope-output.json", + ); + if (!SVELTE_VERSION.startsWith("5")) { + // v4 + if (isSvelte5Only) return; + fs.writeFileSync(scopeFileName, json, "utf8"); + return; + } + + // v5 + if (isSvelte5Only) { + fs.writeFileSync(scopeFileName, json, "utf8"); + return; + } + const scopeFileNameSvelte5 = inputFileName.replace( + /input\.svelte(?:\.[jt]s)?$/u, + "scope-output-svelte5.json", + ); + if (!fs.existsSync(scopeFileName)) { + fs.writeFileSync(scopeFileNameSvelte5, json, "utf8"); + return; + } + const baseScope = JSON.parse(fs.readFileSync(scopeFileName, "utf8")); + const scope = JSON.parse(json); + if ( + JSON.stringify({ + ...baseScope, + variables: SVELTE5_SCOPE_VARIABLES_BASE, + }) === JSON.stringify(scope) + ) { + return; + } + + for (const key of Object.keys(scope)) { + if ( + baseScope[key] && + JSON.stringify(baseScope[key]) === JSON.stringify(scope[key]) + ) { + delete scope[key]; + } + } + fs.writeFileSync( + scopeFileNameSvelte5, + JSON.stringify(scope, null, 2), + "utf8", + ); +} + function* listupFixturesImpl(dir: string): Iterable<{ input: string; inputFileName: string; outputFileName: string; - scopeFileName: string; typeFileName: string | null; config: Linter.ParserOptions; requirements: { scope?: Record; }; getScopeFile: () => string | null; + writeScopeFile: (json: string) => void; getRuleOutputFileName: (ruleName: string) => string; meetRequirements: (key: "test" | "scope" | "parse") => boolean; }> { @@ -158,10 +213,6 @@ function* listupFixturesImpl(dir: string): Iterable<{ /input\.svelte$/u, "output.json", ); - const scopeFileName = inputFileName.replace( - /input\.svelte$/u, - "scope-output.json", - ); const typeFileName = inputFileName.replace( /input\.svelte$/u, "type-output.svelte", @@ -186,14 +237,15 @@ function* listupFixturesImpl(dir: string): Iterable<{ input, inputFileName, outputFileName, - scopeFileName, typeFileName: fs.existsSync(typeFileName) ? typeFileName : null, config, requirements, getScopeFile: () => getScopeFile(inputFileName, isSvelte5Only), + writeScopeFile: (json: string) => + writeScopeFile(inputFileName, json, isSvelte5Only), getRuleOutputFileName: (ruleName) => { return inputFileName.replace( - /input\.svelte$/u, + /input\.svelte(?:\.[jt]s)?$/u, `${ruleName}-result.json`, ); }, diff --git a/tools/update-fixtures.ts b/tools/update-fixtures.ts index 53bdc6b4..beaec29b 100644 --- a/tools/update-fixtures.ts +++ b/tools/update-fixtures.ts @@ -68,7 +68,7 @@ for (const { input, inputFileName, outputFileName, - scopeFileName, + writeScopeFile, typeFileName, config, meetRequirements, @@ -85,7 +85,7 @@ for (const { const astJson = astToJson(result.ast); fs.writeFileSync(outputFileName, astJson, "utf8"); const scopeJson = scopeToJSON(result.scopeManager); - fs.writeFileSync(scopeFileName, scopeJson, "utf8"); + writeScopeFile(scopeJson); if (typeFileName) { fs.writeFileSync(typeFileName, buildTypes(input, result), "utf8");