Skip to content

fix: correct detection of runes mode in parsed files #636

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/brown-owls-bow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte-eslint-parser": patch
---

fix: correct detection of runes mode in parsed files
9 changes: 8 additions & 1 deletion src/parser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import type { NormalizedParserOptions } from "./parser-options.js";
import { isTypeScript, normalizeParserOptions } from "./parser-options.js";
import { getFragmentFromRoot } from "./compat.js";
import {
hasRunesSymbol,
resolveSvelteParseContextForSvelte,
resolveSvelteParseContextForSvelteScript,
type SvelteParseContext,
Expand Down Expand Up @@ -142,6 +143,7 @@ function parseAsSvelte(
ctx,
parserOptions,
);

const svelteParseContext = resolveSvelteParseContextForSvelte(
svelteConfig,
parserOptions,
Expand All @@ -161,6 +163,7 @@ function parseAsSvelte(
scripts.attrs,
parserOptions,
);

ctx.scriptLet.restore(resultScript);
ctx.tokens.push(...resultScript.ast.tokens);
ctx.comments.push(...resultScript.ast.comments);
Expand Down Expand Up @@ -254,7 +257,11 @@ function parseAsSvelte(
styleNodeLoc,
styleNodeRange,
styleSelectorNodeLoc,
svelteParseContext,
svelteParseContext: {
...svelteParseContext,
// The compiler decides if runes mode is used after parsing.
runes: svelteParseContext.runes ?? hasRunesSymbol(resultScript.ast),
},
});
resultScript.visitorKeys = Object.assign({}, KEYS, resultScript.visitorKeys);

Expand Down
32 changes: 16 additions & 16 deletions src/parser/svelte-parse-context.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type * as Compiler from "./svelte-ast-types-for-v5.js";
import type * as SvAST from "./svelte-ast-types.js";
import type * as ESTree from "estree";
import type { NormalizedParserOptions } from "./parser-options.js";
import { compilerVersion, svelteVersion } from "./svelte-version.js";
import type { SvelteConfig } from "../svelte-config/index.js";
import { traverseNodes } from "../traverse.js";
import type { ESLintProgram } from "./index.js";

const runeSymbols: string[] = [
"$state",
Expand All @@ -19,11 +19,16 @@ const runeSymbols: string[] = [
/** The context for parsing. */
export type SvelteParseContext = {
/**
* Whether to use Runes mode.
* May be `true` if the user is using Svelte v5.
* Resolved from `svelte.config.js` or `parserOptions`, but may be overridden by `<svelte:options>`.
* Determines if the file is in Runes mode.
*
* - Svelte 3/4 does not support Runes mode.
* - Checks if `runes` configuration exists in:
* - `parserOptions`
* - `svelte.config.js`
* - `<svelte:options>` in the Svelte file.
* - Returns `true` if the `runes` symbol is present in the Svelte file.
*/
runes: boolean;
runes?: boolean;
/** The version of "svelte/compiler". */
compilerVersion: string;
/** The result of static analysis of `svelte.config.js`. */
Expand All @@ -36,7 +41,7 @@ export function resolveSvelteParseContextForSvelte(
svelteAst: Compiler.Root | SvAST.AstLegacy,
): SvelteParseContext {
return {
runes: isRunes(svelteConfig, parserOptions, svelteAst),
runes: isRunesAsParseContext(svelteConfig, parserOptions, svelteAst),
compilerVersion,
svelteConfig,
};
Expand All @@ -53,11 +58,11 @@ export function resolveSvelteParseContextForSvelteScript(
};
}

function isRunes(
function isRunesAsParseContext(
svelteConfig: SvelteConfig | null,
parserOptions: NormalizedParserOptions,
svelteAst: Compiler.Root | SvAST.AstLegacy,
): boolean {
): boolean | undefined {
// Svelte 3/4 does not support Runes mode.
if (!svelteVersion.gte(5)) {
return false;
Expand All @@ -77,17 +82,12 @@ function isRunes(
return svelteOptions?.runes;
}

// Static analysis.
const { module, instance } = svelteAst;
return (
(module != null && hasRuneSymbol(module)) ||
(instance != null && hasRuneSymbol(instance))
);
return undefined;
}

function hasRuneSymbol(ast: Compiler.Script | SvAST.Script): boolean {
export function hasRunesSymbol(ast: ESLintProgram): boolean {
let hasRuneSymbol = false;
traverseNodes(ast as unknown as ESTree.Node, {
traverseNodes(ast, {
enterNode(node) {
if (hasRuneSymbol) {
return;
Expand Down
Loading