Compare commits
210 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
afdde8b3a7 | ||
|
|
918ddb4124 | ||
|
|
18739ad78b | ||
|
|
74a84e2473 | ||
|
|
52ffc65a06 | ||
|
|
d4fbfbbe47 | ||
|
|
781467d423 | ||
|
|
44f5b7d013 | ||
|
|
dcf7f0b70b | ||
|
|
03efd69998 | ||
|
|
213003a007 | ||
|
|
b0503f2efe | ||
|
|
c8cf7ff286 | ||
|
|
ceb32816d7 | ||
|
|
32b94899a6 | ||
|
|
d951082b09 | ||
|
|
b49f6e33d6 | ||
|
|
c5fa6425a5 | ||
|
|
70d896d609 | ||
|
|
e00fa8c9b1 | ||
|
|
13933bc9b8 | ||
|
|
295bab544b | ||
|
|
cfe05ca10d | ||
|
|
da72182219 | ||
|
|
005f5b2174 | ||
|
|
8551c94fa0 | ||
|
|
c509d06bc2 | ||
|
|
f21743012b | ||
|
|
22dc33f93c | ||
|
|
6df6652629 | ||
|
|
4942ba6dd7 | ||
|
|
b63bd8cba0 | ||
|
|
cd1eb11171 | ||
|
|
6e1c12fd78 | ||
|
|
a4ea71b85d | ||
|
|
1df69bdbeb | ||
|
|
84c2766756 | ||
|
|
1f000ac83d | ||
|
|
322aa246b8 | ||
|
|
601bbb86cd | ||
|
|
5c39685650 | ||
|
|
e6ac5eccd0 | ||
|
|
c4c9f7984f | ||
|
|
8d472a14be | ||
|
|
4fd007948c | ||
|
|
f4e516e8ce | ||
|
|
7fcd8ea642 | ||
|
|
add0a7d5fe | ||
|
|
e9445c9277 | ||
|
|
df66fa0a7f | ||
|
|
0a85211e8e | ||
|
|
ac8fc24fdb | ||
|
|
71c4a29a78 | ||
|
|
08dde22939 | ||
|
|
d3ea0fe84f | ||
|
|
22851cd1ac | ||
|
|
987dc0692f | ||
|
|
560d60e3e7 | ||
|
|
e96d02c972 | ||
|
|
13a2d469a3 | ||
|
|
0b232ed228 | ||
|
|
9053e0dedf | ||
|
|
873e820210 | ||
|
|
27bb08eadf | ||
|
|
bea903c69f | ||
|
|
198a9c6ead | ||
|
|
9c92679053 | ||
|
|
87d2bdadeb | ||
|
|
fa71f2a7f9 | ||
|
|
66e7134335 | ||
|
|
99a1cf08f9 | ||
|
|
5de9c22452 | ||
|
|
694a8dc456 | ||
|
|
2df1ca3971 | ||
|
|
6c7cdc9b7c | ||
|
|
52b1d055ac | ||
|
|
2a316bf362 | ||
|
|
26ce0b8c8b | ||
|
|
275c4cc34e | ||
|
|
bc914e6f23 | ||
|
|
4ea425ac7b | ||
|
|
bd255257f1 | ||
|
|
41f072d112 | ||
|
|
2a4c8e8423 | ||
|
|
122705bdaf | ||
|
|
f84e0ba640 | ||
|
|
64216ec384 | ||
|
|
a948928973 | ||
|
|
5ab891b39d | ||
|
|
2a742b5fac | ||
|
|
cd6b678b51 | ||
|
|
c290a008b2 | ||
|
|
e8f0c8e8a4 | ||
|
|
22eb8edf41 | ||
|
|
a3eb8900a9 | ||
|
|
bf74f5a634 | ||
|
|
98e9a45f63 | ||
|
|
1d91a758b4 | ||
|
|
b94b4b335b | ||
|
|
104b1f4c9b | ||
|
|
00c95f6a94 | ||
|
|
f14dc60934 | ||
|
|
2fc8010034 | ||
|
|
882ed48438 | ||
|
|
49578fe223 | ||
|
|
01bdb7efdc | ||
|
|
fc7fc384c9 | ||
|
|
bd78e3ed4d | ||
|
|
1f990f4030 | ||
|
|
d8e4a4a776 | ||
|
|
473b6d6a91 | ||
|
|
3d975da530 | ||
|
|
1a4110e0a1 | ||
|
|
a03d491ac6 | ||
|
|
e01e010577 | ||
|
|
132ac00ec7 | ||
|
|
1673f78ab5 | ||
|
|
76b8a06e90 | ||
|
|
711f84c826 | ||
|
|
4b4ff007e9 | ||
|
|
e31e68cae3 | ||
|
|
778b4e3a27 | ||
|
|
6011db7ae1 | ||
|
|
363b1631f5 | ||
|
|
01286ebd5b | ||
|
|
fe918a3535 | ||
|
|
2fb6c7820c | ||
|
|
e7cbbefc11 | ||
|
|
f4fdc3adc6 | ||
|
|
2cd49b08ec | ||
|
|
1b16ff3a0b | ||
|
|
b7c298a0ee | ||
|
|
5da4397392 | ||
|
|
ea5954121e | ||
|
|
37b2e747d5 | ||
|
|
3dd6eb1202 | ||
|
|
319ed09407 | ||
|
|
77f9596682 | ||
|
|
00cb90541a | ||
|
|
d26f441a5c | ||
|
|
11fc7cd0ce | ||
|
|
c38428c536 | ||
|
|
bf40849e35 | ||
|
|
194fb77b9c | ||
|
|
ceb8e812b6 | ||
|
|
3a261a5e05 | ||
|
|
4c1ad04b2c | ||
|
|
0d9e6aca67 | ||
|
|
aac6303696 | ||
|
|
833a4900da | ||
|
|
525169460a | ||
|
|
13785bddb0 | ||
|
|
84332da399 | ||
|
|
aae7d8190b | ||
|
|
03dcc335d8 | ||
|
|
2267344471 | ||
|
|
593e4ca670 | ||
|
|
e563783a1e | ||
|
|
615425c808 | ||
|
|
55e2010311 | ||
|
|
74f40c2824 | ||
|
|
c26ce1c114 | ||
|
|
2537768772 | ||
|
|
8af05634fa | ||
|
|
74614e712e | ||
|
|
ad923cf8fa | ||
|
|
c7d69b2f92 | ||
|
|
2a0b63f3bb | ||
|
|
01934b6960 | ||
|
|
8ffd2e843f | ||
|
|
6d68b3da4d | ||
|
|
79b4207b44 | ||
|
|
30a90d2554 | ||
|
|
bac8758369 | ||
|
|
d00faf4a72 | ||
|
|
b986a4e482 | ||
|
|
f11f364d2c | ||
|
|
c4a0f62eef | ||
|
|
072fff0f4c | ||
|
|
59b81b389a | ||
|
|
4a439857d2 | ||
|
|
187f7ba01e | ||
|
|
e6da342e8b | ||
|
|
1c22c608a9 | ||
|
|
8e96130bc0 | ||
|
|
0f47814b41 | ||
|
|
b32d56a168 | ||
|
|
5f26a0c5cc | ||
|
|
2245bbc6c4 | ||
|
|
b8d61f1528 | ||
|
|
e97e916b5c | ||
|
|
b688154aee | ||
|
|
ccb56fe33f | ||
|
|
5e67dfbd9e | ||
|
|
6f6d9bd5a8 | ||
|
|
61b9c28ce6 | ||
|
|
d11d0d3216 | ||
|
|
bd876f7a4d | ||
|
|
f9f48620de | ||
|
|
ce9a82c194 | ||
|
|
24f6dfa8e2 | ||
|
|
f592f95a68 | ||
|
|
2e0cd0f349 | ||
|
|
e17d5bdb13 | ||
|
|
c1c22ed5f7 | ||
|
|
bf07f54e57 | ||
|
|
eef0344cb2 | ||
|
|
88a85c0ca8 | ||
|
|
302e6f4caa | ||
|
|
36c16a0227 |
@@ -1,3 +1,3 @@
|
||||
node_modules
|
||||
test
|
||||
lib/6to5/templates
|
||||
lib/6to5/transformation/templates
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
node_modules
|
||||
*.log
|
||||
*.cache
|
||||
lib/6to5/templates
|
||||
lib/6to5/transformation/templates
|
||||
test
|
||||
benchmark
|
||||
Makefile
|
||||
|
||||
71
CHANGELOG.md
71
CHANGELOG.md
@@ -2,6 +2,77 @@
|
||||
|
||||
Gaps between patch versions are faulty/broken releases.
|
||||
|
||||
## 2.2.0
|
||||
|
||||
* Make `system` module formatter modules anonymous by default.
|
||||
* Fix duplicate comments being output, breaking code.
|
||||
|
||||
## 2.1.0
|
||||
|
||||
* Add `cache` option to register hook.
|
||||
* Update `core-js`.
|
||||
* Fix starting newline not being added on case statements.
|
||||
* Fix destructuring `VariableDeclaration`s not inside `BlockStatement`s and `Program`.
|
||||
|
||||
## 2.0.4
|
||||
|
||||
* Avoid being greedy when destructuring array iterables.
|
||||
|
||||
## 2.0.3
|
||||
|
||||
* Hoist function declarations in system module formatter for circular references.
|
||||
* Hoist default function declarations in umd and amd module formatters for circular references.
|
||||
|
||||
## 2.0.2
|
||||
|
||||
* Inherit comments in `for-of` transformer.
|
||||
* Remove `interopRequire` from `system` module formatter.
|
||||
|
||||
## 2.0.1
|
||||
|
||||
* Remap `UpdateExpression` module export binding.
|
||||
* Fix automatic closure on `PrivateDeclaration` in classes.
|
||||
|
||||
## 2.0.0
|
||||
|
||||
* Make string literal generation only escapes unicode that it has to.
|
||||
* Internal code generation format options have been exposed.
|
||||
* Change playground method binding operator from `:` to `#` removing ambiguous syntax with terns.
|
||||
* Fix rest parameters in async and generator functions.
|
||||
* Export/import declarations replace by the modules transformer now inherit comments.
|
||||
* Added playground flag to `6to5-node`.
|
||||
* `6to5-node` now behaves the same as `node`.
|
||||
* `6to5-node` now uses `kexec` to become the forked process to correctly propagate signals on unix.
|
||||
* Constants are now block scoped.
|
||||
* Exposed ast transformer.
|
||||
* Merged `commonInterop` and `common` module formatters.
|
||||
* Fix generator comprehensions not inheriting `arguments`, `this` etc.
|
||||
* Object and class mutator shorthand are now enumerable.
|
||||
* Remove regenerator `Generator has already finished` error which isn't spec-compliant.
|
||||
* Expose internal `spec` transformers that nicen up code output.
|
||||
* Add export variable declaration default initializers.
|
||||
* Propagate export declaration reassignments.
|
||||
* Add initializer default to block scoped variable declarations within a loop.
|
||||
* Flow type support.
|
||||
* Make async/await contextual keywords.
|
||||
* Allow `yield`ing of non-objects.
|
||||
* Class declarations now lack an IIFE.
|
||||
* Support falsy and `null` super classes.
|
||||
* Add support for experimental abstract references `private` declarations.
|
||||
* Leave out IIFE for class declarations.
|
||||
* Switched to [core-js](https://github.com/zloirock/core-js) from [es6-symbol](https://github.com/medikoo/es6-symbol) and [es6-shim](https://github.com/paulmillr/es6-shim/) for built-in polyfill.
|
||||
* `amd` and `umd` module formatters now behave the same as `common` with `interopRequire`.
|
||||
* Micro-optimizations to boost performance by 200%.
|
||||
* Rename module formatter methods `import` to `importDeclaration` and `export` to `exportDeclaration`.
|
||||
* Support multiple declarators in export variable declarations.
|
||||
* Freeze tagged template literal object.
|
||||
* Remove inlined `regenerator` fork.
|
||||
* Remove `ParenthesizedExpression`.
|
||||
* Rename `object-spread` helper to `object-without-properties`.
|
||||
* Rename `class-props` helper to `prototype-properties`.
|
||||
* Rename `extends` helper to `inherits`.
|
||||
* Completely rewritten `system` module formatter.
|
||||
|
||||
## 1.15.0
|
||||
|
||||
* Don't alias `GeneratorFunction` and check the name which causes minifiers to remove the name and throw an error later on when we check if it's set.
|
||||
|
||||
14
README.md
14
README.md
@@ -3,6 +3,10 @@
|
||||
</p>
|
||||
|
||||
<p align="center">
|
||||
<a href="https://gratipay.com/sebmck">
|
||||
<img alt="Gratipay" src="https://img.shields.io/gratipay/sebmck.svg?style=flat">
|
||||
</a>
|
||||
|
||||
<a href="https://travis-ci.org/6to5/6to5">
|
||||
<img alt="Travis Status" src="http://img.shields.io/travis/6to5/6to5/master.svg?style=flat&label=travis">
|
||||
</a>
|
||||
@@ -24,7 +28,11 @@
|
||||
</a>
|
||||
</p>
|
||||
|
||||
**6to5** turns ES6+ code into vanilla ES5, so you can use next generation features **today.**
|
||||
<p align="center">
|
||||
<strong>6to5</strong> turns ES6+ code into vanilla ES5, so you can use next generation features <strong>today.</strong>
|
||||
</p>
|
||||
|
||||
For more information view the [documentation](https://6to5.github.io). For
|
||||
support visit the [gitter room](https://gitter.im/6to5/6to5).
|
||||
<p align="center">
|
||||
For more information view the <a href="https://6to5.github.io">documentation</a>. For
|
||||
support visit the <a href="https://gitter.im/6to5/6to5">gitter room</a>.
|
||||
</p>
|
||||
|
||||
@@ -2,9 +2,9 @@ Error.stackTraceLimit = Infinity;
|
||||
|
||||
var jsTrans = require("jstransform");
|
||||
var traceur = require("traceur");
|
||||
var es6tr = require("es6-transpiler");
|
||||
//var es6tr = require("es6-transpiler");
|
||||
var es6now = require("es6now");
|
||||
var esnext = require("esnext");
|
||||
//var esnext = require("esnext");
|
||||
var to5 = require("../lib/6to5");
|
||||
|
||||
//var uglify = require("uglify-js");
|
||||
@@ -56,12 +56,12 @@ var compilers = {
|
||||
}
|
||||
},
|
||||
|
||||
esnext: {
|
||||
/*esnext: {
|
||||
runtime: readResolve("esnext/node_modules/regenerator/runtime.js") || readResolve("regenerator/runtime.js"),
|
||||
compile: function (code) {
|
||||
return esnext.compile(code).code;
|
||||
}
|
||||
},
|
||||
},*/
|
||||
|
||||
es6now: {
|
||||
runtime: readResolve("es6now/runtime/ES6.js"),
|
||||
@@ -70,13 +70,13 @@ var compilers = {
|
||||
}
|
||||
},
|
||||
|
||||
"es6-transpiler": {
|
||||
/*"es6-transpiler": {
|
||||
compile: function (code) {
|
||||
var result = es6tr.run({ src: code });
|
||||
if (result.errors.length) throw new Error(result.join("; "));
|
||||
return result.src;
|
||||
}
|
||||
},
|
||||
},*/
|
||||
|
||||
jstransform: {
|
||||
compile: function (code) {
|
||||
|
||||
@@ -5,7 +5,6 @@
|
||||
* when found, before invoking the "real" _6to5-node(1) executable.
|
||||
*/
|
||||
|
||||
var spawn = require("child_process").spawn;
|
||||
var args = ["--harmony", __dirname + "/_6to5-node"];
|
||||
|
||||
process.argv.slice(2).forEach(function(arg){
|
||||
@@ -49,13 +48,21 @@ process.argv.slice(2).forEach(function(arg){
|
||||
}
|
||||
});
|
||||
|
||||
var proc = spawn(process.argv[0], args, { stdio: "inherit" });
|
||||
proc.on("exit", function (code, signal) {
|
||||
process.on("exit", function () {
|
||||
if (signal) {
|
||||
process.kill(process.pid, signal);
|
||||
} else {
|
||||
process.exit(code);
|
||||
}
|
||||
try {
|
||||
var kexec = require("kexec");
|
||||
kexec(process.argv[0], args);
|
||||
} catch (err) {
|
||||
if (err.code !== "MODULE_NOT_FOUND") throw err;
|
||||
|
||||
var child_process = require("child_process");
|
||||
var proc = child_process.spawn(process.argv[0], args, { stdio: "inherit" });
|
||||
proc.on("exit", function (code, signal) {
|
||||
process.on("exit", function () {
|
||||
if (signal) {
|
||||
process.kill(process.pid, signal);
|
||||
} else {
|
||||
process.exit(code);
|
||||
}
|
||||
})
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
var runtime = require("../lib/6to5/runtime");
|
||||
var runtime = require("../lib/6to5/runtime-generator");
|
||||
console.log(runtime(process.argv[2]));
|
||||
|
||||
@@ -113,16 +113,14 @@ module.exports = function (commander, filenames) {
|
||||
walk();
|
||||
|
||||
if (commander.watch) {
|
||||
var watcher = chokidar.watch(filenames, {
|
||||
chokidar.watch(filenames, {
|
||||
persistent: true,
|
||||
ignoreInitial: true
|
||||
});
|
||||
|
||||
_.each(["add", "change", "unlink"], function (type) {
|
||||
watcher.on(type, function (filename) {
|
||||
}).on("all", function (type, filename) {
|
||||
if (type === "add" || type === "change" || type === "unlink" ) {
|
||||
console.log(type, filename);
|
||||
walk();
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
@@ -20,7 +20,8 @@ commander.option("-b, --blacklist [blacklist]", "Blacklist of transformers to NO
|
||||
commander.option("-o, --out-file [out]", "Compile all input files into a single file");
|
||||
commander.option("-d, --out-dir [out]", "Compile an input directory of modules into an output directory");
|
||||
commander.option("-c, --remove-comments", "Remove comments from the compiled code", false);
|
||||
commander.option("-a, --amd-module-ids", "Insert module id in AMD modules", false);
|
||||
commander.option("-a, --amd-module-ids", "Insert module id in AMD modules", false); // todo: remove in 3.0.0
|
||||
commander.option("-m, --module-ids", "Insert module id in modules", false);
|
||||
|
||||
commander.on("--help", function(){
|
||||
var outKeys = function (title, obj) {
|
||||
@@ -89,9 +90,9 @@ if (errors.length) {
|
||||
|
||||
exports.opts = {
|
||||
sourceMapName: commander.outFile,
|
||||
amdModuleIds: commander.amdModuleIds,
|
||||
experimental: commander.experimental,
|
||||
playground: commander.playground,
|
||||
moduleIds: commander.amdModuleIds || commander.moduleIds,
|
||||
blacklist: commander.blacklist,
|
||||
whitelist: commander.whitelist,
|
||||
sourceMap: commander.sourceMaps || commander.sourceMapsInline,
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
var commander = require("commander");
|
||||
var path = require("path");
|
||||
var repl = require("repl");
|
||||
var to5 = require("../lib/6to5");
|
||||
var util = require("../lib/6to5/util");
|
||||
var vm = require("vm");
|
||||
var _ = require("lodash");
|
||||
var roadrunner = require("roadrunner");
|
||||
var commander = require("commander");
|
||||
var Module = require("module");
|
||||
var path = require("path");
|
||||
var repl = require("repl");
|
||||
var to5 = require("../lib/6to5");
|
||||
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");
|
||||
@@ -14,28 +16,33 @@ commander.option("-i, --ignore [regex]", "Ignore all files that match this regex
|
||||
commander.option("-x, --extensions [extensions]", "List of extensions to hook into [.es6,.js]");
|
||||
commander.option("-r, --experimental", "Enable experimental support for proposed ES7 features");
|
||||
commander.option("-g, --playground", "Enable playground support");
|
||||
commander.option("-c, --cache", "Cache compiled files and require paths");
|
||||
|
||||
var pkg = require("../package.json");
|
||||
commander.version(pkg.version);
|
||||
commander.usage("[options] [ -e script | script.js ] [arguments]");
|
||||
commander.parse(process.argv);
|
||||
|
||||
if (commander.cache) roadrunner.load();
|
||||
|
||||
//
|
||||
|
||||
to5.register({
|
||||
experimental: commander.experimental,
|
||||
extensions: commander.extensions,
|
||||
ignore: commander.ignore
|
||||
playground: commander.playground,
|
||||
ignore: commander.ignore,
|
||||
cache: commander.cache && roadrunner.get("6to5")
|
||||
});
|
||||
|
||||
//
|
||||
|
||||
var _eval = function (code, filename) {
|
||||
code = to5.transform(code, {
|
||||
modules: "commonInterop",
|
||||
filename: filename,
|
||||
blacklist: ["useStrict"],
|
||||
experimental: commander.experimental
|
||||
experimental: commander.experimental,
|
||||
playground: commander.playground
|
||||
}).code;
|
||||
return vm.runInThisContext(code, filename);
|
||||
};
|
||||
@@ -44,21 +51,34 @@ if (commander.eval) {
|
||||
var result = _eval(commander.eval, "eval");
|
||||
if (commander.print) console.log(result);
|
||||
} else {
|
||||
var filenames = commander.args;
|
||||
if (commander.args.length) {
|
||||
// slice all arguments up to the first filename since they're 6to5 args that we handle
|
||||
var args = process.argv.slice(2);
|
||||
|
||||
if (filenames.length) {
|
||||
_.each(filenames, function (filename) {
|
||||
if (!util.isAbsolute(filename)) {
|
||||
filename = path.join(process.cwd(), filename);
|
||||
var i = 0;
|
||||
_.each(args, function (arg, i2) {
|
||||
if (arg[0] !== "-") {
|
||||
i = i2;
|
||||
return false;
|
||||
}
|
||||
|
||||
require(require.resolve(filename));
|
||||
});
|
||||
args = args.slice(i);
|
||||
|
||||
// make the filename absolute
|
||||
var filename = args[0]
|
||||
if (!util.isAbsolute(filename)) args[0] = path.join(process.cwd(), filename);
|
||||
|
||||
// add back on node and concat the sliced args
|
||||
process.argv = ["node"].concat(args);
|
||||
|
||||
Module.runMain();
|
||||
} else {
|
||||
replStart();
|
||||
}
|
||||
}
|
||||
|
||||
if (commander.cache) roadrunner.save();
|
||||
|
||||
function replStart() {
|
||||
repl.start({
|
||||
prompt: "> ",
|
||||
|
||||
@@ -2,22 +2,31 @@
|
||||
|
||||
In order for certain features to work they require certain polyfills. You can
|
||||
satisfy **all** 6to5 feature requirements by using the included
|
||||
[polyfill](polyfill.md). You may alternatively selectively include what you need:
|
||||
[polyfill](polyfill.md).
|
||||
|
||||
| Feature | Requirements |
|
||||
| --------------------------- | ---------------------------------------------------------------------------------------------------------------------------- |
|
||||
| Abstract References | [experimental](usage.md#experimental), `Symbol` |
|
||||
| Array destructuring | `Array.isArray`, `Array.from` |
|
||||
| Async functions, Generators | [experimental](usage.md#experimental), [regenerator runtime](https://github.com/facebook/regenerator/blob/master/runtime.js) |
|
||||
| Comprehensions | [experimental](usage.md#experimental), `Array.isArray`, `Array.from` |
|
||||
| For Of | `Symbol`, `prototype[Symbol.iterator]` |
|
||||
| Object spread/rest | [experimental](usage.md#experimental), `Object.assign` |
|
||||
| Spread | `Array.isArray`, `Array.from` |
|
||||
You may alternatively selectively include what you need:
|
||||
|
||||
## Classes
|
||||
| Feature | Requirements |
|
||||
| --------------------------- | ---------------------------------------------------------------------------------------------------------------------- |
|
||||
| Abstract References | [experimental](experimental.md), `Symbol` |
|
||||
| Array destructuring | `Array.isArray`, `Array.from` |
|
||||
| Async functions, Generators | [experimental](experimental.md), [regenerator runtime](https://github.com/facebook/regenerator/blob/master/runtime.js) |
|
||||
| Comprehensions | [experimental](experimental.md), `Array.isArray`, `Array.from` |
|
||||
| For Of | `Symbol`, `prototype[Symbol.iterator]` |
|
||||
| Object spread/rest | [experimental](experimental.md), `Object.assign` |
|
||||
| Spread | `Array.isArray`, `Array.from` |
|
||||
|
||||
Built-in classes such as `Date`, `Array` and `DOM` cannot be subclassed due to
|
||||
limitations in ES5 implementations.
|
||||
## ES5
|
||||
|
||||
Since 6to5 assumes that your code will be ran in an ES5 environment it uses ES5
|
||||
functions. So if you're using an environment that has limited or no support for
|
||||
ES5 such as lower versions of IE then using the
|
||||
[es5-shim](https://github.com/es-shims/es5-shim) along with the
|
||||
[6to5 polyfill](polyfill.md) will add support for these methods.
|
||||
|
||||
## Internet Explorer
|
||||
|
||||
### Classes (9 and below)
|
||||
|
||||
If you're inheriting from a class then static properties are inherited from it
|
||||
via [\_\_proto\_\_](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/proto),
|
||||
@@ -39,3 +48,12 @@ class Bar extends Foo {
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Getters/setters (8 and below)
|
||||
|
||||
In IE8 `Object.defineProperty` can only be used on DOM objects. This is
|
||||
unfortunate as it's required to set getters and setters. Due to this if
|
||||
you plan on supporting IE8 or below then the user of getters and setters
|
||||
isn't recommended.
|
||||
|
||||
Reference: [MDN](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty#Internet_Explorer_8_specific_notes).
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
# Differences
|
||||
|
||||
## Philosophy
|
||||
There are three main points that separate 6to5 from all other transpilers.
|
||||
|
||||
### Readable code
|
||||
|
||||
The fundamental concept behind 6to5 is that the generated code must be close as
|
||||
possible to the original, retaining all the same formatting and readability.
|
||||
@@ -51,7 +53,27 @@ As you can tell, it's not very pretty, unreadable even. Instead of mapping
|
||||
directly to a runtime, like other transpilers, 6to5 maps directly to the
|
||||
equivalent ES5.
|
||||
|
||||
Sometimes there are little things that 6to5 needs
|
||||
Sometimes there are little inline functions that 6to5 needs. These are
|
||||
placed at the top of your file much like coffee-script does. If these
|
||||
are bother you then you can use the [optional runtime](optional-runtime.md).
|
||||
We promise that these inline functions will never be significant and will
|
||||
always be used as little as possible.
|
||||
|
||||
### Static analysis
|
||||
|
||||
6to5 uses a lot of static analysis to simplify code as much as possible.
|
||||
Not many other transpilers do this, and those that do don't do it nearly
|
||||
as much as 6to5. This process is pretty intensive but it leads to higher
|
||||
quality code.
|
||||
|
||||
### Spec compliancy
|
||||
|
||||
6to5 prides itself on
|
||||
[spec compliancy](https://kangax.github.io/compat-table/es6/). We have
|
||||
excellent support for edgecases, something that many other transpilers
|
||||
suffer from, including Traceur. When you use 6to5 you can be confident
|
||||
that when you turn it off and use your code in a full ES6 environment
|
||||
**it'll just work**.
|
||||
|
||||
## Comparison to other transpilers
|
||||
|
||||
@@ -62,40 +84,44 @@ Sometimes there are little things that 6to5 needs
|
||||
| Source maps | ✓ | ✓ | ✓ | ✓ | | ✓ |
|
||||
| No compiler global pollution | ✓ | | ✓ | ✓ | | ✓ |
|
||||
| Optional runtime | ✓ | | ✓ | | | ✓ |
|
||||
| Browser support | ✓ | ✓ | | ✓ | | |
|
||||
| Browser compiler | ✓ | ✓ | | ✓ | | |
|
||||
|
||||
### Language Support
|
||||
|
||||
| | 6to5 | Traceur | es6-transpiler | esnext | es6now | jstransform |
|
||||
| ---------------------------- | ----- | ------- | -------------- | ------ | ------ | ----------- |
|
||||
| Abstract references | ✓ | | | | | |
|
||||
| Array comprehension | ✓ | ✓ | ✓ | | | |
|
||||
| Arrow functions | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
|
||||
| Async functions | ✓ | ✓ | | ✓ | | |
|
||||
| Classes | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
|
||||
| Computed property names | ✓ | ✓ | ✓ | ✓ | ✓ | |
|
||||
| Constants | ✓ | ✓ | ✓ | | | |
|
||||
| Default parameters | ✓ | ✓ | ✓ | ✓ | ✓ | |
|
||||
| Destructuring | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
|
||||
| Exponentiation operator | ✓ | ✓ | | | | |
|
||||
| For-of | ✓ | ✓ | ✓ | ✓ | ✓ | |
|
||||
| Generators | ✓ | ✓ | | ✓ | | |
|
||||
| Generator comprehension | ✓ | ✓ | | | | |
|
||||
| Let scoping | ✓ | ✓ | ✓ | | | |
|
||||
| Modules | ✓ | ✓ | | | ✓ | |
|
||||
| Object rest/spread | ✓ | | | | | ✓ |
|
||||
| Property method assignment | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
|
||||
| Property name shorthand | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
|
||||
| Rest parameters | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
|
||||
| Spread | ✓ | ✓ | ✓ | ✓ | ✓ | |
|
||||
| Template literals | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
|
||||
| Unicode regex | ✓ | ✓ | ✓ | | | |
|
||||
| | 6to5 | Traceur | es6-transpiler | esnext | es6now | jstransform |
|
||||
| ---------------------------- | ---- | ------- | -------------- | ------ | ------ | ----------- |
|
||||
| Abstract references | ✓ | | | | | |
|
||||
| Array comprehension | ✓ | ✓ | ✓ | | | |
|
||||
| Arrow functions | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
|
||||
| Async functions | ✓ | ✓ | | ✓ | | |
|
||||
| Async generator functions | ✓ | ✓ | | | | |
|
||||
| Classes | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
|
||||
| Computed property names | ✓ | ✓ | ✓ | ✓ | ✓ | |
|
||||
| Constants | ✓ | ✓ | ✓ | | | |
|
||||
| Default parameters | ✓ | ✓ | ✓ | ✓ | ✓ | |
|
||||
| Destructuring | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
|
||||
| Exponentiation operator | ✓ | ✓ | | | | |
|
||||
| Flow types | ✓ | | | | | ✓ |
|
||||
| For-of | ✓ | ✓ | ✓ | ✓ | ✓ | |
|
||||
| Generators | ✓ | ✓ | | ✓ | | |
|
||||
| Generator comprehension | ✓ | ✓ | | | | |
|
||||
| JSX | ✓ | | | | | |
|
||||
| Let scoping | ✓ | ✓ | ✓ | | | |
|
||||
| Modules | ✓ | ✓ | | | ✓ | |
|
||||
| Object rest/spread | ✓ | | | | | ✓ |
|
||||
| Property method assignment | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
|
||||
| Property name shorthand | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
|
||||
| Rest parameters | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
|
||||
| React | ✓ | | | | | |
|
||||
| Spread | ✓ | ✓ | ✓ | ✓ | ✓ | |
|
||||
| Template literals | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
|
||||
| Unicode regex | ✓ | ✓ | ✓ | | | |
|
||||
|
||||
### [Traceur](https://github.com/google/traceur-compiler)
|
||||
|
||||
Traceur requires quite a bulky runtime (~75KB) and produces quite verbose code.
|
||||
While this can be trimmed down by selectively building the runtime, it's an
|
||||
unneccesary step when a large runtime can be eliminated entirely.
|
||||
unnecessary step when a large runtime can be eliminated entirely.
|
||||
|
||||
### [es6now](https://github.com/zenparsing/es6now)
|
||||
|
||||
|
||||
8
doc/experimental.md
Normal file
8
doc/experimental.md
Normal file
@@ -0,0 +1,8 @@
|
||||
## 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).
|
||||
|
||||
**WARNING:** These proposals are subject to change so use with
|
||||
**extreme caution**.
|
||||
@@ -1,6 +1,6 @@
|
||||
# Features
|
||||
|
||||
## Abstract references ([experimental](usage.md#experimental)) ([spec](https://github.com/zenparsing/es-abstract-refs))
|
||||
## Abstract references ([experimental](experimental.md)) ([spec](https://github.com/zenparsing/es-abstract-refs))
|
||||
|
||||
```javascript
|
||||
foo::bar;
|
||||
@@ -8,7 +8,7 @@ foo::bar = baz;
|
||||
delete foo::bar;
|
||||
```
|
||||
|
||||
## Array comprehensions ([experimental](usage.md#experimental))
|
||||
## Array comprehensions ([experimental](experimental.md))
|
||||
|
||||
```javascript
|
||||
var results = [for (c of customers) if (c.city == "Seattle") { name: c.name, age: c.age }]
|
||||
@@ -39,7 +39,7 @@ var bob = {
|
||||
};
|
||||
```
|
||||
|
||||
## Async functions ([experimental](usage.md#experimental)) ([spec](https://github.com/lukehoban/ecmascript-asyncawait))
|
||||
## Async functions ([experimental](experimental.md)) ([spec](https://github.com/lukehoban/ecmascript-asyncawait))
|
||||
|
||||
```javascript
|
||||
async function chainAnimationsAsync(elem, animations) {
|
||||
@@ -49,6 +49,12 @@ async function chainAnimationsAsync(elem, animations) {
|
||||
}
|
||||
```
|
||||
|
||||
## Async generator functions ([experimental](experimental.md)) ([spec](https://github.com/jhusain/asyncgenerator))
|
||||
|
||||
```javascript
|
||||
|
||||
```
|
||||
|
||||
## Classes
|
||||
|
||||
```javascript
|
||||
@@ -130,7 +136,7 @@ var [a] = [];
|
||||
a === undefined;
|
||||
```
|
||||
|
||||
## Exponentiation operator ([experimental](usage.md#experimental)) ([spec](https://github.com/rwaldron/exponentiation-operator))
|
||||
## Exponentiation operator ([experimental](experimental.md)) ([spec](https://github.com/rwaldron/exponentiation-operator))
|
||||
|
||||
```javascript
|
||||
var a = 2;
|
||||
@@ -167,7 +173,7 @@ for (var n of fibonacci()) {
|
||||
}
|
||||
```
|
||||
|
||||
## Generator comprehensions ([experimental](usage.md#experimental))
|
||||
## Generator comprehensions ([experimental](experimental.md))
|
||||
|
||||
```javascript
|
||||
var nums = [1, 2, 3, 4, 5, 6];
|
||||
@@ -208,7 +214,7 @@ export default test;
|
||||
0o767 === 503; // true
|
||||
```
|
||||
|
||||
## Object spread/rest ([experimental](usage.md#experimental)) ([spec](https://github.com/sebmarkbage/ecmascript-rest-spread))
|
||||
## Object spread/rest ([experimental](experimental.md)) ([spec](https://github.com/sebmarkbage/ecmascript-rest-spread))
|
||||
|
||||
```javascript
|
||||
var n = { x, y, ...z };
|
||||
|
||||
13
doc/index.md
13
doc/index.md
@@ -35,23 +35,24 @@ And it doesn't end here! To see all the ways you can use 6to5, check out the
|
||||
|
||||
## [Features](features.md)
|
||||
|
||||
- [Abstract references](features.md#abstract-references) ([experimental](usage.md#experimental))
|
||||
- [Array comprehension](features.md#array-comprehension) ([experimental](usage.md#experimental))
|
||||
- [Async functions](features.md#async-functions) ([experimental](usage.md#experimental))
|
||||
- [Abstract references](features.md#abstract-references) ([experimental](experimental.md))
|
||||
- [Array comprehension](features.md#array-comprehension) ([experimental](experimental.md))
|
||||
- [Async functions](features.md#async-functions) ([experimental](experimental.md))
|
||||
- [Async generator functions](features.md#async-generator-functions) ([experimental](experimental.md))
|
||||
- [Arrow functions](features.md#arrow-functions)
|
||||
- [Classes](features.md#classes)
|
||||
- [Computed property names](features.md#computed-property-names)
|
||||
- [Constants](features.md#constants)
|
||||
- [Default parameters](features.md#default-parameters)
|
||||
- [Destructuring](features.md#destructuring)
|
||||
- [Exponentiation operator](features.md#exponentiation-operator) ([experimental](usage.md#experimental))
|
||||
- [Exponentiation operator](features.md#exponentiation-operator) ([experimental](experimental.md))
|
||||
- [For-of](features.md#for-of)
|
||||
- [Generators](features.md#generators)
|
||||
- [Generator comprehension](features.md#generator-comprehension) ([experimental](usage.md#experimental))
|
||||
- [Generator comprehension](features.md#generator-comprehension) ([experimental](experimental.md))
|
||||
- [Let scoping](features.md#let-scoping)
|
||||
- [Modules](features.md#modules)
|
||||
- [Numeric literals](features.md#numeric-literals)
|
||||
- [Object Rest/Spread](features.md#object-rest-spread) ([experimental](usage.md#experimental))
|
||||
- [Object Rest/Spread](features.md#object-rest-spread) ([experimental](experimental.md))
|
||||
- [Property method assignment](features.md#property-method-assignment)
|
||||
- [Property name shorthand](features.md#property-name-shorthand)
|
||||
- [React/JSX](react.md)
|
||||
|
||||
111
doc/modules.md
111
doc/modules.md
@@ -19,12 +19,11 @@ to5.transform('import "foo";', { modules: "common" });
|
||||
|
||||
* [AMD](#amd)
|
||||
* [Common (Default)](#common-default)
|
||||
* [Common Interop](#common-interop)
|
||||
* [Ignore](#ignore)
|
||||
* [System](#system)
|
||||
* [UMD](#umd)
|
||||
|
||||
### Common (Default)
|
||||
### Common
|
||||
|
||||
```sh
|
||||
$ 6to5 --modules common
|
||||
@@ -33,6 +32,11 @@ $ 6to5 --modules common
|
||||
**In**
|
||||
|
||||
```javascript
|
||||
export default test;
|
||||
|
||||
export {test};
|
||||
export var test = 5;
|
||||
|
||||
import "foo";
|
||||
|
||||
import foo from "foo";
|
||||
@@ -40,95 +44,30 @@ import * as foo from "foo";
|
||||
|
||||
import {bar} from "foo";
|
||||
import {foo as bar} from "foo";
|
||||
|
||||
export {test};
|
||||
export var test = 5;
|
||||
|
||||
export default test;
|
||||
```
|
||||
|
||||
**Out**
|
||||
|
||||
```javascript
|
||||
require("foo");
|
||||
"use strict";
|
||||
|
||||
var foo = require("foo").default;
|
||||
var foo = require("foo");
|
||||
|
||||
var bar = require("foo").bar;
|
||||
var bar = require("foo").foo;
|
||||
|
||||
exports.test = test;
|
||||
var test = 5; exports.test = test;
|
||||
|
||||
exports.default = test;
|
||||
```
|
||||
|
||||
### Common interop
|
||||
|
||||
```sh
|
||||
$ 6to5 --modules commonInterop
|
||||
```
|
||||
|
||||
**In**
|
||||
|
||||
```javascript
|
||||
import "foo";
|
||||
|
||||
import foo from "foo";
|
||||
import * as foo from "foo";
|
||||
|
||||
import {bar} from "foo";
|
||||
import {foo as bar} from "foo";
|
||||
|
||||
export {test};
|
||||
export var test = 5;
|
||||
|
||||
export default test;
|
||||
```
|
||||
|
||||
**Out**
|
||||
|
||||
```javascript
|
||||
var _interopRequire = function (obj) {
|
||||
return obj && (obj["default"] || obj);
|
||||
};
|
||||
|
||||
require("foo");
|
||||
|
||||
var foo = _interopRequire(require("foo"));
|
||||
var foo = require("foo");
|
||||
|
||||
var bar = require("foo").bar;
|
||||
var bar = require("foo").foo;
|
||||
exports = module.exports = test;
|
||||
|
||||
exports.test = test;
|
||||
var test = exports.test = 5;
|
||||
|
||||
exports["default"] = test;
|
||||
```
|
||||
require("foo");
|
||||
|
||||
#### module.exports behaviour
|
||||
var foo = _interopRequire(require("foo"));
|
||||
|
||||
If there exist no other non-default `export`s then `default exports` are
|
||||
exported as `module.exports` instead of `exports.default`.
|
||||
var foo = require("foo");
|
||||
|
||||
**In**
|
||||
|
||||
```javascript
|
||||
export default function foo() {
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
**Out**
|
||||
|
||||
```javascript
|
||||
module.exports = foo;
|
||||
|
||||
function foo() {
|
||||
|
||||
}
|
||||
var bar = require("foo").bar;
|
||||
var bar = require("foo").foo;
|
||||
```
|
||||
|
||||
### AMD
|
||||
@@ -151,9 +90,14 @@ export function bar() {
|
||||
|
||||
```javascript
|
||||
define(["exports", "foo"], function (exports, _foo) {
|
||||
exports.bar = bar;
|
||||
"use strict";
|
||||
|
||||
var foo = _foo.default;
|
||||
var _interopRequire = function (obj) {
|
||||
return obj && (obj["default"] || obj);
|
||||
};
|
||||
|
||||
exports.bar = bar;
|
||||
var foo = _interopRequire(_foo);
|
||||
|
||||
function bar() {
|
||||
return foo("foobar");
|
||||
@@ -192,10 +136,15 @@ export function bar() {
|
||||
} else if (typeof exports !== "undefined") {
|
||||
factory(exports, require("foo"));
|
||||
}
|
||||
})(function (exports) {
|
||||
exports.bar = bar;
|
||||
})(function (exports, _foo) {
|
||||
"use strict";
|
||||
|
||||
var foo = _foo.default;
|
||||
var _interopRequire = function (obj) {
|
||||
return obj && (obj["default"] || obj);
|
||||
};
|
||||
|
||||
exports.bar = bar;
|
||||
var foo = _interopRequire(_foo);
|
||||
|
||||
function bar() {
|
||||
return foo("foobar");
|
||||
@@ -288,7 +237,7 @@ ModuleFormatter.prototype.transform = function (ast) {
|
||||
// feel free to modify this however
|
||||
};
|
||||
|
||||
ModuleFormatter.prototype.import = function (node, nodes) {
|
||||
ModuleFormatter.prototype.importDeclaration = function (node, nodes) {
|
||||
// node is an ImportDeclaration
|
||||
};
|
||||
|
||||
@@ -297,7 +246,7 @@ ModuleFormatter.prototype.importSpecifier = function (specifier, node, nodes) {
|
||||
// node is an ImportDeclaration
|
||||
};
|
||||
|
||||
ModuleFormatter.prototype.export = function (node, nodes) {
|
||||
ModuleFormatter.prototype.exportDeclaration = function (node, nodes) {
|
||||
// node is an ExportDeclaration
|
||||
};
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ Playground is a proving ground for **possible** ES7 proposals.
|
||||
to5.transform("code", { playground: true });
|
||||
```
|
||||
|
||||
**NOTE:** Enabling `playground` also enables [experimental support](usage.md#experimental).
|
||||
**NOTE:** Enabling `playground` also enables [experimental support](experimental.md).
|
||||
|
||||
## Features
|
||||
|
||||
@@ -52,11 +52,11 @@ if (!Object.prototype.hasOwnProperty.call(obj, "x")) obj.x = 2;
|
||||
### Method binding
|
||||
|
||||
```javascript
|
||||
var fn = obj:method;
|
||||
var fn = obj:method("foob");
|
||||
var fn = obj#method;
|
||||
var fn = obj#method("foob");
|
||||
|
||||
["foo", "bar"].map(:toUpperCase); // ["FOO", "BAR"]
|
||||
[1.1234, 23.53245, 3].map(:toFixed(2)); // ["1.12", "23.53", "3.00"]
|
||||
["foo", "bar"].map(#toUpperCase); // ["FOO", "BAR"]
|
||||
[1.1234, 23.53245, 3].map(#toFixed(2)); // ["1.12", "23.53", "3.00"]
|
||||
```
|
||||
|
||||
equivalent to:
|
||||
|
||||
@@ -1,18 +1,33 @@
|
||||
# Plugins
|
||||
|
||||
- [Broccoli](https://github.com/very-geek/broccoli-6to5-transpiler)
|
||||
- [Browserify](https://github.com/6to5/6to5-browserify)
|
||||
- [Brunch](https://github.com/es128/6to5-brunch)
|
||||
- [Duo](https://github.com/bdo-labs/duo6to5)
|
||||
- [Connect](https://github.com/6to5/6to5-connect)
|
||||
- [Gobble](https://github.com/gobblejs/gobble-6to5)
|
||||
- [Gulp](https://github.com/sindresorhus/gulp-6to5)
|
||||
- [Grunt](https://github.com/sindresorhus/grunt-6to5)
|
||||
## Build systems
|
||||
|
||||
- [Broccoli](https://github.com/6to5/broccoli-6to5-transpiler)
|
||||
- [Browserify](https://github.com/6to5/6to5ify)
|
||||
- [Brunch](https://github.com/6to5/6to5-brunch)
|
||||
- [Duo](https://github.com/6to5/duo6to5)
|
||||
- [Gobble](https://github.com/6to5/gobble-6to5)
|
||||
- [Gulp](https://github.com/6to5/gulp-6to5)
|
||||
- [Grunt](https://github.com/6to5/grunt-6to5)
|
||||
- [Sprockets](https://github.com/josh/sprockets-es6) or via [Browserify](https://github.com/6to5/6to5-rails)
|
||||
- [webpack](https://github.com/6to5/6to5-loader)
|
||||
|
||||
## Integrations
|
||||
|
||||
- [Isparta](https://github.com/douglasduteil/isparta) - Code coverage with `karma` and `instanbul` using 6to5
|
||||
- [Jade](https://github.com/Apoxx/jade-6to5)
|
||||
- [Jest](https://github.com/6to5/6to5-jest)
|
||||
- [JSXHint](https://github.com/STRML/JSXHint) - A wrapper around JSHint to allow linting of JSX files
|
||||
- [Karma](https://github.com/shuhei/karma-6to5-preprocessor)
|
||||
|
||||
## Bridges
|
||||
|
||||
- [Ruby](https://github.com/6to5/6to5-ruby)
|
||||
|
||||
## Testing
|
||||
|
||||
- [Jest](https://github.com/6to5/6to5-jest)
|
||||
- [Karma](https://github.com/6to5/karma-6to5-preprocessor)
|
||||
- [Mocha](https://github.com/6to5/6to5-mocha)
|
||||
- [Rails](https://github.com/josh/sprockets-es6) or via [browserify-rails](https://github.com/6to5/6to5-rails)
|
||||
- [webpack](https://github.com/Couto/6to5-loader)
|
||||
|
||||
## Misc
|
||||
|
||||
- [Connect](https://github.com/6to5/6to5-connect)
|
||||
- [Jade](https://github.com/6to5/jade-6to5)
|
||||
|
||||
@@ -1,16 +1,15 @@
|
||||
# Polyfill
|
||||
|
||||
6to5 includes a polyfill that includes the
|
||||
[regenerator runtime](https://github.com/facebook/regenerator/blob/master/runtime.js) and the
|
||||
[es6-shim](https://github.com/paulmillr/es6-shim) and
|
||||
[es6-symbol](https://github.com/medikoo/es6-symbol) polyfills.
|
||||
6to5 includes a polyfill that includes a custom
|
||||
[regenerator runtime](https://github.com/facebook/regenerator/blob/master/runtime.js) and
|
||||
[core.js](https://github.com/zloirock/core-js).
|
||||
|
||||
This will emulate a full ES6 environment. This polyfill is automatically loaded
|
||||
when using [6to5-node](usage.md#node).
|
||||
when using [6to5-node](usage.md#node) and [6to5/register](usage.md#register-hook).
|
||||
|
||||
## Usage
|
||||
|
||||
### Node
|
||||
### Node/Browserify
|
||||
|
||||
You need to include the polyfill require at the top the **entry point** to your
|
||||
application.
|
||||
@@ -19,9 +18,17 @@ application.
|
||||
require("6to5/polyfill");
|
||||
```
|
||||
|
||||
Fortunately, this is automatically loaded when using:
|
||||
|
||||
```javascript
|
||||
require("6to5/register");
|
||||
```
|
||||
|
||||
### Browser
|
||||
|
||||
Available from the `browser-polyfill.js` file within the 6to5 directory of an
|
||||
npm release. This needs to be included **before** all your compiled 6to5 code.
|
||||
You can either prepend it to your compiled code or include it in a `<script>`
|
||||
before it.
|
||||
|
||||
**NOTE:** Do not `require` this via browserify etc, use `6to5/polyfill`.
|
||||
|
||||
81
doc/usage.md
81
doc/usage.md
@@ -95,6 +95,15 @@ to5.transformFile("filename.js", options, function (err, result) {
|
||||
});
|
||||
```
|
||||
|
||||
### to5.transform.fromAst(ast, [code], [opts])
|
||||
|
||||
```javascript
|
||||
var result = to5.transform.fromAst(ast, "var a = 2;", opts);
|
||||
result.code;
|
||||
result.map;
|
||||
result.ast;
|
||||
```
|
||||
|
||||
#### Options
|
||||
|
||||
```javascript
|
||||
@@ -143,23 +152,57 @@ to5.transformFile("filename.js", options, function (err, result) {
|
||||
// Default: `sourceRoot` option.
|
||||
moduleRoot: "my-app",
|
||||
|
||||
// If truthy, insert an explicit id for each defined AMD module.
|
||||
// By default, AMD modules are anonymous.
|
||||
// If truthy, insert an explicit id for each defined AMD/System module.
|
||||
// By default, AMD/System modules are anonymous.
|
||||
// Default: false
|
||||
amdModuleIds: true,
|
||||
moduleIds: true,
|
||||
|
||||
// Optionally replace all 6to5 helper declarations with a referenece to this
|
||||
// variable. If set to `true` then the default namespace is used "to5Runtime".
|
||||
// Default: false
|
||||
runtime: true,
|
||||
|
||||
// Output comments in generated output
|
||||
// Default: true
|
||||
comments: false,
|
||||
|
||||
// Enable support for experimental ES7 features
|
||||
// Default: false
|
||||
experimental: true
|
||||
experimental: true,
|
||||
|
||||
// Set this to `false` if you don't want the transformed AST in the returned
|
||||
// result
|
||||
// Default: true
|
||||
ast: true,
|
||||
|
||||
// Set this to `false` if you don't want the transformed code in the returned
|
||||
// result
|
||||
// Default: true
|
||||
code: true,
|
||||
|
||||
format: {
|
||||
// Output comments in generated output
|
||||
// Default: true
|
||||
comments: true,
|
||||
|
||||
// Do not include superfluous whitespace characters and line terminators
|
||||
// Default: false
|
||||
compact: false,
|
||||
|
||||
indent: {
|
||||
// Preserve parentheses in new expressions that have no arguments
|
||||
// Default: true
|
||||
parentheses: true,
|
||||
|
||||
// Adjust the indentation of multiline comments to keep asterisks vertically aligned
|
||||
// Default: true
|
||||
adjustMultilineComment: true,
|
||||
|
||||
// Indent string
|
||||
// Default: " "
|
||||
style: " ",
|
||||
|
||||
// Base indent level
|
||||
// Default: 0
|
||||
base: 0
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -167,7 +210,10 @@ to5.transformFile("filename.js", options, function (err, result) {
|
||||
|
||||
All subsequent files required by node with the extensions `.es6` and `.js` will
|
||||
be transformed by 6to5. The polyfill specified in [Polyfill](polyfill.md) is
|
||||
also required.
|
||||
also required; but this is automatically loaded when using:
|
||||
|
||||
Source maps are automatically configured so if any errors a thrown then line
|
||||
number info is mapped and you'll get the correct source location.
|
||||
|
||||
```javascript
|
||||
require("6to5/register");
|
||||
@@ -184,6 +230,8 @@ require("6to5/register")({
|
||||
});
|
||||
```
|
||||
|
||||
**NOTE:** Using 6to5-node or `require("6to5/register")`, 6to5 will check what features are available in the current environment and blacklists them if they're supported.
|
||||
|
||||
#### Options
|
||||
|
||||
```javascript
|
||||
@@ -202,15 +250,10 @@ require("6to5/register")({
|
||||
|
||||
// This will remove the currently hooked extensions of .es6 and .js so you'll
|
||||
// have to add them back if you want them to be used again.
|
||||
extensions: [".js", ".es6"]
|
||||
extensions: [".js", ".es6"],
|
||||
|
||||
// Enables `roadrunner` cache that will save to a `.roadrunner.json` file in your cwd
|
||||
// Do not check this into git as it's user-specific
|
||||
cache: true
|
||||
});
|
||||
```
|
||||
|
||||
## 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).
|
||||
|
||||
**WARNING:** These proposals are subject to change so use with
|
||||
**extreme caution**.
|
||||
|
||||
@@ -16,13 +16,14 @@ function File(opts) {
|
||||
}
|
||||
|
||||
File.declarations = [
|
||||
"extends",
|
||||
"class-props",
|
||||
"inherits",
|
||||
"prototype-properties",
|
||||
"apply-constructor",
|
||||
"tagged-template-literal",
|
||||
"interop-require",
|
||||
"to-array",
|
||||
"object-spread",
|
||||
"sliced-to-array",
|
||||
"object-without-properties",
|
||||
"has-own",
|
||||
"slice"
|
||||
];
|
||||
@@ -34,6 +35,7 @@ File.normaliseOptions = function (opts) {
|
||||
experimental: false,
|
||||
playground: false,
|
||||
whitespace: true,
|
||||
moduleIds: opts.amdModuleIds || false,
|
||||
blacklist: [],
|
||||
whitelist: [],
|
||||
sourceMap: false,
|
||||
@@ -82,13 +84,19 @@ File.normaliseOptions = function (opts) {
|
||||
return opts;
|
||||
};
|
||||
|
||||
File.prototype.toArray = function (node) {
|
||||
File.prototype.toArray = function (node, i) {
|
||||
if (t.isArrayExpression(node)) {
|
||||
return node;
|
||||
} else if (t.isIdentifier(node) && node.name === "arguments") {
|
||||
return t.callExpression(t.memberExpression(this.addDeclaration("slice"), t.identifier("call")), [node]);
|
||||
} else {
|
||||
return t.callExpression(this.addDeclaration("to-array"), [node]);
|
||||
var declarationName = "to-array";
|
||||
var args = [node];
|
||||
if (i) {
|
||||
args.push(t.literal(i));
|
||||
declarationName = "sliced-to-array";
|
||||
}
|
||||
return t.callExpression(this.addDeclaration(declarationName), args);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -101,7 +109,7 @@ File.prototype.getModuleFormatter = function (type) {
|
||||
}
|
||||
|
||||
if (!ModuleFormatter) {
|
||||
throw new ReferenceError("unknown module formatter type " + type);
|
||||
throw new ReferenceError("Unknown module formatter type " + JSON.stringify(type));
|
||||
}
|
||||
|
||||
return new ModuleFormatter(this);
|
||||
@@ -156,14 +164,16 @@ File.prototype.errorWithNode = function (node, msg, Error) {
|
||||
return err;
|
||||
};
|
||||
|
||||
File.prototype.parse = function (code) {
|
||||
File.prototype.addCode = function (code) {
|
||||
code = (code || "") + "";
|
||||
this.code = code;
|
||||
return this.parseShebang(code);
|
||||
};
|
||||
|
||||
File.prototype.parse = function (code) {
|
||||
var self = this;
|
||||
|
||||
this.code = code;
|
||||
|
||||
code = this.parseShebang(code);
|
||||
code = this.addCode(code);
|
||||
|
||||
return util.parse(this.opts, code, function (tree) {
|
||||
self.transform(tree);
|
||||
|
||||
@@ -35,7 +35,7 @@ Buffer.prototype.dedent = function () {
|
||||
};
|
||||
|
||||
Buffer.prototype.semicolon = function () {
|
||||
if (this.format.semicolons) this.push(";");
|
||||
this.push(";");
|
||||
};
|
||||
|
||||
Buffer.prototype.ensureSemicolon = function () {
|
||||
@@ -66,9 +66,7 @@ Buffer.prototype.removeLast = function (cha) {
|
||||
};
|
||||
|
||||
Buffer.prototype.newline = function (i, removeLast) {
|
||||
if (!this.buf) return;
|
||||
if (this.format.compact) return;
|
||||
if (this.endsWith("{\n")) return;
|
||||
|
||||
if (_.isBoolean(i)) {
|
||||
removeLast = i;
|
||||
@@ -76,6 +74,7 @@ Buffer.prototype.newline = function (i, removeLast) {
|
||||
}
|
||||
|
||||
if (_.isNumber(i)) {
|
||||
if (this.endsWith("{\n")) i--;
|
||||
if (this.endsWith(util.repeat(i, "\n"))) return;
|
||||
|
||||
var self = this;
|
||||
@@ -88,7 +87,10 @@ Buffer.prototype.newline = function (i, removeLast) {
|
||||
if (removeLast && this.isLast("\n")) this.removeLast("\n");
|
||||
|
||||
this.removeLast(" ");
|
||||
|
||||
// remove whitespace if last character was a newline
|
||||
this.buf = this.buf.replace(/\n +$/, "\n");
|
||||
|
||||
this._push("\n");
|
||||
};
|
||||
|
||||
|
||||
@@ -37,7 +37,6 @@ _.each(Buffer.prototype, function (fn, key) {
|
||||
CodeGenerator.normaliseOptions = function (opts) {
|
||||
return _.merge({
|
||||
parentheses: true,
|
||||
semicolons: true,
|
||||
comments: opts.comments == null || opts.comments,
|
||||
compact: false,
|
||||
indent: {
|
||||
@@ -58,6 +57,7 @@ CodeGenerator.generators = {
|
||||
methods: require("./generators/methods"),
|
||||
modules: require("./generators/modules"),
|
||||
types: require("./generators/types"),
|
||||
flow: require("./generators/flow"),
|
||||
base: require("./generators/base"),
|
||||
jsx: require("./generators/jsx")
|
||||
};
|
||||
@@ -71,6 +71,12 @@ CodeGenerator.prototype.generate = function () {
|
||||
|
||||
this.print(ast);
|
||||
|
||||
var comments = [];
|
||||
_.each(ast.comments, function (comment) {
|
||||
if (!comment._displayed) comments.push(comment);
|
||||
});
|
||||
this._printComments(comments);
|
||||
|
||||
return {
|
||||
map: this.map.get(),
|
||||
code: this.buffer.get()
|
||||
@@ -132,6 +138,9 @@ CodeGenerator.prototype.print = function (node, parent, opts) {
|
||||
var needs = n.needsWhitespaceAfter;
|
||||
if (leading) needs = n.needsWhitespaceBefore;
|
||||
lines += needs(node, parent);
|
||||
|
||||
// generated nodes can't add starting file whitespace
|
||||
if (!self.buffer.get()) lines = 0;
|
||||
}
|
||||
|
||||
self.newline(lines);
|
||||
@@ -147,7 +156,7 @@ CodeGenerator.prototype.print = function (node, parent, opts) {
|
||||
|
||||
// only compute if this node needs parens if our parent has been changed
|
||||
// since acorn would've wrapped us in a ParanthesizedExpression
|
||||
var needsParens = parent !== node._parent && n.needsParens(node, parent);
|
||||
var needsParens = n.needsParens(node, parent);
|
||||
if (needsParens) this.push("(");
|
||||
|
||||
this[node.type](node, this.buildPrint(node), parent);
|
||||
@@ -259,6 +268,14 @@ CodeGenerator.prototype._printComments = function (comments) {
|
||||
var self = this;
|
||||
|
||||
_.each(comments, function (comment) {
|
||||
// find the original comment in the ast and set it as displayed
|
||||
_.each(self.ast.comments, function (origComment) {
|
||||
if (origComment.start === comment.start) {
|
||||
origComment._displayed = true;
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
// whitespace before
|
||||
self.newline(self.whitespace.getNewlinesBefore(comment));
|
||||
|
||||
|
||||
@@ -18,12 +18,6 @@ exports.UnaryExpression = function (node, print) {
|
||||
print(node.argument);
|
||||
};
|
||||
|
||||
exports.ParenthesizedExpression = function (node, print) {
|
||||
this.push("(");
|
||||
print(node.expression);
|
||||
this.push(")");
|
||||
};
|
||||
|
||||
exports.UpdateExpression = function (node, print) {
|
||||
if (node.prefix) {
|
||||
this.push(node.operator);
|
||||
@@ -98,8 +92,11 @@ exports.AssignmentExpression = function (node, print) {
|
||||
print(node.right);
|
||||
};
|
||||
|
||||
var SCIENTIFIC_NOTATION = /e/i;
|
||||
|
||||
exports.MemberExpression = function (node, print) {
|
||||
print(node.object);
|
||||
var obj = node.object;
|
||||
print(obj);
|
||||
|
||||
if (node.computed) {
|
||||
this.push("[");
|
||||
@@ -107,7 +104,7 @@ exports.MemberExpression = function (node, print) {
|
||||
this.push("]");
|
||||
} else {
|
||||
// 5..toFixed(2);
|
||||
if (t.isLiteral(node.object) && util.isInteger(node.object.value)) {
|
||||
if (t.isLiteral(obj) && util.isInteger(obj.value) && !SCIENTIFIC_NOTATION.test(obj.value.toString())) {
|
||||
this.push(".");
|
||||
}
|
||||
|
||||
|
||||
3
lib/6to5/generation/generators/flow.js
Normal file
3
lib/6to5/generation/generators/flow.js
Normal file
@@ -0,0 +1,3 @@
|
||||
exports.ClassProperty = function () {
|
||||
throw new Error("not implemented");
|
||||
};
|
||||
@@ -1,7 +1,14 @@
|
||||
var t = require("../../types");
|
||||
var _ = require("lodash");
|
||||
|
||||
exports.ImportSpecifier =
|
||||
exports.ImportSpecifier = function (node, print) {
|
||||
if (node.id && node.id.name === "default") {
|
||||
print(node.name);
|
||||
} else {
|
||||
return exports.ExportSpecifier.apply(this, arguments);
|
||||
}
|
||||
};
|
||||
|
||||
exports.ExportSpecifier = function (node, print) {
|
||||
print(node.id);
|
||||
if (node.name) {
|
||||
@@ -62,7 +69,9 @@ exports.ImportDeclaration = function (node, print) {
|
||||
self.push(", ");
|
||||
}
|
||||
|
||||
if (!spec.default && spec.type !== "ImportBatchSpecifier" && !foundImportSpecifier) {
|
||||
var isDefault = spec.id && spec.id.name === "default";
|
||||
|
||||
if (!isDefault && spec.type !== "ImportBatchSpecifier" && !foundImportSpecifier) {
|
||||
foundImportSpecifier = true;
|
||||
self.push("{ ");
|
||||
}
|
||||
|
||||
@@ -146,7 +146,7 @@ exports.SwitchCase = function (node, print) {
|
||||
this.push("default:");
|
||||
}
|
||||
|
||||
this.space();
|
||||
this.newline();
|
||||
print.sequence(node.consequent, { indent: true });
|
||||
};
|
||||
|
||||
@@ -164,6 +164,12 @@ exports.VariableDeclaration = function (node, print, parent) {
|
||||
}
|
||||
};
|
||||
|
||||
exports.PrivateDeclaration = function (node, print) {
|
||||
this.push("private ");
|
||||
print.join(node.declarations, { separator: ", " });
|
||||
this.semicolon();
|
||||
};
|
||||
|
||||
exports.VariableDeclarator = function (node, print) {
|
||||
if (node.init) {
|
||||
print(node.id);
|
||||
|
||||
@@ -84,8 +84,8 @@ exports.Literal = function (node) {
|
||||
if (type === "string") {
|
||||
val = JSON.stringify(val);
|
||||
|
||||
// escape unicode characters
|
||||
val = val.replace(/[\u007f-\uffff]/g, function (c) {
|
||||
// escape illegal js but valid json unicode characters
|
||||
val = val.replace(/[\u000A\u000D\u2028\u2029]/g, function (c) {
|
||||
return "\\u" + ("0000" + c.charCodeAt(0).toString(16)).slice(-4);
|
||||
});
|
||||
|
||||
|
||||
@@ -92,13 +92,6 @@ exports.YieldExpression = function (node, parent) {
|
||||
t.isYieldExpression(parent);
|
||||
};
|
||||
|
||||
exports.Literal = function (node, parent) {
|
||||
// (1).valueOf()
|
||||
if (_.isNumber(node.value) && t.isMemberExpression(parent) && parent.object === node) {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
exports.ClassExpression = function (node, parent) {
|
||||
return t.isExpressionStatement(parent);
|
||||
};
|
||||
|
||||
@@ -38,7 +38,11 @@ Whitespace.prototype.getNewlinesAfter = function (node) {
|
||||
}
|
||||
});
|
||||
|
||||
return this.getNewlinesBetween(startToken, endToken);
|
||||
if (endToken.type.type === "eof") {
|
||||
return 1;
|
||||
} else {
|
||||
return this.getNewlinesBetween(startToken, endToken);
|
||||
}
|
||||
};
|
||||
|
||||
Whitespace.prototype.getNewlinesBetween = function (startToken, endToken) {
|
||||
|
||||
@@ -3,7 +3,11 @@ var util = require("./util");
|
||||
var fs = require("fs");
|
||||
var _ = require("lodash");
|
||||
|
||||
exports.runtime = require("./runtime");
|
||||
exports.Transformer = require("./transformation/transformer");
|
||||
|
||||
exports.types = require("./types");
|
||||
|
||||
exports.runtime = require("./runtime-generator");
|
||||
|
||||
exports.register = function (opts) {
|
||||
var register = require("./register");
|
||||
|
||||
@@ -1,22 +1,17 @@
|
||||
var t = require("./types");
|
||||
var _ = require("lodash");
|
||||
|
||||
// estraverse
|
||||
|
||||
var estraverse = require("estraverse");
|
||||
_.extend(estraverse.VisitorKeys, t.VISITOR_KEYS);
|
||||
|
||||
// regenerator/recast/ast-types
|
||||
|
||||
var types = require("ast-types");
|
||||
var def = types.Type.def;
|
||||
var or = types.Type.or;
|
||||
|
||||
// Program wrapper
|
||||
def("File")
|
||||
.bases("Node")
|
||||
.build("program")
|
||||
.field("program", def("Program"));
|
||||
|
||||
// Non-standard Acorn type
|
||||
def("ParenthesizedExpression")
|
||||
.bases("Expression")
|
||||
.build("expression")
|
||||
.field("expression", def("Expression"));
|
||||
|
||||
// Acorn - Same as ImportNamespaceSpecifier but `id` is `name`
|
||||
def("ImportBatchSpecifier")
|
||||
.bases("Specifier")
|
||||
@@ -30,6 +25,11 @@ def("VirtualPropertyExpression")
|
||||
.field("object", def("Expression"))
|
||||
.field("property", or(def("Identifier"), def("Expression")));
|
||||
|
||||
def("PrivateDeclaration")
|
||||
.bases("Declaration")
|
||||
.build("declarations")
|
||||
.field("declarations", [def("Identifier")]);
|
||||
|
||||
// Playground
|
||||
def("BindMemberExpression")
|
||||
.bases("Expression")
|
||||
@@ -45,6 +45,3 @@ def("BindFunctionExpression")
|
||||
.field("arguments", [def("Expression")]);
|
||||
|
||||
types.finalize();
|
||||
|
||||
var estraverse = require("estraverse");
|
||||
_.extend(estraverse.VisitorKeys, t.VISITOR_KEYS);
|
||||
|
||||
@@ -1,37 +1,2 @@
|
||||
/* jshint newcap: false */
|
||||
|
||||
var ensureSymbol = function (key) {
|
||||
Symbol[key] = Symbol[key] || Symbol();
|
||||
};
|
||||
|
||||
var ensureProto = function (Constructor, key, val) {
|
||||
var proto = Constructor.prototype;
|
||||
proto[key] = proto[key] || val;
|
||||
};
|
||||
|
||||
//
|
||||
|
||||
if (typeof Symbol === "undefined") {
|
||||
require("es6-symbol/implement");
|
||||
}
|
||||
|
||||
require("es6-shim");
|
||||
require("./transformation/transformers/es6-generators/runtime");
|
||||
|
||||
// Abstract references
|
||||
|
||||
ensureSymbol("referenceGet");
|
||||
ensureSymbol("referenceSet");
|
||||
ensureSymbol("referenceDelete");
|
||||
|
||||
ensureProto(Function, Symbol.referenceGet, function () { return this; });
|
||||
|
||||
ensureProto(Map, Symbol.referenceGet, Map.prototype.get);
|
||||
ensureProto(Map, Symbol.referenceSet, Map.prototype.set);
|
||||
ensureProto(Map, Symbol.referenceDelete, Map.prototype.delete);
|
||||
|
||||
if (global.WeakMap) {
|
||||
ensureProto(WeakMap, Symbol.referenceGet, WeakMap.prototype.get);
|
||||
ensureProto(WeakMap, Symbol.referenceSet, WeakMap.prototype.set);
|
||||
ensureProto(WeakMap, Symbol.referenceDelete, WeakMap.prototype.delete);
|
||||
}
|
||||
require("core-js/shim");
|
||||
require("regenerator/runtime");
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
require("./polyfill");
|
||||
|
||||
var sourceMapSupport = require("source-map-support");
|
||||
var roadrunner = require("roadrunner");
|
||||
var util = require("./util");
|
||||
var to5 = require("./index");
|
||||
var fs = require("fs");
|
||||
@@ -38,15 +39,22 @@ var blacklistTest = function (transformer, code) {
|
||||
};
|
||||
|
||||
blacklistTest("arrayComprehension", "var foo = [for (foo of bar) foo * foo];");
|
||||
//blacklistTest("generatorComprehension", "");
|
||||
blacklistTest("generatorComprehension", "var foo = (for (foo of bar) foo * foo)");
|
||||
blacklistTest("arrowFunctions", "var foo = x => x * x;");
|
||||
blacklistTest("classes", "class Foo {}");
|
||||
blacklistTest("computedPropertyNames", "var foo = { [foo]: bar };");
|
||||
//blacklistTest("constants", "const foo = 0;");
|
||||
blacklistTest("constants", function () {
|
||||
try {
|
||||
new Function("const foo = 'foo';\nfoo = 'wow';");
|
||||
} catch (err) {
|
||||
return; // constants are supported
|
||||
}
|
||||
throw new SyntaxError;
|
||||
});
|
||||
blacklistTest("defaultParamaters", "var foo = function (bar = 0) {};");
|
||||
blacklistTest("destructuring", "var { x, y } = { x: 0, y: 0 };");
|
||||
blacklistTest("forOf", "for (var foo of bar) {}");
|
||||
blacklistTest("generators", "function* foo() {}");
|
||||
blacklistTest("generators", "function* foo() {}\nasync function bar() {}"); // generators/async functions delegated to same transformer
|
||||
blacklistTest("letScoping", "let foo = 0;");
|
||||
blacklistTest("modules", 'import foo from "from";');
|
||||
blacklistTest("propertyMethodAssignment", "{ get foo() {} }");
|
||||
@@ -89,7 +97,6 @@ var loader = function (m, filename) {
|
||||
whitelist: whitelist,
|
||||
blacklist: blacklist,
|
||||
sourceMap: true,
|
||||
modules: "commonInterop",
|
||||
ast: false
|
||||
}, transformOpts));
|
||||
|
||||
@@ -131,6 +138,11 @@ module.exports = function (opts) {
|
||||
|
||||
if (opts.cache) cache = opts.cache;
|
||||
if (opts.cache === false) cache = null;
|
||||
if (opts.cache === true) {
|
||||
roadrunner.load();
|
||||
roadrunner.setup();
|
||||
cache = roadrunner.get("6to5");
|
||||
}
|
||||
|
||||
_.extend(transformOpts, opts);
|
||||
};
|
||||
|
||||
@@ -1,2 +0,0 @@
|
||||
SUPER_NAME.apply(this, arguments);
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
(function () {
|
||||
var CLASS_NAME = function () {
|
||||
|
||||
};
|
||||
|
||||
return CLASS_NAME;
|
||||
})()
|
||||
@@ -1 +0,0 @@
|
||||
exports.VARIABLE_NAME = OBJECT.KEY;
|
||||
@@ -1 +0,0 @@
|
||||
exports.default = VALUE;
|
||||
@@ -1 +0,0 @@
|
||||
System.register(MODULE_NAME, MODULE_DEPENDENCIES, MODULE_BODY);
|
||||
@@ -1,4 +0,0 @@
|
||||
(function (strings, raw) {
|
||||
return Object.defineProperties(strings, { raw: { value: raw } });
|
||||
});
|
||||
|
||||
204
lib/6to5/transformation/modules/_default.js
Normal file
204
lib/6to5/transformation/modules/_default.js
Normal file
@@ -0,0 +1,204 @@
|
||||
module.exports = DefaultFormatter;
|
||||
|
||||
var traverse = require("../../traverse");
|
||||
var util = require("../../util");
|
||||
var t = require("../../types");
|
||||
var _ = require("lodash");
|
||||
|
||||
function DefaultFormatter(file) {
|
||||
this.file = file;
|
||||
|
||||
this.localExports = this.getLocalExports();
|
||||
this.remapAssignments();
|
||||
}
|
||||
|
||||
DefaultFormatter.prototype.getLocalExports = function () {
|
||||
var localExports = {};
|
||||
|
||||
traverse(this.file.ast, {
|
||||
enter: function (node) {
|
||||
var declar = node && node.declaration;
|
||||
if (t.isExportDeclaration(node) && declar && t.isStatement(declar)) {
|
||||
_.extend(localExports, t.getIds(declar, true));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return localExports;
|
||||
};
|
||||
|
||||
DefaultFormatter.prototype.remapExportAssignment = function (node) {
|
||||
return t.assignmentExpression(
|
||||
"=",
|
||||
node.left,
|
||||
t.assignmentExpression(
|
||||
node.operator,
|
||||
t.memberExpression(t.identifier("exports"), node.left),
|
||||
node.right
|
||||
)
|
||||
);
|
||||
};
|
||||
|
||||
DefaultFormatter.prototype.remapAssignments = function () {
|
||||
var localExports = this.localExports;
|
||||
var self = this;
|
||||
|
||||
var isLocalReference = function (node, scope) {
|
||||
var name = node.name;
|
||||
return t.isIdentifier(node) && localExports[name] && localExports[name] === scope.get(name, true);
|
||||
};
|
||||
|
||||
traverse(this.file.ast, {
|
||||
enter: function (node, parent, scope) {
|
||||
if (t.isUpdateExpression(node) && isLocalReference(node.argument, scope)) {
|
||||
this.stop();
|
||||
|
||||
// expand to long file assignment expression
|
||||
var assign = t.assignmentExpression(node.operator[0] + "=", node.argument, t.literal(1));
|
||||
|
||||
// remap this assignment expression
|
||||
var remapped = self.remapExportAssignment(assign);
|
||||
|
||||
// we don't need to change the result
|
||||
if (t.isExpressionStatement(parent) || node.prefix) {
|
||||
return remapped;
|
||||
}
|
||||
|
||||
var nodes = [];
|
||||
nodes.push(remapped);
|
||||
|
||||
var operator;
|
||||
if (node.operator === "--") {
|
||||
operator = "+";
|
||||
} else { // "++"
|
||||
operator = "-";
|
||||
}
|
||||
nodes.push(t.binaryExpression(operator, node.argument, t.literal(1)));
|
||||
|
||||
return t.sequenceExpression(nodes);
|
||||
}
|
||||
|
||||
if (t.isAssignmentExpression(node) && isLocalReference(node.left, scope)) {
|
||||
this.stop();
|
||||
return self.remapExportAssignment(node);
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
DefaultFormatter.prototype.getModuleName = function () {
|
||||
var opts = this.file.opts;
|
||||
var filenameRelative = opts.filenameRelative;
|
||||
var moduleName = "";
|
||||
|
||||
if (opts.moduleRoot) {
|
||||
moduleName = opts.moduleRoot + "/";
|
||||
}
|
||||
|
||||
if (!opts.filenameRelative) {
|
||||
return moduleName + opts.filename.replace(/^\//, "");
|
||||
}
|
||||
|
||||
if (opts.sourceRoot) {
|
||||
// remove sourceRoot from filename
|
||||
var sourceRootRegEx = new RegExp("^" + opts.sourceRoot + "\/?");
|
||||
filenameRelative = filenameRelative.replace(sourceRootRegEx, "");
|
||||
}
|
||||
|
||||
// remove extension
|
||||
filenameRelative = filenameRelative.replace(/\.(.*?)$/, "");
|
||||
|
||||
moduleName += filenameRelative;
|
||||
|
||||
return moduleName;
|
||||
};
|
||||
|
||||
DefaultFormatter.prototype._pushStatement = function (ref, nodes) {
|
||||
if (t.isClass(ref) || t.isFunction(ref)) {
|
||||
if (ref.id) {
|
||||
nodes.push(t.toStatement(ref));
|
||||
ref = ref.id;
|
||||
}
|
||||
}
|
||||
|
||||
return ref;
|
||||
};
|
||||
|
||||
DefaultFormatter.prototype._hoistExport = function (declar, assign) {
|
||||
if (t.isFunctionDeclaration(declar)) {
|
||||
assign._blockHoist = true;
|
||||
}
|
||||
|
||||
return assign;
|
||||
};
|
||||
|
||||
DefaultFormatter.prototype._exportSpecifier = function (getRef, specifier, node, nodes) {
|
||||
var inherits = false;
|
||||
if (node.specifiers.length === 1) inherits = node;
|
||||
|
||||
if (node.source) {
|
||||
if (t.isExportBatchSpecifier(specifier)) {
|
||||
// export * from "foo";
|
||||
nodes.push(this._exportsWildcard(getRef()));
|
||||
} else {
|
||||
// export { foo } from "test";
|
||||
nodes.push(this._exportsAssign(
|
||||
t.getSpecifierName(specifier),
|
||||
t.memberExpression(getRef(), specifier.id)
|
||||
));
|
||||
}
|
||||
} else {
|
||||
// export { foo };
|
||||
nodes.push(this._exportsAssign(t.getSpecifierName(specifier), specifier.id));
|
||||
}
|
||||
};
|
||||
|
||||
DefaultFormatter.prototype._exportsWildcard = function (objectIdentifier) {
|
||||
return util.template("exports-wildcard", {
|
||||
OBJECT: objectIdentifier
|
||||
}, true);
|
||||
};
|
||||
|
||||
DefaultFormatter.prototype._exportsAssign = function (id, init) {
|
||||
return util.template("exports-assign", {
|
||||
VALUE: init,
|
||||
KEY: id
|
||||
}, true);
|
||||
};
|
||||
|
||||
DefaultFormatter.prototype.exportDeclaration = function (node, nodes) {
|
||||
var declar = node.declaration;
|
||||
|
||||
var id = declar.id;
|
||||
|
||||
if (node.default) {
|
||||
id = t.identifier("default");
|
||||
}
|
||||
|
||||
var assign;
|
||||
|
||||
if (t.isVariableDeclaration(declar)) {
|
||||
for (var i in declar.declarations) {
|
||||
var decl = declar.declarations[i];
|
||||
|
||||
decl.init = this._exportsAssign(decl.id, decl.init).expression;
|
||||
|
||||
var newDeclar = t.variableDeclaration(declar.kind, [decl]);
|
||||
if (i === "0") t.inherits(newDeclar, declar);
|
||||
nodes.push(newDeclar);
|
||||
}
|
||||
} else {
|
||||
var ref = declar;
|
||||
|
||||
if (t.isFunctionDeclaration(declar) || t.isClassDeclaration(declar)) {
|
||||
ref = declar.id;
|
||||
nodes.push(declar);
|
||||
}
|
||||
|
||||
assign = this._exportsAssign(id, ref);
|
||||
|
||||
nodes.push(assign);
|
||||
|
||||
this._hoistExport(declar, assign);
|
||||
}
|
||||
};
|
||||
@@ -1,16 +1,28 @@
|
||||
module.exports = AMDFormatter;
|
||||
|
||||
var CommonJSFormatter = require("./common");
|
||||
var util = require("../../util");
|
||||
var t = require("../../types");
|
||||
var _ = require("lodash");
|
||||
var DefaultFormatter = require("./_default");
|
||||
var util = require("../../util");
|
||||
var t = require("../../types");
|
||||
var _ = require("lodash");
|
||||
|
||||
function AMDFormatter(file) {
|
||||
this.file = file;
|
||||
function AMDFormatter() {
|
||||
DefaultFormatter.apply(this, arguments);
|
||||
this.ids = {};
|
||||
}
|
||||
|
||||
util.inherits(AMDFormatter, CommonJSFormatter);
|
||||
util.inherits(AMDFormatter, DefaultFormatter);
|
||||
|
||||
AMDFormatter.prototype.buildDependencyLiterals = function () {
|
||||
var names = [];
|
||||
for (var name in this.ids) {
|
||||
names.push(t.literal(name));
|
||||
}
|
||||
return names;
|
||||
};
|
||||
|
||||
/**
|
||||
* Wrap the entire body in a `define` wrapper.
|
||||
*/
|
||||
|
||||
AMDFormatter.prototype.transform = function (ast) {
|
||||
var program = ast.program;
|
||||
@@ -18,10 +30,7 @@ AMDFormatter.prototype.transform = function (ast) {
|
||||
|
||||
// build an array of module names
|
||||
|
||||
var names = [t.literal("exports")];
|
||||
_.each(this.ids, function (id, name) {
|
||||
names.push(t.literal(name));
|
||||
});
|
||||
var names = [t.literal("exports")].concat(this.buildDependencyLiterals());
|
||||
names = t.arrayExpression(names);
|
||||
|
||||
// build up define container
|
||||
@@ -40,9 +49,14 @@ AMDFormatter.prototype.transform = function (ast) {
|
||||
program.body = [t.expressionStatement(call)];
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the AMD module name that we'll prepend to the wrapper
|
||||
* to define this module
|
||||
*/
|
||||
|
||||
AMDFormatter.prototype.getModuleName = function () {
|
||||
if (this.file.opts.amdModuleIds) {
|
||||
return CommonJSFormatter.prototype.getModuleName.apply(this, arguments);
|
||||
if (this.file.opts.moduleIds) {
|
||||
return DefaultFormatter.prototype.getModuleName.apply(this, arguments);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
@@ -59,27 +73,22 @@ AMDFormatter.prototype._push = function (node) {
|
||||
}
|
||||
};
|
||||
|
||||
AMDFormatter.prototype.import = function (node) {
|
||||
AMDFormatter.prototype.importDeclaration = function (node) {
|
||||
this._push(node);
|
||||
};
|
||||
|
||||
AMDFormatter.prototype.importSpecifier = function (specifier, node, nodes) {
|
||||
var key = t.getSpecifierName(specifier);
|
||||
var id = specifier.id;
|
||||
|
||||
// import foo from "foo";
|
||||
if (specifier.default) {
|
||||
id = t.identifier("default");
|
||||
}
|
||||
|
||||
var ref;
|
||||
var ref = this._push(node);
|
||||
|
||||
if (t.isImportBatchSpecifier(specifier)) {
|
||||
// import * as bar from "foo";
|
||||
ref = this._push(node);
|
||||
} else {
|
||||
} else if (t.isSpecifierDefault(specifier) && !this.noInteropRequire) {
|
||||
// import foo from "foo";
|
||||
ref = t.memberExpression(this._push(node), id, false);
|
||||
ref = t.callExpression(this.file.addDeclaration("interop-require"), [ref]);
|
||||
} else {
|
||||
// import {foo} from "foo";
|
||||
ref = t.memberExpression(ref, specifier.id, false);
|
||||
}
|
||||
|
||||
nodes.push(t.variableDeclaration("var", [
|
||||
|
||||
@@ -1,56 +0,0 @@
|
||||
module.exports = CommonJSInteropFormatter;
|
||||
|
||||
var CommonJSFormatter = require("./common");
|
||||
var util = require("../../util");
|
||||
var t = require("../../types");
|
||||
var _ = require("lodash");
|
||||
|
||||
function CommonJSInteropFormatter(file) {
|
||||
CommonJSFormatter.apply(this, arguments);
|
||||
|
||||
var has = false;
|
||||
_.each(file.ast.program.body, function (node) {
|
||||
if (t.isExportDeclaration(node) && !node.default) has = true;
|
||||
});
|
||||
this.has = has;
|
||||
}
|
||||
|
||||
util.inherits(CommonJSInteropFormatter, CommonJSFormatter);
|
||||
|
||||
CommonJSInteropFormatter.prototype.importSpecifier = function (specifier, node, nodes) {
|
||||
var variableName = t.getSpecifierName(specifier);
|
||||
|
||||
// import foo from "foo";
|
||||
if (specifier.default) {
|
||||
nodes.push(t.variableDeclaration("var", [
|
||||
t.variableDeclarator(variableName,
|
||||
t.callExpression(this.file.addDeclaration("interop-require"), [util.template("require", {
|
||||
MODULE_NAME: node.source.raw
|
||||
})])
|
||||
)
|
||||
]));
|
||||
} else {
|
||||
CommonJSFormatter.prototype.importSpecifier.apply(this, arguments);
|
||||
}
|
||||
};
|
||||
|
||||
CommonJSInteropFormatter.prototype.export = function (node, nodes) {
|
||||
if (node.default && !this.has) {
|
||||
var declar = node.declaration;
|
||||
|
||||
// module.exports = VALUE;
|
||||
var assign = util.template("exports-default-module", {
|
||||
VALUE: this._pushStatement(declar, nodes)
|
||||
}, true);
|
||||
|
||||
// hoist to the top if this default is a function
|
||||
nodes.push(this._hoistExport(declar, assign));
|
||||
return;
|
||||
}
|
||||
|
||||
CommonJSFormatter.prototype.export.apply(this, arguments);
|
||||
};
|
||||
|
||||
CommonJSInteropFormatter.prototype.exportSpecifier = function () {
|
||||
CommonJSFormatter.prototype.exportSpecifier.apply(this, arguments);
|
||||
};
|
||||
@@ -1,161 +1,77 @@
|
||||
module.exports = CommonJSFormatter;
|
||||
|
||||
var util = require("../../util");
|
||||
var t = require("../../types");
|
||||
var DefaultFormatter = require("./_default");
|
||||
var traverse = require("../../traverse");
|
||||
var util = require("../../util");
|
||||
var t = require("../../types");
|
||||
|
||||
function CommonJSFormatter(file) {
|
||||
this.file = file;
|
||||
DefaultFormatter.apply(this, arguments);
|
||||
|
||||
var hasNonDefaultExports = false;
|
||||
traverse(file.ast, {
|
||||
enter: function (node) {
|
||||
if (t.isExportDeclaration(node) && !node.default) hasNonDefaultExports = true;
|
||||
}
|
||||
});
|
||||
this.hasNonDefaultExports = hasNonDefaultExports;
|
||||
}
|
||||
|
||||
CommonJSFormatter.prototype.getModuleName = function () {
|
||||
var opts = this.file.opts;
|
||||
var filenameRelative = opts.filenameRelative;
|
||||
var moduleName = "";
|
||||
|
||||
if (opts.moduleRoot) {
|
||||
moduleName = opts.moduleRoot + "/";
|
||||
}
|
||||
|
||||
if (!opts.filenameRelative) {
|
||||
return moduleName + opts.filename.replace(/^\//, "");
|
||||
}
|
||||
|
||||
if (opts.sourceRoot) {
|
||||
// remove sourceRoot from filename
|
||||
var sourceRootRegEx = new RegExp("^" + opts.sourceRoot + "\/?");
|
||||
filenameRelative = filenameRelative.replace(sourceRootRegEx, "");
|
||||
}
|
||||
|
||||
// remove extension
|
||||
filenameRelative = filenameRelative.replace(/\.(.*?)$/, "");
|
||||
|
||||
moduleName += filenameRelative;
|
||||
|
||||
return moduleName;
|
||||
};
|
||||
|
||||
CommonJSFormatter.prototype.import = function (node, nodes) {
|
||||
// import "foo";
|
||||
nodes.push(util.template("require", {
|
||||
//inherits: node,
|
||||
|
||||
MODULE_NAME: node.source.raw
|
||||
}, true));
|
||||
};
|
||||
util.inherits(CommonJSFormatter, DefaultFormatter);
|
||||
|
||||
CommonJSFormatter.prototype.importSpecifier = function (specifier, node, nodes) {
|
||||
var variableName = t.getSpecifierName(specifier);
|
||||
|
||||
// import foo from "foo";
|
||||
if (specifier.default) {
|
||||
specifier.id = t.identifier("default");
|
||||
if (t.isSpecifierDefault(specifier)) {
|
||||
nodes.push(t.variableDeclaration("var", [
|
||||
t.variableDeclarator(variableName,
|
||||
t.callExpression(this.file.addDeclaration("interop-require"), [util.template("require", {
|
||||
MODULE_NAME: node.source
|
||||
})])
|
||||
)
|
||||
]));
|
||||
} else {
|
||||
// import foo from "foo";
|
||||
|
||||
var templateName = "require-assign";
|
||||
|
||||
// import * as bar from "foo";
|
||||
if (specifier.type !== "ImportBatchSpecifier") templateName += "-key";
|
||||
|
||||
nodes.push(util.template(templateName, {
|
||||
VARIABLE_NAME: variableName,
|
||||
MODULE_NAME: node.source,
|
||||
KEY: specifier.id
|
||||
}));
|
||||
}
|
||||
|
||||
var templateName = "require-assign";
|
||||
|
||||
// import * as bar from "foo";
|
||||
if (specifier.type !== "ImportBatchSpecifier") templateName += "-key";
|
||||
|
||||
nodes.push(util.template(templateName, {
|
||||
//inherits: node.specifiers.length === 1 && node,
|
||||
|
||||
VARIABLE_NAME: variableName,
|
||||
MODULE_NAME: node.source.raw,
|
||||
KEY: specifier.id
|
||||
}));
|
||||
};
|
||||
|
||||
CommonJSFormatter.prototype._hoistExport = function (declar, assign) {
|
||||
if (t.isFunctionDeclaration(declar)) {
|
||||
assign._blockHoist = true;
|
||||
}
|
||||
|
||||
return assign;
|
||||
CommonJSFormatter.prototype.importDeclaration = function (node, nodes) {
|
||||
// import "foo";
|
||||
nodes.push(util.template("require", {
|
||||
MODULE_NAME: node.source
|
||||
}, true));
|
||||
};
|
||||
|
||||
CommonJSFormatter.prototype._pushStatement = function (ref, nodes) {
|
||||
if (t.isClass(ref) || t.isFunction(ref)) {
|
||||
if (ref.id) {
|
||||
nodes.push(t.toStatement(ref));
|
||||
ref = ref.id;
|
||||
}
|
||||
}
|
||||
return ref;
|
||||
};
|
||||
|
||||
CommonJSFormatter.prototype.export = function (node, nodes) {
|
||||
var declar = node.declaration;
|
||||
|
||||
CommonJSFormatter.prototype.exportDeclaration = function (node, nodes) {
|
||||
if (node.default) {
|
||||
nodes.push(util.template("exports-default", {
|
||||
//inherits: node,
|
||||
var declar = node.declaration;
|
||||
|
||||
// module.exports = VALUE;
|
||||
var templateName = "exports-default-module";
|
||||
|
||||
// exports = module.exports = VALUE;
|
||||
if (this.hasNonDefaultExports) templateName = "exports-default-module-override";
|
||||
|
||||
var assign = util.template(templateName, {
|
||||
VALUE: this._pushStatement(declar, nodes)
|
||||
}, true));
|
||||
}, true);
|
||||
|
||||
// hoist to the top if this default is a function
|
||||
nodes.push(this._hoistExport(declar, assign));
|
||||
} else {
|
||||
var assign;
|
||||
|
||||
if (t.isVariableDeclaration(declar)) {
|
||||
var decl = declar.declarations[0];
|
||||
|
||||
if (decl.init) {
|
||||
decl.init = util.template("exports-assign", {
|
||||
//inherits: node,
|
||||
|
||||
VALUE: decl.init,
|
||||
KEY: decl.id
|
||||
});
|
||||
}
|
||||
|
||||
nodes.push(declar);
|
||||
} else {
|
||||
assign = util.template("exports-assign", {
|
||||
//inherits: node,
|
||||
|
||||
VALUE: declar.id,
|
||||
KEY: declar.id
|
||||
}, true);
|
||||
|
||||
nodes.push(t.toStatement(declar));
|
||||
nodes.push(assign);
|
||||
|
||||
this._hoistExport(declar, assign);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
CommonJSFormatter.prototype._exportSpecifier = function (getRef, specifier, node, nodes) {
|
||||
var variableName = t.getSpecifierName(specifier);
|
||||
|
||||
var inherits = false;
|
||||
if (node.specifiers.length === 1) inherits = node;
|
||||
|
||||
if (node.source) {
|
||||
if (t.isExportBatchSpecifier(specifier)) {
|
||||
// export * from "foo";
|
||||
nodes.push(util.template("exports-wildcard", {
|
||||
//inherits: inherits,
|
||||
|
||||
OBJECT: getRef()
|
||||
}, true));
|
||||
} else {
|
||||
// export { foo } from "test";
|
||||
nodes.push(util.template("exports-assign-key", {
|
||||
//inherits: inherits,
|
||||
|
||||
VARIABLE_NAME: variableName.name,
|
||||
OBJECT: getRef(),
|
||||
KEY: specifier.id
|
||||
}, true));
|
||||
}
|
||||
} else {
|
||||
// export { foo };
|
||||
nodes.push(util.template("exports-assign", {
|
||||
//inherits: inherits,
|
||||
|
||||
VALUE: specifier.id,
|
||||
KEY: variableName
|
||||
}, true));
|
||||
DefaultFormatter.prototype.exportDeclaration.apply(this, arguments);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -6,19 +6,13 @@ function IgnoreFormatter() {
|
||||
|
||||
}
|
||||
|
||||
IgnoreFormatter.prototype.import = function () {
|
||||
|
||||
};
|
||||
|
||||
IgnoreFormatter.prototype.importSpecifier = function () {
|
||||
|
||||
};
|
||||
|
||||
IgnoreFormatter.prototype.export = function (node, nodes) {
|
||||
IgnoreFormatter.prototype.exportDeclaration = function (node, nodes) {
|
||||
var declar = t.toStatement(node.declaration, true);
|
||||
if (declar) nodes.push(t.inherits(declar, node));
|
||||
};
|
||||
|
||||
IgnoreFormatter.prototype.importDeclaration =
|
||||
IgnoreFormatter.prototype.importSpecifier =
|
||||
IgnoreFormatter.prototype.exportSpecifier = function () {
|
||||
|
||||
};
|
||||
|
||||
@@ -1,202 +1,102 @@
|
||||
module.exports = SystemFormatter;
|
||||
|
||||
var util = require("../../util");
|
||||
var t = require("../../types");
|
||||
var _ = require("lodash");
|
||||
|
||||
var SETTER_MODULE_NAMESPACE = t.identifier("m");
|
||||
var DEFAULT_IDENTIFIER = t.identifier("default");
|
||||
var NULL_SETTER = t.literal(null);
|
||||
var AMDFormatter = require("./amd");
|
||||
var traverse = require("../../traverse");
|
||||
var util = require("../../util");
|
||||
var t = require("../../types");
|
||||
var _ = require("lodash");
|
||||
|
||||
function SystemFormatter(file) {
|
||||
this.exportedStatements = [];
|
||||
this.importedModule = {};
|
||||
this.exportIdentifier = file.generateUidIdentifier("export");
|
||||
this.noInteropRequire = true;
|
||||
|
||||
this.exportIdentifier = file.generateUidIdentifier("export");
|
||||
this.file = file;
|
||||
AMDFormatter.apply(this, arguments);
|
||||
}
|
||||
|
||||
util.inherits(SystemFormatter, AMDFormatter);
|
||||
|
||||
SystemFormatter.prototype._exportsWildcard = function (objectIdentifier) {
|
||||
var leftIdentifier = t.identifier("i");
|
||||
var valIdentifier = t.memberExpression(objectIdentifier, leftIdentifier, true);
|
||||
|
||||
var left = t.variableDeclaration("var", [
|
||||
t.variableDeclarator(leftIdentifier)
|
||||
]);
|
||||
|
||||
var right = objectIdentifier;
|
||||
|
||||
var block = t.blockStatement([
|
||||
this.buildExportCall(leftIdentifier, valIdentifier)
|
||||
]);
|
||||
|
||||
return t.forInStatement(left, right, block);
|
||||
};
|
||||
|
||||
SystemFormatter.prototype._exportsAssign = function (id, init) {
|
||||
return this.buildExportCall(t.literal(id.name), init, true);
|
||||
};
|
||||
|
||||
SystemFormatter.prototype.remapExportAssignment = function (node) {
|
||||
return this.buildExportCall(t.literal(node.left.name), node);
|
||||
};
|
||||
|
||||
SystemFormatter.prototype.buildExportCall = function (id, init, isStatement) {
|
||||
var call = t.callExpression(this.exportIdentifier, [id, init]);
|
||||
if (isStatement) {
|
||||
return t.expressionStatement(call);
|
||||
} else {
|
||||
return call;
|
||||
}
|
||||
};
|
||||
|
||||
SystemFormatter.prototype.buildRunnerSetters = function () {
|
||||
return t.arrayExpression(_.map(this.ids, function (uid) {
|
||||
var moduleIdentifier = t.identifier("m");
|
||||
|
||||
return t.functionExpression(null, [moduleIdentifier], t.blockStatement([
|
||||
t.assignmentExpression("=", uid, moduleIdentifier)
|
||||
]));
|
||||
}));
|
||||
};
|
||||
|
||||
SystemFormatter.prototype.transform = function (ast) {
|
||||
var program = ast.program;
|
||||
var body = program.body;
|
||||
|
||||
// extract the module name
|
||||
var moduleName = this.file.opts.filename
|
||||
.replace(/^.*\//, "").replace(/\..*$/, "");
|
||||
var moduleName = this.getModuleName();
|
||||
var moduleNameLiteral = t.literal(moduleName);
|
||||
|
||||
// build an array of module names
|
||||
var dependencies = Object.keys(this.importedModule).map(t.literal);
|
||||
var runner = util.template("system", {
|
||||
MODULE_NAME: moduleNameLiteral,
|
||||
MODULE_DEPENDENCIES: t.arrayExpression(this.buildDependencyLiterals()),
|
||||
EXPORT_IDENTIFIER: this.exportIdentifier,
|
||||
SETTERS: this.buildRunnerSetters(),
|
||||
EXECUTE: t.functionExpression(null, [], t.blockStatement(program.body))
|
||||
}, true);
|
||||
|
||||
// generate the __moduleName variable
|
||||
var moduleNameVariableNode = t.variableDeclaration("var", [
|
||||
t.variableDeclarator(
|
||||
t.identifier("__moduleName"),
|
||||
t.literal(moduleName)
|
||||
)
|
||||
]);
|
||||
body.splice(1, 0, moduleNameVariableNode);
|
||||
var handlerBody = runner.expression.arguments[2].body.body;
|
||||
if (!moduleName) runner.expression.arguments.shift();
|
||||
|
||||
// generate an array of import variables
|
||||
var returnStatement = handlerBody.pop();
|
||||
|
||||
var declaredSetters = _(this.importedModule)
|
||||
.map()
|
||||
.flatten()
|
||||
.pluck("variableName")
|
||||
.pluck("name")
|
||||
.uniq()
|
||||
.map(t.identifier)
|
||||
.map(function (name) {
|
||||
return t.variableDeclarator(name);
|
||||
})
|
||||
.value();
|
||||
// hoist up function declarations for circular references
|
||||
traverse(program, {
|
||||
enter: function (node) {
|
||||
if (t.isFunction(node)) this.stop();
|
||||
|
||||
if (declaredSetters.length) {
|
||||
body.splice(2, 0, t.variableDeclaration("var", declaredSetters));
|
||||
}
|
||||
|
||||
// generate the execute function expression
|
||||
var executeFunctionExpression = t.functionExpression(
|
||||
null, [], t.blockStatement(this.exportedStatements)
|
||||
);
|
||||
|
||||
// generate the execute function expression
|
||||
var settersArrayExpression = t.arrayExpression(this._buildSetters());
|
||||
|
||||
// generate the return statement
|
||||
var moduleReturnStatement = t.returnStatement(t.objectExpression([
|
||||
t.property("init", t.identifier("setters"), settersArrayExpression),
|
||||
t.property("init", t.identifier("execute"), executeFunctionExpression)
|
||||
]));
|
||||
body.push(moduleReturnStatement);
|
||||
|
||||
// runner
|
||||
var runner = util.template("register", {
|
||||
MODULE_NAME: t.literal(moduleName),
|
||||
MODULE_DEPENDENCIES: t.arrayExpression(dependencies),
|
||||
MODULE_BODY: t.functionExpression(
|
||||
null,
|
||||
[this.exportIdentifier],
|
||||
t.blockStatement(body)
|
||||
)
|
||||
});
|
||||
|
||||
program.body = [t.expressionStatement(runner)];
|
||||
};
|
||||
|
||||
SystemFormatter.prototype._buildSetters = function () {
|
||||
// generate setters array expression elements
|
||||
return _.map(this.importedModule, function (specs) {
|
||||
if (!specs.length) {
|
||||
return NULL_SETTER;
|
||||
}
|
||||
|
||||
var expressionStatements = _.map(specs, function (spec) {
|
||||
var right = SETTER_MODULE_NAMESPACE;
|
||||
if (!spec.isBatch) {
|
||||
right = t.memberExpression(right, spec.key);
|
||||
if (t.isFunctionDeclaration(node) || node._blockHoist) {
|
||||
handlerBody.push(node);
|
||||
this.remove();
|
||||
}
|
||||
|
||||
return t.expressionStatement(
|
||||
t.assignmentExpression("=", spec.variableName, right
|
||||
)
|
||||
);
|
||||
});
|
||||
|
||||
return t.functionExpression(
|
||||
null, [SETTER_MODULE_NAMESPACE], t.blockStatement(expressionStatements)
|
||||
);
|
||||
});
|
||||
};
|
||||
|
||||
SystemFormatter.prototype.import = function (node) {
|
||||
var MODULE_NAME = node.source.value;
|
||||
this.importedModule[MODULE_NAME] = this.importedModule[MODULE_NAME] || [];
|
||||
};
|
||||
|
||||
SystemFormatter.prototype.importSpecifier = function (specifier, node) {
|
||||
var variableName = t.getSpecifierName(specifier);
|
||||
|
||||
// import foo from "foo";
|
||||
if (specifier.default) {
|
||||
specifier.id = DEFAULT_IDENTIFIER;
|
||||
}
|
||||
|
||||
var MODULE_NAME = node.source.value;
|
||||
|
||||
this.importedModule[MODULE_NAME] = this.importedModule[MODULE_NAME] || [];
|
||||
|
||||
this.importedModule[MODULE_NAME].push({
|
||||
variableName: variableName,
|
||||
isBatch: specifier.type === "ImportBatchSpecifier",
|
||||
key: specifier.id
|
||||
});
|
||||
};
|
||||
|
||||
SystemFormatter.prototype._export = function (name, identifier) {
|
||||
this.exportedStatements.push(t.expressionStatement(
|
||||
t.callExpression(this.exportIdentifier, [t.literal(name), identifier])
|
||||
));
|
||||
};
|
||||
|
||||
SystemFormatter.prototype.export = function (node, nodes) {
|
||||
var declar = node.declaration;
|
||||
var variableName, identifier;
|
||||
|
||||
if (node.default) {
|
||||
// export default foo
|
||||
variableName = DEFAULT_IDENTIFIER.name;
|
||||
if (t.isClass(declar) || t.isFunction(declar)) {
|
||||
if (!declar.id) {
|
||||
declar.id = this.file.generateUidIdentifier("anonymous");
|
||||
}
|
||||
|
||||
nodes.push(t.toStatement(declar));
|
||||
declar = declar.id;
|
||||
}
|
||||
});
|
||||
|
||||
identifier = declar;
|
||||
} else if (t.isVariableDeclaration(declar)) {
|
||||
// export var foo
|
||||
variableName = declar.declarations[0].id.name;
|
||||
identifier = declar.declarations[0].id;
|
||||
|
||||
nodes.push(declar);
|
||||
} else {
|
||||
// export function foo () {}
|
||||
variableName = declar.id.name;
|
||||
identifier = declar.id;
|
||||
|
||||
nodes.push(declar);
|
||||
if (!_.isEmpty(this.ids)) {
|
||||
handlerBody.push(t.variableDeclaration("var", _.map(this.ids, function (uid) {
|
||||
return t.variableDeclarator(uid);
|
||||
})));
|
||||
}
|
||||
|
||||
this._export(variableName, identifier);
|
||||
};
|
||||
|
||||
SystemFormatter.prototype.exportSpecifier = function (specifier, node) {
|
||||
var variableName = t.getSpecifierName(specifier);
|
||||
|
||||
if (node.source) {
|
||||
if (t.isExportBatchSpecifier(specifier)) {
|
||||
// export * from "foo";
|
||||
var exportIdentifier = t.identifier("exports");
|
||||
this.exportedStatements.push(
|
||||
t.variableDeclaration("var", [
|
||||
t.variableDeclarator(exportIdentifier, this.exportIdentifier)
|
||||
])
|
||||
);
|
||||
|
||||
this.exportedStatements.push(util.template("exports-wildcard", {
|
||||
OBJECT: t.identifier(node.source.value)
|
||||
}, true));
|
||||
} else {
|
||||
// export { foo } from "test";
|
||||
this._export(variableName.name, t.memberExpression(
|
||||
t.identifier(node.source.value),
|
||||
specifier.id
|
||||
));
|
||||
}
|
||||
} else {
|
||||
// export { foo };
|
||||
this._export(variableName.name, specifier.id);
|
||||
}
|
||||
handlerBody.push(returnStatement);
|
||||
|
||||
program.body = [runner];
|
||||
};
|
||||
|
||||
@@ -18,9 +18,9 @@ UMDFormatter.prototype.transform = function (ast) {
|
||||
// build an array of module names
|
||||
|
||||
var names = [];
|
||||
_.each(this.ids, function (id, name) {
|
||||
for (var name in this.ids) {
|
||||
names.push(t.literal(name));
|
||||
});
|
||||
}
|
||||
|
||||
// factory
|
||||
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
if (SUPER_NAME) {
|
||||
SUPER_NAME.apply(this, arguments);
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
exports = module.exports = VALUE;
|
||||
@@ -1,5 +1,5 @@
|
||||
(function (child, parent) {
|
||||
child.prototype = Object.create(parent.prototype, {
|
||||
child.prototype = Object.create(parent && parent.prototype, {
|
||||
constructor: {
|
||||
value: child,
|
||||
enumerable: false,
|
||||
@@ -7,5 +7,5 @@
|
||||
configurable: true
|
||||
}
|
||||
});
|
||||
child.__proto__ = parent;
|
||||
if (parent) child.__proto__ = parent;
|
||||
})
|
||||
12
lib/6to5/transformation/templates/sliced-to-array.js
Normal file
12
lib/6to5/transformation/templates/sliced-to-array.js
Normal file
@@ -0,0 +1,12 @@
|
||||
(function (arr, i) {
|
||||
if (Array.isArray(arr)) {
|
||||
return arr;
|
||||
} else {
|
||||
var _arr = [];
|
||||
for (var _iterator = arr[Symbol.iterator](), _step; !(_step = _iterator.next()).done;) {
|
||||
_arr.push(_step.value);
|
||||
if (i && _arr.length === i) break;
|
||||
}
|
||||
return _arr;
|
||||
}
|
||||
});
|
||||
6
lib/6to5/transformation/templates/system.js
Normal file
6
lib/6to5/transformation/templates/system.js
Normal file
@@ -0,0 +1,6 @@
|
||||
System.register(MODULE_NAME, MODULE_DEPENDENCIES, function (EXPORT_IDENTIFIER) {
|
||||
return {
|
||||
setters: SETTERS,
|
||||
execute: EXECUTE
|
||||
};
|
||||
});
|
||||
@@ -0,0 +1,6 @@
|
||||
(function (strings, raw) {
|
||||
return Object.freeze(Object.defineProperties(strings, {
|
||||
raw: { value: Object.freeze(raw) }
|
||||
}));
|
||||
});
|
||||
|
||||
@@ -2,6 +2,7 @@ module.exports = transform;
|
||||
|
||||
var Transformer = require("./transformer");
|
||||
var File = require("../file");
|
||||
var util = require("../util");
|
||||
var _ = require("lodash");
|
||||
|
||||
function transform(code, opts) {
|
||||
@@ -9,31 +10,46 @@ function transform(code, opts) {
|
||||
return file.parse(code);
|
||||
}
|
||||
|
||||
transform.fromAst = function (ast, code, opts) {
|
||||
ast = util.normaliseAst(ast);
|
||||
|
||||
var file = new File(opts);
|
||||
file.addCode(code);
|
||||
file.transform(ast);
|
||||
return file.generate();
|
||||
};
|
||||
|
||||
transform._ensureTransformerNames = function (type, keys) {
|
||||
_.each(keys, function (key) {
|
||||
for (var i in keys) {
|
||||
var key = keys[i];
|
||||
if (!_.has(transform.transformers, key)) {
|
||||
throw new ReferenceError("unknown transformer " + key + " specified in " + type);
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
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")
|
||||
common: require("./modules/common"),
|
||||
system: require("./modules/system"),
|
||||
ignore: require("./modules/ignore"),
|
||||
amd: require("./modules/amd"),
|
||||
umd: require("./modules/umd")
|
||||
};
|
||||
|
||||
_.each({
|
||||
// plyground
|
||||
// spec
|
||||
specBlockHoistFunctions: require("./transformers/spec-block-hoist-functions"),
|
||||
specNoForInOfAssignment: require("./transformers/spec-no-for-in-of-assignment"),
|
||||
specNoDuplicateProperties: 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"),
|
||||
|
||||
react: require("./transformers/react"),
|
||||
modules: require("./transformers/es6-modules"),
|
||||
propertyNameShorthand: require("./transformers/es6-property-name-shorthand"),
|
||||
arrayComprehension: require("./transformers/es7-array-comprehension"),
|
||||
@@ -48,28 +64,30 @@ _.each({
|
||||
spread: require("./transformers/es6-spread"),
|
||||
templateLiterals: require("./transformers/es6-template-literals"),
|
||||
propertyMethodAssignment: require("./transformers/es5-property-method-assignment"),
|
||||
defaultParameters: require("./transformers/es6-default-parameters"),
|
||||
restParameters: require("./transformers/es6-rest-parameters"),
|
||||
destructuring: require("./transformers/es6-destructuring"),
|
||||
defaultParameters: require("./transformers/es6-default-parameters"),
|
||||
forOf: require("./transformers/es6-for-of"),
|
||||
unicodeRegex: require("./transformers/es6-unicode-regex"),
|
||||
abstractReferences: require("./transformers/es7-abstract-references"),
|
||||
react: require("./transformers/react"),
|
||||
|
||||
constants: require("./transformers/es6-constants"),
|
||||
letScoping: require("./transformers/es6-let-scoping"),
|
||||
|
||||
generators: require("./transformers/es6-generators"),
|
||||
|
||||
_blockHoist: require("./transformers/_block-hoist"),
|
||||
|
||||
generators: require("./transformers/es6-generators"),
|
||||
restParameters: require("./transformers/es6-rest-parameters"),
|
||||
|
||||
_declarations: require("./transformers/_declarations"),
|
||||
|
||||
// wrap up
|
||||
_aliasFunctions: require("./transformers/_alias-functions"),
|
||||
|
||||
useStrict: require("./transformers/use-strict"),
|
||||
_moduleFormatter: require("./transformers/_module-formatter"),
|
||||
|
||||
_propertyLiterals: require("./transformers/_property-literals"),
|
||||
_memberExpressioLiterals: require("./transformers/_member-expression-literals"),
|
||||
_moduleFormatter: require("./transformers/_module-formatter")
|
||||
// spec
|
||||
specPropertyLiterals: require("./transformers/spec-property-literals"),
|
||||
specMemberExpressionLiterals: require("./transformers/spec-member-expression-literals")
|
||||
}, function (transformer, key) {
|
||||
transform.transformers[key] = new Transformer(key, transformer);
|
||||
});
|
||||
|
||||
@@ -4,8 +4,9 @@ var traverse = require("../traverse");
|
||||
var t = require("../types");
|
||||
var _ = require("lodash");
|
||||
|
||||
function Transformer(key, transformer) {
|
||||
function Transformer(key, transformer, opts) {
|
||||
this.transformer = Transformer.normalise(transformer);
|
||||
this.opts = opts || {};
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
@@ -18,37 +19,38 @@ Transformer.normalise = function (transformer) {
|
||||
if (type[0] === "_") return;
|
||||
if (_.isFunction(fns)) fns = { enter: fns };
|
||||
transformer[type] = fns;
|
||||
|
||||
var aliases = t.FLIPPED_ALIAS_KEYS[type];
|
||||
if (aliases) {
|
||||
_.each(aliases, function (alias) {
|
||||
transformer[alias] = fns;
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
return transformer;
|
||||
};
|
||||
|
||||
Transformer.prototype.astRun = function (file, key) {
|
||||
var transformer = this.transformer;
|
||||
var ast = file.ast;
|
||||
|
||||
if (transformer.ast && transformer.ast[key]) {
|
||||
transformer.ast[key](ast, file);
|
||||
}
|
||||
};
|
||||
|
||||
Transformer.prototype.transform = function (file) {
|
||||
if (!this.canRun(file)) return;
|
||||
|
||||
var transformer = this.transformer;
|
||||
var ast = file.ast;
|
||||
|
||||
var astRun = function (key) {
|
||||
if (transformer.ast && transformer.ast[key]) {
|
||||
transformer.ast[key](ast, file);
|
||||
}
|
||||
};
|
||||
|
||||
astRun("enter");
|
||||
this.astRun(file, "enter");
|
||||
|
||||
var build = function (exit) {
|
||||
return function (node, parent, scope) {
|
||||
// add any node type aliases that exist
|
||||
var types = [node.type].concat(t.ALIAS_KEYS[node.type] || []);
|
||||
|
||||
var fns = transformer.all;
|
||||
|
||||
_.each(types, function (type) {
|
||||
fns = transformer[type] || fns;
|
||||
});
|
||||
|
||||
// this transformer cannot deal with this node type
|
||||
var fns = transformer[node.type];
|
||||
if (!fns) return;
|
||||
|
||||
var fn = fns.enter;
|
||||
@@ -64,20 +66,19 @@ Transformer.prototype.transform = function (file) {
|
||||
exit: build(true)
|
||||
});
|
||||
|
||||
astRun("exit");
|
||||
this.astRun(file, "exit");
|
||||
};
|
||||
|
||||
Transformer.prototype.canRun = function (file) {
|
||||
var opts = file.opts;
|
||||
var key = this.key;
|
||||
if (key[0] === "_") return true;
|
||||
|
||||
var blacklist = opts.blacklist;
|
||||
if (blacklist.length && _.contains(blacklist, key)) return false;
|
||||
|
||||
if (key[0] !== "_") {
|
||||
var whitelist = opts.whitelist;
|
||||
if (whitelist.length && !_.contains(whitelist, key)) return false;
|
||||
}
|
||||
var whitelist = opts.whitelist;
|
||||
if (whitelist.length && !_.contains(whitelist, key)) return false;
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
@@ -15,38 +15,42 @@ var go = function (getBody, node, file, scope) {
|
||||
|
||||
// traverse the function and find all alias functions so we can alias
|
||||
// `arguments` and `this` if necessary
|
||||
traverse(node, function (node) {
|
||||
if (!node._aliasFunction) {
|
||||
if (t.isFunction(node)) {
|
||||
// stop traversal of this node as it'll be hit again by this transformer
|
||||
return false;
|
||||
} else {
|
||||
return;
|
||||
traverse(node, {
|
||||
enter: function (node) {
|
||||
if (!node._aliasFunction) {
|
||||
if (t.isFunction(node)) {
|
||||
// stop traversal of this node as it'll be hit again by this transformer
|
||||
return false;
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// traverse all child nodes of this function and find `arguments` and `this`
|
||||
traverse(node, {
|
||||
enter: function (node, parent) {
|
||||
if (t.isFunction(node) && !node._aliasFunction) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (node._ignoreAliasFunctions) return false;
|
||||
|
||||
var getId;
|
||||
|
||||
if (t.isIdentifier(node) && node.name === "arguments") {
|
||||
getId = getArgumentsId;
|
||||
} else if (t.isThisExpression(node)) {
|
||||
getId = getThisId;
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
|
||||
if (t.isReferenced(node, parent)) return getId();
|
||||
}
|
||||
});
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// traverse all child nodes of this function and find `arguments` and `this`
|
||||
traverse(node, function (node, parent) {
|
||||
if (t.isFunction(node) && !node._aliasFunction) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (node._ignoreAliasFunctions) return false;
|
||||
|
||||
var getId;
|
||||
|
||||
if (t.isIdentifier(node) && node.name === "arguments") {
|
||||
getId = getArgumentsId;
|
||||
} else if (t.isThisExpression(node)) {
|
||||
getId = getThisId;
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
|
||||
if (t.isReferenced(node, parent)) return getId();
|
||||
});
|
||||
|
||||
return false;
|
||||
});
|
||||
|
||||
var body;
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
var t = require("../../types");
|
||||
var _ = require("lodash");
|
||||
|
||||
exports.BlockStatement =
|
||||
exports.Program = function (node) {
|
||||
var kinds = {};
|
||||
var kind;
|
||||
|
||||
_.each(node._declarations, function (declar) {
|
||||
var kind = declar.kind || "var";
|
||||
for (var i in node._declarations) {
|
||||
var declar = node._declarations[i];
|
||||
|
||||
kind = declar.kind || "var";
|
||||
var declarNode = t.variableDeclarator(declar.id, declar.init);
|
||||
|
||||
if (!declar.init) {
|
||||
@@ -15,9 +17,9 @@ exports.Program = function (node) {
|
||||
} else {
|
||||
node.body.unshift(t.variableDeclaration(kind, [declarNode]));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
_.each(kinds, function (declars, kind) {
|
||||
node.body.unshift(t.variableDeclaration(kind, declars));
|
||||
});
|
||||
for (kind in kinds) {
|
||||
node.body.unshift(t.variableDeclaration(kind, kinds[kind]));
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
var util = require("../../util");
|
||||
var _ = require("lodash");
|
||||
|
||||
exports.Property = function (node) {
|
||||
if (node.method) node.method = false;
|
||||
@@ -7,9 +6,11 @@ exports.Property = function (node) {
|
||||
|
||||
exports.ObjectExpression = function (node, parent, file) {
|
||||
var mutatorMap = {};
|
||||
var hasAny = false;
|
||||
|
||||
node.properties = node.properties.filter(function (prop) {
|
||||
if (prop.kind === "get" || prop.kind === "set") {
|
||||
hasAny = true;
|
||||
util.pushMutatorMap(mutatorMap, prop.key, prop.kind, prop.value);
|
||||
return false;
|
||||
} else {
|
||||
@@ -17,7 +18,7 @@ exports.ObjectExpression = function (node, parent, file) {
|
||||
}
|
||||
});
|
||||
|
||||
if (_.isEmpty(mutatorMap)) return;
|
||||
if (!hasAny) return;
|
||||
|
||||
var objId = util.getUid(parent, file);
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ var t = require("../../types");
|
||||
exports.ArrowFunctionExpression = function (node) {
|
||||
t.ensureBlock(node);
|
||||
|
||||
node._aliasFunction = true;
|
||||
node._aliasFunction = "arrow";
|
||||
node.expression = false;
|
||||
node.type = "FunctionExpression";
|
||||
|
||||
|
||||
@@ -1,27 +1,38 @@
|
||||
var traverse = require("../../traverse");
|
||||
var util = require("../../util");
|
||||
var t = require("../../types");
|
||||
var _ = require("lodash");
|
||||
|
||||
exports.ClassDeclaration = function (node, parent, file, scope) {
|
||||
var built = new Class(node, file, scope).run();
|
||||
var closure = true;
|
||||
if (t.isProgram(parent) || t.isBlockStatement(parent)) {
|
||||
closure = false;
|
||||
}
|
||||
|
||||
var declar = t.variableDeclaration("let", [
|
||||
t.variableDeclarator(node.id, built)
|
||||
]);
|
||||
t.inheritsComments(declar, node);
|
||||
return declar;
|
||||
var factory = new Class(node, file, scope, closure);
|
||||
var newNode = factory.run();
|
||||
if (factory.closure) {
|
||||
if (closure) {
|
||||
// declaration in an expression context...
|
||||
// export default class Foo {}
|
||||
scope.push({
|
||||
kind: "var",
|
||||
key: node.id.key,
|
||||
id: node.id
|
||||
});
|
||||
return t.assignmentExpression("=", node.id, newNode);
|
||||
} else {
|
||||
// likely has a PrivateDeclaration etc
|
||||
return t.variableDeclaration("let", [
|
||||
t.variableDeclarator(node.id, newNode)
|
||||
]);
|
||||
}
|
||||
} else {
|
||||
return newNode;
|
||||
}
|
||||
};
|
||||
|
||||
exports.ClassExpression = function (node, parent, file, scope) {
|
||||
return new Class(node, file, scope).run();
|
||||
};
|
||||
|
||||
var getMemberExpressionObject = function (node) {
|
||||
while (t.isMemberExpression(node)) {
|
||||
node = node.object;
|
||||
}
|
||||
return node;
|
||||
return new Class(node, file, scope, true).run();
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -30,12 +41,17 @@ var getMemberExpressionObject = function (node) {
|
||||
* @param {Node} node
|
||||
* @param {File} file
|
||||
* @param {Scope} scope
|
||||
* @param {Boolean} closure
|
||||
*/
|
||||
|
||||
function Class(node, file, scope) {
|
||||
this.scope = scope;
|
||||
this.node = node;
|
||||
this.file = file;
|
||||
function Class(node, file, scope, closure) {
|
||||
this.closure = closure;
|
||||
this.scope = scope;
|
||||
this.node = node;
|
||||
this.file = file;
|
||||
|
||||
this.hasInstanceMutators = false;
|
||||
this.hasStaticMutators = false;
|
||||
|
||||
this.instanceMutatorMap = {};
|
||||
this.staticMutatorMap = {};
|
||||
@@ -51,50 +67,61 @@ function Class(node, file, scope) {
|
||||
*/
|
||||
|
||||
Class.prototype.run = function () {
|
||||
var superClassArgument = this.superName;
|
||||
var superClassCallee = this.superName;
|
||||
var superName = this.superName;
|
||||
var className = this.className;
|
||||
var file = this.file;
|
||||
var superName = this.superName;
|
||||
var className = this.className;
|
||||
var file = this.file;
|
||||
|
||||
if (superName) {
|
||||
if (t.isMemberExpression(superName)) {
|
||||
superClassArgument = superClassCallee = getMemberExpressionObject(superName);
|
||||
} else if (!t.isIdentifier(superName)) {
|
||||
superClassArgument = superName;
|
||||
superClassCallee = superName = file.generateUidIdentifier("ref", this.scope);
|
||||
}
|
||||
//
|
||||
|
||||
var body = this.body = [];
|
||||
|
||||
var constructor = t.functionExpression(null, [], t.blockStatement([]));
|
||||
if (this.node.id) constructor.id = className;
|
||||
this.constructor = constructor;
|
||||
|
||||
body.push(t.variableDeclaration("let", [
|
||||
t.variableDeclarator(className, constructor)
|
||||
]));
|
||||
|
||||
//
|
||||
|
||||
if (superName && t.isDynamic(superName)) {
|
||||
// so we're only evaluating it once
|
||||
var superRefName = "super";
|
||||
if (className) superRefName = className.name + "Super";
|
||||
|
||||
var superRef = file.generateUidIdentifier(superRefName, this.scope);
|
||||
body.unshift(t.variableDeclaration("var", [
|
||||
t.variableDeclarator(superRef, superName)
|
||||
]));
|
||||
superName = superRef;
|
||||
}
|
||||
|
||||
this.superName = superName;
|
||||
|
||||
var container = util.template("class", {
|
||||
CLASS_NAME: className
|
||||
});
|
||||
|
||||
var block = container.callee.expression.body;
|
||||
var body = this.body = block.body;
|
||||
var constructor = this.constructor = body[0].declarations[0].init;
|
||||
|
||||
if (this.node.id) constructor.id = className;
|
||||
|
||||
var returnStatement = body.pop();
|
||||
//
|
||||
|
||||
if (superName) {
|
||||
body.push(t.expressionStatement(t.callExpression(file.addDeclaration("extends"), [className, superName])));
|
||||
|
||||
container.arguments.push(superClassArgument);
|
||||
container.callee.expression.params.push(superClassCallee);
|
||||
body.push(t.expressionStatement(t.callExpression(file.addDeclaration("inherits"), [className, superName])));
|
||||
}
|
||||
|
||||
this.buildBody();
|
||||
|
||||
if (body.length === 1) {
|
||||
// only a constructor so no need for a closure container
|
||||
return constructor;
|
||||
t.inheritsComments(body[0], this.node);
|
||||
|
||||
if (this.closure) {
|
||||
if (body.length === 1) {
|
||||
// only a constructor so no need for a closure container
|
||||
return constructor;
|
||||
} else {
|
||||
body.push(t.returnStatement(className));
|
||||
return t.callExpression(
|
||||
t.functionExpression(null, [], t.blockStatement(body)),
|
||||
[]
|
||||
);
|
||||
}
|
||||
} else {
|
||||
body.push(returnStatement);
|
||||
return container;
|
||||
return body;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -110,15 +137,21 @@ Class.prototype.buildBody = function () {
|
||||
var body = this.body;
|
||||
var self = this;
|
||||
|
||||
_.each(classBody, function (node) {
|
||||
self.replaceInstanceSuperReferences(node);
|
||||
for (var i in classBody) {
|
||||
var node = classBody[i];
|
||||
if (t.isMethodDefinition(node)) {
|
||||
self.replaceInstanceSuperReferences(node);
|
||||
|
||||
if (node.key.name === "constructor") {
|
||||
self.pushConstructor(node);
|
||||
} else {
|
||||
self.pushMethod(node);
|
||||
if (node.key.name === "constructor") {
|
||||
self.pushConstructor(node);
|
||||
} else {
|
||||
self.pushMethod(node);
|
||||
}
|
||||
} else if (t.isPrivateDeclaration(node)) {
|
||||
self.closure = true;
|
||||
body.unshift(node);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (!this.hasConstructor && superName) {
|
||||
constructor.body.body.push(util.template("class-super-constructor-call", {
|
||||
@@ -129,7 +162,7 @@ Class.prototype.buildBody = function () {
|
||||
var instanceProps;
|
||||
var staticProps;
|
||||
|
||||
if (!_.isEmpty(this.instanceMutatorMap)) {
|
||||
if (this.hasInstanceMutators) {
|
||||
var protoId = util.template("prototype-identifier", {
|
||||
CLASS_NAME: className
|
||||
});
|
||||
@@ -137,7 +170,7 @@ Class.prototype.buildBody = function () {
|
||||
instanceProps = util.buildDefineProperties(this.instanceMutatorMap, protoId);
|
||||
}
|
||||
|
||||
if (!_.isEmpty(this.staticMutatorMap)) {
|
||||
if (this.hasStaticMutators) {
|
||||
staticProps = util.buildDefineProperties(this.staticMutatorMap, className);
|
||||
}
|
||||
|
||||
@@ -148,7 +181,7 @@ Class.prototype.buildBody = function () {
|
||||
if (instanceProps) args.push(instanceProps);
|
||||
|
||||
body.push(t.expressionStatement(
|
||||
t.callExpression(this.file.addDeclaration("class-props"), args)
|
||||
t.callExpression(this.file.addDeclaration("prototype-properties"), args)
|
||||
));
|
||||
}
|
||||
};
|
||||
@@ -171,11 +204,18 @@ Class.prototype.pushMethod = function (node) {
|
||||
if (!node.static) className = t.memberExpression(className, t.identifier("prototype"));
|
||||
methodName = t.memberExpression(className, methodName, node.computed);
|
||||
|
||||
this.body.push(t.expressionStatement(t.assignmentExpression("=", methodName, node.value)));
|
||||
var expr = t.expressionStatement(t.assignmentExpression("=", methodName, node.value));
|
||||
t.inheritsComments(expr, node);
|
||||
this.body.push(expr);
|
||||
} else {
|
||||
// mutator
|
||||
var mutatorMap = this.instanceMutatorMap;
|
||||
if (node.static) mutatorMap = this.staticMutatorMap;
|
||||
if (node.static) {
|
||||
this.hasStaticMutators = true;
|
||||
mutatorMap = this.staticMutatorMap;
|
||||
} else {
|
||||
this.hasInstanceMutators = true;
|
||||
}
|
||||
util.pushMutatorMap(mutatorMap, methodName, kind, node);
|
||||
}
|
||||
};
|
||||
@@ -232,17 +272,19 @@ Class.prototype.replaceInstanceSuperReferences = function (methodNode) {
|
||||
var method = methodNode.value;
|
||||
var self = this;
|
||||
|
||||
traverse(method, function (node, parent) {
|
||||
if (t.isIdentifier(node, { name: "super" })) {
|
||||
return self.superIdentifier(methodNode, node, parent);
|
||||
} else if (t.isCallExpression(node)) {
|
||||
var callee = node.callee;
|
||||
if (!t.isMemberExpression(callee)) return;
|
||||
if (callee.object.name !== "super") return;
|
||||
traverse(method, {
|
||||
enter: function (node, parent) {
|
||||
if (t.isIdentifier(node, { name: "super" })) {
|
||||
return self.superIdentifier(methodNode, node, parent);
|
||||
} else if (t.isCallExpression(node)) {
|
||||
var callee = node.callee;
|
||||
if (!t.isMemberExpression(callee)) return;
|
||||
if (callee.object.name !== "super") return;
|
||||
|
||||
// super.test(); -> ClassName.prototype.MethodName.call(this);
|
||||
callee.property = t.memberExpression(callee.property, t.identifier("call"));
|
||||
node.arguments.unshift(t.thisExpression());
|
||||
// super.test(); -> ClassName.prototype.MethodName.call(this);
|
||||
callee.property = t.memberExpression(callee.property, t.identifier("call"));
|
||||
node.arguments.unshift(t.thisExpression());
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
@@ -262,6 +304,7 @@ Class.prototype.pushConstructor = function (method) {
|
||||
var fn = method.value;
|
||||
|
||||
this.hasConstructor = true;
|
||||
|
||||
t.inherits(construct, fn);
|
||||
t.inheritsComments(construct, method);
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
var util = require("../../util");
|
||||
var t = require("../../types");
|
||||
var _ = require("lodash");
|
||||
|
||||
exports.ObjectExpression = function (node, parent, file) {
|
||||
var hasComputed = false;
|
||||
@@ -26,12 +25,13 @@ exports.ObjectExpression = function (node, parent, file) {
|
||||
OBJECT: node
|
||||
});
|
||||
|
||||
var containerCallee = container.callee.expression;
|
||||
var containerCallee = container.callee;
|
||||
var containerBody = containerCallee.body.body;
|
||||
|
||||
containerCallee._aliasFunction = true;
|
||||
|
||||
_.each(computed, function (prop) {
|
||||
for (var i in computed) {
|
||||
var prop = computed[i];
|
||||
containerBody.unshift(
|
||||
t.expressionStatement(
|
||||
t.assignmentExpression(
|
||||
@@ -41,7 +41,7 @@ exports.ObjectExpression = function (node, parent, file) {
|
||||
)
|
||||
)
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
return container;
|
||||
};
|
||||
|
||||
@@ -7,48 +7,76 @@ exports.BlockStatement =
|
||||
exports.ForInStatement =
|
||||
exports.ForOfStatement =
|
||||
exports.ForStatement = function (node, parent, file) {
|
||||
var hasConstants = false;
|
||||
var constants = {};
|
||||
|
||||
var check = function (parent, names) {
|
||||
_.each(names, function (nameNode, name) {
|
||||
if (!_.has(constants, name)) return;
|
||||
if (parent && t.isBlockStatement(parent) && parent !== constants[name]) return;
|
||||
/**
|
||||
* Check the results of `util.getIds` as `names` generated from a
|
||||
* node against it's parent.
|
||||
*/
|
||||
|
||||
var check = function (parent, names, scope) {
|
||||
for (var name in names) {
|
||||
var nameNode = names[name];
|
||||
if (!_.has(constants, name)) continue;
|
||||
if (parent && t.isBlockStatement(parent) && parent !== constants[name]) continue;
|
||||
|
||||
if (scope) {
|
||||
var defined = scope.get(name);
|
||||
if (defined && defined === nameNode) continue;
|
||||
}
|
||||
|
||||
throw file.errorWithNode(nameNode, name + " is read-only");
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
var getIds = function (node) {
|
||||
return t.getIds(node, true, ["MemberExpression"]);
|
||||
};
|
||||
|
||||
/**
|
||||
* Collect all constants in this scope.
|
||||
*/
|
||||
|
||||
_.each(node.body, function (child, parent) {
|
||||
if (child && t.isVariableDeclaration(child, { kind: "const" })) {
|
||||
_.each(child.declarations, function (declar) {
|
||||
_.each(getIds(declar), function (nameNode, name) {
|
||||
if (t.isExportDeclaration(child)) {
|
||||
child = child.declaration;
|
||||
}
|
||||
|
||||
if (t.isVariableDeclaration(child, { kind: "const" })) {
|
||||
for (var i in child.declarations) {
|
||||
var declar = child.declarations[i];
|
||||
|
||||
var ids = getIds(declar);
|
||||
for (var name in ids) {
|
||||
var nameNode = ids[name];
|
||||
|
||||
var names = {};
|
||||
names[name] = nameNode;
|
||||
check(parent, names);
|
||||
|
||||
constants[name] = parent;
|
||||
});
|
||||
hasConstants = true;
|
||||
}
|
||||
|
||||
declar._ignoreConstant = true;
|
||||
});
|
||||
}
|
||||
|
||||
child._ignoreConstant = true;
|
||||
child.kind = "let";
|
||||
}
|
||||
});
|
||||
|
||||
if (_.isEmpty(constants)) return;
|
||||
if (!hasConstants) return;
|
||||
|
||||
traverse(node, function (child, parent) {
|
||||
if (child._ignoreConstant) return;
|
||||
if (t.isVariableDeclaration(child)) return;
|
||||
traverse(node, {
|
||||
enter: function (child, parent, scope) {
|
||||
if (child._ignoreConstant) return;
|
||||
if (t.isVariableDeclaration(child)) return;
|
||||
|
||||
if (t.isVariableDeclarator(child) || t.isDeclaration(child) || t.isAssignmentExpression(child)) {
|
||||
check(parent, getIds(child));
|
||||
if (t.isVariableDeclarator(child) || t.isDeclaration(child) || t.isAssignmentExpression(child)) {
|
||||
check(parent, getIds(child), scope);
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
@@ -12,9 +12,12 @@ exports.Function = function (node, parent, file, scope) {
|
||||
});
|
||||
|
||||
var iife = false;
|
||||
var i;
|
||||
var def;
|
||||
|
||||
_.each(node.defaults, function (def, i) {
|
||||
if (!def) return;
|
||||
for (i in node.defaults) {
|
||||
def = node.defaults[i];
|
||||
if (!def) continue;
|
||||
|
||||
var param = node.params[i];
|
||||
|
||||
@@ -24,7 +27,7 @@ exports.Function = function (node, parent, file, scope) {
|
||||
var check = function (node, parent) {
|
||||
if (!t.isIdentifier(node) || !t.isReferenced(node, parent)) return;
|
||||
|
||||
if (_.contains(ids, node.name)) {
|
||||
if (ids.indexOf(node.name) >= 0) {
|
||||
throw file.errorWithNode(node, "Temporal dead zone - accessing a variable before it's initialized");
|
||||
}
|
||||
|
||||
@@ -34,26 +37,27 @@ exports.Function = function (node, parent, file, scope) {
|
||||
};
|
||||
|
||||
check(def, node);
|
||||
traverse(def, check);
|
||||
traverse(def, { enter: check });
|
||||
});
|
||||
|
||||
// we're accessing a variable that's already defined within this function
|
||||
var has = scope.get(param.name);
|
||||
if (has && !_.contains(node.params, has)) {
|
||||
if (has && node.params.indexOf(has) < 0) {
|
||||
iife = true;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
var body = [];
|
||||
|
||||
_.each(node.defaults, function (def, i) {
|
||||
if (!def) return;
|
||||
for (i in node.defaults) {
|
||||
def = node.defaults[i];
|
||||
if (!def) continue;
|
||||
|
||||
body.push(util.template("if-undefined-set-to", {
|
||||
VARIABLE: node.params[i],
|
||||
DEFAULT: def
|
||||
}, true));
|
||||
});
|
||||
}
|
||||
|
||||
if (iife) {
|
||||
var container = t.functionExpression(null, [], node.body, node.generator);
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
// TODO: Clean up
|
||||
|
||||
var t = require("../../types");
|
||||
var _ = require("lodash");
|
||||
|
||||
var buildVariableAssign = function (opts, id, init) {
|
||||
var op = opts.operator;
|
||||
@@ -27,23 +26,26 @@ var push = function (opts, nodes, elem, parentId) {
|
||||
};
|
||||
|
||||
var pushObjectPattern = function (opts, nodes, pattern, parentId) {
|
||||
_.each(pattern.properties, function (prop, i) {
|
||||
for (var i in pattern.properties) {
|
||||
var prop = pattern.properties[i];
|
||||
if (t.isSpreadProperty(prop)) {
|
||||
// get all the keys that appear in this object before the current spread
|
||||
var keys = [];
|
||||
_.each(pattern.properties, function (prop2, i2) {
|
||||
if (i2 >= i) return false;
|
||||
if (t.isSpreadProperty(prop2)) return;
|
||||
for (var i2 in pattern.properties) {
|
||||
var prop2 = pattern.properties[i2];
|
||||
|
||||
if (i2 >= i) break;
|
||||
if (t.isSpreadProperty(prop2)) continue;
|
||||
|
||||
var key = prop2.key;
|
||||
if (t.isIdentifier(key)) {
|
||||
key = t.literal(prop2.key.name);
|
||||
}
|
||||
keys.push(key);
|
||||
});
|
||||
}
|
||||
keys = t.arrayExpression(keys);
|
||||
|
||||
var value = t.callExpression(opts.file.addDeclaration("object-spread"), [parentId, keys]);
|
||||
var value = t.callExpression(opts.file.addDeclaration("object-without-properties"), [parentId, keys]);
|
||||
nodes.push(buildVariableAssign(opts, prop.argument, value));
|
||||
} else {
|
||||
var pattern2 = prop.value;
|
||||
@@ -55,25 +57,42 @@ var pushObjectPattern = function (opts, nodes, pattern, parentId) {
|
||||
nodes.push(buildVariableAssign(opts, pattern2, patternId2));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
var pushArrayPattern = function (opts, nodes, pattern, parentId) {
|
||||
if (!pattern.elements) return;
|
||||
|
||||
var i;
|
||||
|
||||
var hasSpreadElement = false;
|
||||
for (i in pattern.elements) {
|
||||
if (t.isSpreadElement(pattern.elements[i])) {
|
||||
hasSpreadElement = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
var toArray = opts.file.toArray(parentId, !hasSpreadElement && pattern.elements.length);
|
||||
|
||||
var _parentId = opts.file.generateUidIdentifier("ref", opts.scope);
|
||||
nodes.push(t.variableDeclaration("var", [
|
||||
t.variableDeclarator(_parentId, opts.file.toArray(parentId))
|
||||
t.variableDeclarator(_parentId, toArray)
|
||||
]));
|
||||
parentId = _parentId;
|
||||
|
||||
_.each(pattern.elements, function (elem, i) {
|
||||
if (!elem) return;
|
||||
for (i in pattern.elements) {
|
||||
var elem = pattern.elements[i];
|
||||
if (!elem) continue;
|
||||
|
||||
i = +i;
|
||||
|
||||
var newPatternId;
|
||||
|
||||
if (t.isSpreadElement(elem)) {
|
||||
newPatternId = opts.file.toArray(parentId);
|
||||
|
||||
if (+i > 0) {
|
||||
if (i > 0) {
|
||||
newPatternId = t.callExpression(t.memberExpression(newPatternId, t.identifier("slice")), [t.literal(i)]);
|
||||
}
|
||||
|
||||
@@ -83,7 +102,7 @@ var pushArrayPattern = function (opts, nodes, pattern, parentId) {
|
||||
}
|
||||
|
||||
push(opts, nodes, elem, newPatternId);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
var pushPattern = function (opts) {
|
||||
@@ -215,17 +234,22 @@ exports.VariableDeclaration = function (node, parent, file, scope) {
|
||||
if (t.isForInStatement(parent) || t.isForOfStatement(parent)) return;
|
||||
|
||||
var nodes = [];
|
||||
var i;
|
||||
var declar;
|
||||
|
||||
var hasPattern = false;
|
||||
_.each(node.declarations, function (declar) {
|
||||
for (i in node.declarations) {
|
||||
declar = node.declarations[i];
|
||||
if (t.isPattern(declar.id)) {
|
||||
hasPattern = true;
|
||||
return false;
|
||||
break;
|
||||
}
|
||||
});
|
||||
}
|
||||
if (!hasPattern) return;
|
||||
|
||||
_.each(node.declarations, function (declar) {
|
||||
for (i in node.declarations) {
|
||||
declar = node.declarations[i];
|
||||
|
||||
var patternId = declar.init;
|
||||
var pattern = declar.id;
|
||||
var opts = {
|
||||
@@ -241,12 +265,13 @@ exports.VariableDeclaration = function (node, parent, file, scope) {
|
||||
} else {
|
||||
nodes.push(buildVariableAssign(opts, declar.id, declar.init));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (!t.isProgram(parent) && !t.isBlockStatement(parent)) {
|
||||
var declar;
|
||||
declar = null;
|
||||
|
||||
_.each(nodes, function (node) {
|
||||
for (i in nodes) {
|
||||
node = nodes[i];
|
||||
declar = declar || t.variableDeclaration(node.kind, []);
|
||||
|
||||
if (!t.isVariableDeclaration(node) && declar.kind !== node.kind) {
|
||||
@@ -254,7 +279,7 @@ exports.VariableDeclaration = function (node, parent, file, scope) {
|
||||
}
|
||||
|
||||
declar.declarations = declar.declarations.concat(node.declarations);
|
||||
});
|
||||
}
|
||||
|
||||
return declar;
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ exports.ForOfStatement = function (node, parent, file, scope) {
|
||||
OBJECT: node.right
|
||||
});
|
||||
|
||||
t.inheritsComments(node2, node);
|
||||
t.ensureBlock(node);
|
||||
|
||||
var block = node2.body;
|
||||
|
||||
1
lib/6to5/transformation/transformers/es6-generators.js
Normal file
1
lib/6to5/transformation/transformers/es6-generators.js
Normal file
@@ -0,0 +1 @@
|
||||
module.exports = require("regenerator").transform;
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user