Skip to content

Commit 6526483

Browse files
committed
fix: resolved an issue where $props was incorrectly recognized as a store when variables shared names with runes, such as $props and props.
1 parent c5fe769 commit 6526483

11 files changed

+2420
-4
lines changed

.changeset/icy-pianos-film.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"svelte-eslint-parser": patch
3+
---
4+
5+
fix: resolved issue of `$props` incorrectly detected as store when using variables named after runes like `$props` and `props`

src/parser/analyze-scope.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
import { addElementToSortedArray } from "../utils/index.js";
1717
import type { NormalizedParserOptions } from "./parser-options.js";
1818
import type { SvelteParseContext } from "./svelte-parse-context.js";
19+
import { getGlobalsForSvelte } from "./globals.js";
1920
/**
2021
* Analyze scope
2122
*/
@@ -105,7 +106,10 @@ export function analyzeReactiveScope(scopeManager: ScopeManager): void {
105106
/**
106107
* Analyze store scope. e.g. $count
107108
*/
108-
export function analyzeStoreScope(scopeManager: ScopeManager): void {
109+
export function analyzeStoreScope(
110+
scopeManager: ScopeManager,
111+
svelteParseContext: SvelteParseContext,
112+
): void {
109113
const moduleScope = scopeManager.scopes.find(
110114
(scope) => scope.type === "module",
111115
);
@@ -114,8 +118,13 @@ export function analyzeStoreScope(scopeManager: ScopeManager): void {
114118
}
115119
const toBeMarkAsUsedReferences: Reference[] = [];
116120

121+
const globals = getGlobalsForSvelte(svelteParseContext);
122+
117123
for (const reference of [...scopeManager.globalScope.through]) {
118-
if (reference.identifier.name.startsWith("$")) {
124+
if (
125+
reference.identifier.name.startsWith("$") &&
126+
!globals.includes(reference.identifier.name as never)
127+
) {
119128
const realName = reference.identifier.name.slice(1);
120129
const variable = moduleScope.set.get(realName);
121130
if (variable) {

src/parser/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,9 @@ function parseAsSvelte(
170170
sortNodes(ctx.comments);
171171
sortNodes(ctx.tokens);
172172
extractTokens(ctx);
173-
analyzeStoreScope(resultScript.scopeManager!);
173+
analyzeStoreScope(resultScript.scopeManager!, svelteParseContext);
174174
analyzeReactiveScope(resultScript.scopeManager!);
175-
analyzeStoreScope(resultScript.scopeManager!); // for reactive vars
175+
analyzeStoreScope(resultScript.scopeManager!, svelteParseContext); // for reactive vars
176176
analyzeSnippetsScope(ctx.snippets, resultScript.scopeManager!);
177177

178178
// Add $$xxx variable
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"svelteConfig": {
3+
"compilerOptions": {
4+
"runes": true
5+
}
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<script>
2+
// It should not be recognized as a store.
3+
const props = $props();
4+
</script>
5+
6+
<span>{props}</span>

0 commit comments

Comments
 (0)