diff --git a/lib/6to5/transformation/transform.js b/lib/6to5/transformation/transform.js index 4824e44548..497d65d376 100644 --- a/lib/6to5/transformation/transform.js +++ b/lib/6to5/transformation/transform.js @@ -20,16 +20,21 @@ transform._ensureTransformerNames = function (type, keys) { transform.transformers = {}; transform.moduleFormatters = { - common: require("./modules/common"), - commonInterop: require("./modules/common-interop"), - system: require("./modules/system"), - ignore: require("./modules/ignore"), - amd: require("./modules/amd"), - umd: require("./modules/umd") + commonStrict: require("./modules/common-strict"), + common: require("./modules/common"), + system: require("./modules/system"), + ignore: require("./modules/ignore"), + amd: require("./modules/amd"), + umd: require("./modules/umd") }; _.each({ - // plyground + // spec + _blockHoistFunctions: require("./transformers/spec-block-hoist-functions"), + _noForInOfAssignment: require("./transformers/spec-no-for-in-of-assignment"), + _noDuplicateProperties: require("./transformers/spec-no-duplicate-properties"), + + // playground methodBinding: require("./transformers/playground-method-binding"), memoizationOperator: require("./transformers/playground-memoization-operator"), objectGetterMemoization: require("./transformers/playground-object-getter-memoization"), @@ -59,16 +64,18 @@ _.each({ constants: require("./transformers/es6-constants"), letScoping: require("./transformers/es6-let-scoping"), - generators: require("./transformers/es6-generators"), - _blockHoist: require("./transformers/_block-hoist"), _declarations: require("./transformers/_declarations"), + + generators: require("./transformers/es6-generators"), + + // spec + _propertyLiterals: require("./transformers/spec-property-literals"), + _memberExpressioLiterals: require("./transformers/spec-member-expression-literals"), + + // wrap up _aliasFunctions: require("./transformers/_alias-functions"), - useStrict: require("./transformers/use-strict"), - - _propertyLiterals: require("./transformers/_property-literals"), - _memberExpressioLiterals: require("./transformers/_member-expression-literals"), _moduleFormatter: require("./transformers/_module-formatter") }, function (transformer, key) { transform.transformers[key] = new Transformer(key, transformer); diff --git a/lib/6to5/transformation/transformers/spec-block-hoist-functions.js b/lib/6to5/transformation/transformers/spec-block-hoist-functions.js new file mode 100644 index 0000000000..8dae49c00c --- /dev/null +++ b/lib/6to5/transformation/transformers/spec-block-hoist-functions.js @@ -0,0 +1,19 @@ +var t = require("../../types"); +var _ = require("lodash"); + +exports.BlockStatement = function (node, parent) { + if (t.isFunction(parent)) return; + + node.body = node.body.map(function (node) { + if (t.isFunction(node)) { + node.type = "FunctionExpression"; + var declar = t.variableDeclaration("let", [ + t.variableDeclarator(node.id, node) + ]); + declar._blockHoist = true; + return declar; + } else { + return node; + } + }); +}; diff --git a/lib/6to5/transformation/transformers/_member-expression-literals.js b/lib/6to5/transformation/transformers/spec-member-expression-literals.js similarity index 100% rename from lib/6to5/transformation/transformers/_member-expression-literals.js rename to lib/6to5/transformation/transformers/spec-member-expression-literals.js diff --git a/lib/6to5/transformation/transformers/spec-no-duplicate-properties.js b/lib/6to5/transformation/transformers/spec-no-duplicate-properties.js new file mode 100644 index 0000000000..6bbfb3b2f9 --- /dev/null +++ b/lib/6to5/transformation/transformers/spec-no-duplicate-properties.js @@ -0,0 +1,25 @@ +var t = require("../../types"); +var _ = require("lodash"); + +exports.ObjectExpression = function (node, parent, file) { + var keys = []; + + _.each(node.properties, function (prop) { + if (prop.computed || prop.kind !== "init") return; + + var key = prop.key; + if (t.isIdentifier(key)) { + key = key.name; + } else if (t.isLiteral(key)) { + key = key.value; + } else { + return; + } + + if (_.contains(keys, key)) { + throw file.errorWithNode(prop.key, "Duplicate property key"); + } else { + keys.push(key); + } + }); +}; diff --git a/lib/6to5/transformation/transformers/spec-no-for-in-of-assignment.js b/lib/6to5/transformation/transformers/spec-no-for-in-of-assignment.js new file mode 100644 index 0000000000..5af12108f1 --- /dev/null +++ b/lib/6to5/transformation/transformers/spec-no-for-in-of-assignment.js @@ -0,0 +1,10 @@ +var t = require("../../types"); + +exports.ForInStatement = +exports.ForOfStatement = function (node, parent, file) { + var left = node.left; + if (t.isVariableDeclaration(left)) { + var declar = left.declarations[0]; + if (declar.init) throw file.errorWithNode(declar, "No assignments allowed in for-in/of head"); + } +}; diff --git a/lib/6to5/transformation/transformers/_property-literals.js b/lib/6to5/transformation/transformers/spec-property-literals.js similarity index 100% rename from lib/6to5/transformation/transformers/_property-literals.js rename to lib/6to5/transformation/transformers/spec-property-literals.js diff --git a/test/fixtures/transformation/misc/property-literals/actual.js b/test/fixtures/transformation/misc/property-literals/actual.js deleted file mode 100644 index 229c4e0f42..0000000000 --- a/test/fixtures/transformation/misc/property-literals/actual.js +++ /dev/null @@ -1,3 +0,0 @@ -var obj = { - "foobar": "lol" -}; diff --git a/test/fixtures/transformation/misc/property-literals/expected.js b/test/fixtures/transformation/misc/property-literals/expected.js deleted file mode 100644 index 5222f9aedf..0000000000 --- a/test/fixtures/transformation/misc/property-literals/expected.js +++ /dev/null @@ -1,5 +0,0 @@ -"use strict"; - -var obj = { - foobar: "lol" -}; diff --git a/test/fixtures/transformation/spec-no-duplicate-properties/identifiers/actual.js b/test/fixtures/transformation/spec-no-duplicate-properties/identifiers/actual.js new file mode 100644 index 0000000000..7dd5168e50 --- /dev/null +++ b/test/fixtures/transformation/spec-no-duplicate-properties/identifiers/actual.js @@ -0,0 +1 @@ +var obj = { a: 1, a: 2 }; diff --git a/test/fixtures/transformation/spec-no-duplicate-properties/literals/actual.js b/test/fixtures/transformation/spec-no-duplicate-properties/literals/actual.js new file mode 100644 index 0000000000..85b7f9f02a --- /dev/null +++ b/test/fixtures/transformation/spec-no-duplicate-properties/literals/actual.js @@ -0,0 +1 @@ +var obj = { "a": 1, "a": 2 }; diff --git a/test/fixtures/transformation/spec-no-duplicate-properties/mixed/actual.js b/test/fixtures/transformation/spec-no-duplicate-properties/mixed/actual.js new file mode 100644 index 0000000000..eace9c1b04 --- /dev/null +++ b/test/fixtures/transformation/spec-no-duplicate-properties/mixed/actual.js @@ -0,0 +1 @@ +var obj = { a: 1, "a": 2 }; diff --git a/test/fixtures/transformation/spec-no-duplicate-properties/options.json b/test/fixtures/transformation/spec-no-duplicate-properties/options.json new file mode 100644 index 0000000000..32dce7785e --- /dev/null +++ b/test/fixtures/transformation/spec-no-duplicate-properties/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Duplicate property key" +} diff --git a/test/fixtures/transformation/spec-no-for-in-of-assignment/in/actual.js b/test/fixtures/transformation/spec-no-for-in-of-assignment/in/actual.js new file mode 100644 index 0000000000..167407a7da --- /dev/null +++ b/test/fixtures/transformation/spec-no-for-in-of-assignment/in/actual.js @@ -0,0 +1,3 @@ +for (var i = 0 in obj) { + +} diff --git a/test/fixtures/transformation/spec-no-for-in-of-assignment/of/actual.js b/test/fixtures/transformation/spec-no-for-in-of-assignment/of/actual.js new file mode 100644 index 0000000000..56e4dfef82 --- /dev/null +++ b/test/fixtures/transformation/spec-no-for-in-of-assignment/of/actual.js @@ -0,0 +1,3 @@ +for (var i = 0 of obj) { + +} diff --git a/test/fixtures/transformation/spec-no-for-in-of-assignment/options.json b/test/fixtures/transformation/spec-no-for-in-of-assignment/options.json new file mode 100644 index 0000000000..c7ae9b8aa4 --- /dev/null +++ b/test/fixtures/transformation/spec-no-for-in-of-assignment/options.json @@ -0,0 +1,3 @@ +{ + "throws": "No assignments allowed in for-in/of head" +} diff --git a/test/fixtures/transformation/spec/block-hoist-functions/exec.js b/test/fixtures/transformation/spec/block-hoist-functions/exec.js new file mode 100644 index 0000000000..f3475e0a9e --- /dev/null +++ b/test/fixtures/transformation/spec/block-hoist-functions/exec.js @@ -0,0 +1,5 @@ +function f() { return 1; } +{ + function f() { return 2; } +} +assert.equal(f(), 1); diff --git a/test/fixtures/transformation/misc/_member-expression-literals/actual.js b/test/fixtures/transformation/spec/member-expression-literals/actual.js similarity index 100% rename from test/fixtures/transformation/misc/_member-expression-literals/actual.js rename to test/fixtures/transformation/spec/member-expression-literals/actual.js diff --git a/test/fixtures/transformation/misc/_member-expression-literals/expected.js b/test/fixtures/transformation/spec/member-expression-literals/expected.js similarity index 100% rename from test/fixtures/transformation/misc/_member-expression-literals/expected.js rename to test/fixtures/transformation/spec/member-expression-literals/expected.js diff --git a/test/fixtures/transformation/misc/_property-literals/actual.js b/test/fixtures/transformation/spec/property-literals/actual.js similarity index 100% rename from test/fixtures/transformation/misc/_property-literals/actual.js rename to test/fixtures/transformation/spec/property-literals/actual.js diff --git a/test/fixtures/transformation/misc/_property-literals/expected.js b/test/fixtures/transformation/spec/property-literals/expected.js similarity index 100% rename from test/fixtures/transformation/misc/_property-literals/expected.js rename to test/fixtures/transformation/spec/property-literals/expected.js