lebab
Advanced tools
Comparing version 3.2.2 to 3.2.3
@@ -82,4 +82,9 @@ "use strict"; | ||
// True when dealing with any kind of while-loop | ||
function isAnyWhileStatement(node) { | ||
return node.type === 'WhileStatement' || node.type === 'DoWhileStatement'; | ||
} | ||
// Program node works almost like a function: | ||
// it hoists all variables which can be tranformed to block-scoped let/const. | ||
// it hoists all variables which can be transformed to block-scoped let/const. | ||
// It just doesn't have name and parameters. | ||
@@ -143,2 +148,6 @@ // So we create an implied FunctionScope and BlockScope. | ||
} | ||
if (isLexicalVariableProhibited(group)) { | ||
logWarningForVarKind(group.getNode()); | ||
return; | ||
} | ||
var commonKind = group.getCommonKind(); | ||
@@ -173,2 +182,25 @@ if (commonKind) { | ||
} | ||
// let and const declarations aren't allowed in all the same places where | ||
// var declarations are allowed. Notably, only var-declaration can occur | ||
// directlt in if-statement (and other similar statements) body: | ||
// | ||
// if (true) var x = 10; | ||
// | ||
// let or const can only be used when the variable is declared in inside | ||
// a block-statement: | ||
// | ||
// if (true) { const x = 10; } | ||
// | ||
function isLexicalVariableProhibited(group) { | ||
var node = group.getNode(); | ||
var parentNode = group.getParentNode(); | ||
if (parentNode.type === 'IfStatement') { | ||
return parentNode.consequent === node || parentNode.alternate === node; | ||
} | ||
if (isAnyForStatement(parentNode) || isAnyWhileStatement(parentNode)) { | ||
return parentNode.body === node; | ||
} | ||
return false; | ||
} | ||
function logWarningForVarKind(node) { | ||
@@ -175,0 +207,0 @@ if (node.kind === 'var') { |
{ | ||
"name": "lebab", | ||
"version": "3.2.2", | ||
"version": "3.2.3", | ||
"description": "Turn your ES5 code into readable ES6/ES7", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
279086
5817