Skip to content

Commit dc6a5f0

Browse files
committed
add rune tests (wip)
1 parent fd8ffa7 commit dc6a5f0

12 files changed

+2854
-74
lines changed

.eslintrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ module.exports = {
2020
"no-lonely-if": "off",
2121
"no-shadow": "off",
2222
"no-warning-comments": "warn",
23-
"require-jsdoc": "error",
23+
"require-jsdoc": "off",
2424
"prettier/prettier": [
2525
"error",
2626
{},

benchmark/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// eslint-disable-next-line eslint-comments/disable-enable-pair -- ignore
2-
/* eslint-disable require-jsdoc, no-console -- ignore */
2+
/* eslint-disable no-console -- ignore */
33
import * as Benchmark from "benchmark";
44
import fs from "fs";
55
import { parseForESLint } from "../src/index";

src/parser/typescript/analyze/index.ts

Lines changed: 88 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -215,84 +215,76 @@ function analyzeDollarDollarVariables(
215215
);
216216
}
217217

218-
for (const svelte5Global of globalsForSvelte5) {
219-
switch (svelte5Global) {
220-
case "$state": {
221-
if (
222-
scopeManager.globalScope!.through.some(
223-
(reference) => reference.identifier.name === svelte5Global,
224-
)
225-
) {
226-
appendDeclareVirtualScript(
227-
svelte5Global,
228-
"<T>(initial: T): T",
229-
"function",
230-
);
231-
appendDeclareVirtualScript(
232-
svelte5Global,
233-
"<T>(): T | undefined",
234-
"function",
235-
);
218+
addSvelte5Globals();
219+
220+
function addSvelte5Globals() {
221+
for (const svelte5Global of globalsForSvelte5) {
222+
switch (svelte5Global) {
223+
case "$state": {
224+
if (
225+
scopeManager.globalScope!.through.some(
226+
(reference) => reference.identifier.name === svelte5Global,
227+
)
228+
) {
229+
appendDeclareFunctionVirtualScript(
230+
svelte5Global,
231+
"<T>(initial: T): T",
232+
);
233+
appendDeclareFunctionVirtualScript(
234+
svelte5Global,
235+
"<T>(): T | undefined",
236+
);
237+
}
238+
break;
236239
}
237-
break;
238-
}
239-
case "$derived": {
240-
if (
241-
scopeManager.globalScope!.through.some(
242-
(reference) => reference.identifier.name === svelte5Global,
243-
)
244-
) {
245-
appendDeclareVirtualScript(
246-
svelte5Global,
247-
"<T>(expression: T): T",
248-
"function",
249-
);
240+
case "$derived": {
241+
if (
242+
scopeManager.globalScope!.through.some(
243+
(reference) => reference.identifier.name === svelte5Global,
244+
)
245+
) {
246+
appendDeclareFunctionVirtualScript(
247+
svelte5Global,
248+
"<T>(expression: T): T",
249+
);
250+
}
251+
break;
250252
}
251-
break;
252-
}
253-
case "$effect":
254-
case "$effect.pre": {
255-
if (
256-
scopeManager.globalScope!.through.some(
257-
(reference) => reference.identifier.name === svelte5Global,
258-
)
259-
) {
260-
appendDeclareVirtualScript(
261-
svelte5Global,
262-
"(fn: () => void | (() => void)): void",
263-
"function",
264-
);
253+
case "$effect":
254+
case "$effect.pre": {
255+
if (
256+
scopeManager.globalScope!.through.some(
257+
(reference) => reference.identifier.name === svelte5Global,
258+
)
259+
) {
260+
appendDeclareFunctionVirtualScript(
261+
svelte5Global,
262+
"(fn: () => void | (() => void)): void",
263+
);
264+
}
265+
break;
265266
}
266-
break;
267-
}
268-
case "$props": {
269-
if (
270-
scopeManager.globalScope!.through.some(
271-
(reference) => reference.identifier.name === svelte5Global,
272-
)
273-
) {
274-
appendDeclareVirtualScript(svelte5Global, "<T>(): T", "function");
267+
case "$props": {
268+
if (
269+
scopeManager.globalScope!.through.some(
270+
(reference) => reference.identifier.name === svelte5Global,
271+
)
272+
) {
273+
appendDeclareFunctionVirtualScript(svelte5Global, "<T>(): T");
274+
}
275+
break;
276+
}
277+
default: {
278+
const _: never = svelte5Global;
279+
throw Error(`Unknown global: ${_}`);
275280
}
276-
break;
277-
}
278-
default: {
279-
const _: never = svelte5Global;
280-
throw Error(`Unknown global: ${_}`);
281281
}
282282
}
283283
}
284284

285285
/** Append declare virtual script */
286-
function appendDeclareVirtualScript(
287-
name: string,
288-
type: string,
289-
letOrFunction: "let" | "function" = "let",
290-
) {
291-
if (letOrFunction === "let") {
292-
ctx.appendVirtualScript(`declare let ${name}: ${type};`);
293-
} else {
294-
ctx.appendVirtualScript(`declare function ${name}${type};`);
295-
}
286+
function appendDeclareVirtualScript(name: string, type: string) {
287+
ctx.appendVirtualScript(`declare let ${name}: ${type};`);
296288
ctx.restoreContext.addRestoreStatementProcess((node, result) => {
297289
if (
298290
node.type !== "VariableDeclaration" ||
@@ -317,6 +309,33 @@ function analyzeDollarDollarVariables(
317309
return true;
318310
});
319311
}
312+
313+
/** Append declare virtual script */
314+
function appendDeclareFunctionVirtualScript(name: string, type: string) {
315+
ctx.appendVirtualScript(`declare function ${name}${type};`);
316+
ctx.restoreContext.addRestoreStatementProcess((node, result) => {
317+
if (
318+
node.type !== "TSDeclareFunction" ||
319+
!node.declare ||
320+
node.id?.type !== "Identifier" ||
321+
node.id.name !== name
322+
) {
323+
return false;
324+
}
325+
const program = result.ast;
326+
program.body.splice(program.body.indexOf(node), 1);
327+
328+
const scopeManager = result.scopeManager as ScopeManager;
329+
330+
// Remove `declare` variable
331+
removeAllScopeAndVariableAndReference(node, {
332+
visitorKeys: result.visitorKeys,
333+
scopeManager,
334+
});
335+
336+
return true;
337+
});
338+
}
320339
}
321340

322341
/**
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<script lang="ts">
2+
let count = $state(0);
3+
let name = $state();
4+
</script>
5+
6+
<button on:click="{() => count++}">
7+
clicks: {count}
8+
</button>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[
2+
{
3+
"ruleId": "no-unused-vars",
4+
"code": "name",
5+
"line": 3,
6+
"column": 7
7+
}
8+
]

0 commit comments

Comments
 (0)