Skip to content

Commit f5b7974

Browse files
Fix recognition of catch-declared variables (#237)
* Fix recognition of catch-declared variables * Do not skip catch-param-lex-open test, as it now passes Co-authored-by: Luís Reis <[email protected]>
1 parent be5abe3 commit f5b7974

File tree

4 files changed

+51
-1
lines changed

4 files changed

+51
-1
lines changed

scripts/test262-data.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ exports.file_list = [
514514
//"language/statements/block/scope-lex-open.js",
515515
"language/statements/for-of/scope-head-lex-open.js",
516516
"language/statements/for-of/scope-body-lex-open.js",
517-
"language/statements/try/scope-catch-param-lex-open.js",
517+
//"language/statements/try/scope-catch-param-lex-open.js",
518518
//"language/statements/try/scope-catch-block-lex-open.js",
519519
//"language/expressions/class/scope-name-lex-close.js",
520520
//"language/expressions/call/scope-lex-close.js",

src/program/types/CatchClause.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import Node from '../Node.js';
2+
import Scope from '../Scope.js';
3+
4+
export default class CatchClause extends Node {
5+
initialise(transforms) {
6+
this.createdDeclarations = [];
7+
this.scope = new Scope({
8+
block: true,
9+
parent: this.parent.findScope(false),
10+
declare: id => this.createdDeclarations.push(id)
11+
});
12+
13+
this.scope.addDeclaration(this.param, 'catch');
14+
15+
super.initialise(transforms);
16+
this.scope.consolidate();
17+
}
18+
19+
findScope(functionScope) {
20+
return functionScope
21+
? this.parent.findScope(functionScope)
22+
: this.scope;
23+
}
24+
}
25+

src/program/types/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import AwaitExpression from './AwaitExpression.js';
55
import BinaryExpression from './BinaryExpression.js';
66
import BreakStatement from './BreakStatement.js';
77
import CallExpression from './CallExpression.js';
8+
import CatchClause from './CatchClause.js';
89
import ClassBody from './ClassBody.js';
910
import ClassDeclaration from './ClassDeclaration.js';
1011
import ClassExpression from './ClassExpression.js';
@@ -55,6 +56,7 @@ export default {
5556
BinaryExpression,
5657
BreakStatement,
5758
CallExpression,
59+
CatchClause,
5860
ClassBody,
5961
ClassDeclaration,
6062
ClassExpression,

test/samples/misc.js

+23
Original file line numberDiff line numberDiff line change
@@ -185,5 +185,28 @@ module.exports = [
185185
return bar;
186186
}
187187
`
188+
},
189+
190+
{
191+
description: 'catch clauses have their own scope',
192+
input: `
193+
const l = 2;
194+
try {
195+
throw new Error();
196+
} catch(l) {
197+
l = 1;
198+
alert(l);
199+
}
200+
`,
201+
output: `
202+
var l = 2;
203+
try {
204+
throw new Error();
205+
} catch(l$1) {
206+
l$1 = 1;
207+
alert(l$1);
208+
}
209+
`
188210
}
211+
189212
];

0 commit comments

Comments
 (0)