Skip to content
This repository was archived by the owner on Jun 23, 2025. It is now read-only.

Commit 8082caa

Browse files
authored
fix: Definition#name in catch patterns should be Identifier node (#127)
* fix: `Definition#name` in catch patterns should be `Identifier` node * use `ecmaVersion:6` in the test
1 parent 861f8e5 commit 8082caa

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

lib/referencer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ class Referencer extends esrecurse.Visitor {
395395
this.currentScope().__define(pattern,
396396
new Definition(
397397
Variable.CatchClause,
398-
node.param,
398+
pattern,
399399
node,
400400
null,
401401
null,

tests/catch-scope.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,96 @@ describe("catch", () => {
5656
expect(scope.type).to.be.equal("catch");
5757
expect(scope.variables).to.have.length(1);
5858
expect(scope.variables[0].name).to.be.equal("e");
59+
expect(scope.variables[0].defs).to.have.length(1);
60+
expect(scope.variables[0].defs[0].type).to.be.equal("CatchClause");
61+
expect(scope.variables[0].defs[0].name.type).to.be.equal("Identifier");
62+
expect(scope.variables[0].defs[0].name.name).to.be.equal("e");
63+
expect(scope.variables[0].defs[0].node.type).to.be.equal("CatchClause");
64+
expect(scope.variables[0].defs[0].parent).to.be.equal(null);
5965
expect(scope.isArgumentsMaterialized()).to.be.true;
6066
expect(scope.references).to.have.length(0);
6167
});
68+
69+
it("param can be a pattern", () => {
70+
const ast = espree(`
71+
(function () {
72+
const default_id = 0;
73+
try {
74+
} catch ({ message, id = default_id, args: [arg1, arg2] }) {
75+
}
76+
}());
77+
`);
78+
79+
const scopeManager = analyze(ast, { ecmaVersion: 6 });
80+
81+
expect(scopeManager.scopes).to.have.length(5);
82+
83+
const globalScope = scopeManager.scopes[0];
84+
85+
expect(globalScope.type).to.be.equal("global");
86+
expect(globalScope.variables).to.have.length(0);
87+
expect(globalScope.references).to.have.length(0);
88+
89+
const functionScope = scopeManager.scopes[1];
90+
91+
expect(functionScope.type).to.be.equal("function");
92+
expect(functionScope.variables).to.have.length(2);
93+
expect(functionScope.variables[0].name).to.be.equal("arguments");
94+
expect(functionScope.variables[1].name).to.be.equal("default_id");
95+
expect(functionScope.references).to.have.length(1);
96+
expect(functionScope.references[0].from).to.be.equal(functionScope);
97+
expect(functionScope.references[0].resolved).to.be.equal(functionScope.variables[1]);
98+
99+
const tryBlockScope = scopeManager.scopes[2];
100+
101+
expect(tryBlockScope.type).to.be.equal("block");
102+
expect(tryBlockScope.variables).to.have.length(0);
103+
expect(tryBlockScope.references).to.have.length(0);
104+
105+
const catchScope = scopeManager.scopes[3];
106+
107+
expect(catchScope.type).to.be.equal("catch");
108+
expect(catchScope.variables).to.have.length(4);
109+
expect(catchScope.variables[0].name).to.be.equal("message");
110+
expect(catchScope.variables[0].defs).to.have.length(1);
111+
expect(catchScope.variables[0].defs[0].type).to.be.equal("CatchClause");
112+
expect(catchScope.variables[0].defs[0].name.type).to.be.equal("Identifier");
113+
expect(catchScope.variables[0].defs[0].name.name).to.be.equal("message");
114+
expect(catchScope.variables[0].defs[0].node.type).to.be.equal("CatchClause");
115+
expect(catchScope.variables[0].defs[0].parent).to.be.equal(null);
116+
expect(catchScope.variables[1].name).to.be.equal("id");
117+
expect(catchScope.variables[1].defs).to.have.length(1);
118+
expect(catchScope.variables[1].defs[0].type).to.be.equal("CatchClause");
119+
expect(catchScope.variables[1].defs[0].name.type).to.be.equal("Identifier");
120+
expect(catchScope.variables[1].defs[0].name.name).to.be.equal("id");
121+
expect(catchScope.variables[1].defs[0].node.type).to.be.equal("CatchClause");
122+
expect(catchScope.variables[1].defs[0].parent).to.be.equal(null);
123+
expect(catchScope.variables[2].name).to.be.equal("arg1");
124+
expect(catchScope.variables[2].defs).to.have.length(1);
125+
expect(catchScope.variables[2].defs[0].type).to.be.equal("CatchClause");
126+
expect(catchScope.variables[2].defs[0].name.type).to.be.equal("Identifier");
127+
expect(catchScope.variables[2].defs[0].name.name).to.be.equal("arg1");
128+
expect(catchScope.variables[2].defs[0].node.type).to.be.equal("CatchClause");
129+
expect(catchScope.variables[2].defs[0].parent).to.be.equal(null);
130+
expect(catchScope.variables[3].name).to.be.equal("arg2");
131+
expect(catchScope.variables[3].defs).to.have.length(1);
132+
expect(catchScope.variables[3].defs[0].type).to.be.equal("CatchClause");
133+
expect(catchScope.variables[3].defs[0].name.type).to.be.equal("Identifier");
134+
expect(catchScope.variables[3].defs[0].name.name).to.be.equal("arg2");
135+
expect(catchScope.variables[3].defs[0].node.type).to.be.equal("CatchClause");
136+
expect(catchScope.variables[3].defs[0].parent).to.be.equal(null);
137+
expect(catchScope.references).to.have.length(2);
138+
expect(catchScope.references[0].from).to.be.equal(catchScope);
139+
expect(catchScope.references[0].resolved).to.be.equal(catchScope.variables[1]);
140+
expect(catchScope.references[1].from).to.be.equal(catchScope);
141+
expect(catchScope.references[1].resolved).to.be.equal(functionScope.variables[1]);
142+
143+
const catchBlockScope = scopeManager.scopes[4];
144+
145+
expect(catchBlockScope.type).to.be.equal("block");
146+
expect(catchBlockScope.variables).to.have.length(0);
147+
expect(catchBlockScope.references).to.have.length(0);
148+
});
62149
});
63150

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

0 commit comments

Comments
 (0)