From 1f274a3b958f22a755f6ee875049ce6728b562c6 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sun, 19 Oct 2014 13:21:56 +1100 Subject: [PATCH] monkeypatch in acorn instead of esprima - immediately fixes #38, fixes #67 @thejameskyle --- lib/6to5/modules/common.js | 12 +++++++- lib/6to5/transformers/array-comprehension.js | 6 ---- lib/6to5/util.js | 30 +++++++++++++++---- package.json | 3 +- .../array-comprehension/no-in/actual.js | 1 - .../array-comprehension/no-in/options.json | 3 -- .../default-parameters/actual.js | 5 ++++ .../default-parameters/expected.js | 8 +++++ .../destructuring-parameters/actual.js | 2 ++ .../destructuring-parameters/expected.js | 6 ++++ .../syntax/errors/syntax/options.json | 2 +- 11 files changed, 60 insertions(+), 18 deletions(-) delete mode 100644 test/fixtures/syntax/array-comprehension/no-in/actual.js delete mode 100644 test/fixtures/syntax/array-comprehension/no-in/options.json create mode 100644 test/fixtures/syntax/arrow-functions/default-parameters/actual.js create mode 100644 test/fixtures/syntax/arrow-functions/default-parameters/expected.js create mode 100644 test/fixtures/syntax/arrow-functions/destructuring-parameters/actual.js create mode 100644 test/fixtures/syntax/arrow-functions/destructuring-parameters/expected.js diff --git a/lib/6to5/modules/common.js b/lib/6to5/modules/common.js index a860eaf0cd..0e7f95f69a 100644 --- a/lib/6to5/modules/common.js +++ b/lib/6to5/modules/common.js @@ -1,5 +1,15 @@ module.exports = CommonJSModuleFormatter; +var types = require("ast-types"); +var def = types.Type.def; + +def("ImportBatchSpecifier") + .bases("Specifier") + .build("id") + .field("id", def("Identifier")); + +types.finalize(); + var util = require("../util"); var b = require("recast").types.builders; @@ -30,7 +40,7 @@ CommonJSModuleFormatter.prototype.importSpecifier = function (specifier, node, n var templateName = "require-assign"; // import * as bar from "foo"; - if (specifier.type !== "ImportNamespaceSpecifier") templateName += "-key"; + if (specifier.type !== "ImportBatchSpecifier") templateName += "-key"; nodes.push(util.template(templateName, { VARIABLE_NAME: variableName.name, diff --git a/lib/6to5/transformers/array-comprehension.js b/lib/6to5/transformers/array-comprehension.js index afc9a81c22..a4b03a1ebe 100644 --- a/lib/6to5/transformers/array-comprehension.js +++ b/lib/6to5/transformers/array-comprehension.js @@ -68,12 +68,6 @@ var multiple = function (node, file) { }; exports.ComprehensionExpression = function (node, parent, file) { - _.each(node.blocks, function (block) { - if (!block.of) { - throw util.errorWithNode(block, "for-in array comprehension is not supported"); - } - }); - if (node.blocks.length === 1) { return single(node); } else { diff --git a/lib/6to5/util.js b/lib/6to5/util.js index bfef7374f6..a4c0317040 100644 --- a/lib/6to5/util.js +++ b/lib/6to5/util.js @@ -1,6 +1,7 @@ var traverse = require("./traverse"); var astTypes = require("recast").types; var recast = require("recast"); +var acorn = require("acorn"); var path = require("path"); var fs = require("fs"); var _ = require("lodash"); @@ -237,6 +238,27 @@ exports.repeat = function (width, cha) { exports.parse = function (opts, code, callback) { try { var recastOpts = {}; + + recastOpts.esprima = { + parse: function (src) { + var comments = []; + var tokens = []; + + var ast = acorn.parse(src, { + ecmaVersion: 6, + locations: true, + onComment: comments, + onToken: tokens, + ranges: true + }); + + ast.tokens = tokens; + ast.comments = comments; + + return ast; + } + }; + if (opts.sourceMap) { recastOpts.sourceFileName = opts.sourceFileName; recastOpts.sourceRoot = opts.sourceRoot; @@ -254,11 +276,9 @@ exports.parse = function (opts, code, callback) { err._6to5 = true; err.message = opts.filename + ": " + err.message; - if (err.lineNumber) { - var frame = exports.codeFrame(code, err.lineNumber, err.column); - var err2 = new SyntaxError(err.message + frame); - err2._6to5 = true; - throw err2; + if (err.loc) { + var frame = exports.codeFrame(code, err.loc.line, err.loc.column); + err.message = err.message + frame; } } diff --git a/package.json b/package.json index 06d4e8b4f9..90f7dbdf86 100644 --- a/package.json +++ b/package.json @@ -46,7 +46,8 @@ "regenerator": "0.6.7", "chokidar": "^0.9.0", "source-map-support": "^0.2.7", - "esutils": "^1.1.4" + "esutils": "^1.1.4", + "acorn": "^0.9.0" }, "devDependencies": { "es6-transpiler": "0.7.17", diff --git a/test/fixtures/syntax/array-comprehension/no-in/actual.js b/test/fixtures/syntax/array-comprehension/no-in/actual.js deleted file mode 100644 index 7737a13f4a..0000000000 --- a/test/fixtures/syntax/array-comprehension/no-in/actual.js +++ /dev/null @@ -1 +0,0 @@ -var arr = [for (i in [1, 2, 3]) i * i]; diff --git a/test/fixtures/syntax/array-comprehension/no-in/options.json b/test/fixtures/syntax/array-comprehension/no-in/options.json deleted file mode 100644 index 765cdaab02..0000000000 --- a/test/fixtures/syntax/array-comprehension/no-in/options.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "throws": "for-in array comprehension is not supported" -} diff --git a/test/fixtures/syntax/arrow-functions/default-parameters/actual.js b/test/fixtures/syntax/arrow-functions/default-parameters/actual.js new file mode 100644 index 0000000000..4eee9c372e --- /dev/null +++ b/test/fixtures/syntax/arrow-functions/default-parameters/actual.js @@ -0,0 +1,5 @@ +var some = (count = "30") => { + console.log("count", count); +}; + +some(); diff --git a/test/fixtures/syntax/arrow-functions/default-parameters/expected.js b/test/fixtures/syntax/arrow-functions/default-parameters/expected.js new file mode 100644 index 0000000000..baba36b081 --- /dev/null +++ b/test/fixtures/syntax/arrow-functions/default-parameters/expected.js @@ -0,0 +1,8 @@ +"use strict"; + +var some = function (count) { + if (count === undefined) count = "30"; + console.log("count", count); +}; + +some(); diff --git a/test/fixtures/syntax/arrow-functions/destructuring-parameters/actual.js b/test/fixtures/syntax/arrow-functions/destructuring-parameters/actual.js new file mode 100644 index 0000000000..74d7040eb7 --- /dev/null +++ b/test/fixtures/syntax/arrow-functions/destructuring-parameters/actual.js @@ -0,0 +1,2 @@ +var a = ({ target }) => console.log(target); +a({ target: "I am a target" }); diff --git a/test/fixtures/syntax/arrow-functions/destructuring-parameters/expected.js b/test/fixtures/syntax/arrow-functions/destructuring-parameters/expected.js new file mode 100644 index 0000000000..07b761fb1a --- /dev/null +++ b/test/fixtures/syntax/arrow-functions/destructuring-parameters/expected.js @@ -0,0 +1,6 @@ +"use strict"; +var a = function (_ref) { + var target = _ref.target; + return console.log(target); +}; +a({ target: "I am a target" }); diff --git a/test/fixtures/syntax/errors/syntax/options.json b/test/fixtures/syntax/errors/syntax/options.json index 3f3c5af1e7..ff732a8c5d 100644 --- a/test/fixtures/syntax/errors/syntax/options.json +++ b/test/fixtures/syntax/errors/syntax/options.json @@ -1,3 +1,3 @@ { - "throws": "Line 2: Unexpected token ILLEGAL" + "throws": "Unexpected character '@'" }