Skip to content

Commit 78f2923

Browse files
authored
fix: correct detection of runes mode in parsed files (#636)
1 parent d8add86 commit 78f2923

File tree

3 files changed

+29
-17
lines changed

3 files changed

+29
-17
lines changed

.changeset/brown-owls-bow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"svelte-eslint-parser": patch
3+
---
4+
5+
fix: correct detection of runes mode in parsed files

src/parser/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import type { NormalizedParserOptions } from "./parser-options.js";
5050
import { isTypeScript, normalizeParserOptions } from "./parser-options.js";
5151
import { getFragmentFromRoot } from "./compat.js";
5252
import {
53+
hasRunesSymbol,
5354
resolveSvelteParseContextForSvelte,
5455
resolveSvelteParseContextForSvelteScript,
5556
type SvelteParseContext,
@@ -142,6 +143,7 @@ function parseAsSvelte(
142143
ctx,
143144
parserOptions,
144145
);
146+
145147
const svelteParseContext = resolveSvelteParseContextForSvelte(
146148
svelteConfig,
147149
parserOptions,
@@ -161,6 +163,7 @@ function parseAsSvelte(
161163
scripts.attrs,
162164
parserOptions,
163165
);
166+
164167
ctx.scriptLet.restore(resultScript);
165168
ctx.tokens.push(...resultScript.ast.tokens);
166169
ctx.comments.push(...resultScript.ast.comments);
@@ -254,7 +257,11 @@ function parseAsSvelte(
254257
styleNodeLoc,
255258
styleNodeRange,
256259
styleSelectorNodeLoc,
257-
svelteParseContext,
260+
svelteParseContext: {
261+
...svelteParseContext,
262+
// The compiler decides if runes mode is used after parsing.
263+
runes: svelteParseContext.runes ?? hasRunesSymbol(resultScript.ast),
264+
},
258265
});
259266
resultScript.visitorKeys = Object.assign({}, KEYS, resultScript.visitorKeys);
260267

src/parser/svelte-parse-context.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import type * as Compiler from "./svelte-ast-types-for-v5.js";
22
import type * as SvAST from "./svelte-ast-types.js";
3-
import type * as ESTree from "estree";
43
import type { NormalizedParserOptions } from "./parser-options.js";
54
import { compilerVersion, svelteVersion } from "./svelte-version.js";
65
import type { SvelteConfig } from "../svelte-config/index.js";
76
import { traverseNodes } from "../traverse.js";
7+
import type { ESLintProgram } from "./index.js";
88

99
const runeSymbols: string[] = [
1010
"$state",
@@ -19,11 +19,16 @@ const runeSymbols: string[] = [
1919
/** The context for parsing. */
2020
export type SvelteParseContext = {
2121
/**
22-
* Whether to use Runes mode.
23-
* May be `true` if the user is using Svelte v5.
24-
* Resolved from `svelte.config.js` or `parserOptions`, but may be overridden by `<svelte:options>`.
22+
* Determines if the file is in Runes mode.
23+
*
24+
* - Svelte 3/4 does not support Runes mode.
25+
* - Checks if `runes` configuration exists in:
26+
* - `parserOptions`
27+
* - `svelte.config.js`
28+
* - `<svelte:options>` in the Svelte file.
29+
* - Returns `true` if the `runes` symbol is present in the Svelte file.
2530
*/
26-
runes: boolean;
31+
runes?: boolean;
2732
/** The version of "svelte/compiler". */
2833
compilerVersion: string;
2934
/** The result of static analysis of `svelte.config.js`. */
@@ -36,7 +41,7 @@ export function resolveSvelteParseContextForSvelte(
3641
svelteAst: Compiler.Root | SvAST.AstLegacy,
3742
): SvelteParseContext {
3843
return {
39-
runes: isRunes(svelteConfig, parserOptions, svelteAst),
44+
runes: isRunesAsParseContext(svelteConfig, parserOptions, svelteAst),
4045
compilerVersion,
4146
svelteConfig,
4247
};
@@ -53,11 +58,11 @@ export function resolveSvelteParseContextForSvelteScript(
5358
};
5459
}
5560

56-
function isRunes(
61+
function isRunesAsParseContext(
5762
svelteConfig: SvelteConfig | null,
5863
parserOptions: NormalizedParserOptions,
5964
svelteAst: Compiler.Root | SvAST.AstLegacy,
60-
): boolean {
65+
): boolean | undefined {
6166
// Svelte 3/4 does not support Runes mode.
6267
if (!svelteVersion.gte(5)) {
6368
return false;
@@ -77,17 +82,12 @@ function isRunes(
7782
return svelteOptions?.runes;
7883
}
7984

80-
// Static analysis.
81-
const { module, instance } = svelteAst;
82-
return (
83-
(module != null && hasRuneSymbol(module)) ||
84-
(instance != null && hasRuneSymbol(instance))
85-
);
85+
return undefined;
8686
}
8787

88-
function hasRuneSymbol(ast: Compiler.Script | SvAST.Script): boolean {
88+
export function hasRunesSymbol(ast: ESLintProgram): boolean {
8989
let hasRuneSymbol = false;
90-
traverseNodes(ast as unknown as ESTree.Node, {
90+
traverseNodes(ast, {
9191
enterNode(node) {
9292
if (hasRuneSymbol) {
9393
return;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy