From 58d7a5e0697f19ea6f03a2eeb71796688d9e9f94 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Tue, 10 Mar 2015 06:13:32 +1100 Subject: [PATCH] more versatile scope pushing --- .../transformers/es6/destructuring.js | 5 +---- .../transformers/es6/object-super.js | 5 +---- src/babel/traversal/scope.js | 18 +++++++++--------- src/babel/types/index.js | 1 - 4 files changed, 11 insertions(+), 18 deletions(-) diff --git a/src/babel/transformation/transformers/es6/destructuring.js b/src/babel/transformation/transformers/es6/destructuring.js index 70168b16c7..9651bb2b54 100644 --- a/src/babel/transformation/transformers/es6/destructuring.js +++ b/src/babel/transformation/transformers/es6/destructuring.js @@ -135,10 +135,7 @@ export function AssignmentExpression(node, parent, scope, file) { if (!t.isPattern(node.left)) return; var ref = scope.generateUidIdentifier("temp"); - scope.push({ - key: ref.name, - id: ref - }); + scope.push({ id: ref }); var nodes = []; nodes.push(t.assignmentExpression("=", ref, node.right)); diff --git a/src/babel/transformation/transformers/es6/object-super.js b/src/babel/transformation/transformers/es6/object-super.js index 7b9fce9f52..cf23f6bba0 100644 --- a/src/babel/transformation/transformers/es6/object-super.js +++ b/src/babel/transformation/transformers/es6/object-super.js @@ -40,10 +40,7 @@ export function ObjectExpression(node, parent, scope, file) { } if (objectRef) { - scope.push({ - id: objectRef - }); - + scope.push({ id: objectRef }); return t.assignmentExpression("=", objectRef, node); } } diff --git a/src/babel/traversal/scope.js b/src/babel/traversal/scope.js index 846bc9ef04..453b758982 100644 --- a/src/babel/traversal/scope.js +++ b/src/babel/traversal/scope.js @@ -581,16 +581,16 @@ export default class Scope { block = block.body; } - if (t.isBlockStatement(block) || t.isProgram(block)) { - block._declarations ||= {}; - block._declarations[opts.key || opts.id.name] = { - kind: opts.kind || "var", - id: opts.id, - init: opts.init - }; - } else { - throw new TypeError(`cannot add a declaration here in node type ${block.type}`); + if (!t.isBlockStatement(block) && !t.isProgram(block)) { + block = this.getBlockParent().block; } + + block._declarations ||= {}; + block._declarations[opts.key || opts.id.name] = { + kind: opts.kind || "var", + id: opts.id, + init: opts.init + }; } /** diff --git a/src/babel/types/index.js b/src/babel/types/index.js index 4b51c2a1bb..58d115ae1b 100644 --- a/src/babel/types/index.js +++ b/src/babel/types/index.js @@ -154,7 +154,6 @@ t.toSequenceExpression = function (nodes: Array, scope: Scope): Object { each(node.declarations, function (declar) { scope.push({ kind: node.kind, - key: declar.id.name, id: declar.id }); exprs.push(t.assignmentExpression("=", declar.id, declar.init));