Skip to content
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

fix: assign actual runes value to SvelteParseContext #633

Merged
merged 3 commits into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 5 additions & 13 deletions src/parser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ import type { NormalizedParserOptions } from "./parser-options.js";
import { isTypeScript, normalizeParserOptions } from "./parser-options.js";
import { getFragmentFromRoot } from "./compat.js";
import {
isEnableRunes,
resolveSvelteParseContextForSvelte,
resolveSvelteParseContextForSvelteScript,
type SvelteParseContext,
Expand Down Expand Up @@ -117,20 +116,13 @@ export function parseForESLint(code: string, options?: any): ParseResult {
const parserOptions = normalizeParserOptions(options);

if (
isEnableRunes(svelteConfig, parserOptions) &&
parserOptions.filePath &&
!parserOptions.filePath.endsWith(".svelte") &&
// If no `filePath` is set in ESLint, "<input>" will be specified.
parserOptions.filePath !== "<input>"
(parserOptions.filePath.endsWith(".svelte.js") ||
parserOptions.filePath.endsWith(".svelte.ts"))
Comment on lines +120 to +121
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think only js and ts file can be svelte script file. So I think we can make the terms simpler.

https://svelte.dev/docs/svelte/svelte-js-files

) {
const trimmed = code.trim();
if (!trimmed.startsWith("<") && !trimmed.endsWith(">")) {
const svelteParseContext = resolveSvelteParseContextForSvelteScript(
svelteConfig,
parserOptions,
);
return parseAsScript(code, parserOptions, svelteParseContext);
}
const svelteParseContext =
resolveSvelteParseContextForSvelteScript(svelteConfig);
return parseAsScript(code, parserOptions, svelteParseContext);
}

return parseAsSvelte(code, svelteConfig, parserOptions);
Expand Down
96 changes: 63 additions & 33 deletions src/parser/svelte-parse-context.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
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";

const runeSymbols: string[] = [
"$state",
"$derived",
"$effect",
"$props",
"$bindable",
"$inspect",
"$host",
] as const;

/** The context for parsing. */
export type SvelteParseContext = {
Expand All @@ -18,55 +30,73 @@ export type SvelteParseContext = {
svelteConfig: SvelteConfig | null;
};

export function isEnableRunes(
svelteConfig: SvelteConfig | null,
parserOptions: NormalizedParserOptions,
): boolean {
if (!svelteVersion.gte(5)) return false;
if (parserOptions.svelteFeatures?.runes != null) {
return Boolean(parserOptions.svelteFeatures.runes);
}
if (svelteConfig?.compilerOptions?.runes != null) {
return Boolean(svelteConfig.compilerOptions.runes);
}
return true;
}

export function resolveSvelteParseContextForSvelte(
svelteConfig: SvelteConfig | null,
parserOptions: NormalizedParserOptions,
svelteAst: Compiler.Root | SvAST.AstLegacy,
): SvelteParseContext {
const svelteOptions = (svelteAst as Compiler.Root).options;
if (svelteOptions?.runes != null) {
return {
runes: svelteOptions.runes,
compilerVersion,
svelteConfig,
};
}

return {
runes: isEnableRunes(svelteConfig, parserOptions),
runes: isRunes(svelteConfig, parserOptions, svelteAst),
compilerVersion,
svelteConfig,
};
}

export function resolveSvelteParseContextForSvelteScript(
svelteConfig: SvelteConfig | null,
parserOptions: NormalizedParserOptions,
): SvelteParseContext {
return resolveSvelteParseContext(svelteConfig, parserOptions);
}

function resolveSvelteParseContext(
svelteConfig: SvelteConfig | null,
parserOptions: NormalizedParserOptions,
): SvelteParseContext {
return {
runes: isEnableRunes(svelteConfig, parserOptions),
// .svelte.js files are always in Runes mode for Svelte 5.
runes: svelteVersion.gte(5),
compilerVersion,
svelteConfig,
};
}

function isRunes(
svelteConfig: SvelteConfig | null,
parserOptions: NormalizedParserOptions,
svelteAst: Compiler.Root | SvAST.AstLegacy,
): boolean {
// Svelte 3/4 does not support Runes mode.
if (!svelteVersion.gte(5)) {
return false;
}

// Compiler option.
if (parserOptions.svelteFeatures?.runes != null) {
return parserOptions.svelteFeatures?.runes;
}
if (svelteConfig?.compilerOptions?.runes != null) {
return svelteConfig?.compilerOptions?.runes;
}

// `<svelte:options>`.
const svelteOptions = (svelteAst as Compiler.Root).options;
if (svelteOptions?.runes != null) {
return svelteOptions?.runes;
}

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

function hasRuneSymbol(ast: Compiler.Script | SvAST.Script): boolean {
let hasRuneSymbol = false;
traverseNodes(ast as unknown as ESTree.Node, {
enterNode(node) {
if (node.type === "Identifier" && runeSymbols.includes(node.name)) {
hasRuneSymbol = true;
}
},
leaveNode() {
// do nothing
},
});

return hasRuneSymbol;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/** Config for testing */
export default {};
Loading