put experimental ES7 features behind a flag --experimental and experimental option

This commit is contained in:
Sebastian McKenzie 2014-11-21 19:36:35 +11:00
parent 8b46cce466
commit bd91bbee71
12 changed files with 62 additions and 43 deletions

View File

@ -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.

View File

@ -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
};

View File

@ -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
});
//

View File

@ -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];

View File

@ -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)

View File

@ -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).

View File

@ -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

View File

@ -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);
};

View File

@ -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,

View File

@ -0,0 +1,3 @@
{
"experimental": true
}

View File

@ -1,3 +1,4 @@
{
"asyncExec": true
"asyncExec": true,
"experimental": true
}

View File

@ -0,0 +1,3 @@
{
"experimental": true
}