Compare commits
57 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5f9e954d21 | ||
|
|
a03fd0f43a | ||
|
|
dcc5eaa95e | ||
|
|
3b7ce5aaa1 | ||
|
|
6811f071a9 | ||
|
|
9abda34e59 | ||
|
|
417ba2bd92 | ||
|
|
e23f8e92ba | ||
|
|
ef2638eb89 | ||
|
|
a7d860aab4 | ||
|
|
0e3498d785 | ||
|
|
45fe1d0d47 | ||
|
|
d5c2647701 | ||
|
|
af4d8a27aa | ||
|
|
8136b4c40b | ||
|
|
65ba4d35e7 | ||
|
|
e09312f127 | ||
|
|
1b0e42ad03 | ||
|
|
e21a0d0a8b | ||
|
|
ce170f7646 | ||
|
|
5f8420f23e | ||
|
|
ce4220644d | ||
|
|
aa50a450bd | ||
|
|
1e6b8d80bb | ||
|
|
035829e726 | ||
|
|
2b70df4141 | ||
|
|
1e7b7b3e0c | ||
|
|
b8b670e607 | ||
|
|
3467d509f7 | ||
|
|
66cc6bea08 | ||
|
|
dba935c63d | ||
|
|
73f65ae634 | ||
|
|
e263757509 | ||
|
|
b8a80364df | ||
|
|
19ba55410b | ||
|
|
c65197f006 | ||
|
|
98a04a070f | ||
|
|
2bda223001 | ||
|
|
ddefc09510 | ||
|
|
489547b77b | ||
|
|
a16685d36d | ||
|
|
891bbba375 | ||
|
|
7f2335974b | ||
|
|
ca597a2306 | ||
|
|
45bab5709f | ||
|
|
0683591c6c | ||
|
|
44966849f9 | ||
|
|
e76462303e | ||
|
|
fceff4b52f | ||
|
|
209093a3f0 | ||
|
|
7013e970d8 | ||
|
|
42a7973a9d | ||
|
|
a75248d2d2 | ||
|
|
1f274a3b95 | ||
|
|
5bc4dfc14c | ||
|
|
4241227dbe | ||
|
|
a1adca6b65 |
145
MODULES.md
Normal file
145
MODULES.md
Normal file
@@ -0,0 +1,145 @@
|
||||
# Modules
|
||||
|
||||
## Usage
|
||||
|
||||
### CLI
|
||||
|
||||
$ 6to5 --modules common script.js
|
||||
|
||||
### Node
|
||||
|
||||
```javascript
|
||||
var to5 = require("6to5");
|
||||
to5.transform('import "foo";', { modules: "common" });
|
||||
```
|
||||
|
||||
## Formats
|
||||
|
||||
### Common (Default)
|
||||
|
||||
**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
|
||||
require("foo");
|
||||
|
||||
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;
|
||||
```
|
||||
|
||||
### AMD
|
||||
|
||||
**In**
|
||||
|
||||
```javascript
|
||||
import foo from "foo";
|
||||
|
||||
export function bar() {
|
||||
return foo("foobar");
|
||||
}
|
||||
```
|
||||
|
||||
**Out**
|
||||
|
||||
```javascript
|
||||
define(["exports", "foo"], function (exports, _foo) {
|
||||
exports.bar = bar;
|
||||
|
||||
var foo = _foo.default;
|
||||
|
||||
function bar() {
|
||||
return foo("foobar");
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
### UMD
|
||||
|
||||
**In**
|
||||
|
||||
```javascript
|
||||
import foo from "foo";
|
||||
|
||||
export function bar() {
|
||||
return foo("foobar");
|
||||
}
|
||||
```
|
||||
|
||||
**Out**
|
||||
|
||||
```javascript
|
||||
(function (factory) {
|
||||
if (typeof define === "function" && define.amd) {
|
||||
define(["exports", "foo"], factory);
|
||||
} else if (typeof exports !== "undefined") {
|
||||
factory(exports, require("foo"));
|
||||
}
|
||||
})(function (exports) {
|
||||
exports.bar = bar;
|
||||
|
||||
var foo = _foo.default;
|
||||
|
||||
function bar() {
|
||||
return foo("foobar");
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
## Custom
|
||||
|
||||
You can alternatively specify module names instead of one of the built-in types.
|
||||
|
||||
```javascript
|
||||
module.exports = ModuleFormatter;
|
||||
|
||||
function ModuleFormatter() {
|
||||
|
||||
}
|
||||
|
||||
ModuleFormatter.prototype.transform = function (ast) {
|
||||
// this is ran after all transformers have had their turn at modifying the ast
|
||||
// feel free to modify this however
|
||||
};
|
||||
|
||||
ModuleFormatter.prototype.import = function (node, nodes) {
|
||||
// node is an ImportDeclaration
|
||||
};
|
||||
|
||||
ModuleFormatter.prototype.importSpecifier = function (specifier, node, nodes) {
|
||||
// specifier is an ImportSpecifier
|
||||
// node is an ImportDeclaration
|
||||
};
|
||||
|
||||
ModuleFormatter.prototype.export = function (node, nodes) {
|
||||
// node is an ExportDeclaration
|
||||
};
|
||||
|
||||
ModuleFormatter.prototype.exportSpecifier = function (specifier, node, nodes) {
|
||||
// specifier is an ExportSpecifier
|
||||
// node is an ExportDeclaration
|
||||
};
|
||||
```
|
||||
6
Makefile
6
Makefile
@@ -10,6 +10,7 @@ clean:
|
||||
rm -rf coverage templates.json test/tmp dist
|
||||
|
||||
bench:
|
||||
npm install es6-transpiler traceur esnext es6now jstransform
|
||||
node node_modules/matcha/bin/_matcha
|
||||
|
||||
test:
|
||||
@@ -34,7 +35,7 @@ build:
|
||||
|
||||
node bin/cache-templates
|
||||
|
||||
browserify lib/6to5/transform.js -s to5 >dist/6to5.js
|
||||
browserify lib/6to5/browser.js -s to5 >dist/6to5.js
|
||||
uglifyjs dist/6to5.js >dist/6to5.min.js
|
||||
|
||||
rm -rf templates.json
|
||||
@@ -47,6 +48,9 @@ publish:
|
||||
node bin/cache-templates
|
||||
test -f templates.json
|
||||
|
||||
make build
|
||||
cp dist/6to5.js browser.js
|
||||
|
||||
read -p "Version: " version; \
|
||||
npm version $$version --message "v%s"
|
||||
npm publish
|
||||
|
||||
33
README.md
33
README.md
@@ -154,6 +154,11 @@ to5.transformFile("filename.js", options, function (err, result) {
|
||||
// Run `6to5 --help` to see a full list of transformers.
|
||||
whitelist: [],
|
||||
|
||||
// Module formatter to use
|
||||
// Run `6to5 --help` to see a full list of module formatters.
|
||||
// Default: "common"
|
||||
modules: "common",
|
||||
|
||||
// If truthy, adds a `map` property to returned output.
|
||||
// If set to "inline", a comment with a sourceMappingURL directive is added to
|
||||
// the bottom of the returned code.
|
||||
@@ -224,32 +229,12 @@ To test 6to5 in your browser run:
|
||||
|
||||
And open `test/browser.html` in your browser if it doesn't open automatically.
|
||||
|
||||
## Modules
|
||||
## [Modules](MODULES.md)
|
||||
|
||||
6to5 modules compile straight to CommonJS, because of this various liberties are
|
||||
taken into account to make their usage easier.
|
||||
See [Modules - Common](MODULES.md#common-default) for documentation on the
|
||||
default module formatting.
|
||||
|
||||
```javascript
|
||||
import "foo"; // require("foo");
|
||||
|
||||
import foo from "foo"; // var foo = require("foo").default;
|
||||
import * as foo from "foo"; // var foo = require("foo");
|
||||
|
||||
import {bar} from "foo"; // var bar = require("foo").bar;
|
||||
import {foo as bar} from "foo"; // var bar = require("foo").foo;
|
||||
|
||||
export {test}; // exports.test = test;
|
||||
export var test = 5; // var test = 5; exports.test = test;
|
||||
|
||||
export default test; // exports.default = test;
|
||||
```
|
||||
|
||||
If you'd like to disable this behaviour and use the more ES6-like
|
||||
[es6-module-transpiler](https://github.com/esnext/es6-module-transpiler) you can
|
||||
use the following:
|
||||
|
||||
$ 6to5 script.js -o script-compiled.js --blacklist modules
|
||||
$ compile-modules convert script-compiled.js -o script-compiled.js
|
||||
Alternatively see [Modules](MODULES.md) for all other supported module formatting types.
|
||||
|
||||
## Caveats
|
||||
|
||||
|
||||
@@ -17,7 +17,12 @@ var vm = require("vm");
|
||||
var _ = require("lodash");
|
||||
|
||||
var readResolve = function (filename) {
|
||||
return fs.readFileSync(require.resolve(filename), "utf8");
|
||||
try {
|
||||
filename = require.resolve(filename);
|
||||
} catch (err) {
|
||||
return null;
|
||||
}
|
||||
return fs.readFileSync(filename, "utf8");
|
||||
};
|
||||
|
||||
var getVersion = function (name) {
|
||||
@@ -38,6 +43,7 @@ _.each([
|
||||
|
||||
var compilers = {
|
||||
"6to5": {
|
||||
version: getVersion(".."),
|
||||
compile: function (code, filename) {
|
||||
return to5.transform(code, { filename: filename }).code;
|
||||
}
|
||||
@@ -54,7 +60,7 @@ var compilers = {
|
||||
},
|
||||
|
||||
esnext: {
|
||||
runtime: readResolve("esnext/node_modules/regenerator/runtime.js"),
|
||||
runtime: readResolve("esnext/node_modules/regenerator/runtime.js") || readResolve("regenerator/runtime.js"),
|
||||
compile: function (code, filename) {
|
||||
return esnext.compile(code).code;
|
||||
}
|
||||
@@ -87,7 +93,7 @@ var compilers = {
|
||||
var uglifyTitle = "uglify v" + getVersion("uglify-js");
|
||||
|
||||
_.each(compilers, function (compiler, name) {
|
||||
compiler.title = name + " v" + getVersion(name);
|
||||
compiler.title = name + " v" + (compiler.version || getVersion(name));
|
||||
});
|
||||
|
||||
//
|
||||
@@ -96,14 +102,13 @@ var sizeBenchmark = function (code, loc, name, compiler) {
|
||||
var log = function (output, title) {
|
||||
title = [compiler.title].concat(title || []).join(" + ");
|
||||
|
||||
var kilo = (output.length / 1024).toFixed(2);
|
||||
|
||||
var text;
|
||||
var color;
|
||||
if (output === false) {
|
||||
if (output.stack) {
|
||||
text = "error";
|
||||
color = "red";
|
||||
} else {
|
||||
var kilo = (output.length / 1024).toFixed(2);
|
||||
text = kilo + "KB";
|
||||
color = "cyan";
|
||||
}
|
||||
@@ -111,6 +116,10 @@ var sizeBenchmark = function (code, loc, name, compiler) {
|
||||
text = matcha.utils.color(matcha.utils.padBefore(text, 22), color);
|
||||
|
||||
console.log(text, matcha.utils.color("» " + title, "gray"));
|
||||
|
||||
if (output.stack) {
|
||||
console.error(output.stack);
|
||||
}
|
||||
};
|
||||
|
||||
var go = function (getOutput, title) {
|
||||
@@ -118,7 +127,7 @@ var sizeBenchmark = function (code, loc, name, compiler) {
|
||||
try {
|
||||
code = getOutput();
|
||||
} catch (err) {
|
||||
log(false, title);
|
||||
log(err, title);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -127,13 +136,13 @@ var sizeBenchmark = function (code, loc, name, compiler) {
|
||||
|
||||
var output;
|
||||
go(function () {
|
||||
return output = output || compiler.compile(code, loc);
|
||||
return output = compiler.compile(code, loc);
|
||||
});
|
||||
if (!output) return;
|
||||
|
||||
go(function () {
|
||||
return uglify.minify(output, { fromString: true }).code;
|
||||
}, uglifyTitle);
|
||||
//go(function () {
|
||||
// return uglify.minify(output, { fromString: true }).code;
|
||||
//}, uglifyTitle);
|
||||
};
|
||||
|
||||
//
|
||||
|
||||
@@ -16,19 +16,27 @@ commander.option("-s, --source-maps", "Save source map alongside the compiled co
|
||||
commander.option("-f, --filename [filename]", "Filename to use when reading from stdin - this will be used in source-maps, errors etc [stdin]", "stdin");
|
||||
commander.option("-w, --watch", "Recompile files on changes");
|
||||
|
||||
commander.option("-m, --modules [modules]", "Module formatter type to use [common]", "common");
|
||||
commander.option("-w, --whitelist [whitelist]", "Whitelist of transformers to ONLY use", util2.list);
|
||||
commander.option("-b, --blacklist [blacklist]", "Blacklist of transformers to NOT use", util2.list);
|
||||
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.on("--help", function(){
|
||||
console.log(" Transformers:");
|
||||
console.log();
|
||||
_.each(_.keys(transform.transformers).sort(), function (key) {
|
||||
if (key[0] === "_") return;
|
||||
console.log(" - " + key);
|
||||
});
|
||||
console.log();
|
||||
var outKeys = function (title, obj) {
|
||||
console.log(" " + title + ":");
|
||||
console.log();
|
||||
|
||||
_.each(_.keys(obj).sort(), function (key) {
|
||||
if (key[0] === "_") return;
|
||||
console.log(" - " + key);
|
||||
});
|
||||
|
||||
console.log();
|
||||
};
|
||||
|
||||
outKeys("Transformers", transform.transformers);
|
||||
outKeys("Module formatters", transform.moduleFormatters);
|
||||
});
|
||||
|
||||
var pkg = require("../../package.json");
|
||||
@@ -83,7 +91,8 @@ exports.opts = {
|
||||
sourceMapName: commander.outFile,
|
||||
blacklist: commander.blacklist,
|
||||
whitelist: commander.whitelist,
|
||||
sourceMap: commander.sourceMaps || commander.sourceMapsInline
|
||||
sourceMap: commander.sourceMaps || commander.sourceMapsInline,
|
||||
modules: commander.modules
|
||||
};
|
||||
|
||||
var fn;
|
||||
|
||||
3
lib/6to5/browser.js
Normal file
3
lib/6to5/browser.js
Normal file
@@ -0,0 +1,3 @@
|
||||
var transform = require("./transform");
|
||||
|
||||
module.exports = transform;
|
||||
@@ -3,15 +3,17 @@ module.exports = File;
|
||||
var SHEBANG_REGEX = /^\#\!.*/;
|
||||
|
||||
var transform = require("./transform");
|
||||
var recast = require("recast");
|
||||
var recast = require("acorn-recast");
|
||||
var util = require("./util");
|
||||
var b = require("recast").types.builders;
|
||||
var b = require("acorn-ast-types").builders;
|
||||
var _ = require("lodash");
|
||||
|
||||
function File(opts) {
|
||||
this.opts = File.normaliseOptions(opts);
|
||||
this.moduleFormatter = this.getModuleFormatter(opts.modules);
|
||||
|
||||
this.declarations = {};
|
||||
this.uids = {};
|
||||
this.opts = File.normaliseOptions(opts);
|
||||
this.ast = {};
|
||||
}
|
||||
|
||||
@@ -22,7 +24,8 @@ File.normaliseOptions = function (opts) {
|
||||
blacklist: [],
|
||||
whitelist: [],
|
||||
sourceMap: false,
|
||||
filename: "unknown"
|
||||
filename: "unknown",
|
||||
modules: "common"
|
||||
});
|
||||
|
||||
_.defaults(opts, {
|
||||
@@ -36,6 +39,21 @@ File.normaliseOptions = function (opts) {
|
||||
return opts;
|
||||
};
|
||||
|
||||
File.prototype.getModuleFormatter = function (type) {
|
||||
var ModuleLoader = transform.moduleFormatters[type];
|
||||
|
||||
if (!ModuleLoader) {
|
||||
var loc = util.resolve(type);
|
||||
if (loc) ModuleLoader = require(loc);
|
||||
}
|
||||
|
||||
if (!ModuleLoader) {
|
||||
throw new ReferenceError("unknown module formatter type " + type);
|
||||
}
|
||||
|
||||
return new ModuleLoader(this);
|
||||
};
|
||||
|
||||
File.prototype.parseShebang = function (code) {
|
||||
var shebangMatch = code.match(SHEBANG_REGEX);
|
||||
if (shebangMatch) {
|
||||
@@ -66,7 +84,8 @@ File.prototype.parse = function (code) {
|
||||
code = this.parseShebang(code);
|
||||
|
||||
return util.parse(this.opts, code, function (tree) {
|
||||
return self.transform(tree);
|
||||
self.transform(tree);
|
||||
return self.generate();
|
||||
});
|
||||
};
|
||||
|
||||
@@ -78,17 +97,13 @@ File.prototype.transform = function (ast) {
|
||||
_.each(transform.transformers, function (transformer) {
|
||||
transformer.transform(self);
|
||||
});
|
||||
|
||||
return this.generate();
|
||||
};
|
||||
|
||||
File.prototype.generate = function () {
|
||||
var opts = this.opts;
|
||||
var ast = this.ast;
|
||||
|
||||
var printOpts = {
|
||||
tabWidth: 2
|
||||
};
|
||||
var printOpts = { tabWidth: 2 };
|
||||
|
||||
if (opts.sourceMap) {
|
||||
printOpts.sourceMapName = opts.sourceMapName;
|
||||
@@ -114,6 +129,17 @@ File.prototype.generate = function () {
|
||||
};
|
||||
|
||||
File.prototype.generateUid = function (name) {
|
||||
// replace all non-valid identifiers with dashes
|
||||
name = name.replace(/[^a-zA-Z0-9]/g, "-");
|
||||
|
||||
// remove all dashes and numbers from start of name
|
||||
name = name.replace(/^[-0-9]+/, "");
|
||||
|
||||
// camel case
|
||||
name = name.replace(/[-_\s]+(.)?/g, function (match, c) {
|
||||
return c ? c.toUpperCase() : "";
|
||||
});
|
||||
|
||||
var uids = this.uids;
|
||||
var i = uids[name] || 1;
|
||||
|
||||
|
||||
82
lib/6to5/modules/amd.js
Normal file
82
lib/6to5/modules/amd.js
Normal file
@@ -0,0 +1,82 @@
|
||||
module.exports = AMDFormatter;
|
||||
|
||||
var CommonJSFormatter = require("./common");
|
||||
var util = require("../util");
|
||||
var b = require("acorn-ast-types").builders;
|
||||
var _ = require("lodash");
|
||||
|
||||
function AMDFormatter(file) {
|
||||
this.file = file;
|
||||
this.ids = {};
|
||||
}
|
||||
|
||||
util.inherits(AMDFormatter, CommonJSFormatter);
|
||||
|
||||
AMDFormatter.prototype.transform = function (ast) {
|
||||
var program = ast.program;
|
||||
var body = program.body;
|
||||
|
||||
// build an array of module names
|
||||
|
||||
var names = [b.literal("exports")];
|
||||
_.each(this.ids, function (id, name) {
|
||||
names.push(b.literal(name));
|
||||
});
|
||||
names = b.arrayExpression(names);
|
||||
|
||||
// build up define container
|
||||
|
||||
var params = _.values(this.ids);
|
||||
params.unshift(b.identifier("exports"));
|
||||
|
||||
var container = b.functionExpression(null, params, b.blockStatement(body));
|
||||
var call = b.callExpression(b.identifier("define"), [names, container]);
|
||||
|
||||
program.body = [b.expressionStatement(call)];
|
||||
};
|
||||
|
||||
AMDFormatter.prototype._push = function (node) {
|
||||
var id = node.source.value;
|
||||
var ids = this.ids;
|
||||
|
||||
if (ids[id]) {
|
||||
return ids[id];
|
||||
} else {
|
||||
return this.ids[id] = b.identifier(this.file.generateUid(id));
|
||||
}
|
||||
};
|
||||
|
||||
AMDFormatter.prototype.import = function (node) {
|
||||
this._push(node);
|
||||
};
|
||||
|
||||
AMDFormatter.prototype.importSpecifier = function (specifier, node, nodes) {
|
||||
var key = util.getSpecifierName(specifier);
|
||||
var id = specifier.id;
|
||||
|
||||
// import foo from "foo";
|
||||
if (specifier.default) {
|
||||
id = b.identifier("default");
|
||||
}
|
||||
|
||||
var ref;
|
||||
|
||||
if (specifier.type === "ImportBatchSpecifier") {
|
||||
// import * as bar from "foo";
|
||||
ref = this._push(node);
|
||||
} else {
|
||||
// import foo from "foo";
|
||||
ref = b.memberExpression(this._push(node), id, false);
|
||||
}
|
||||
|
||||
nodes.push(b.variableDeclaration("var", [
|
||||
b.variableDeclarator(key, ref)
|
||||
]));
|
||||
};
|
||||
|
||||
AMDFormatter.prototype.exportSpecifier = function (specifier, node, nodes) {
|
||||
var self = this;
|
||||
return this._exportSpecifier(function () {
|
||||
return self._push(node);
|
||||
}, specifier, node, nodes);
|
||||
};
|
||||
97
lib/6to5/modules/common.js
Normal file
97
lib/6to5/modules/common.js
Normal file
@@ -0,0 +1,97 @@
|
||||
module.exports = CommonJSFormatter;
|
||||
|
||||
var util = require("../util");
|
||||
var b = require("acorn-ast-types").builders;
|
||||
|
||||
function CommonJSFormatter(file) {
|
||||
this.file = file;
|
||||
}
|
||||
|
||||
CommonJSFormatter.prototype.import = function (node, nodes) {
|
||||
// import "foo";
|
||||
nodes.push(util.template("require", {
|
||||
MODULE_NAME: node.source.raw
|
||||
}, true));
|
||||
};
|
||||
|
||||
CommonJSFormatter.prototype.importSpecifier = function (specifier, node, nodes) {
|
||||
var variableName = util.getSpecifierName(specifier);
|
||||
|
||||
// import foo from "foo";
|
||||
if (specifier.default) {
|
||||
specifier.id = b.identifier("default");
|
||||
}
|
||||
|
||||
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.raw,
|
||||
KEY: specifier.id
|
||||
}));
|
||||
};
|
||||
|
||||
CommonJSFormatter.prototype.export = function (node, nodes) {
|
||||
var declar = node.declaration;
|
||||
|
||||
if (node.default) {
|
||||
util.ensureExpressionType(declar);
|
||||
|
||||
nodes.push(util.template("exports-default", {
|
||||
VALUE: declar
|
||||
}, true));
|
||||
} else {
|
||||
var id = declar.id;
|
||||
if (declar.type === "VariableDeclaration") {
|
||||
id = declar.declarations[0].id;
|
||||
}
|
||||
|
||||
var assign = util.template("exports-assign", {
|
||||
VALUE: id,
|
||||
KEY: id
|
||||
}, true);
|
||||
|
||||
nodes.push(declar);
|
||||
|
||||
if (declar.type === "FunctionDeclaration") {
|
||||
assign._blockHoist = true;
|
||||
}
|
||||
|
||||
nodes.push(assign);
|
||||
}
|
||||
};
|
||||
|
||||
CommonJSFormatter.prototype._exportSpecifier = function (getRef, specifier, node, nodes) {
|
||||
var variableName = util.getSpecifierName(specifier);
|
||||
|
||||
if (node.source) {
|
||||
if (specifier.type === "ExportBatchSpecifier") {
|
||||
// export * from "foo";
|
||||
nodes.push(util.template("exports-wildcard", {
|
||||
OBJECT: getRef()
|
||||
}, true));
|
||||
} else {
|
||||
// export { foo } from "test";
|
||||
nodes.push(util.template("exports-assign-key", {
|
||||
VARIABLE_NAME: variableName.name,
|
||||
OBJECT: getRef(),
|
||||
KEY: specifier.id
|
||||
}, true));
|
||||
}
|
||||
} else {
|
||||
// export { foo };
|
||||
nodes.push(util.template("exports-assign", {
|
||||
VALUE: specifier.id,
|
||||
KEY: variableName
|
||||
}, true));
|
||||
}
|
||||
};
|
||||
|
||||
CommonJSFormatter.prototype.exportSpecifier = function (specifier, node, nodes) {
|
||||
return this._exportSpecifier(function () {
|
||||
return b.callExpression(b.identifier("require"), [node.source]);
|
||||
}, specifier, node, nodes);
|
||||
};
|
||||
47
lib/6to5/modules/umd.js
Normal file
47
lib/6to5/modules/umd.js
Normal file
@@ -0,0 +1,47 @@
|
||||
module.exports = UMDFormatter;
|
||||
|
||||
var AMDFormatter = require("./amd");
|
||||
var util = require("../util");
|
||||
var b = require("acorn-ast-types").builders;
|
||||
var _ = require("lodash");
|
||||
|
||||
function UMDFormatter(file) {
|
||||
this.file = file;
|
||||
this.ids = {};
|
||||
}
|
||||
|
||||
util.inherits(UMDFormatter, AMDFormatter);
|
||||
|
||||
UMDFormatter.prototype.transform = function (ast) {
|
||||
var program = ast.program;
|
||||
var body = program.body;
|
||||
|
||||
// build an array of module names
|
||||
|
||||
var names = [];
|
||||
_.each(this.ids, function (id, name) {
|
||||
names.push(b.literal(name));
|
||||
});
|
||||
|
||||
// factory
|
||||
|
||||
var ids = _.values(this.ids);
|
||||
var args = [b.identifier("exports")].concat(ids);
|
||||
|
||||
var factory = b.functionExpression(null, args, b.blockStatement(body));
|
||||
|
||||
// runner
|
||||
|
||||
var runner = util.template("umd-runner-body", {
|
||||
AMD_ARGUMENTS: b.arrayExpression([b.literal("exports")].concat(names)),
|
||||
|
||||
COMMON_ARGUMENTS: names.map(function (name) {
|
||||
return b.callExpression(b.identifier("require"), [name]);
|
||||
})
|
||||
});
|
||||
|
||||
//
|
||||
|
||||
var call = b.callExpression(runner, [factory]);
|
||||
program.body = [b.expressionStatement(call)];
|
||||
};
|
||||
@@ -1,3 +1,3 @@
|
||||
require("es6-symbol/implement");
|
||||
require("es6-shim");
|
||||
require("regenerator").runtime();
|
||||
require("regenerator/runtime");
|
||||
|
||||
@@ -21,7 +21,7 @@ sourceMapSupport.install({
|
||||
//
|
||||
|
||||
var ignoreRegex = /node_modules/;
|
||||
var exts = [];
|
||||
var exts = {};
|
||||
var maps = {};
|
||||
var old = require.extensions[".js"];
|
||||
|
||||
@@ -40,13 +40,14 @@ var loader = function (m, filename) {
|
||||
};
|
||||
|
||||
var hookExtensions = function (_exts) {
|
||||
_.each(exts, function (ext) {
|
||||
delete require.extensions[ext];
|
||||
_.each(exts, function (old, ext) {
|
||||
require.extensions[ext] = old;
|
||||
});
|
||||
|
||||
exts = _exts;
|
||||
exts = {};
|
||||
|
||||
_.each(exts, function (ext) {
|
||||
_.each(_exts, function (ext) {
|
||||
exts[ext] = require.extensions[ext];
|
||||
require.extensions[ext] = loader;
|
||||
});
|
||||
};
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
[].concat(ARGUMENT);
|
||||
@@ -1 +0,0 @@
|
||||
CLASS_NAME.__proto__ = SUPER_NAME;
|
||||
@@ -1,8 +0,0 @@
|
||||
CLASS_NAME.prototype = Object.create(SUPER_NAME.prototype, {
|
||||
constructor: {
|
||||
value: CLASS_NAME,
|
||||
enumerable: false,
|
||||
writable: true,
|
||||
configurable: true
|
||||
}
|
||||
});
|
||||
1
lib/6to5/templates/exports-assign-key.js
Normal file
1
lib/6to5/templates/exports-assign-key.js
Normal file
@@ -0,0 +1 @@
|
||||
exports.VARIABLE_NAME = OBJECT.KEY;
|
||||
@@ -1 +0,0 @@
|
||||
exports.VARIABLE_NAME = require(MODULE_NAME).KEY;
|
||||
@@ -2,4 +2,4 @@
|
||||
for (var i in obj) {
|
||||
exports[i] = obj[i];
|
||||
}
|
||||
}(require(MODULE_NAME)));
|
||||
})(OBJECT);
|
||||
|
||||
11
lib/6to5/templates/extends.js
Normal file
11
lib/6to5/templates/extends.js
Normal file
@@ -0,0 +1,11 @@
|
||||
(function (child, parent) {
|
||||
child.prototype = Object.create(parent.prototype, {
|
||||
constructor: {
|
||||
value: child,
|
||||
enumerable: false,
|
||||
writable: true,
|
||||
configurable: true
|
||||
}
|
||||
});
|
||||
child.__proto__ = parent;
|
||||
})
|
||||
7
lib/6to5/templates/umd-runner-body.js
Normal file
7
lib/6to5/templates/umd-runner-body.js
Normal file
@@ -0,0 +1,7 @@
|
||||
(function (factory) {
|
||||
if (typeof define === "function" && define.amd) {
|
||||
define(AMD_ARGUMENTS, factory);
|
||||
} else if (typeof exports !== "undefined") {
|
||||
factory(exports, COMMON_ARGUMENTS);
|
||||
}
|
||||
});
|
||||
@@ -2,7 +2,7 @@ module.exports = transform;
|
||||
|
||||
var Transformer = require("./transformer");
|
||||
var sourceMap = require("source-map");
|
||||
var recast = require("recast");
|
||||
var recast = require("acorn-recast");
|
||||
var File = require("./file");
|
||||
var util = require("./util");
|
||||
var _ = require("lodash");
|
||||
@@ -71,7 +71,7 @@ transform.test = function (task, assert) {
|
||||
|
||||
transform._ensureTransformerNames = function (type, keys) {
|
||||
_.each(keys, function (key) {
|
||||
if (key[0] === "_" || !_.has(transform.transformers, key)) {
|
||||
if (!_.has(transform.transformers, key)) {
|
||||
throw new ReferenceError("unknown transformer " + key + " specified in " + type);
|
||||
}
|
||||
});
|
||||
@@ -102,10 +102,17 @@ transform.transformers = {
|
||||
_aliasFunctions: require("./transformers/_alias-functions"),
|
||||
_blockHoist: require("./transformers/_block-hoist"),
|
||||
_declarations: require("./transformers/_declarations"),
|
||||
_moduleFormatter: require("./transformers/_module-formatter"),
|
||||
|
||||
useStrict: require("./transformers/use-strict")
|
||||
};
|
||||
|
||||
transform.moduleFormatters = {
|
||||
common: require("./modules/common"),
|
||||
amd: require("./modules/amd"),
|
||||
umd: require("./modules/umd")
|
||||
};
|
||||
|
||||
_.each(transform.transformers, function (transformer, key) {
|
||||
transform.transformers[key] = new Transformer(key, transformer);
|
||||
});
|
||||
|
||||
@@ -11,13 +11,13 @@ function Transformer(key, transformer) {
|
||||
Transformer.normalise = function (transformer) {
|
||||
if (_.isFunction(transformer)) {
|
||||
transformer = { ast: transformer };
|
||||
} else {
|
||||
_.each(transformer, function (fns, type) {
|
||||
if (type === "ast") return;
|
||||
if (_.isFunction(fns)) fns = { enter: fns };
|
||||
transformer[type] = fns;
|
||||
});
|
||||
}
|
||||
|
||||
_.each(transformer, function (fns, type) {
|
||||
if (_.isFunction(fns)) fns = { enter: fns };
|
||||
transformer[type] = fns;
|
||||
});
|
||||
|
||||
return transformer;
|
||||
};
|
||||
|
||||
@@ -27,8 +27,8 @@ Transformer.prototype.transform = function (file) {
|
||||
var transformer = this.transformer;
|
||||
var ast = file.ast;
|
||||
|
||||
if (transformer.ast) {
|
||||
transformer.ast(ast, file);
|
||||
if (transformer.ast && transformer.ast.enter) {
|
||||
transformer.ast.enter(ast, file);
|
||||
}
|
||||
|
||||
var build = function (exit) {
|
||||
@@ -57,11 +57,13 @@ Transformer.prototype.transform = function (file) {
|
||||
enter: build(),
|
||||
exit: build(true)
|
||||
});
|
||||
|
||||
if (transformer.ast && transformer.ast.exit) {
|
||||
transformer.ast.exit(ast, file);
|
||||
}
|
||||
};
|
||||
|
||||
Transformer.prototype.canRun = function (file) {
|
||||
if (this.key[0] === "_") return true;
|
||||
|
||||
var opts = file.opts;
|
||||
|
||||
var blacklist = opts.blacklist;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
var traverse = require("../traverse");
|
||||
var util = require("../util");
|
||||
var b = require("recast").types.builders;
|
||||
var b = require("acorn-ast-types").builders;
|
||||
|
||||
var go = function (getBody, node, file) {
|
||||
var argumentsId;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
var b = require("recast").types.builders;
|
||||
var b = require("acorn-ast-types").builders;
|
||||
var _ = require("lodash");
|
||||
|
||||
module.exports = function (ast, file) {
|
||||
|
||||
11
lib/6to5/transformers/_module-formatter.js
Normal file
11
lib/6to5/transformers/_module-formatter.js
Normal file
@@ -0,0 +1,11 @@
|
||||
var transform = require("../transform");
|
||||
|
||||
exports.ast = {
|
||||
exit: function (ast, file) {
|
||||
if (!transform.transformers.modules.canRun(file)) return;
|
||||
|
||||
if (file.moduleFormatter.transform) {
|
||||
file.moduleFormatter.transform(ast);
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -1,6 +1,5 @@
|
||||
var util = require("../util");
|
||||
var b = require("recast").types.builders;
|
||||
var _ = require("lodash");
|
||||
var b = require("acorn-ast-types").builders;
|
||||
|
||||
var single = function (node) {
|
||||
var block = node.blocks[0];
|
||||
@@ -68,12 +67,6 @@ var multiple = function (node, file) {
|
||||
};
|
||||
|
||||
exports.ComprehensionExpression = function (node, parent, file) {
|
||||
_.each(node.blocks, function (block) {
|
||||
if (!block.of) {
|
||||
throw util.errorWithNode(block, "for-in array comprehension is not supported");
|
||||
}
|
||||
});
|
||||
|
||||
if (node.blocks.length === 1) {
|
||||
return single(node);
|
||||
} else {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
var traverse = require("../traverse");
|
||||
var util = require("../util");
|
||||
var b = require("recast").types.builders;
|
||||
var b = require("acorn-ast-types").builders;
|
||||
var _ = require("lodash");
|
||||
|
||||
exports.ClassDeclaration = function (node, parent, file) {
|
||||
@@ -50,17 +50,7 @@ var buildClass = function (node, file) {
|
||||
var returnStatement = body.pop();
|
||||
|
||||
if (superName) {
|
||||
// inherit prototype
|
||||
body.push(util.template("class-inherits-prototype", {
|
||||
SUPER_NAME: superName,
|
||||
CLASS_NAME: className
|
||||
}, true));
|
||||
|
||||
// inherit static properties
|
||||
body.push(util.template("class-inherits-properties", {
|
||||
SUPER_NAME: superName,
|
||||
CLASS_NAME: className
|
||||
}, true));
|
||||
body.push(b.expressionStatement(b.callExpression(file.addDeclaration("extends"), [className, superName])));
|
||||
|
||||
container.arguments.push(superClassArgument);
|
||||
container.callee.params.push(superClassCallee);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
var util = require("../util");
|
||||
var b = require("recast").types.builders;
|
||||
var b = require("acorn-ast-types").builders;
|
||||
var _ = require("lodash");
|
||||
|
||||
exports.ObjectExpression = function (node, parent, file) {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
var traverse = require("../traverse");
|
||||
var util = require("../util");
|
||||
var b = require("recast").types.builders;
|
||||
var b = require("acorn-ast-types").builders;
|
||||
var _ = require("lodash");
|
||||
|
||||
var buildVariableAssign = function (kind, id, init) {
|
||||
@@ -42,6 +42,8 @@ var pushArrayPattern = function (kind, nodes, pattern, parentId) {
|
||||
|
||||
if (elem.type === "Identifier") {
|
||||
nodes.push(buildVariableAssign(kind, elem, newPatternId));
|
||||
} else if (elem.type === "MemberExpression") {
|
||||
nodes.push(buildVariableAssign(false, elem, newPatternId));
|
||||
} else {
|
||||
push(kind, nodes, elem, newPatternId);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
var util = require("../util");
|
||||
var b = require("recast").types.builders;
|
||||
var b = require("acorn-ast-types").builders;
|
||||
|
||||
exports.ForOfStatement = function (node, parent, file) {
|
||||
var left = node.left;
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// https://github.com/RReverser/jsx-transpiler
|
||||
|
||||
var esutils = require("esutils");
|
||||
var b = require("recast").types.builders;
|
||||
var b = require("acorn-ast-types").builders;
|
||||
var _ = require("lodash");
|
||||
|
||||
var JSX_ANNOTATION_REGEX = /^\*\s*@jsx\s+([^\s]+)/;
|
||||
@@ -85,13 +85,11 @@ exports.XJSElement = {
|
||||
exit: function (node) {
|
||||
var callExpr = node.openingElement;
|
||||
var children = node.children;
|
||||
var args = callExpr.arguments;
|
||||
|
||||
switch (children.length) {
|
||||
case 0: break;
|
||||
case 1: args.push(children[0]); break;
|
||||
default: args.push(b.arrayExpression(children));
|
||||
}
|
||||
_.each(children, function (child) {
|
||||
delete child.raw;
|
||||
callExpr.arguments.push(child);
|
||||
});
|
||||
|
||||
callExpr.loc = node.loc;
|
||||
return callExpr;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
var traverse = require("../traverse");
|
||||
var util = require("../util");
|
||||
var b = require("recast").types.builders;
|
||||
var b = require("acorn-ast-types").builders;
|
||||
var _ = require("lodash");
|
||||
|
||||
exports.VariableDeclaration = function (node, parent, file) {
|
||||
|
||||
@@ -1,110 +1,28 @@
|
||||
var util = require("../util");
|
||||
var b = require("recast").types.builders;
|
||||
var _ = require("lodash");
|
||||
var _ = require("lodash");
|
||||
|
||||
exports.ImportDeclaration = function (node) {
|
||||
exports.ImportDeclaration = function (node, parent, file) {
|
||||
var nodes = [];
|
||||
|
||||
if (node.specifiers.length) {
|
||||
_.each(node.specifiers, function (specifier) {
|
||||
var variableName = getSpecifierName(specifier);
|
||||
var key = specifier.id.name;
|
||||
|
||||
// import foo from "foo";
|
||||
if (specifier.type === "ImportDefaultSpecifier") {
|
||||
key = b.identifier("default");
|
||||
}
|
||||
|
||||
var templateName = "require-assign";
|
||||
|
||||
// import * as bar from "foo";
|
||||
if (specifier.type !== "ImportNamespaceSpecifier") templateName += "-key";
|
||||
|
||||
nodes.push(util.template(templateName, {
|
||||
VARIABLE_NAME: variableName.name,
|
||||
MODULE_NAME: node.source.raw,
|
||||
KEY: key
|
||||
}));
|
||||
file.moduleFormatter.importSpecifier(specifier, node, nodes);
|
||||
});
|
||||
} else {
|
||||
// import "foo";
|
||||
nodes.push(util.template("require", {
|
||||
MODULE_NAME: node.source.raw
|
||||
}, true));
|
||||
file.moduleFormatter.import(node, nodes);
|
||||
}
|
||||
|
||||
return nodes;
|
||||
};
|
||||
|
||||
var pushExportSpecifiers = function (node, nodes) {
|
||||
_.each(node.specifiers, function (specifier) {
|
||||
var variableName = getSpecifierName(specifier);
|
||||
|
||||
if (node.source) {
|
||||
if (specifier.type === "ExportBatchSpecifier") {
|
||||
// export * from "foo";
|
||||
nodes.push(util.template("exports-wildcard", {
|
||||
MODULE_NAME: node.source.raw
|
||||
}, true));
|
||||
} else {
|
||||
// export { foo } from "test";
|
||||
nodes.push(util.template("exports-require-assign-key", {
|
||||
VARIABLE_NAME: variableName.name,
|
||||
MODULE_NAME: node.source.raw,
|
||||
KEY: specifier.id
|
||||
}, true));
|
||||
}
|
||||
} else {
|
||||
// export { foo };
|
||||
nodes.push(util.template("exports-assign", {
|
||||
VALUE: specifier.id,
|
||||
KEY: variableName
|
||||
}, true));
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
var getSpecifierName = function (specifier) {
|
||||
return specifier.name || specifier.id;
|
||||
};
|
||||
|
||||
var pushExportDeclaration = function (node, parent, nodes) {
|
||||
var declar = node.declaration;
|
||||
|
||||
if (node.default) {
|
||||
util.ensureExpressionType(declar);
|
||||
|
||||
nodes.push(util.template("exports-default", {
|
||||
VALUE: declar
|
||||
}, true));
|
||||
} else {
|
||||
var id = declar.id;
|
||||
if (declar.type === "VariableDeclaration") {
|
||||
id = declar.declarations[0].id;
|
||||
}
|
||||
|
||||
var assign = util.template("exports-assign", {
|
||||
VALUE: id,
|
||||
KEY: id
|
||||
}, true);
|
||||
|
||||
nodes.push(declar);
|
||||
|
||||
if (declar.type === "FunctionDeclaration") {
|
||||
assign._blockHoist = true;
|
||||
}
|
||||
|
||||
nodes.push(assign);
|
||||
}
|
||||
};
|
||||
|
||||
exports.ExportDeclaration = function (node, parent) {
|
||||
exports.ExportDeclaration = function (node, parent, file) {
|
||||
var nodes = [];
|
||||
|
||||
if (node.declaration) {
|
||||
pushExportDeclaration(node, parent, nodes);
|
||||
file.moduleFormatter.export(node, nodes);
|
||||
} else {
|
||||
pushExportSpecifiers(node, nodes);
|
||||
_.each(node.specifiers, function (specifier) {
|
||||
file.moduleFormatter.exportSpecifier(specifier, node, nodes);
|
||||
});
|
||||
}
|
||||
|
||||
return nodes;
|
||||
|
||||
2
lib/6to5/transformers/react.js
vendored
2
lib/6to5/transformers/react.js
vendored
@@ -1,4 +1,4 @@
|
||||
var b = require("recast").types.builders;
|
||||
var b = require("acorn-ast-types").builders;
|
||||
var _ = require("lodash");
|
||||
|
||||
var addDisplayName = function (id, call) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
var util = require("../util");
|
||||
var b = require("recast").types.builders;
|
||||
var b = require("acorn-ast-types").builders;
|
||||
|
||||
exports.Function = function (node, parent, file) {
|
||||
if (!node.rest) return;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
var util = require("../util");
|
||||
var b = require("recast").types.builders;
|
||||
var b = require("acorn-ast-types").builders;
|
||||
var _ = require("lodash");
|
||||
|
||||
var getSpreadLiteral = function (spread, file) {
|
||||
@@ -14,42 +14,68 @@ var getSpreadLiteral = function (spread, file) {
|
||||
};
|
||||
|
||||
var hasSpread = function (nodes) {
|
||||
return nodes.length && _.last(nodes).type === "SpreadElement";
|
||||
var has = false;
|
||||
_.each(nodes, function (node) {
|
||||
if (node.type === "SpreadElement") {
|
||||
has = true;
|
||||
return false;
|
||||
}
|
||||
});
|
||||
return has;
|
||||
};
|
||||
|
||||
var build = function (props, file) {
|
||||
var nodes = [];
|
||||
|
||||
var _props = [];
|
||||
|
||||
var push = function () {
|
||||
if (!_props.length) return;
|
||||
nodes.push(b.arrayExpression(_props));
|
||||
_props = [];
|
||||
};
|
||||
|
||||
_.each(props, function (prop) {
|
||||
if (prop.type === "SpreadElement") {
|
||||
push();
|
||||
nodes.push(getSpreadLiteral(prop, file));
|
||||
} else {
|
||||
_props.push(prop);
|
||||
}
|
||||
});
|
||||
|
||||
push();
|
||||
|
||||
return nodes;
|
||||
};
|
||||
|
||||
exports.ArrayExpression = function (node, parent, file) {
|
||||
var elements = node.elements;
|
||||
if (!hasSpread(elements)) return;
|
||||
|
||||
var spread = elements.pop();
|
||||
var nodes = build(elements, file);
|
||||
var first = nodes.shift();
|
||||
|
||||
var concat = util.template("array-concat", {
|
||||
ARGUMENT: getSpreadLiteral(spread, file)
|
||||
});
|
||||
if (!nodes.length) return first;
|
||||
|
||||
concat.callee.object.elements = elements;
|
||||
|
||||
return concat;
|
||||
return b.callExpression(b.memberExpression(first, b.identifier("concat"), false), nodes);
|
||||
};
|
||||
|
||||
exports.CallExpression = function (node, parent, file) {
|
||||
var args = node.arguments;
|
||||
if (!hasSpread(args)) return;
|
||||
|
||||
var spread = args.pop();
|
||||
|
||||
var spreadLiteral = getSpreadLiteral(spread, file);
|
||||
var contextLiteral = b.literal(null);
|
||||
|
||||
node.arguments = [];
|
||||
|
||||
if (args.length) {
|
||||
var concat = util.template("array-concat");
|
||||
concat.arguments = [spreadLiteral];
|
||||
concat.callee.object.elements = args;
|
||||
node.arguments.push(concat);
|
||||
var nodes = build(args, file);
|
||||
var first = nodes.shift();
|
||||
|
||||
if (nodes.length) {
|
||||
node.arguments.push(b.callExpression(b.memberExpression(first, b.identifier("concat"), false), nodes));
|
||||
} else {
|
||||
node.arguments.push(spreadLiteral);
|
||||
node.arguments.push(first);
|
||||
}
|
||||
|
||||
var callee = node.callee;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
var b = require("recast").types.builders;
|
||||
var b = require("acorn-ast-types").builders;
|
||||
var _ = require("lodash");
|
||||
|
||||
var buildBinaryExpression = function (left, right) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
var rewritePattern = require("regexpu/rewrite-pattern");
|
||||
var b = require("recast").types.builders;
|
||||
var b = require("acorn-ast-types").builders;
|
||||
var _ = require("lodash");
|
||||
|
||||
exports.Literal = function (node) {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
var b = require("recast").types.builders;
|
||||
var b = require("acorn-ast-types").builders;
|
||||
|
||||
module.exports = function (ast, file) {
|
||||
var body = ast.program.body;
|
||||
|
||||
@@ -1,13 +1,16 @@
|
||||
var traverse = require("./traverse");
|
||||
var astTypes = require("recast").types;
|
||||
var recast = require("recast");
|
||||
var astTypes = require("acorn-ast-types");
|
||||
var recast = require("acorn-recast");
|
||||
var path = require("path");
|
||||
var util = require("util");
|
||||
var fs = require("fs");
|
||||
var _ = require("lodash");
|
||||
|
||||
var n = astTypes.namedTypes;
|
||||
var b = astTypes.builders;
|
||||
|
||||
exports.inherits = util.inherits;
|
||||
|
||||
exports.ensureBlock = function (node) {
|
||||
var block = node.body;
|
||||
if (block.type === "BlockStatement") return;
|
||||
@@ -20,10 +23,22 @@ exports.ensureBlock = function (node) {
|
||||
node.body = b.blockStatement(block);
|
||||
};
|
||||
|
||||
exports.resolve = function (loc) {
|
||||
try {
|
||||
return require.resolve(loc);
|
||||
} catch (err) {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
exports.list = function (val) {
|
||||
return val ? val.split(",") : [];
|
||||
};
|
||||
|
||||
exports.getSpecifierName = function (specifier) {
|
||||
return specifier.name || specifier.id;
|
||||
};
|
||||
|
||||
exports.ensureExpressionType = function (node) {
|
||||
node.type = {
|
||||
FunctionDeclaration: "FunctionExpression",
|
||||
@@ -107,7 +122,7 @@ exports.errorWithNode = function (node, msg) {
|
||||
|
||||
exports.canCompile = function (filename) {
|
||||
var ext = path.extname(filename);
|
||||
return _.contains([".js", ".es6", ".jsx"], ext);
|
||||
return _.contains([".js", ".es6"], ext);
|
||||
};
|
||||
|
||||
exports.sourceMapToComment = function (map) {
|
||||
@@ -237,6 +252,7 @@ exports.repeat = function (width, cha) {
|
||||
exports.parse = function (opts, code, callback) {
|
||||
try {
|
||||
var recastOpts = {};
|
||||
|
||||
if (opts.sourceMap) {
|
||||
recastOpts.sourceFileName = opts.sourceFileName;
|
||||
recastOpts.sourceRoot = opts.sourceRoot;
|
||||
@@ -252,14 +268,15 @@ exports.parse = function (opts, code, callback) {
|
||||
} catch (err) {
|
||||
if (!err._6to5) {
|
||||
err._6to5 = true;
|
||||
err.message = opts.filename + ": " + err.message;
|
||||
var message = opts.filename + ": " + err.message;
|
||||
|
||||
if (err.lineNumber) {
|
||||
var frame = exports.codeFrame(code, err.lineNumber, err.column);
|
||||
var err2 = new SyntaxError(err.message + frame);
|
||||
err2._6to5 = true;
|
||||
throw err2;
|
||||
if (err.loc) {
|
||||
var frame = exports.codeFrame(code, err.loc.line, err.loc.column);
|
||||
message += frame;
|
||||
}
|
||||
|
||||
err.stack = err.stack.replace(err.message, message);
|
||||
err.message = message;
|
||||
}
|
||||
|
||||
throw err;
|
||||
|
||||
18
package.json
18
package.json
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "6to5",
|
||||
"description": "Turn ES6 code into readable vanilla ES5 with source maps",
|
||||
"version": "1.9.9",
|
||||
"version": "1.10.4",
|
||||
"author": "Sebastian McKenzie <sebmck@gmail.com>",
|
||||
"homepage": "https://github.com/sebmck/6to5",
|
||||
"repository": {
|
||||
@@ -34,7 +34,7 @@
|
||||
"test": "make test"
|
||||
},
|
||||
"dependencies": {
|
||||
"commander": "2.3.0",
|
||||
"commander": "2.4.0",
|
||||
"fs-readdir-recursive": "0.0.2",
|
||||
"lodash": "2.4.1",
|
||||
"mkdirp": "0.5.0",
|
||||
@@ -44,19 +44,17 @@
|
||||
"recast": "0.8.0",
|
||||
"source-map": "0.1.40",
|
||||
"regenerator": "0.6.7",
|
||||
"chokidar": "^0.9.0",
|
||||
"source-map-support": "^0.2.7",
|
||||
"esutils": "^1.1.4"
|
||||
"chokidar": "0.10.0",
|
||||
"source-map-support": "0.2.7",
|
||||
"esutils": "1.1.4",
|
||||
"acorn-jsx": "https://github.com/sebmck/acorn-jsx/archive/master.tar.gz",
|
||||
"acorn-recast": "0.8.0-3",
|
||||
"acorn-ast-types": "0.5.3-1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"es6-transpiler": "0.7.17",
|
||||
"istanbul": "0.3.2",
|
||||
"matcha": "0.5.0",
|
||||
"mocha": "1.21.5",
|
||||
"traceur": "0.0.67",
|
||||
"esnext": "0.11.1",
|
||||
"es6now": "0.8.11",
|
||||
"jstransform": "6.3.2",
|
||||
"uglify-js": "2.4.15",
|
||||
"browserify": "6.1.0",
|
||||
"proclaim": "2.0.0",
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
var fs = require("fs");
|
||||
var _ = require("lodash");
|
||||
|
||||
var fixturesDir = __dirname + "/fixtures/syntax";
|
||||
var fixturesDir = __dirname + "/fixtures/transformation";
|
||||
|
||||
var humanise = function (val) {
|
||||
return val.replace(/-/g, " ");
|
||||
@@ -17,7 +17,7 @@ var readFile = function (filename) {
|
||||
|
||||
exports.run = function (suites, transform, assert) {
|
||||
_.each(suites, function (testSuite) {
|
||||
suite("syntax/" + testSuite.title, function () {
|
||||
suite("transformation/" + testSuite.title, function () {
|
||||
_.each(testSuite.tests, function (task) {
|
||||
test(task.title, function () {
|
||||
var run = function () {
|
||||
|
||||
@@ -1 +1 @@
|
||||
Error: test.js: Line 2: Unexpected token ILLEGAL
|
||||
SyntaxError: test.js: Unexpected character '@'
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
var arr = [for (i in [1, 2, 3]) i * i];
|
||||
@@ -1,3 +0,0 @@
|
||||
{
|
||||
"throws": "for-in array comprehension is not supported"
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
class Test extends Foo {
|
||||
constructor() {
|
||||
woops.super.test();
|
||||
super();
|
||||
super.test();
|
||||
foob(super);
|
||||
|
||||
super(...arguments);
|
||||
super("test", ...arguments);
|
||||
|
||||
super.test(...arguments);
|
||||
super.test("test", ...arguments);
|
||||
}
|
||||
|
||||
test() {
|
||||
super();
|
||||
super(...arguments);
|
||||
super("test", ...arguments);
|
||||
}
|
||||
|
||||
static foo() {
|
||||
super();
|
||||
super(...arguments);
|
||||
super("test", ...arguments);
|
||||
}
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
{
|
||||
"throws": "Line 2: Unexpected token ILLEGAL"
|
||||
}
|
||||
0
test/fixtures/transformation/api/blacklist/untitled
vendored
Normal file
0
test/fixtures/transformation/api/blacklist/untitled
vendored
Normal file
5
test/fixtures/transformation/arrow-functions/default-parameters/actual.js
vendored
Normal file
5
test/fixtures/transformation/arrow-functions/default-parameters/actual.js
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
var some = (count = "30") => {
|
||||
console.log("count", count);
|
||||
};
|
||||
|
||||
some();
|
||||
8
test/fixtures/transformation/arrow-functions/default-parameters/expected.js
vendored
Normal file
8
test/fixtures/transformation/arrow-functions/default-parameters/expected.js
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
"use strict";
|
||||
|
||||
var some = function (count) {
|
||||
if (count === undefined) count = "30";
|
||||
console.log("count", count);
|
||||
};
|
||||
|
||||
some();
|
||||
2
test/fixtures/transformation/arrow-functions/destructuring-parameters/actual.js
vendored
Normal file
2
test/fixtures/transformation/arrow-functions/destructuring-parameters/actual.js
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
var a = ({ target }) => console.log(target);
|
||||
a({ target: "I am a target" });
|
||||
6
test/fixtures/transformation/arrow-functions/destructuring-parameters/expected.js
vendored
Normal file
6
test/fixtures/transformation/arrow-functions/destructuring-parameters/expected.js
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
"use strict";
|
||||
var a = function (_ref) {
|
||||
var target = _ref.target;
|
||||
return console.log(target);
|
||||
};
|
||||
a({ target: "I am a target" });
|
||||
26
test/fixtures/transformation/classes/accessing-super-class/actual.js
vendored
Normal file
26
test/fixtures/transformation/classes/accessing-super-class/actual.js
vendored
Normal file
@@ -0,0 +1,26 @@
|
||||
class Test extends Foo {
|
||||
constructor() {
|
||||
woops.super.test();
|
||||
super();
|
||||
super.test();
|
||||
foob(super);
|
||||
|
||||
super(...arguments);
|
||||
super("test", ...arguments);
|
||||
|
||||
super.test(...arguments);
|
||||
super.test("test", ...arguments);
|
||||
}
|
||||
|
||||
test() {
|
||||
super();
|
||||
super(...arguments);
|
||||
super("test", ...arguments);
|
||||
}
|
||||
|
||||
static foo() {
|
||||
super();
|
||||
super(...arguments);
|
||||
super("test", ...arguments);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,17 @@
|
||||
"use strict";
|
||||
var _slice = Array.prototype.slice;
|
||||
var _extends = function (child, parent) {
|
||||
child.prototype = Object.create(parent.prototype, {
|
||||
constructor: {
|
||||
value: child,
|
||||
enumerable: false,
|
||||
writable: true,
|
||||
configurable: true
|
||||
}
|
||||
});
|
||||
|
||||
child.__proto__ = parent;
|
||||
};
|
||||
|
||||
var Test = function(Foo) {
|
||||
var Test = function Test() {
|
||||
@@ -13,16 +25,7 @@ var Test = function(Foo) {
|
||||
Foo.prototype.test.call.apply(Foo.prototype, [this, "test"].concat(_slice.call(arguments)));
|
||||
};
|
||||
|
||||
Test.prototype = Object.create(Foo.prototype, {
|
||||
constructor: {
|
||||
value: Test,
|
||||
enumerable: false,
|
||||
writable: true,
|
||||
configurable: true
|
||||
}
|
||||
});
|
||||
|
||||
Test.__proto__ = Foo;
|
||||
_extends(Test, Foo);
|
||||
|
||||
Object.defineProperties(Test.prototype, {
|
||||
test: {
|
||||
@@ -49,4 +52,4 @@ var Test = function(Foo) {
|
||||
});
|
||||
|
||||
return Test;
|
||||
}(Foo);
|
||||
}(Foo);
|
||||
@@ -1,6 +1,6 @@
|
||||
class Test extends Foo {
|
||||
constructor() {
|
||||
super.test;
|
||||
super.test.whatever;
|
||||
super.test;
|
||||
super.test.whatever;
|
||||
}
|
||||
}
|
||||
@@ -1,20 +1,25 @@
|
||||
"use strict";
|
||||
|
||||
var _extends = function (child, parent) {
|
||||
child.prototype = Object.create(parent.prototype, {
|
||||
constructor: {
|
||||
value: child,
|
||||
enumerable: false,
|
||||
writable: true,
|
||||
configurable: true
|
||||
}
|
||||
});
|
||||
|
||||
child.__proto__ = parent;
|
||||
};
|
||||
|
||||
var Test = function(Foo) {
|
||||
var Test = function Test() {
|
||||
Foo.prototype.test;
|
||||
Foo.prototype.test.whatever;
|
||||
};
|
||||
|
||||
Test.prototype = Object.create(Foo.prototype, {
|
||||
constructor: {
|
||||
value: Test,
|
||||
enumerable: false,
|
||||
writable: true,
|
||||
configurable: true
|
||||
}
|
||||
});
|
||||
_extends(Test, Foo);
|
||||
|
||||
Test.__proto__ = Foo;
|
||||
return Test;
|
||||
}(Foo);
|
||||
}(Foo);
|
||||
@@ -1,10 +1,10 @@
|
||||
class Test extends Foo {
|
||||
constructor() {
|
||||
super.test.whatever();
|
||||
super.test();
|
||||
super.test.whatever();
|
||||
super.test();
|
||||
}
|
||||
|
||||
static test() {
|
||||
return super.wow();
|
||||
return super.wow();
|
||||
}
|
||||
}
|
||||
@@ -1,21 +1,26 @@
|
||||
"use strict";
|
||||
|
||||
var _extends = function (child, parent) {
|
||||
child.prototype = Object.create(parent.prototype, {
|
||||
constructor: {
|
||||
value: child,
|
||||
enumerable: false,
|
||||
writable: true,
|
||||
configurable: true
|
||||
}
|
||||
});
|
||||
|
||||
child.__proto__ = parent;
|
||||
};
|
||||
|
||||
|
||||
var Test = function(Foo) {
|
||||
var Test = function Test() {
|
||||
Foo.prototype.test.whatever();
|
||||
Foo.prototype.test.call(this);
|
||||
};
|
||||
|
||||
Test.prototype = Object.create(Foo.prototype, {
|
||||
constructor: {
|
||||
value: Test,
|
||||
enumerable: false,
|
||||
writable: true,
|
||||
configurable: true
|
||||
}
|
||||
});
|
||||
|
||||
Test.__proto__ = Foo;
|
||||
_extends(Test, Foo);
|
||||
|
||||
Object.defineProperties(Test, {
|
||||
test: {
|
||||
@@ -28,4 +33,4 @@ var Test = function(Foo) {
|
||||
});
|
||||
|
||||
return Test;
|
||||
}(Foo);
|
||||
}(Foo);
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user