Skip to content

feat: Support runes #425

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 27 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
5 changes: 5 additions & 0 deletions .changeset/blue-pets-play.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte-eslint-parser": minor
---

feat: Support runes
2 changes: 1 addition & 1 deletion explorer-v2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"eslint-scope": "^7.0.0",
"esquery": "^1.5.0",
"pako": "^2.0.3",
"svelte": "^4.0.0",
"svelte": "^5.0.0-next.2",
"svelte-eslint-parser": "link:..",
"tslib": "^2.5.0"
},
Expand Down
17 changes: 17 additions & 0 deletions src/parser/globals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { VERSION as SVELTE_VERSION } from "svelte/compiler";

const globalsForSvelte4: Readonly<string[]> = [
"$$slots",
"$$props",
"$$restProps",
] as const;
export const globalsForSvelte5 = [
"$state",
"$derived",
"$effect",
"$effect.pre",
Copy link
Member

Choose a reason for hiding this comment

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

Since $effect.pre is a member of $effect, I don't think it is necessary to define it here.

Suggested change
"$effect.pre",

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed!

"$props",
] as const;
export const globals = SVELTE_VERSION.startsWith("5")
? [...globalsForSvelte4, ...globalsForSvelte5]
: globalsForSvelte4;
3 changes: 2 additions & 1 deletion src/parser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
styleNodeLoc,
styleNodeRange,
} from "./style-context";
import { globals } from "./globals";

export {
StyleContext,
Expand Down Expand Up @@ -122,7 +123,7 @@ export function parseForESLint(
analyzeStoreScope(resultScript.scopeManager!); // for reactive vars

// Add $$xxx variable
for (const $$name of ["$$slots", "$$props", "$$restProps"]) {
for (const $$name of globals) {
const globalScope = resultScript.scopeManager!.globalScope;
const variable = new Variable();
variable.name = $$name;
Expand Down
85 changes: 80 additions & 5 deletions src/parser/typescript/analyze/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ import { VirtualTypeScriptContext } from "../context";
import type { TSESParseForESLintResult } from "../types";
import type ESTree from "estree";
import type { SvelteAttribute, SvelteHTMLElement } from "../../../ast";
import { globalsForSvelte5, globals } from "../../../parser/globals";

export type AnalyzeTypeScriptContext = {
slots: Set<SvelteHTMLElement>;
};

const RESERVED_NAMES = new Set<string>(["$$props", "$$restProps", "$$slots"]);
/**
* Analyze TypeScript source code.
* Generate virtual code to provide correct type information for Svelte store reference namess and scopes.
Expand Down Expand Up @@ -75,8 +75,8 @@ function analyzeStoreReferenceNames(
if (
// Begin with `$`.
reference.identifier.name.startsWith("$") &&
// Ignore it is a reserved variable.
!RESERVED_NAMES.has(reference.identifier.name) &&
// Ignore globals
!globals.includes(reference.identifier.name) &&
// Ignore if it is already defined.
!programScope.set.has(reference.identifier.name)
) {
Expand Down Expand Up @@ -215,9 +215,84 @@ function analyzeDollarDollarVariables(
);
}

for (const svelte5Global of globalsForSvelte5) {
switch (svelte5Global) {
case "$state": {
if (
scopeManager.globalScope!.through.some(
(reference) => reference.identifier.name === svelte5Global,
)
) {
appendDeclareVirtualScript(
svelte5Global,
"<T>(initial: T): T",
"function",
);
appendDeclareVirtualScript(
svelte5Global,
"<T>(): T | undefined",
"function",
);
}
break;
}
case "$derived": {
if (
scopeManager.globalScope!.through.some(
(reference) => reference.identifier.name === svelte5Global,
)
) {
appendDeclareVirtualScript(
svelte5Global,
"<T>(expression: T): T",
"function",
);
}
break;
}
case "$effect":
case "$effect.pre": {
if (
scopeManager.globalScope!.through.some(
(reference) => reference.identifier.name === svelte5Global,
)
) {
appendDeclareVirtualScript(
svelte5Global,
"(fn: () => void | (() => void)): void",
"function",
);
}
break;
}
case "$props": {
if (
scopeManager.globalScope!.through.some(
(reference) => reference.identifier.name === svelte5Global,
)
) {
appendDeclareVirtualScript(svelte5Global, "<T>(): T", "function");
}
break;
}
default: {
const _: never = svelte5Global;
throw Error(`Unknown global: ${_}`);
}
}
}

/** Append declare virtual script */
function appendDeclareVirtualScript(name: string, type: string) {
ctx.appendVirtualScript(`declare let ${name}: ${type};`);
function appendDeclareVirtualScript(
name: string,
type: string,
letOrFunction: "let" | "function" = "let",
) {
if (letOrFunction === "let") {
ctx.appendVirtualScript(`declare let ${name}: ${type};`);
} else {
ctx.appendVirtualScript(`declare function ${name}${type};`);
}
ctx.restoreContext.addRestoreStatementProcess((node, result) => {
if (
node.type !== "VariableDeclaration" ||
Expand Down
144 changes: 144 additions & 0 deletions tests/fixtures/parser/ast/$$slots-scope-output-svelte5.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
{
"type": "global",
"variables": [
{
"name": "$$slots",
"identifiers": [],
"defs": [],
"references": [
{
"identifier": {
"type": "Identifier",
"name": "$$slots",
"range": [
5,
12
],
"loc": {
"start": {
"line": 1,
"column": 5
},
"end": {
"line": 1,
"column": 12
}
}
},
"from": "module",
"init": null,
"resolved": null
}
]
},
{
"name": "$$props",
"identifiers": [],
"defs": [],
"references": []
},
{
"name": "$$restProps",
"identifiers": [],
"defs": [],
"references": []
},
{
"name": "$state",
"identifiers": [],
"defs": [],
"references": []
},
{
"name": "$derived",
"identifiers": [],
"defs": [],
"references": []
},
{
"name": "$effect",
"identifiers": [],
"defs": [],
"references": []
},
{
"name": "$effect.pre",
"identifiers": [],
"defs": [],
"references": []
},
{
"name": "$props",
"identifiers": [],
"defs": [],
"references": []
}
],
"references": [],
"childScopes": [
{
"type": "module",
"variables": [],
"references": [
{
"identifier": {
"type": "Identifier",
"name": "$$slots",
"range": [
5,
12
],
"loc": {
"start": {
"line": 1,
"column": 5
},
"end": {
"line": 1,
"column": 12
}
}
},
"from": "module",
"init": null,
"resolved": null
}
],
"childScopes": [
{
"type": "block",
"variables": [],
"references": [],
"childScopes": [],
"through": []
}
],
"through": [
{
"identifier": {
"type": "Identifier",
"name": "$$slots",
"range": [
5,
12
],
"loc": {
"start": {
"line": 1,
"column": 5
},
"end": {
"line": 1,
"column": 12
}
}
},
"from": "module",
"init": null,
"resolved": null
}
]
}
],
"through": []
}
Loading