Skip to content

fix: crash in @typescript-eslint/no-misused-promises rule #236

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 2 commits into from
Oct 27, 2022
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/ninety-cheetahs-remain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte-eslint-parser": patch
---

fix: crash in `@typescript-eslint/no-misused-promises` rule
20 changes: 14 additions & 6 deletions docs/internal-mechanism.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,22 @@ Parse the following virtual script code as a script:

export let foo: { bar: number } | null = null

$: function $_reactiveStatementScopeFunction1(){console.log(foo && foo.bar);}
$: function $_reactiveStatementScopeFunction1(){
console.log(foo && foo.bar);
}

$: let r = $_reactiveVariableScopeFunction2();
function $_reactiveVariableScopeFunction2(){return foo && foo.bar;}
$: let r =$_reactiveVariableScopeFunction2();
function $_reactiveVariableScopeFunction2(){
let $_tmpVar3;
return ($_tmpVar3 = foo && foo.bar);
}

$: let { bar: n } = $_reactiveVariableScopeFunction3();
function $_reactiveVariableScopeFunction3(){return foo || { bar: 42 };}
;function $_render4(){
$: let { bar: n } =$_reactiveVariableScopeFunction4();
function $_reactiveVariableScopeFunction4(){
let $_tmpVar5;
return ($_tmpVar5 = foo || { bar: 42 });
}
;function $_render6(){

(foo && foo.bar);
}
Expand Down
82 changes: 53 additions & 29 deletions src/parser/typescript/analyze/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,15 +231,15 @@ function transformForDeclareReactiveVar(
//
// To:
// $: let id = fn()
// function fn () { return x + y; }
// function fn () { let tmp; return (tmp = x + y); }
//
//
// From:
// $: ({id} = foo);
//
// To:
// $: let {id} = fn()
// function fn () { return foo; }
// function fn () { let tmp; return (tmp = foo); }

/**
* The opening paren tokens for
Expand Down Expand Up @@ -297,6 +297,7 @@ function transformForDeclareReactiveVar(
}

const functionId = ctx.generateUniqueId("reactiveVariableScopeFunction");
const tmpVarId = ctx.generateUniqueId("tmpVar");
for (const token of openParens) {
ctx.appendOriginal(token.range[0]);
ctx.skipOriginalOffset(token.range[1] - token.range[0]);
Expand All @@ -306,7 +307,7 @@ function transformForDeclareReactiveVar(
ctx.appendVirtualScript("let ");
ctx.appendOriginal(eq ? eq.range[1] : expression.right.range[0]);
ctx.appendVirtualScript(
`${functionId}();\nfunction ${functionId}(){return (`
`${functionId}();\nfunction ${functionId}(){let ${tmpVarId};return (${tmpVarId} = `
);
ctx.appendOriginal(expression.right.range[1]);
ctx.appendVirtualScript(`)`);
Expand Down Expand Up @@ -347,46 +348,61 @@ function transformForDeclareReactiveVar(
!fnDecl ||
fnDecl.type !== "FunctionDeclaration" ||
fnDecl.id.name !== functionId ||
fnDecl.body.body.length !== 1 ||
fnDecl.body.body[0].type !== "ReturnStatement"
fnDecl.body.body.length !== 2 ||
fnDecl.body.body[0].type !== "VariableDeclaration" ||
fnDecl.body.body[1].type !== "ReturnStatement"
) {
return false;
}
const returnStatement = fnDecl.body.body[0];
if (returnStatement.argument?.type !== expression.right.type) {
const tmpVarDeclaration = fnDecl.body.body[0];
if (
tmpVarDeclaration.declarations.length !== 1 ||
tmpVarDeclaration.declarations[0].type !== "VariableDeclarator"
) {
return false;
}
const tempVarDeclId = tmpVarDeclaration.declarations[0].id;
if (
tempVarDeclId.type !== "Identifier" ||
tempVarDeclId.name !== tmpVarId
) {
return false;
}
const returnStatement = fnDecl.body.body[1];
const assignment = returnStatement.argument;
if (
assignment?.type !== "AssignmentExpression" ||
assignment.left.type !== "Identifier" ||
assignment.right.type !== expression.right.type
) {
return false;
}
const tempLeft = assignment.left;
// Remove function declaration
program.body.splice(nextIndex, 1);
// Restore expression statement
const newExpression: TSESTree.AssignmentExpression = {
type: "AssignmentExpression" as TSESTree.AssignmentExpression["type"],
operator: "=",
left: idDecl.id,
right: returnStatement.argument,
loc: {
start: idDecl.id.loc.start,
end: expressionCloseParen
? expressionCloseParen.loc.end
: returnStatement.argument.loc.end,
},
range: [
idDecl.id.range[0],
expressionCloseParen
? expressionCloseParen.range[1]
: returnStatement.argument.range[1],
],
assignment.left = idDecl.id;
assignment.loc = {
start: idDecl.id.loc.start,
end: expressionCloseParen
? expressionCloseParen.loc.end
: assignment.right.loc.end,
};
idDecl.id.parent = newExpression;
returnStatement.argument.parent = newExpression;
assignment.range = [
idDecl.id.range[0],
expressionCloseParen
? expressionCloseParen.range[1]
: assignment.right.range[1],
];
idDecl.id.parent = assignment;
const newBody: TSESTree.ExpressionStatement = {
type: "ExpressionStatement" as TSESTree.ExpressionStatement["type"],
expression: newExpression,
expression: assignment,
loc: statement.body.loc,
range: statement.body.range,
parent: reactiveStatement,
};
newExpression.parent = newBody;
assignment.parent = newBody;
reactiveStatement.body = newBody;
// Restore statement end location
reactiveStatement.range[1] = returnStatement.range[1];
Expand All @@ -401,12 +417,20 @@ function transformForDeclareReactiveVar(
);

const scopeManager = result.scopeManager as ScopeManager;
removeAllScopeAndVariableAndReference(tmpVarDeclaration, {
visitorKeys: result.visitorKeys,
scopeManager,
});
removeFunctionScope(fnDecl, scopeManager);

const scope = getProgramScope(scopeManager);
for (const reference of getAllReferences(idDecl.id, scope)) {
reference.writeExpr = newExpression.right as ESTree.Expression;
reference.writeExpr = assignment.right as ESTree.Expression;
}

removeIdentifierReference(tempLeft, scope);
removeIdentifierVariable(tempVarDeclId, scope);

removeIdentifierReference(idDecl.init.callee, scope);
removeIdentifierVariable(idDecl.id, scope);
return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script lang="ts">
$: noMisusedPromisesvar = 42;
</script>

{noMisusedPromisesvar}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* eslint eslint-comments/require-description: 0, @typescript-eslint/explicit-module-boundary-types: 0 */
import type { Linter } from "eslint";
import { BASIC_PARSER_OPTIONS } from "../../../src/parser/test-utils";
import { rules } from "@typescript-eslint/eslint-plugin";
export function setupLinter(linter: Linter) {
linter.defineRule(
"@typescript-eslint/no-misused-promises",
rules["no-misused-promises"] as never
);
}

export function getConfig() {
return {
parser: "svelte-eslint-parser",
parserOptions: BASIC_PARSER_OPTIONS,
rules: {
"@typescript-eslint/no-misused-promises": "error",
},
env: {
browser: true,
es2021: true,
},
};
}