Skip to content
This repository has been archived by the owner on Aug 15, 2024. It is now read-only.

fix: Definition#name in catch patterns should be Identifier node #127

Merged
merged 2 commits into from
Jul 8, 2024
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
2 changes: 1 addition & 1 deletion lib/referencer.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ class Referencer extends esrecurse.Visitor {
this.currentScope().__define(pattern,
new Definition(
Variable.CatchClause,
node.param,
pattern,
node,
null,
null,
Expand Down
87 changes: 87 additions & 0 deletions tests/catch-scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,96 @@ describe("catch", () => {
expect(scope.type).to.be.equal("catch");
expect(scope.variables).to.have.length(1);
expect(scope.variables[0].name).to.be.equal("e");
expect(scope.variables[0].defs).to.have.length(1);
expect(scope.variables[0].defs[0].type).to.be.equal("CatchClause");
expect(scope.variables[0].defs[0].name.type).to.be.equal("Identifier");
expect(scope.variables[0].defs[0].name.name).to.be.equal("e");
expect(scope.variables[0].defs[0].node.type).to.be.equal("CatchClause");
expect(scope.variables[0].defs[0].parent).to.be.equal(null);
expect(scope.isArgumentsMaterialized()).to.be.true;
expect(scope.references).to.have.length(0);
});

it("param can be a pattern", () => {
const ast = espree(`
(function () {
const default_id = 0;
try {
} catch ({ message, id = default_id, args: [arg1, arg2] }) {
}
}());
`);

const scopeManager = analyze(ast, { ecmaVersion: 6 });

expect(scopeManager.scopes).to.have.length(5);

const globalScope = scopeManager.scopes[0];

expect(globalScope.type).to.be.equal("global");
expect(globalScope.variables).to.have.length(0);
expect(globalScope.references).to.have.length(0);

const functionScope = scopeManager.scopes[1];

expect(functionScope.type).to.be.equal("function");
expect(functionScope.variables).to.have.length(2);
expect(functionScope.variables[0].name).to.be.equal("arguments");
expect(functionScope.variables[1].name).to.be.equal("default_id");
expect(functionScope.references).to.have.length(1);
expect(functionScope.references[0].from).to.be.equal(functionScope);
expect(functionScope.references[0].resolved).to.be.equal(functionScope.variables[1]);

const tryBlockScope = scopeManager.scopes[2];

expect(tryBlockScope.type).to.be.equal("block");
expect(tryBlockScope.variables).to.have.length(0);
expect(tryBlockScope.references).to.have.length(0);

const catchScope = scopeManager.scopes[3];

expect(catchScope.type).to.be.equal("catch");
expect(catchScope.variables).to.have.length(4);
expect(catchScope.variables[0].name).to.be.equal("message");
expect(catchScope.variables[0].defs).to.have.length(1);
expect(catchScope.variables[0].defs[0].type).to.be.equal("CatchClause");
expect(catchScope.variables[0].defs[0].name.type).to.be.equal("Identifier");
expect(catchScope.variables[0].defs[0].name.name).to.be.equal("message");
expect(catchScope.variables[0].defs[0].node.type).to.be.equal("CatchClause");
expect(catchScope.variables[0].defs[0].parent).to.be.equal(null);
expect(catchScope.variables[1].name).to.be.equal("id");
expect(catchScope.variables[1].defs).to.have.length(1);
expect(catchScope.variables[1].defs[0].type).to.be.equal("CatchClause");
expect(catchScope.variables[1].defs[0].name.type).to.be.equal("Identifier");
expect(catchScope.variables[1].defs[0].name.name).to.be.equal("id");
expect(catchScope.variables[1].defs[0].node.type).to.be.equal("CatchClause");
expect(catchScope.variables[1].defs[0].parent).to.be.equal(null);
expect(catchScope.variables[2].name).to.be.equal("arg1");
expect(catchScope.variables[2].defs).to.have.length(1);
expect(catchScope.variables[2].defs[0].type).to.be.equal("CatchClause");
expect(catchScope.variables[2].defs[0].name.type).to.be.equal("Identifier");
expect(catchScope.variables[2].defs[0].name.name).to.be.equal("arg1");
expect(catchScope.variables[2].defs[0].node.type).to.be.equal("CatchClause");
expect(catchScope.variables[2].defs[0].parent).to.be.equal(null);
expect(catchScope.variables[3].name).to.be.equal("arg2");
expect(catchScope.variables[3].defs).to.have.length(1);
expect(catchScope.variables[3].defs[0].type).to.be.equal("CatchClause");
expect(catchScope.variables[3].defs[0].name.type).to.be.equal("Identifier");
expect(catchScope.variables[3].defs[0].name.name).to.be.equal("arg2");
expect(catchScope.variables[3].defs[0].node.type).to.be.equal("CatchClause");
expect(catchScope.variables[3].defs[0].parent).to.be.equal(null);
expect(catchScope.references).to.have.length(2);
expect(catchScope.references[0].from).to.be.equal(catchScope);
expect(catchScope.references[0].resolved).to.be.equal(catchScope.variables[1]);
expect(catchScope.references[1].from).to.be.equal(catchScope);
expect(catchScope.references[1].resolved).to.be.equal(functionScope.variables[1]);

const catchBlockScope = scopeManager.scopes[4];

expect(catchBlockScope.type).to.be.equal("block");
expect(catchBlockScope.variables).to.have.length(0);
expect(catchBlockScope.references).to.have.length(0);
});
});

// vim: set sw=4 ts=4 et tw=80 :