diff --git a/.jshintrc b/.jshintrc index 84b94e7dd8..9e68224789 100644 --- a/.jshintrc +++ b/.jshintrc @@ -20,6 +20,7 @@ "maxdepth": 4, "globals": { + "window": true, "suite": true, "set": true, "before": true, diff --git a/lib/6to5/browser.js b/lib/6to5/browser.js index 4f51333e4e..be90701548 100644 --- a/lib/6to5/browser.js +++ b/lib/6to5/browser.js @@ -7,7 +7,7 @@ Error.captureStackTrace = Error.captureStackTrace || function (obj) { getFileName: function () { return "filename"; }, getLineNumber: function () { return 1; }, getColumnNumber: function () { return 1; }, - getFunctionName: function () { return "functionName" } + getFunctionName: function () { return "functionName"; } }; obj.stack = Error.prepareStackTrace(obj, [frame, frame, frame]); @@ -32,7 +32,7 @@ transform.eval = function (code, opts) { transform.run = function (code, opts) { opts = opts || {}; opts.sourceMap = "inline"; - return Function(transform(code, opts).code)(); + return new Function(transform(code, opts).code)(); }; transform.load = function (url, callback, opts, hold) { @@ -43,12 +43,12 @@ transform.load = function (url, callback, opts, hold) { xhr.open("GET", url, true); if ("overrideMimeType" in xhr) xhr.overrideMimeType("text/plain"); - xhr.onreadystatechange = function() { + xhr.onreadystatechange = function () { if (xhr.readyState !== 4) return; var status = xhr.status; if (status === 0 || status === 200) { - var param = [xhr.responseText, options]; + var param = [xhr.responseText, opts]; if (!hold) transform.run.apply(transform, param); if (callback) callback(param); } else { @@ -77,7 +77,7 @@ var runScripts = function () { var opts = {}; if (script.src) { - transform.load(script.src, function(param) { + transform.load(script.src, function (param) { scripts[i] = param; exec(); }, opts, true); @@ -93,7 +93,7 @@ var runScripts = function () { if (types.indexOf(_script.type) >= 0) scripts.push(_script); } - for (var i in scripts) { + for (i in scripts) { run(scripts[i], i); } diff --git a/lib/6to5/file.js b/lib/6to5/file.js index a53f7ab60e..17ecaaf143 100644 --- a/lib/6to5/file.js +++ b/lib/6to5/file.js @@ -82,7 +82,7 @@ File.prototype.addDeclaration = function (name) { }; File.prototype.errorWithNode = function (node, msg) { - var loc + var loc; if (node.loc) { loc = node.loc.start; diff --git a/lib/6to5/generator.js b/lib/6to5/generator.js index d22edeec38..4fd56c4ef5 100644 --- a/lib/6to5/generator.js +++ b/lib/6to5/generator.js @@ -6,13 +6,12 @@ module.exports = function (code, ast, opts) { module.exports.CodeGenerator = CodeGenerator; var sourceMap = require("source-map"); -var assert = require("assert"); var util = require("./util"); var t = require("./types"); var _ = require("lodash"); function CodeGenerator(code, ast, opts) { - opts = opts || {} + opts = opts || {}; this.tabWidth = 2; this.code = code; @@ -205,7 +204,7 @@ CodeGenerator.prototype.printSequence = function (print, nodes, opts) { return self.hasWhitespaceBetween(startToken, endToken); }; - opts.print = function (node, i) { + opts.print = function (node) { var needs = function (fn) { if (!self.opts.whitespace) return; diff --git a/lib/6to5/generators/expressions.js b/lib/6to5/generators/expressions.js index 6e17fa83fa..0c32498fea 100644 --- a/lib/6to5/generators/expressions.js +++ b/lib/6to5/generators/expressions.js @@ -1,5 +1,4 @@ var t = require("../types"); -var _ = require("lodash"); exports.UnaryExpression = function (node, print) { this.push(node.operator); @@ -75,8 +74,8 @@ exports.ExpressionStatement = function (node, print) { exports.BinaryExpression = exports.LogicalExpression = -exports.AssignmentExpression = function (node, print, parent) { - print(node.left) +exports.AssignmentExpression = function (node, print) { + print(node.left); this.push(" " + node.operator + " "); print(node.right); }; @@ -86,7 +85,7 @@ exports.MemberExpression = function (node, print) { if (node.computed) { this.push("["); - print(node.property) + print(node.property); this.push("]"); } else { this.push("."); diff --git a/lib/6to5/generators/jsx.js b/lib/6to5/generators/jsx.js index fa3ba6e3e7..25cd8b1cad 100644 --- a/lib/6to5/generators/jsx.js +++ b/lib/6to5/generators/jsx.js @@ -14,7 +14,7 @@ exports.XJSIdentifier = function (node) { }; exports.XJSNamespacedName = function (node, print) { - print(node.namespace) + print(node.namespace); this.push(":"); print(node.name); }; @@ -68,7 +68,7 @@ exports.XJSOpeningElement = function (node, print) { this.push(" "); this.printJoin(print, node.attributes, " "); } - this.push(node.selfClosing ? " />" : ">");; + this.push(node.selfClosing ? " />" : ">"); }; exports.XJSClosingElement = function (node, print) { diff --git a/lib/6to5/generators/statements.js b/lib/6to5/generators/statements.js index b3bb3ae078..84b4f28846 100644 --- a/lib/6to5/generators/statements.js +++ b/lib/6to5/generators/statements.js @@ -1,5 +1,4 @@ var t = require("../types"); -var _ = require("lodash"); exports.WithStatement = function (node, print) { this.keyword("with"); @@ -127,7 +126,7 @@ exports.CatchClause = function (node, print) { exports.ThrowStatement = function (node, print) { this.push("throw "); - print(node.argument) + print(node.argument); this.semicolon(); }; diff --git a/lib/6to5/generators/template-literals.js b/lib/6to5/generators/template-literals.js index 21e8399386..3621515fa8 100644 --- a/lib/6to5/generators/template-literals.js +++ b/lib/6to5/generators/template-literals.js @@ -5,7 +5,7 @@ exports.TaggedTemplateExpression = function (node, print) { print(node.quasi); }; -exports.TemplateElement = function (node, print) { +exports.TemplateElement = function (node) { this.push(node.value.raw, true); }; diff --git a/lib/6to5/generators/types.js b/lib/6to5/generators/types.js index 0a26ff2174..bce2bb6aa8 100644 --- a/lib/6to5/generators/types.js +++ b/lib/6to5/generators/types.js @@ -58,7 +58,7 @@ exports.ArrayPattern = function (node, print) { this.push("["); - _.each(elems, function(elem, i) { + _.each(elems, function (elem, i) { if (!elem) { // If the array expression ends with a hole, that hole // will be ignored by the interpreter, but if it ends with diff --git a/lib/6to5/modules/ignore.js b/lib/6to5/modules/ignore.js index 766e9f5ff4..53368ecfeb 100644 --- a/lib/6to5/modules/ignore.js +++ b/lib/6to5/modules/ignore.js @@ -1,14 +1,14 @@ module.exports = IgnoreFormatter; -function IgnoreFormatter(file) { +function IgnoreFormatter() { } -IgnoreFormatter.prototype.import = function (node, nodes) { +IgnoreFormatter.prototype.import = function () { }; -IgnoreFormatter.prototype.importSpecifier = function (specifier, node, nodes) { +IgnoreFormatter.prototype.importSpecifier = function () { }; @@ -16,6 +16,6 @@ IgnoreFormatter.prototype.export = function (node, nodes) { nodes.push(node.declaration); }; -IgnoreFormatter.prototype.exportSpecifier = function (specifier, node, nodes) { +IgnoreFormatter.prototype.exportSpecifier = function () { }; diff --git a/lib/6to5/transform.js b/lib/6to5/transform.js index aa4b114a05..b734387f86 100644 --- a/lib/6to5/transform.js +++ b/lib/6to5/transform.js @@ -1,11 +1,7 @@ module.exports = transform; var Transformer = require("./transformer"); -var generate = require("./generator"); -var assert = require("assert"); var File = require("./file"); -var util = require("./util"); -var chai = require("chai"); var _ = require("lodash"); function transform(code, opts) { diff --git a/lib/6to5/transformer.js b/lib/6to5/transformer.js index 2b8836dee9..59f340efdc 100644 --- a/lib/6to5/transformer.js +++ b/lib/6to5/transformer.js @@ -32,8 +32,6 @@ Transformer.prototype.transform = function (file) { transformer.ast.enter(ast, file); } - var self = this; - var build = function (exit) { return function (node, parent, opts) { // add any node type aliases that exist diff --git a/lib/6to5/transformers/classes.js b/lib/6to5/transformers/classes.js index bcc22c1694..80766479f3 100644 --- a/lib/6to5/transformers/classes.js +++ b/lib/6to5/transformers/classes.js @@ -40,11 +40,11 @@ var buildClass = function (node, file, scope) { CLASS_NAME: className }); - var block = container.callee.body; - var body = block.body; - var construct = body[0].declarations[0].init; + var block = container.callee.body; + var body = block.body; + var constructor = body[0].declarations[0].init; - if (node.id) construct.id = className; + if (node.id) constructor.id = className; var returnStatement = body.pop(); @@ -55,18 +55,32 @@ var buildClass = function (node, file, scope) { container.callee.params.push(superClassCallee); } - buildClassBody(file, construct, body, className, superName, node); + buildClassBody({ + file: file, + body: body, + node: node, + className: className, + superName: superName, + constructor: constructor, + }); if (body.length === 1) { // only a constructor so no need for a closure container - return construct; + return constructor; } else { body.push(returnStatement); return container; } }; -var buildClassBody = function (file, construct, body, className, superName, node) { +var buildClassBody = function (opts) { + var file = opts.file; + var body = opts.body; + var node = opts.node; + var constructor = opts.constructor; + var className = opts.className; + var superName = opts.superName; + var instanceMutatorMap = {}; var staticMutatorMap = {}; var hasConstructor = false; @@ -82,7 +96,7 @@ var buildClassBody = function (file, construct, body, className, superName, node if (methodName === "constructor") { if (node.kind === "") { hasConstructor = true; - addConstructor(construct, method); + addConstructor(constructor, method); } else { throw file.errorWithNode(node, "unknown kind for constructor method"); } @@ -102,7 +116,7 @@ var buildClassBody = function (file, construct, body, className, superName, node }); if (!hasConstructor && superName) { - construct.body.body.push(util.template("class-super-constructor-call", { + constructor.body.body.push(util.template("class-super-constructor-call", { SUPER_NAME: superName }, true)); } diff --git a/lib/6to5/transformers/constants.js b/lib/6to5/transformers/constants.js index 7dc60a4bf8..b3c604fd9c 100644 --- a/lib/6to5/transformers/constants.js +++ b/lib/6to5/transformers/constants.js @@ -1,5 +1,4 @@ var traverse = require("../traverse"); -var util = require("../util"); var t = require("../types"); var _ = require("lodash"); diff --git a/lib/6to5/transformers/destructuring.js b/lib/6to5/transformers/destructuring.js index e9f6a072b8..c9330b819b 100644 --- a/lib/6to5/transformers/destructuring.js +++ b/lib/6to5/transformers/destructuring.js @@ -45,9 +45,16 @@ var pushArrayPattern = function (kind, nodes, pattern, parentId) { }); }; -var pushPattern = function (kind, nodes, pattern, parentId, file, scope) { +var pushPattern = function (opts) { + var kind = opts.kind; + var nodes = opts.nodes; + var pattern = opts.pattern; + var parentId = opts.id; + var file = opts.file; + var scope = opts.scope; + if (parentId.type !== "MemberExpression" && parentId.type !== "Identifier") { - var key = t.identifier(file.generateUid("ref")); + var key = t.identifier(file.generateUid("ref", scope)); nodes.push(t.variableDeclaration("var", [ t.variableDeclarator(key, parentId) @@ -92,7 +99,16 @@ exports.Function = function (node, parent, file, scope) { hasDestructuring = true; var parentId = t.identifier(file.generateUid("ref", scope)); - pushPattern("var", nodes, pattern, parentId, file, scope); + + pushPattern({ + kind: "var", + nodes: nodes, + pattern: pattern, + id: parentId, + file: file, + scope: scope + }); + return parentId; }); @@ -140,7 +156,14 @@ exports.VariableDeclaration = function (node, parent, file, scope) { var patternId = declar.init; var pattern = declar.id; if (t.isPattern(pattern) && patternId) { - pushPattern(node.kind, nodes, pattern, patternId, file, scope); + pushPattern({ + kind: node.kind, + nodes: nodes, + pattern: pattern, + id: patternId, + file: file, + scope: scope + }); } else { nodes.push(buildVariableAssign(node.kind, declar.id, declar.init)); } diff --git a/lib/6to5/transformers/let-scoping.js b/lib/6to5/transformers/let-scoping.js index 30365c4d84..9b86af4116 100644 --- a/lib/6to5/transformers/let-scoping.js +++ b/lib/6to5/transformers/let-scoping.js @@ -1,5 +1,4 @@ var traverse = require("../traverse"); -var util = require("../util"); var t = require("../types"); var _ = require("lodash"); @@ -116,7 +115,7 @@ var run = function (forParent, block, parent, file, scope) { traverse(block, function (node, parent, opts) { if (t.isFunction(node)) { - traverse(node, function (node, parent, opts2) { + traverse(node, function (node, parent) { if (!t.isIdentifier(node)) return; if (!t.isReferenced(node, parent)) return; if (opts.scope.hasOwn(node.name)) return; @@ -192,14 +191,13 @@ var run = function (forParent, block, parent, file, scope) { }; // hoist `var` declarations - traverse(block, function (node, parent) { + traverse(block, function (node) { if (t.isForStatement(node)) { if (isVar(node.init)) { node.init = t.sequenceExpression(pushDeclar(node.init)); } } else if (t.isFor(node)) { if (isVar(node.left)) { - body.push() node.left = node.left.declarations[0].id; } } else if (isVar(node)) { diff --git a/lib/6to5/traverse.js b/lib/6to5/traverse.js index 7a46c492d1..acba99bc26 100644 --- a/lib/6to5/traverse.js +++ b/lib/6to5/traverse.js @@ -81,7 +81,7 @@ function traverse(parent, callbacks, opts) { handle(parent, key); } }); -}; +} traverse.removeProperties = function (tree) { var clear = function (node) { diff --git a/lib/6to5/types/index.js b/lib/6to5/types/index.js index 52ce196212..da3049351f 100644 --- a/lib/6to5/types/index.js +++ b/lib/6to5/types/index.js @@ -218,13 +218,13 @@ t.needsParans = function (node, parent) { var parentPos = PRECEDENCE[parentOp]; var nodeOp = node.operator; - var nodeOp = PRECEDENCE[nodeOp]; + var nodePos = PRECEDENCE[nodeOp]; - if (parentPos > nodeOp) { + if (parentPos > nodePos) { return true; } - if (parentPos === nodeOp && parent.right === node) { + if (parentPos === nodePos && parent.right === node) { return true; } } @@ -255,13 +255,13 @@ t.needsParans = function (node, parent) { // if (t.isYieldExpression(node)) { - return t.isBinary(parent) - || t.isUnaryLike(parent) - || t.isCallExpression(parent) - || t.isMemberExpression(parent) - || t.isNewExpression(parent) - || t.isConditionalExpression(parent) - || t.isYieldExpression(parent); + return t.isBinary(parent) || + t.isUnaryLike(parent) || + t.isCallExpression(parent) || + t.isMemberExpression(parent) || + t.isNewExpression(parent) || + t.isConditionalExpression(parent) || + t.isYieldExpression(parent); } if (t.isNewExpression(parent) && parent.callee === node) { @@ -338,8 +338,8 @@ _.each([ [">>", "<<", ">>>"], ["+", "-"], ["*", "/", "%"] -], function(tier, i) { - _.each(tier, function(op) { +], function (tier, i) { + _.each(tier, function (op) { PRECEDENCE[op] = i; }); });