Comparing version 2.13.0 to 2.13.1
@@ -60,2 +60,3 @@ "use strict"; | ||
exports.Program = | ||
exports.BlockStatement = function (block, parent, scope, context, file) { | ||
@@ -86,2 +87,3 @@ if (!t.isLoop(parent)) { | ||
this.outsideLetReferences = {}; | ||
this.hasLetReferences = false; | ||
this.letReferences = {}; | ||
@@ -100,6 +102,11 @@ this.body = []; | ||
// this is a block within a `Function` so we can safely leave it be | ||
if (t.isFunction(this.parent)) return; | ||
var needsClosure = this.getLetReferences(); | ||
this.checkTDZ(); | ||
var needsClosure = this.getLetReferences(); | ||
// this is a block within a `Function/Program` so we can safely leave it be | ||
if (t.isFunction(this.parent) || t.isProgram(this.block)) return; | ||
// we can skip everything | ||
if (!this.hasLetReferences) return; | ||
if (needsClosure) { | ||
@@ -116,2 +123,44 @@ this.needsClosure(); | ||
LetScoping.prototype.checkTDZ = function () { | ||
var state = { | ||
letRefs: this.letReferences, | ||
file: this.file | ||
}; | ||
traverse(this.block, { | ||
enter: function (node, parent, scope, context, state) { | ||
if (!t.isIdentifier(node)) return; | ||
if (!t.isReferenced(node, parent)) return; | ||
var declared = state.letRefs[node.name]; | ||
if (!declared) return; | ||
// declared node is different in this scope | ||
if (scope.get(node.name, true) !== declared) return; | ||
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.start.line < declaredLoc.start.line; | ||
if (referenceLoc.start.line === declaredLoc.start.line) { | ||
// this reference appears on the same line | ||
// check it appears before the declaration | ||
before = referenceLoc.start.col < declaredLoc.start.col; | ||
} | ||
if (before) { | ||
throw state.file.errorWithNode(node, "Temporal dead zone - accessing a variable before it's initialized"); | ||
} | ||
} | ||
}, this.scope, state); | ||
}; | ||
/** | ||
* Description | ||
*/ | ||
LetScoping.prototype.remap = function () { | ||
@@ -246,4 +295,8 @@ var letRefs = this.letReferences; | ||
_.extend(this.letReferences, keys); | ||
this.hasLetReferences = true; | ||
} | ||
// no let references so we can just quit | ||
if (!this.hasLetReferences) return; | ||
// set let references to plain var references | ||
@@ -250,0 +303,0 @@ standardiseLets(declarators); |
@@ -10,2 +10,5 @@ "use strict"; | ||
// this is to avoid triggering the TDZ detection | ||
node.id.loc = null; | ||
var declar = t.variableDeclaration("let", [ | ||
@@ -12,0 +15,0 @@ t.variableDeclarator(node.id, t.toExpression(node)) |
{ | ||
"name": "6to5-core", | ||
"description": "Turn ES6 code into readable vanilla ES5 with source maps", | ||
"version": "2.13.0", | ||
"version": "2.13.1", | ||
"author": "Sebastian McKenzie <sebmck@gmail.com>", | ||
@@ -6,0 +6,0 @@ "homepage": "https://6to5.org/", |
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Unidentified License
License(Experimental) Something that seems like a license was found, but its contents could not be matched with a known license.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Unidentified License
License(Experimental) Something that seems like a license was found, but its contents could not be matched with a known license.
Found 1 instance in 1 package
4344808
18302