From 62bc3641afc125276605fb7f5ea0f1f07457d9c5 Mon Sep 17 00:00:00 2001 From: Marijn Haverbeke Date: Tue, 2 Apr 2013 09:30:35 +0200 Subject: [PATCH] Use a single .handler property on TryStatements As the wiki page for the spec currently seems to prescribe. --- acorn.js | 8 ++++---- acorn_loose.js | 8 ++++---- index.html | 8 ++++---- test/tests.js | 27 +++++++++++++-------------- util/walk.js | 11 +++++------ 5 files changed, 30 insertions(+), 32 deletions(-) diff --git a/acorn.js b/acorn.js index 0500d20078..510cc45a70 100644 --- a/acorn.js +++ b/acorn.js @@ -1235,8 +1235,8 @@ case _try: next(); node.block = parseBlock(); - node.handlers = []; - while (tokType === _catch) { + node.handler = null; + if (tokType === _catch) { var clause = startNode(); next(); expect(_parenL); @@ -1246,10 +1246,10 @@ expect(_parenR); clause.guard = null; clause.body = parseBlock(); - node.handlers.push(finishNode(clause, "CatchClause")); + node.handler = finishNode(clause, "CatchClause"); } node.finalizer = eat(_finally) ? parseBlock() : null; - if (!node.handlers.length && !node.finalizer) + if (!node.handler && !node.finalizer) raise(node.start, "Missing catch or finally clause"); return finishNode(node, "TryStatement"); diff --git a/acorn_loose.js b/acorn_loose.js index 114e0c95ff..e91bad342c 100644 --- a/acorn_loose.js +++ b/acorn_loose.js @@ -377,8 +377,8 @@ case tt.try: next(); node.block = parseBlock(); - node.handlers = []; - while (token.type === tt.catch) { + node.handler = null; + if (token.type === tt.catch) { var clause = startNode(); next(); expect(tt.parenL); @@ -386,10 +386,10 @@ expect(tt.parenR); clause.guard = null; clause.body = parseBlock(); - node.handlers.push(finishNode(clause, "CatchClause")); + node.handler = finishNode(clause, "CatchClause"); } node.finalizer = eat(tt.finally) ? parseBlock() : null; - if (!node.handlers.length && !node.finalizer) return node.block; + if (!node.handler && !node.finalizer) return node.block; return finishNode(node, "TryStatement"); case tt.var: diff --git a/index.html b/index.html index 76826c38e0..ade82c8ec3 100644 --- a/index.html +++ b/index.html @@ -901,8 +901,8 @@ adding statements to.

case _try: next(); node.block = parseBlock(); - node.handlers = []; - while (tokType === _catch) { + node.handler = null; + if (tokType === _catch) { var clause = startNode(); next(); expect(_parenL); @@ -912,10 +912,10 @@ adding statements to.

expect(_parenR); clause.guard = null; clause.body = parseBlock(); - node.handlers.push(finishNode(clause, "CatchClause")); + node.handler = finishNode(clause, "CatchClause"); } node.finalizer = eat(_finally) ? parseBlock() : null; - if (!node.handlers.length && !node.finalizer) + if (!node.handler && !node.finalizer) raise(node.start, "Missing catch or finally clause"); return finishNode(node, "TryStatement"); diff --git a/test/tests.js b/test/tests.js index 4bc875470d..da467f17b9 100644 --- a/test/tests.js +++ b/test/tests.js @@ -21819,8 +21819,7 @@ test("try { } catch (e) { }", { } } }, - handlers: [ - { + handler: { type: "CatchClause", param: { type: "Identifier", @@ -21862,7 +21861,7 @@ test("try { } catch (e) { }", { } } } - ], + , finalizer: null, loc: { start: { @@ -21907,7 +21906,7 @@ test("try { } catch (eval) { }", { } } }, - handlers: [ + handler: { type: "CatchClause", param: { @@ -21950,7 +21949,7 @@ test("try { } catch (eval) { }", { } } } - ], + , finalizer: null, loc: { start: { @@ -21995,7 +21994,7 @@ test("try { } catch (arguments) { }", { } } }, - handlers: [ + handler: { type: "CatchClause", param: { @@ -22038,7 +22037,7 @@ test("try { } catch (arguments) { }", { } } } - ], + , finalizer: null, loc: { start: { @@ -22083,7 +22082,7 @@ test("try { } catch (e) { say(e) }", { } } }, - handlers: [ + handler: { type: "CatchClause", param: { @@ -22183,7 +22182,7 @@ test("try { } catch (e) { say(e) }", { } } } - ], + , finalizer: null, loc: { start: { @@ -22228,7 +22227,7 @@ test("try { } finally { cleanup(stuff) }", { } } }, - handlers: [], + handler: null, finalizer: { type: "BlockStatement", body: [ @@ -22385,7 +22384,7 @@ test("try { doThat(); } catch (e) { say(e) }", { } } }, - handlers: [ + handler: { type: "CatchClause", param: { @@ -22485,7 +22484,7 @@ test("try { doThat(); } catch (e) { say(e) }", { } } } - ], + , finalizer: null, loc: { start: { @@ -22572,7 +22571,7 @@ test("try { doThat(); } catch (e) { say(e) } finally { cleanup(stuff) }", { } } }, - handlers: [ + handler: { type: "CatchClause", param: { @@ -22672,7 +22671,7 @@ test("try { doThat(); } catch (e) { say(e) } finally { cleanup(stuff) }", { } } } - ], + , finalizer: { type: "BlockStatement", body: [ diff --git a/util/walk.js b/util/walk.js index 054c289ef7..ea7eea19ec 100644 --- a/util/walk.js +++ b/util/walk.js @@ -188,8 +188,7 @@ }; base.TryStatement = function(node, st, c) { c(node.block, st, "Statement"); - for (var i = 0; i < node.handlers.length; ++i) - c(node.handlers[i].body, st, "ScopeBody"); + if (node.handler) c(node.handler.body, st, "ScopeBody"); if (node.finalizer) c(node.finalizer, st, "Statement"); }; base.WhileStatement = function(node, st, c) { @@ -290,10 +289,10 @@ }, TryStatement: function(node, scope, c) { c(node.block, scope, "Statement"); - for (var i = 0; i < node.handlers.length; ++i) { - var handler = node.handlers[i], inner = makeScope(scope); - inner.vars[handler.param.name] = {type: "catch clause", node: handler.param}; - c(handler.body, inner, "ScopeBody"); + if (node.handler) { + var inner = makeScope(scope); + inner.vars[node.handler.param.name] = {type: "catch clause", node: node.handler.param}; + c(node.handler.body, inner, "ScopeBody"); } if (node.finalizer) c(node.finalizer, scope, "Statement"); },