Skip to content

fix: virtual references remained #266

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 2 commits into from
Jan 15, 2023
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/twelve-boats-tease.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte-eslint-parser": patch
---

fix: virtual references remained
36 changes: 23 additions & 13 deletions src/context/script-let.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,6 @@ export class ScriptLetContext {
for (const callback of callbacks) {
callback(node as E, result);
}
(node as any).parent = parent;

tokens.shift(); // (
tokens.pop(); // )
tokens.pop(); // ;

if (isTS) {
removeScope(
Expand All @@ -184,6 +179,12 @@ export class ScriptLetContext {
);
}

(node as any).parent = parent;

tokens.shift(); // (
tokens.pop(); // )
tokens.pop(); // ;

// Disconnect the tree structure.
exprSt.expression = null as never;
}
Expand Down Expand Up @@ -242,7 +243,6 @@ export class ScriptLetContext {
for (const callback of callbacks) {
callback(node, result);
}
(node as any).parent = parent;

const scope = result.getScope(decl);
for (const variable of scope.variables) {
Expand All @@ -253,6 +253,8 @@ export class ScriptLetContext {
}
}

(node as any).parent = parent;

tokens.shift(); // const
tokens.pop(); // ;

Expand Down Expand Up @@ -340,11 +342,6 @@ export class ScriptLetContext {

// Process for nodes
callback(expr, ctx, idx);
(expr as any).parent = eachBlock;
(ctx as any).parent = eachBlock;
if (idx) {
(idx as any).parent = eachBlock;
}

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

(expr as any).parent = eachBlock;
(ctx as any).parent = eachBlock;
if (idx) {
(idx as any).parent = eachBlock;
}

tokens.shift(); // Array
tokens.shift(); // .
tokens.shift(); // from
Expand Down Expand Up @@ -502,7 +505,7 @@ export class ScriptLetContext {
range,
});
if (this.ctx.isTypeScript()) {
source += ` : (${arrayTypings[index]})`;
source += `: (${arrayTypings[index]})`;
}
}
const restore = this.appendScript(
Expand Down Expand Up @@ -641,6 +644,7 @@ export class ScriptLetContext {
const comments = result.ast.comments;
const processedComments = [];
const nodeToScope = getNodeToScope(result.scopeManager!);
const postprocessList: (() => void)[] = [];

let tok;
while ((tok = tokens.shift())) {
Expand Down Expand Up @@ -749,6 +753,7 @@ export class ScriptLetContext {

result.ast.tokens = processedTokens;
result.ast.comments = processedComments;
postprocessList.forEach((p) => p());

// Helpers
/** Get scope */
Expand All @@ -763,7 +768,12 @@ export class ScriptLetContext {

/** Register node to scope */
function registerNodeToScope(node: any, scope: Scope): void {
scope.block = node;
// If we replace the `scope.block` at this time,
// the scope restore calculation will not work, so we will replace the `scope.block` later.
postprocessList.push(() => {
scope.block = node;
});

const scopes = nodeToScope.get(node);
if (scopes) {
scopes.push(scope);
Expand Down
48 changes: 48 additions & 0 deletions src/scope/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -418,3 +418,51 @@ export function addAllReferences(
(a, b) => a.identifier.range![0] - b.identifier.range![0]
);
}

/**
* Simplify scope data.
* @deprecated For Debug
*/
export function simplifyScope(scope: Scope): unknown {
return {
type: scope.type,
childScopes: scope.childScopes.map(simplifyScope),
block: {
type: scope.block.type,
loc: JSON.stringify(scope.block.loc),
},
variables:
scope.type === "global" ? null : simplifyVariables(scope.variables),
references: scope.references.map(simplifyReference),
through: scope.through.map(simplifyReference),
get original() {
return scope;
},
};
}

/**
* Simplify variables data.
* @deprecated For Debug
*/
function simplifyVariables(variables: Variable[]) {
return Object.fromEntries(
variables.map((v) => [
v.name,
{
loc: JSON.stringify(v.defs[0]?.node.loc),
},
])
);
}

/**
* Simplify reference data.
* @deprecated For Debug
*/
function simplifyReference(reference: Reference) {
return {
name: reference.identifier.name,
loc: JSON.stringify(reference.identifier.loc),
};
}
7 changes: 7 additions & 0 deletions tests/fixtures/parser/ast/await04-input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script lang="ts">
const p = Promise.resolve();
</script>

{#await p}
{:then v}
{/await}
8 changes: 8 additions & 0 deletions tests/fixtures/parser/ast/await04-no-unused-vars-result.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
"ruleId": "no-unused-vars",
"code": "v",
"line": 6,
"column": 8
}
]
Loading