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: resolved issue of $props incorrectly detected as store when using variables named after runes like $props and props #692

Merged
merged 1 commit into from
Mar 27, 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/icy-pianos-film.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte-eslint-parser": patch
---

fix: resolved issue of `$props` incorrectly detected as store when using variables named after runes like `$props` and `props`
13 changes: 11 additions & 2 deletions src/parser/analyze-scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
import { addElementToSortedArray } from "../utils/index.js";
import type { NormalizedParserOptions } from "./parser-options.js";
import type { SvelteParseContext } from "./svelte-parse-context.js";
import { getGlobalsForSvelte } from "./globals.js";
/**
* Analyze scope
*/
Expand Down Expand Up @@ -105,7 +106,10 @@ export function analyzeReactiveScope(scopeManager: ScopeManager): void {
/**
* Analyze store scope. e.g. $count
*/
export function analyzeStoreScope(scopeManager: ScopeManager): void {
export function analyzeStoreScope(
scopeManager: ScopeManager,
svelteParseContext: SvelteParseContext,
): void {
const moduleScope = scopeManager.scopes.find(
(scope) => scope.type === "module",
);
Expand All @@ -114,8 +118,13 @@ export function analyzeStoreScope(scopeManager: ScopeManager): void {
}
const toBeMarkAsUsedReferences: Reference[] = [];

const globals = getGlobalsForSvelte(svelteParseContext);

for (const reference of [...scopeManager.globalScope.through]) {
if (reference.identifier.name.startsWith("$")) {
if (
reference.identifier.name.startsWith("$") &&
!globals.includes(reference.identifier.name as never)
) {
const realName = reference.identifier.name.slice(1);
const variable = moduleScope.set.get(realName);
if (variable) {
Expand Down
4 changes: 2 additions & 2 deletions src/parser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,9 @@ function parseAsSvelte(
sortNodes(ctx.comments);
sortNodes(ctx.tokens);
extractTokens(ctx);
analyzeStoreScope(resultScript.scopeManager!);
analyzeStoreScope(resultScript.scopeManager!, svelteParseContext);
analyzeReactiveScope(resultScript.scopeManager!);
analyzeStoreScope(resultScript.scopeManager!); // for reactive vars
analyzeStoreScope(resultScript.scopeManager!, svelteParseContext); // for reactive vars
analyzeSnippetsScope(ctx.snippets, resultScript.scopeManager!);

// Add $$xxx variable
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"svelteConfig": {
"compilerOptions": {
"runes": true
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<script>
// It should not be recognized as a store.
const props = $props();
</script>

<span>{props}</span>
Loading
Loading