Skip to content

Commit d890090

Browse files
authored
fix: virtual references remained (#266)
* fix: virtual references remained * Create twelve-boats-tease.md
1 parent 684ef36 commit d890090

20 files changed

+22380
-277
lines changed

.changeset/twelve-boats-tease.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"svelte-eslint-parser": patch
3+
---
4+
5+
fix: virtual references remained

src/context/script-let.ts

+23-13
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,6 @@ export class ScriptLetContext {
155155
for (const callback of callbacks) {
156156
callback(node as E, result);
157157
}
158-
(node as any).parent = parent;
159-
160-
tokens.shift(); // (
161-
tokens.pop(); // )
162-
tokens.pop(); // ;
163158

164159
if (isTS) {
165160
removeScope(
@@ -184,6 +179,12 @@ export class ScriptLetContext {
184179
);
185180
}
186181

182+
(node as any).parent = parent;
183+
184+
tokens.shift(); // (
185+
tokens.pop(); // )
186+
tokens.pop(); // ;
187+
187188
// Disconnect the tree structure.
188189
exprSt.expression = null as never;
189190
}
@@ -242,7 +243,6 @@ export class ScriptLetContext {
242243
for (const callback of callbacks) {
243244
callback(node, result);
244245
}
245-
(node as any).parent = parent;
246246

247247
const scope = result.getScope(decl);
248248
for (const variable of scope.variables) {
@@ -253,6 +253,8 @@ export class ScriptLetContext {
253253
}
254254
}
255255

256+
(node as any).parent = parent;
257+
256258
tokens.shift(); // const
257259
tokens.pop(); // ;
258260

@@ -340,11 +342,6 @@ export class ScriptLetContext {
340342

341343
// Process for nodes
342344
callback(expr, ctx, idx);
343-
(expr as any).parent = eachBlock;
344-
(ctx as any).parent = eachBlock;
345-
if (idx) {
346-
(idx as any).parent = eachBlock;
347-
}
348345

349346
// Process for scope
350347
result.registerNodeToScope(eachBlock, scope);
@@ -365,6 +362,12 @@ export class ScriptLetContext {
365362
removeReference(ref, scope.upper!);
366363
}
367364

365+
(expr as any).parent = eachBlock;
366+
(ctx as any).parent = eachBlock;
367+
if (idx) {
368+
(idx as any).parent = eachBlock;
369+
}
370+
368371
tokens.shift(); // Array
369372
tokens.shift(); // .
370373
tokens.shift(); // from
@@ -502,7 +505,7 @@ export class ScriptLetContext {
502505
range,
503506
});
504507
if (this.ctx.isTypeScript()) {
505-
source += ` : (${arrayTypings[index]})`;
508+
source += `: (${arrayTypings[index]})`;
506509
}
507510
}
508511
const restore = this.appendScript(
@@ -641,6 +644,7 @@ export class ScriptLetContext {
641644
const comments = result.ast.comments;
642645
const processedComments = [];
643646
const nodeToScope = getNodeToScope(result.scopeManager!);
647+
const postprocessList: (() => void)[] = [];
644648

645649
let tok;
646650
while ((tok = tokens.shift())) {
@@ -749,6 +753,7 @@ export class ScriptLetContext {
749753

750754
result.ast.tokens = processedTokens;
751755
result.ast.comments = processedComments;
756+
postprocessList.forEach((p) => p());
752757

753758
// Helpers
754759
/** Get scope */
@@ -763,7 +768,12 @@ export class ScriptLetContext {
763768

764769
/** Register node to scope */
765770
function registerNodeToScope(node: any, scope: Scope): void {
766-
scope.block = node;
771+
// If we replace the `scope.block` at this time,
772+
// the scope restore calculation will not work, so we will replace the `scope.block` later.
773+
postprocessList.push(() => {
774+
scope.block = node;
775+
});
776+
767777
const scopes = nodeToScope.get(node);
768778
if (scopes) {
769779
scopes.push(scope);

src/scope/index.ts

+48
Original file line numberDiff line numberDiff line change
@@ -418,3 +418,51 @@ export function addAllReferences(
418418
(a, b) => a.identifier.range![0] - b.identifier.range![0]
419419
);
420420
}
421+
422+
/**
423+
* Simplify scope data.
424+
* @deprecated For Debug
425+
*/
426+
export function simplifyScope(scope: Scope): unknown {
427+
return {
428+
type: scope.type,
429+
childScopes: scope.childScopes.map(simplifyScope),
430+
block: {
431+
type: scope.block.type,
432+
loc: JSON.stringify(scope.block.loc),
433+
},
434+
variables:
435+
scope.type === "global" ? null : simplifyVariables(scope.variables),
436+
references: scope.references.map(simplifyReference),
437+
through: scope.through.map(simplifyReference),
438+
get original() {
439+
return scope;
440+
},
441+
};
442+
}
443+
444+
/**
445+
* Simplify variables data.
446+
* @deprecated For Debug
447+
*/
448+
function simplifyVariables(variables: Variable[]) {
449+
return Object.fromEntries(
450+
variables.map((v) => [
451+
v.name,
452+
{
453+
loc: JSON.stringify(v.defs[0]?.node.loc),
454+
},
455+
])
456+
);
457+
}
458+
459+
/**
460+
* Simplify reference data.
461+
* @deprecated For Debug
462+
*/
463+
function simplifyReference(reference: Reference) {
464+
return {
465+
name: reference.identifier.name,
466+
loc: JSON.stringify(reference.identifier.loc),
467+
};
468+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script lang="ts">
2+
const p = Promise.resolve();
3+
</script>
4+
5+
{#await p}
6+
{:then v}
7+
{/await}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[
2+
{
3+
"ruleId": "no-unused-vars",
4+
"code": "v",
5+
"line": 6,
6+
"column": 8
7+
}
8+
]

0 commit comments

Comments
 (0)