diff --git a/CHANGELOG.md b/CHANGELOG.md index 021437a771..9d2e5af03e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# 1.12.27 + + * Put experimental ES7 features behind a flag `--experimental` and `experimental` option. + # 1.12.26 * Support computed property destructuring. diff --git a/bin/6to5/index.js b/bin/6to5/index.js index 9f41d92530..d79fe08162 100755 --- a/bin/6to5/index.js +++ b/bin/6to5/index.js @@ -11,6 +11,7 @@ commander.option("-s, --source-maps", "Save source map alongside the compiled co commander.option("-f, --filename [filename]", "Filename to use when reading from stdin - this will be used in source-maps, errors etc [stdin]", "stdin"); commander.option("-w, --watch", "Recompile files on changes"); commander.option("-r, --runtime", "Replace 6to5 declarations with references to a runtime"); +commander.option("-e, --experimental", "Enable experimental support for proposed ES7 features"); commander.option("-m, --modules [modules]", "Module formatter type to use [common]", "common"); commander.option("-w, --whitelist [whitelist]", "Whitelist of transformers to ONLY use", util.list); @@ -87,11 +88,12 @@ if (errors.length) { exports.opts = { sourceMapName: commander.outFile, + amdModuleIds: commander.amdModuleIds, + experimental: commander.experimental, blacklist: commander.blacklist, whitelist: commander.whitelist, sourceMap: commander.sourceMaps || commander.sourceMapsInline, comments: !commander.removeComments, - amdModuleIds: commander.amdModuleIds, runtime: commander.runtime, modules: commander.modules }; diff --git a/bin/_6to5-node b/bin/_6to5-node index 89faa4364b..c9fe95968f 100644 --- a/bin/_6to5-node +++ b/bin/_6to5-node @@ -8,10 +8,11 @@ var util = require("../lib/6to5/util"); var vm = require("vm"); var _ = require("lodash"); -commander.option("-e, --eval [script]", "evaluate script"); -commander.option("-p, --print", "evaluate script and print result"); -commander.option("-i, --ignore [regex]", "ignore all files that match this regex when using the require hook"); -commander.option("-x, --extensions [extensions]", "list of extensions to hook into [.es6,.js]", util.list); +commander.option("-e, --eval [script]", "Evaluate script"); +commander.option("-p, --print", "Evaluate script and print result"); +commander.option("-i, --ignore [regex]", "Ignore all files that match this regex when using the require hook"); +commander.option("-x, --extensions [extensions]", "List of extensions to hook into [.es6,.js]"); +commander.option("-r, --experimental", "Enable experimental support for proposed ES7 features"); var pkg = require("../package.json"); commander.version(pkg.version); @@ -20,17 +21,11 @@ commander.parse(process.argv); // -var registerOpts = {}; - -if (commander.ignore) { - registerOpts.ignoreRegex = new RegExp(commander.ignore); -} - -if (commander.extensions && commander.extensions.length) { - registerOpts.extensions = commander.extensions; -} - -to5.register(registerOpts); +to5.register({ + experimental: commander.experimental, + extensions: commander.extensions, + ignore: commander.ignore +}); // diff --git a/doc/features.md b/doc/features.md index 78978ae250..1d2732533d 100644 --- a/doc/features.md +++ b/doc/features.md @@ -1,6 +1,6 @@ # Features -## Array comprehension +## Array comprehension ([experimental](usage.md#experimental)) ```javascript var results = [for (c of customers) if (c.city == "Seattle") { name: c.name, age: c.age }] @@ -31,7 +31,7 @@ var bob = { }; ``` -## Async functions +## Async functions ([experimental](usage.md#experimental)) ```javascript async function chainAnimationsAsync(elem, animations) { @@ -150,7 +150,7 @@ for (var n of fibonacci()) { } ``` -## Generator comprehension +## Generator comprehension ([experimental](usage.md#experimental)) ```javascript var nums = [1, 2, 3, 4, 5, 6]; diff --git a/doc/index.md b/doc/index.md index b61957bc27..005d513b42 100644 --- a/doc/index.md +++ b/doc/index.md @@ -31,8 +31,8 @@ And it doesn't end here! To see all the ways you can use 6to5, check out the ## [Features](features.md) - - [Array comprehension](features.md#array-comprehension) - - [Async functions](features.md#async-functions) + - [Array comprehension](features.md#array-comprehension) ([experimental](usage.md#experimental)) + - [Async functions](features.md#async-functions) ([experimental](usage.md#experimental)) - [Arrow functions](features.md#arrow-functions) - [Classes](features.md#classes) - [Computed property names](features.md#computed-property-names) @@ -41,7 +41,7 @@ And it doesn't end here! To see all the ways you can use 6to5, check out the - [Destructuring](features.md#destructuring) - [For-of](features.md#for-of) - [Generators](features.md#generators) - - [Generator comprehension](features.md#generator-comprehension) + - [Generator comprehension](features.md#generator-comprehension) ([experimental](usage.md#experimental)) - [Let scoping](features.md#let-scoping) - [Modules](features.md#modules) - [Numeric literals](features.md#numeric-literals) diff --git a/doc/usage.md b/doc/usage.md index ce51614591..1e4a75db47 100644 --- a/doc/usage.md +++ b/doc/usage.md @@ -135,7 +135,11 @@ to5.transformFile("filename.js", options, function (err, result) { // Output comments in generated output // Default: true - comments: false + comments: false, + + // Enable support for experimental ES7 features + // Default: false + experimental: true } ``` @@ -181,3 +185,9 @@ require("6to5/register")({ extensions: [".js", ".es6"] }); ``` + +## Experimental + +6to5 also has experimental support for ES7 proposals. You can enable this with +the `experimental: true` option when using the [Node API](#node) or +`--experimental` when using the [CLI](#cli). diff --git a/lib/6to5/file.js b/lib/6to5/file.js index ed343d9298..0e043292c5 100644 --- a/lib/6to5/file.js +++ b/lib/6to5/file.js @@ -25,15 +25,16 @@ File.normaliseOptions = function (opts) { opts = _.cloneDeep(opts || {}); _.defaults(opts, { - whitespace: true, - blacklist: [], - whitelist: [], - sourceMap: false, - comments: true, - filename: "unknown", - modules: "common", - runtime: false, - code: true + experimental: false, + whitespace: true, + blacklist: [], + whitelist: [], + sourceMap: false, + comments: true, + filename: "unknown", + modules: "common", + runtime: false, + code: true }); // normalise windows path separators to unix diff --git a/lib/6to5/register.js b/lib/6to5/register.js index 30fc5e5a82..86035276b5 100644 --- a/lib/6to5/register.js +++ b/lib/6to5/register.js @@ -57,24 +57,25 @@ blacklistTest("unicodeRegex", function () { new RegExp("foo", "u"); }); // -var ignoreRegex = /node_modules/; +var transformOpts = {}; +var ignoreRegex = /node_modules/; var onlyRegex; -var whitelist = []; -var exts = {}; -var maps = {}; -var old = require.extensions[".js"]; +var whitelist = []; +var exts = {}; +var maps = {}; +var old = require.extensions[".js"]; var loader = function (m, filename) { if ((ignoreRegex && ignoreRegex.test(filename)) || (onlyRegex && !onlyRegex.test(filename))) { return old.apply(this, arguments); } - var result = to5.transformFileSync(filename, { + var result = to5.transformFileSync(filename, _.extend({ whitelist: whitelist, blacklist: blacklist, sourceMap: true, modules: "commonInterop" - }); + }, transformOpts)); maps[filename] = result.map; @@ -107,6 +108,5 @@ module.exports = function (opts) { if (opts.extensions) hookExtensions(util.arrayify(opts.extensions)); - if (opts.blacklist) blacklist = util.arrayify(opts.blacklist); - if (opts.whitelist) whitelist = util.arrayify(opts.whitelist); + _.extend(transformOpts, opts); }; diff --git a/lib/6to5/util.js b/lib/6to5/util.js index a7d8ff8415..0a06cfa9c8 100644 --- a/lib/6to5/util.js +++ b/lib/6to5/util.js @@ -218,7 +218,7 @@ exports.parse = function (opts, code, callback) { var ast = acorn.parse(code, { allowReturnOutsideFunction: true, preserveParens: true, - ecmaVersion: Infinity, + ecmaVersion: opts.experimental ? 7 : 6, strictMode: true, onComment: comments, locations: true, diff --git a/test/fixtures/transformation/array-comprehension/options.json b/test/fixtures/transformation/array-comprehension/options.json new file mode 100644 index 0000000000..252f473a73 --- /dev/null +++ b/test/fixtures/transformation/array-comprehension/options.json @@ -0,0 +1,3 @@ +{ + "experimental": true +} diff --git a/test/fixtures/transformation/async/options.json b/test/fixtures/transformation/async/options.json index 165ec035ab..8a6dc3a088 100644 --- a/test/fixtures/transformation/async/options.json +++ b/test/fixtures/transformation/async/options.json @@ -1,3 +1,4 @@ { - "asyncExec": true + "asyncExec": true, + "experimental": true } diff --git a/test/fixtures/transformation/generator-comprehension/options.json b/test/fixtures/transformation/generator-comprehension/options.json new file mode 100644 index 0000000000..252f473a73 --- /dev/null +++ b/test/fixtures/transformation/generator-comprehension/options.json @@ -0,0 +1,3 @@ +{ + "experimental": true +}