From a80945cfb4199e998efbe3432ceba184ec90a403 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sun, 18 Jan 2015 21:33:22 +1100 Subject: [PATCH] ignore function declarations in TDZ detection --- .../transformation/transformers/es6-let-scoping.js | 12 +++++++----- .../transformers/spec-block-scoped-functions.js | 3 +++ 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/6to5/transformation/transformers/es6-let-scoping.js b/lib/6to5/transformation/transformers/es6-let-scoping.js index d48e3fcf74..ebff0352b3 100644 --- a/lib/6to5/transformation/transformers/es6-let-scoping.js +++ b/lib/6to5/transformation/transformers/es6-let-scoping.js @@ -130,16 +130,18 @@ LetScoping.prototype.checkTDZ = function () { // declared node is different in this scope if (scope.get(node.name, true) !== declared) return; - var declaredLoc = declared.loc.start; - var referenceLoc = node.loc.start; + var declaredLoc = declared.loc; + var referenceLoc = node.loc; + + if (!declaredLoc || !referenceLoc) return; // does this reference appear on a line before the declaration? - var before = referenceLoc.line < declaredLoc.line; + var before = referenceLoc.start.line < declaredLoc.start.line; - if (referenceLoc.line === declaredLoc.line) { + if (referenceLoc.start.line === declaredLoc.start.line) { // this reference appears on the same line // check it appears before the declaration - before = referenceLoc.col < declaredLoc.col; + before = referenceLoc.start.col < declaredLoc.start.col; } if (before) { diff --git a/lib/6to5/transformation/transformers/spec-block-scoped-functions.js b/lib/6to5/transformation/transformers/spec-block-scoped-functions.js index 3d8d3449c8..087b881d9f 100644 --- a/lib/6to5/transformation/transformers/spec-block-scoped-functions.js +++ b/lib/6to5/transformation/transformers/spec-block-scoped-functions.js @@ -7,6 +7,9 @@ exports.FunctionDeclaration = function (node, parent) { return; } + // this is to avoid triggering the TDZ detection + node.id.loc = null; + var declar = t.variableDeclaration("let", [ t.variableDeclarator(node.id, t.toExpression(node)) ]);