Compare commits
56 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a29505f75d | ||
|
|
05942d17bf | ||
|
|
be0d4b344b | ||
|
|
f1a2401681 | ||
|
|
2d61672cdb | ||
|
|
1b00ba21a1 | ||
|
|
f8ea386f3c | ||
|
|
2527fffbad | ||
|
|
86498ad990 | ||
|
|
27c8804214 | ||
|
|
2b6f0ee780 | ||
|
|
c7c9660c79 | ||
|
|
ff025e63ec | ||
|
|
9fb6681ad3 | ||
|
|
301887f88d | ||
|
|
b519bb6a68 | ||
|
|
b6e366ad94 | ||
|
|
ba2204156c | ||
|
|
2545292bce | ||
|
|
c763cf364c | ||
|
|
1db7247d9a | ||
|
|
1a1e6bc3ba | ||
|
|
d184bc93b9 | ||
|
|
e12cca974a | ||
|
|
b17bc95b4f | ||
|
|
a71f260377 | ||
|
|
8cbc1f7f06 | ||
|
|
34335018e9 | ||
|
|
136bddab33 | ||
|
|
b2951c5462 | ||
|
|
07de6f5f4a | ||
|
|
99ea00ca18 | ||
|
|
ab5c6a38eb | ||
|
|
52cee84625 | ||
|
|
20dc8b05c9 | ||
|
|
ba5992621d | ||
|
|
cdae98f653 | ||
|
|
81434bb557 | ||
|
|
3af0fc6fb7 | ||
|
|
3f60062ab6 | ||
|
|
76499bb26e | ||
|
|
44f2f701e1 | ||
|
|
032bc0af5e | ||
|
|
0e822a7e54 | ||
|
|
51e521f0e0 | ||
|
|
e5ce69d6af | ||
|
|
e3b8fa93e2 | ||
|
|
f4ce3a23ad | ||
|
|
39f4696ac7 | ||
|
|
4a9918ec6b | ||
|
|
c87e14f6a9 | ||
|
|
680771c81b | ||
|
|
cad3f63723 | ||
|
|
eceda64528 | ||
|
|
b8ec87e058 | ||
|
|
04bd023787 |
36
CHANGELOG.md
36
CHANGELOG.md
@@ -1,3 +1,39 @@
|
||||
# 1.14.3
|
||||
|
||||
* Add playground pretzel maps.
|
||||
|
||||
# 1.14.2
|
||||
|
||||
* Fix `commonInterop` default export handling.
|
||||
* Fix keyworded property key identifiers being turned into computed property key literals.
|
||||
|
||||
# 1.14.1
|
||||
|
||||
* Inherit comments from `ClassDeclaration`.
|
||||
|
||||
# 1.14.0
|
||||
|
||||
* Add [playground](https://6to5.github.io/playground.html).
|
||||
|
||||
# 1.13.13
|
||||
|
||||
* Fix `--debug` in `bin/6to5-node`. Thanks [@timoxley](https://github.com/timoxley).
|
||||
|
||||
# 1.13.12
|
||||
|
||||
* Ignore `XJSEmptyExpression`s in `react` transformer output.
|
||||
|
||||
# 1.13.11
|
||||
|
||||
* Fix `util.regexify` on falsy values.
|
||||
* Fix `_aliasFunction` with rest parameters.
|
||||
* Export as `module.exports` instead of `exports.default` if there are no other `ExportDeclaration`s in `commonInterop` module formatter.
|
||||
* Add `system` module formatter. Thanks [@douglasduteil](https://github.com/douglasduteil).
|
||||
|
||||
# 1.13.10
|
||||
|
||||
* Add support for `AssignmentExpression` destructuring outside of `ExpressionStatement`.
|
||||
|
||||
# 1.13.9
|
||||
|
||||
* Fix `VirtualPropertyExpression` visitor keys.
|
||||
|
||||
@@ -9,7 +9,6 @@
|
||||
* CamelCase all class names
|
||||
* camelBack all variable names
|
||||
* **Spacing**
|
||||
* 80 character line max
|
||||
* Spaces after all keywords
|
||||
* Spaces before all left curly braces
|
||||
* **Comments**
|
||||
|
||||
@@ -14,13 +14,11 @@ process.argv.slice(2).forEach(function(arg){
|
||||
switch (flag) {
|
||||
case "-d":
|
||||
args.unshift("--debug");
|
||||
args.push("--no-timeouts");
|
||||
break;
|
||||
case "debug":
|
||||
case "--debug":
|
||||
case "--debug-brk":
|
||||
args.unshift(arg);
|
||||
args.push("--no-timeouts");
|
||||
break;
|
||||
case "-gc":
|
||||
case "--expose-gc":
|
||||
|
||||
@@ -12,6 +12,7 @@ commander.option("-f, --filename [filename]", "Filename to use when reading from
|
||||
commander.option("-w, --watch", "Recompile files on changes");
|
||||
commander.option("-r, --runtime", "Replace 6to5 declarations with references to a runtime");
|
||||
commander.option("-e, --experimental", "Enable experimental support for proposed ES7 features");
|
||||
commander.option("-p, --playground", "Enable playground support");
|
||||
|
||||
commander.option("-m, --modules [modules]", "Module formatter type to use [common]", "common");
|
||||
commander.option("-w, --whitelist [whitelist]", "Whitelist of transformers to ONLY use", util.list);
|
||||
@@ -90,6 +91,7 @@ exports.opts = {
|
||||
sourceMapName: commander.outFile,
|
||||
amdModuleIds: commander.amdModuleIds,
|
||||
experimental: commander.experimental,
|
||||
playground: commander.playground,
|
||||
blacklist: commander.blacklist,
|
||||
whitelist: commander.whitelist,
|
||||
sourceMap: commander.sourceMaps || commander.sourceMapsInline,
|
||||
|
||||
@@ -13,6 +13,7 @@ commander.option("-p, --print", "Evaluate script and print result");
|
||||
commander.option("-i, --ignore [regex]", "Ignore all files that match this regex when using the require hook");
|
||||
commander.option("-x, --extensions [extensions]", "List of extensions to hook into [.es6,.js]");
|
||||
commander.option("-r, --experimental", "Enable experimental support for proposed ES7 features");
|
||||
commander.option("-g, --playground", "Enable playground support");
|
||||
|
||||
var pkg = require("../package.json");
|
||||
commander.version(pkg.version);
|
||||
|
||||
105
doc/modules.md
105
doc/modules.md
@@ -15,6 +15,13 @@ to5.transform('import "foo";', { modules: "common" });
|
||||
|
||||
## Formats
|
||||
|
||||
* [AMD](#amd)
|
||||
* [Common (Default)](#common-default)
|
||||
* [Common](#common)
|
||||
* [Ignore](#ignore)
|
||||
* [System](#system)
|
||||
* [UMD](#umd)
|
||||
|
||||
### Common (Default)
|
||||
|
||||
**In**
|
||||
@@ -51,6 +58,69 @@ var test = 5; exports.test = test;
|
||||
exports.default = test;
|
||||
```
|
||||
|
||||
### Common interop
|
||||
|
||||
**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.test = test;
|
||||
var test = exports.test = 5;
|
||||
|
||||
exports["default"] = test;
|
||||
```
|
||||
|
||||
#### module.exports behaviour
|
||||
|
||||
If there exist no other non-default `export`s then `default exports` are
|
||||
exported as `module.exports` instead of `exports.default`.
|
||||
|
||||
**In**
|
||||
|
||||
```javascript
|
||||
export default function foo() {
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
**Out**
|
||||
|
||||
```javascript
|
||||
module.exports = foo;
|
||||
|
||||
function foo() {
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
### AMD
|
||||
|
||||
**In**
|
||||
@@ -135,6 +205,41 @@ function bar() {
|
||||
}
|
||||
```
|
||||
|
||||
### System
|
||||
|
||||
**In**
|
||||
|
||||
```javascript
|
||||
import foo from "foo";
|
||||
|
||||
export function bar() {
|
||||
return foo("foobar");
|
||||
}
|
||||
```
|
||||
|
||||
**Out**
|
||||
|
||||
```javascript
|
||||
System.register("bar", ["foo"], function (_export) {
|
||||
"use strict";
|
||||
|
||||
var __moduleName = "bar";
|
||||
|
||||
var foo;
|
||||
function bar() {
|
||||
return foo("foobar");
|
||||
}
|
||||
return {
|
||||
setters: [function (m) {
|
||||
foo = m.default;
|
||||
}],
|
||||
execute: function () {
|
||||
_export("bar", bar);
|
||||
}
|
||||
};
|
||||
});
|
||||
```
|
||||
|
||||
## Custom
|
||||
|
||||
You can alternatively specify module names instead of one of the built-in types.
|
||||
|
||||
77
doc/playground.md
Normal file
77
doc/playground.md
Normal file
@@ -0,0 +1,77 @@
|
||||
# Playground
|
||||
|
||||
Playground is a proving ground for **possible** ES7 proposals.
|
||||
|
||||
**NOTE: These features are in no way endorsed by Ecma International and are not a part of ES6. They might become a part of ECMAScript in the future.**
|
||||
|
||||
## Usage
|
||||
|
||||
$ 6to5 --playground
|
||||
|
||||
```javascript
|
||||
to5.transform("code", { playground: true });
|
||||
```
|
||||
|
||||
**NOTE:** Enabling `playground` also enables [experimental support](usage.md#experimental).
|
||||
|
||||
## Features
|
||||
|
||||
* [Memoization operator](#memoization-operator)
|
||||
* [Method binding](#method-binding)
|
||||
* [Pretzel map](#pretzel-map)
|
||||
|
||||
### Memoization assignment operator
|
||||
|
||||
```javascript
|
||||
var obj = {};
|
||||
obj.x ?= 2;
|
||||
obj.x; // 2
|
||||
|
||||
obj = { x: 1 };
|
||||
obj.x ?= 2;
|
||||
obj.x; // 1
|
||||
|
||||
obj = { x: undefined }
|
||||
obj.x ?= 2;
|
||||
obj.x; // undefined
|
||||
```
|
||||
|
||||
```javascript
|
||||
var obj = {};
|
||||
obj.x ?= 2;
|
||||
```
|
||||
|
||||
equivalent to:
|
||||
|
||||
```javascript
|
||||
var obj = {};
|
||||
if (!Object.prototype.hasOwnProperty.call(obj, "x")) obj.x = 2;
|
||||
```
|
||||
|
||||
### Method binding expression
|
||||
|
||||
```javascript
|
||||
var fn = obj:method;
|
||||
var fn = obj:method("foob");
|
||||
```
|
||||
|
||||
equivalent to:
|
||||
|
||||
```javascript
|
||||
var fn = obj.method.bind(obj);
|
||||
var fn = obj.method.bind(obj, "foob");
|
||||
```
|
||||
|
||||
### Pretzel map
|
||||
|
||||
```javascript
|
||||
["foo", "bar"].map(:toUpperCase); // ["FOO", "BAR"]
|
||||
[1.1234, 23.53245, 3].map(:toFixed(2)); // ["1.12", "23.53", "3.00"]
|
||||
```
|
||||
|
||||
equivalent to:
|
||||
|
||||
```javascript
|
||||
["foo", "bar"].map(function (val) { return val.toUpperCase(); });
|
||||
[1.1234, 23.53245, 3].map(function (val) { return val.toFixed(2); });
|
||||
```
|
||||
@@ -10,9 +10,7 @@ var t = require("./types");
|
||||
var _ = require("lodash");
|
||||
|
||||
function File(opts) {
|
||||
this.opts = File.normaliseOptions(opts);
|
||||
this.moduleFormatter = this.getModuleFormatter(this.opts.modules);
|
||||
|
||||
this.opts = File.normaliseOptions(opts);
|
||||
this.uids = {};
|
||||
this.ast = {};
|
||||
}
|
||||
@@ -24,8 +22,9 @@ File.declarations = [
|
||||
"tagged-template-literal",
|
||||
"interop-require",
|
||||
"to-array",
|
||||
"arguments-to-array",
|
||||
"object-spread"
|
||||
"object-spread",
|
||||
"has-own",
|
||||
"slice"
|
||||
];
|
||||
|
||||
File.normaliseOptions = function (opts) {
|
||||
@@ -33,6 +32,7 @@ File.normaliseOptions = function (opts) {
|
||||
|
||||
_.defaults(opts, {
|
||||
experimental: false,
|
||||
playground: false,
|
||||
whitespace: true,
|
||||
blacklist: [],
|
||||
whitelist: [],
|
||||
@@ -71,6 +71,10 @@ File.normaliseOptions = function (opts) {
|
||||
opts.runtime = "to5Runtime";
|
||||
}
|
||||
|
||||
if (opts.playground) {
|
||||
opts.experimental = true;
|
||||
}
|
||||
|
||||
transform._ensureTransformerNames("blacklist", opts.blacklist);
|
||||
transform._ensureTransformerNames("whitelist", opts.whitelist);
|
||||
|
||||
@@ -80,14 +84,11 @@ File.normaliseOptions = function (opts) {
|
||||
File.prototype.toArray = function (node) {
|
||||
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 templateName = "to-array";
|
||||
if (t.isIdentifier(node) && node.name === "arguments") {
|
||||
templateName = "arguments-to-array";
|
||||
}
|
||||
|
||||
return t.callExpression(this.addDeclaration(templateName), [node]);
|
||||
};
|
||||
|
||||
File.prototype.getModuleFormatter = function (type) {
|
||||
@@ -137,7 +138,11 @@ File.prototype.addDeclaration = function (name) {
|
||||
}
|
||||
|
||||
var uid = this.generateUidIdentifier(name);
|
||||
this.scope.push(name, uid, ref);
|
||||
this.scope.push({
|
||||
key: name,
|
||||
id: uid,
|
||||
init: ref
|
||||
});
|
||||
return uid;
|
||||
};
|
||||
|
||||
@@ -168,6 +173,7 @@ File.prototype.parse = function (code) {
|
||||
File.prototype.transform = function (ast) {
|
||||
this.ast = ast;
|
||||
this.scope = new Scope(ast.program);
|
||||
this.moduleFormatter = this.getModuleFormatter(this.opts.modules);
|
||||
|
||||
var self = this;
|
||||
|
||||
|
||||
@@ -53,6 +53,7 @@ CodeGenerator.generators = {
|
||||
comprehensions: require("./generators/comprehensions"),
|
||||
expressions: require("./generators/expressions"),
|
||||
statements: require("./generators/statements"),
|
||||
playground: require("./generators/playground"),
|
||||
classes: require("./generators/classes"),
|
||||
methods: require("./generators/methods"),
|
||||
modules: require("./generators/modules"),
|
||||
|
||||
7
lib/6to5/generation/generators/playground.js
Normal file
7
lib/6to5/generation/generators/playground.js
Normal file
@@ -0,0 +1,7 @@
|
||||
var _ = require("lodash");
|
||||
|
||||
_.each(["BindMemberExpression", "PretzelMapExpression"], function (type) {
|
||||
exports[type] = function () {
|
||||
throw new ReferenceError("Trying to render non-standard playground node " + JSON.stringify(type));
|
||||
};
|
||||
});
|
||||
@@ -3,6 +3,7 @@ var _ = require("lodash");
|
||||
|
||||
var types = require("ast-types");
|
||||
var def = types.Type.def;
|
||||
var or = types.Type.or;
|
||||
|
||||
// Program wrapper
|
||||
def("File")
|
||||
@@ -16,12 +17,33 @@ def("ParenthesizedExpression")
|
||||
.build("expression")
|
||||
.field("expression", def("Expression"));
|
||||
|
||||
// Same as ImportNamespaceSpecifier but `id` is `name`
|
||||
// Acorn - Same as ImportNamespaceSpecifier but `id` is `name`
|
||||
def("ImportBatchSpecifier")
|
||||
.bases("Specifier")
|
||||
.build("name")
|
||||
.field("name", def("Identifier"));
|
||||
|
||||
// Abstract references
|
||||
def("VirtualPropertyExpression")
|
||||
.bases("Expression")
|
||||
.build("object", "property")
|
||||
.field("object", def("Expression"))
|
||||
.field("property", or(def("Identifier"), def("Expression")));
|
||||
|
||||
// Playground
|
||||
def("BindMemberExpression")
|
||||
.bases("Expression")
|
||||
.build("object", "property", "arguments")
|
||||
.field("object", def("Expression"))
|
||||
.field("property", or(def("Identifier"), def("Expression")))
|
||||
.field("arguments", [def("Expression")]);
|
||||
|
||||
def("PretzelMapExpression")
|
||||
.bases("Expression")
|
||||
.build("callee", "arguments")
|
||||
.field("callee", def("Expression"))
|
||||
.field("arguments", [def("Expression")]);
|
||||
|
||||
types.finalize();
|
||||
|
||||
var estraverse = require("estraverse");
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
(function (args) {
|
||||
var target = new Array(args.length);
|
||||
for (var i = 0; i< args.length; i++) {
|
||||
target[i] = args[i];
|
||||
}
|
||||
return target;
|
||||
})
|
||||
1
lib/6to5/templates/exports-default-module.js
Normal file
1
lib/6to5/templates/exports-default-module.js
Normal file
@@ -0,0 +1 @@
|
||||
module.exports = VALUE;
|
||||
1
lib/6to5/templates/has-own.js
Normal file
1
lib/6to5/templates/has-own.js
Normal file
@@ -0,0 +1 @@
|
||||
Object.prototype.hasOwnProperty;
|
||||
1
lib/6to5/templates/register.js
Normal file
1
lib/6to5/templates/register.js
Normal file
@@ -0,0 +1 @@
|
||||
System.register(MODULE_NAME, MODULE_DEPENDENCIES, MODULE_BODY);
|
||||
1
lib/6to5/templates/slice.js
Normal file
1
lib/6to5/templates/slice.js
Normal file
@@ -0,0 +1 @@
|
||||
Array.prototype.slice;
|
||||
@@ -41,34 +41,11 @@ AMDFormatter.prototype.transform = function (ast) {
|
||||
};
|
||||
|
||||
AMDFormatter.prototype.getModuleName = function () {
|
||||
var opts = this.file.opts;
|
||||
var filenameRelative = opts.filenameRelative;
|
||||
var moduleName = "";
|
||||
|
||||
if (!opts.amdModuleIds) {
|
||||
if (this.file.opts.amdModuleIds) {
|
||||
return CommonJSFormatter.prototype.getModuleName.apply(this, arguments);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
AMDFormatter.prototype._push = function (node) {
|
||||
|
||||
@@ -3,9 +3,16 @@ module.exports = CommonJSInteropFormatter;
|
||||
var CommonJSFormatter = require("./common");
|
||||
var util = require("../../util");
|
||||
var t = require("../../types");
|
||||
var _ = require("lodash");
|
||||
|
||||
function CommonJSInteropFormatter(file) {
|
||||
this.file = 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);
|
||||
@@ -26,3 +33,24 @@ CommonJSInteropFormatter.prototype.importSpecifier = function (specifier, node,
|
||||
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);
|
||||
};
|
||||
|
||||
@@ -7,6 +7,33 @@ function CommonJSFormatter(file) {
|
||||
this.file = file;
|
||||
}
|
||||
|
||||
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", {
|
||||
@@ -38,23 +65,32 @@ CommonJSFormatter.prototype.importSpecifier = function (specifier, node, nodes)
|
||||
}));
|
||||
};
|
||||
|
||||
CommonJSFormatter.prototype._hoistExport = function (declar, assign) {
|
||||
if (t.isFunctionDeclaration(declar)) {
|
||||
assign._blockHoist = true;
|
||||
}
|
||||
|
||||
return assign;
|
||||
};
|
||||
|
||||
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;
|
||||
|
||||
if (node.default) {
|
||||
var ref = declar;
|
||||
|
||||
if (t.isClass(ref) || t.isFunction(ref)) {
|
||||
if (ref.id) {
|
||||
nodes.push(t.toStatement(ref));
|
||||
ref = ref.id;
|
||||
}
|
||||
}
|
||||
|
||||
nodes.push(util.template("exports-default", {
|
||||
//inherits: node,
|
||||
|
||||
VALUE: ref
|
||||
VALUE: this._pushStatement(declar, nodes)
|
||||
}, true));
|
||||
} else {
|
||||
var assign;
|
||||
@@ -83,9 +119,7 @@ CommonJSFormatter.prototype.export = function (node, nodes) {
|
||||
nodes.push(t.toStatement(declar));
|
||||
nodes.push(assign);
|
||||
|
||||
if (t.isFunctionDeclaration(declar)) {
|
||||
assign._blockHoist = true;
|
||||
}
|
||||
this._hoistExport(declar, assign);
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -126,7 +160,7 @@ CommonJSFormatter.prototype._exportSpecifier = function (getRef, specifier, node
|
||||
};
|
||||
|
||||
CommonJSFormatter.prototype.exportSpecifier = function (specifier, node, nodes) {
|
||||
return this._exportSpecifier(function () {
|
||||
this._exportSpecifier(function () {
|
||||
return t.callExpression(t.identifier("require"), [node.source]);
|
||||
}, specifier, node, nodes);
|
||||
};
|
||||
|
||||
202
lib/6to5/transformation/modules/system.js
Normal file
202
lib/6to5/transformation/modules/system.js
Normal file
@@ -0,0 +1,202 @@
|
||||
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);
|
||||
|
||||
function SystemFormatter(file) {
|
||||
this.exportedStatements = [];
|
||||
this.importedModule = {};
|
||||
|
||||
this.exportIdentifier = file.generateUidIdentifier("export");
|
||||
this.file = file;
|
||||
}
|
||||
|
||||
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(/\..*$/, "");
|
||||
|
||||
// build an array of module names
|
||||
var dependencies = Object.keys(this.importedModule).map(t.literal);
|
||||
|
||||
// generate the __moduleName variable
|
||||
var moduleNameVariableNode = t.variableDeclaration("var", [
|
||||
t.variableDeclarator(
|
||||
t.identifier("__moduleName"),
|
||||
t.literal(moduleName)
|
||||
)
|
||||
]);
|
||||
body.splice(1, 0, moduleNameVariableNode);
|
||||
|
||||
// generate an array of import variables
|
||||
|
||||
var declaredSetters = _(this.importedModule)
|
||||
.map()
|
||||
.flatten()
|
||||
.pluck("variableName")
|
||||
.pluck("name")
|
||||
.uniq()
|
||||
.map(t.identifier)
|
||||
.map(function (name) {
|
||||
return t.variableDeclarator(name);
|
||||
})
|
||||
.value();
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
};
|
||||
@@ -22,6 +22,7 @@ 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")
|
||||
@@ -35,7 +36,6 @@ _.each({
|
||||
arrowFunctions: require("./transformers/es6-arrow-functions"),
|
||||
classes: require("./transformers/es6-classes"),
|
||||
|
||||
_propertyLiterals: require("./transformers/_property-literals"),
|
||||
computedPropertyNames: require("./transformers/es6-computed-property-names"),
|
||||
|
||||
objectSpread: require("./transformers/es7-object-spread"),
|
||||
@@ -56,13 +56,19 @@ _.each({
|
||||
|
||||
generators: require("./transformers/es6-generators"),
|
||||
|
||||
// plyground
|
||||
methodBinding: require("./transformers/playground-method-binding"),
|
||||
memoizationOperator: require("./transformers/playground-memoization-operator"),
|
||||
pretzelMap: require("./transformers/playground-pretzel-map"),
|
||||
|
||||
_blockHoist: require("./transformers/_block-hoist"),
|
||||
_declarations: require("./transformers/_declarations"),
|
||||
_aliasFunctions: require("./transformers/_alias-functions"),
|
||||
|
||||
useStrict: require("./transformers/use-strict"),
|
||||
|
||||
_memberExpressionKeywords: require("./transformers/_member-expression-keywords"),
|
||||
_propertyLiterals: require("./transformers/_property-literals"),
|
||||
_memberExpressioLiterals: require("./transformers/_member-expression-literals"),
|
||||
_moduleFormatter: require("./transformers/_module-formatter")
|
||||
}, function (transformer, key) {
|
||||
transform.transformers[key] = new Transformer(key, transformer);
|
||||
|
||||
@@ -31,7 +31,7 @@ var go = function (getBody, node, file, scope) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (node._ignoreAliasFunctions) return;
|
||||
if (node._ignoreAliasFunctions) return false;
|
||||
|
||||
var getId;
|
||||
|
||||
|
||||
@@ -3,9 +3,21 @@ var _ = require("lodash");
|
||||
|
||||
exports.BlockStatement =
|
||||
exports.Program = function (node) {
|
||||
var kinds = {};
|
||||
|
||||
_.each(node._declarations, function (declar) {
|
||||
node.body.unshift(t.variableDeclaration("var", [
|
||||
t.variableDeclarator(declar.id, declar.init)
|
||||
]));
|
||||
var kind = declar.kind || "var";
|
||||
var declarNode = t.variableDeclarator(declar.id, declar.init);
|
||||
|
||||
if (!declar.init) {
|
||||
kinds[kind] = kinds[kind] || [];
|
||||
kinds[kind].push(declarNode);
|
||||
} else {
|
||||
node.body.unshift(t.variableDeclaration(kind, [declarNode]));
|
||||
}
|
||||
});
|
||||
|
||||
_.each(kinds, function (declars, kind) {
|
||||
node.body.unshift(t.variableDeclaration(kind, declars));
|
||||
});
|
||||
};
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
var esutils = require("esutils");
|
||||
var t = require("../../types");
|
||||
|
||||
exports.MemberExpression = function (node) {
|
||||
var prop = node.property;
|
||||
if (t.isIdentifier(prop) && esutils.keyword.isKeywordES6(prop.name, true)) {
|
||||
node.property = t.literal(prop.name);
|
||||
node.computed = true;
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,14 @@
|
||||
var esutils = require("esutils");
|
||||
var t = require("../../types");
|
||||
|
||||
exports.MemberExpression = function (node) {
|
||||
var prop = node.property;
|
||||
if (node.computed && t.isLiteral(prop) && t.isValidIdentifier(prop.value)) {
|
||||
// computed literal that is a valid identifier
|
||||
node.property = t.identifier(prop.value);
|
||||
node.computed = false;
|
||||
} else if (!node.computed && t.isIdentifier(prop) && esutils.keyword.isKeywordES6(prop.name, true)) {
|
||||
node.property = t.literal(prop.name);
|
||||
node.computed = true;
|
||||
}
|
||||
};
|
||||
@@ -1,15 +1,14 @@
|
||||
var esutils = require("esutils");
|
||||
var t = require("../../types");
|
||||
var _ = require("lodash");
|
||||
|
||||
exports.Property = function (node) {
|
||||
// ignore key literals that are valid identifiers
|
||||
var key = node.key;
|
||||
if (t.isLiteral(key) && _.isString(key.value) && esutils.keyword.isIdentifierName(key.value)) {
|
||||
key.type = "Identifier";
|
||||
key.name = key.value;
|
||||
delete key.value;
|
||||
|
||||
if (t.isLiteral(key) && t.isValidIdentifier(key.value)) {
|
||||
// property key is a literal but a valid identifier
|
||||
node.key = t.identifier(key.value);
|
||||
node.computed = false;
|
||||
} else if (!node.computed && t.isIdentifier(key) && esutils.keyword.isKeywordES6(key.name, true)) {
|
||||
// property key is a keyword
|
||||
node.key = t.literal(key.name);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -4,9 +4,13 @@ var t = require("../../types");
|
||||
var _ = require("lodash");
|
||||
|
||||
exports.ClassDeclaration = function (node, parent, file, scope) {
|
||||
return t.variableDeclaration("let", [
|
||||
t.variableDeclarator(node.id, new Class(node, file, scope).run())
|
||||
var built = new Class(node, file, scope).run();
|
||||
|
||||
var declar = t.variableDeclaration("let", [
|
||||
t.variableDeclarator(node.id, built)
|
||||
]);
|
||||
t.inheritsComments(declar, node);
|
||||
return declar;
|
||||
};
|
||||
|
||||
exports.ClassExpression = function (node, parent, file, scope) {
|
||||
|
||||
@@ -1,11 +1,16 @@
|
||||
// TODO: Clean up
|
||||
|
||||
var t = require("../../types");
|
||||
var _ = require("lodash");
|
||||
|
||||
var buildVariableAssign = function (kind, id, init) {
|
||||
if (kind === false) {
|
||||
var buildVariableAssign = function (opts, id, init) {
|
||||
var op = opts.operator;
|
||||
if (t.isMemberExpression(id)) op = "=";
|
||||
|
||||
if (op) {
|
||||
return t.expressionStatement(t.assignmentExpression("=", id, init));
|
||||
} else {
|
||||
return t.variableDeclaration(kind, [
|
||||
return t.variableDeclaration(opts.kind, [
|
||||
t.variableDeclarator(id, init)
|
||||
]);
|
||||
}
|
||||
@@ -16,10 +21,8 @@ var push = function (opts, nodes, elem, parentId) {
|
||||
pushObjectPattern(opts, nodes, elem, parentId);
|
||||
} else if (t.isArrayPattern(elem)) {
|
||||
pushArrayPattern(opts, nodes, elem, parentId);
|
||||
} else if (t.isMemberExpression(elem)) {
|
||||
nodes.push(buildVariableAssign(false, elem, parentId));
|
||||
} else {
|
||||
nodes.push(buildVariableAssign(opts.kind, elem, parentId));
|
||||
nodes.push(buildVariableAssign(opts, elem, parentId));
|
||||
}
|
||||
};
|
||||
|
||||
@@ -41,7 +44,7 @@ var pushObjectPattern = function (opts, nodes, pattern, parentId) {
|
||||
keys = t.arrayExpression(keys);
|
||||
|
||||
var value = t.callExpression(opts.file.addDeclaration("object-spread"), [parentId, keys]);
|
||||
nodes.push(buildVariableAssign(opts.kind, prop.argument, value));
|
||||
nodes.push(buildVariableAssign(opts, prop.argument, value));
|
||||
} else {
|
||||
var pattern2 = prop.value;
|
||||
var patternId2 = t.memberExpression(parentId, prop.key, prop.computed);
|
||||
@@ -49,7 +52,7 @@ var pushObjectPattern = function (opts, nodes, pattern, parentId) {
|
||||
if (t.isPattern(pattern2)) {
|
||||
push(opts, nodes, pattern2, patternId2);
|
||||
} else {
|
||||
nodes.push(buildVariableAssign(opts.kind, pattern2, patternId2));
|
||||
nodes.push(buildVariableAssign(opts, pattern2, patternId2));
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -175,7 +178,7 @@ exports.ExpressionStatement = function (node, parent, file, scope) {
|
||||
]));
|
||||
|
||||
push({
|
||||
kind: false,
|
||||
operator: expr.operator,
|
||||
file: file,
|
||||
scope: scope
|
||||
}, nodes, expr.left, ref);
|
||||
@@ -183,10 +186,29 @@ exports.ExpressionStatement = function (node, parent, file, scope) {
|
||||
return nodes;
|
||||
};
|
||||
|
||||
exports.AssignmentExpression = function (node, parent, file) {
|
||||
exports.AssignmentExpression = function (node, parent, file, scope) {
|
||||
if (parent.type === "ExpressionStatement") return;
|
||||
if (!t.isPattern(node.left)) return;
|
||||
throw file.errorWithNode(node, "AssignmentExpression destructuring outside of a ExpressionStatement is forbidden due to current 6to5 limitations");
|
||||
|
||||
var tempName = file.generateUid("temp", scope);
|
||||
var ref = t.identifier(tempName);
|
||||
scope.push({
|
||||
key: tempName,
|
||||
id: ref
|
||||
});
|
||||
|
||||
var nodes = [];
|
||||
nodes.push(t.assignmentExpression("=", ref, node.right));
|
||||
|
||||
push({
|
||||
operator: node.operator,
|
||||
file: file,
|
||||
scope: scope
|
||||
}, nodes, node.left, ref);
|
||||
|
||||
nodes.push(ref);
|
||||
|
||||
return t.toSequenceExpression(nodes, scope);
|
||||
};
|
||||
|
||||
exports.VariableDeclaration = function (node, parent, file, scope) {
|
||||
@@ -206,17 +228,18 @@ exports.VariableDeclaration = function (node, parent, file, scope) {
|
||||
_.each(node.declarations, function (declar) {
|
||||
var patternId = declar.init;
|
||||
var pattern = declar.id;
|
||||
var opts = {
|
||||
kind: node.kind,
|
||||
nodes: nodes,
|
||||
pattern: pattern,
|
||||
id: patternId,
|
||||
file: file,
|
||||
scope: scope
|
||||
};
|
||||
if (t.isPattern(pattern) && patternId) {
|
||||
pushPattern({
|
||||
kind: node.kind,
|
||||
nodes: nodes,
|
||||
pattern: pattern,
|
||||
id: patternId,
|
||||
file: file,
|
||||
scope: scope
|
||||
});
|
||||
pushPattern(opts);
|
||||
} else {
|
||||
nodes.push(buildVariableAssign(node.kind, declar.id, declar.init));
|
||||
nodes.push(buildVariableAssign(opts, declar.id, declar.init));
|
||||
}
|
||||
});
|
||||
|
||||
@@ -238,4 +261,3 @@ exports.VariableDeclaration = function (node, parent, file, scope) {
|
||||
|
||||
return nodes;
|
||||
};
|
||||
|
||||
|
||||
@@ -56,7 +56,7 @@ var visitor = function (path, file) {
|
||||
);
|
||||
|
||||
var innerFnId = t.identifier(node.id.name + "$");
|
||||
var contextId = file.generateUidIdentifier("context$", scope);
|
||||
var contextId = file.generateUidIdentifier("context", scope);
|
||||
var vars = hoist(path);
|
||||
|
||||
var emitter = new Emitter(contextId);
|
||||
|
||||
@@ -5,10 +5,10 @@ exports.ImportDeclaration = function (node, parent, file) {
|
||||
|
||||
if (node.specifiers.length) {
|
||||
_.each(node.specifiers, function (specifier) {
|
||||
file.moduleFormatter.importSpecifier(specifier, node, nodes);
|
||||
file.moduleFormatter.importSpecifier(specifier, node, nodes, parent);
|
||||
});
|
||||
} else {
|
||||
file.moduleFormatter.import(node, nodes);
|
||||
file.moduleFormatter.import(node, nodes, parent);
|
||||
}
|
||||
|
||||
return nodes;
|
||||
@@ -18,10 +18,10 @@ exports.ExportDeclaration = function (node, parent, file) {
|
||||
var nodes = [];
|
||||
|
||||
if (node.declaration) {
|
||||
file.moduleFormatter.export(node, nodes);
|
||||
file.moduleFormatter.export(node, nodes, parent);
|
||||
} else {
|
||||
_.each(node.specifiers, function (specifier) {
|
||||
file.moduleFormatter.exportSpecifier(specifier, node, nodes);
|
||||
file.moduleFormatter.exportSpecifier(specifier, node, nodes, parent);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -8,13 +8,10 @@ exports.Function = function (node, parent, file) {
|
||||
|
||||
t.ensureBlock(node);
|
||||
|
||||
var call = t.callExpression(
|
||||
file.addDeclaration("arguments-to-array"),
|
||||
[t.identifier("arguments")]
|
||||
);
|
||||
var call = file.toArray(t.identifier("arguments"));
|
||||
|
||||
if (node.params.length) {
|
||||
call = t.callExpression(t.memberExpression(call, t.identifier("slice")), [t.literal(node.params.length)]);
|
||||
call.arguments.push(t.literal(node.params.length));
|
||||
}
|
||||
|
||||
call._ignoreAliasFunctions = true;
|
||||
|
||||
@@ -29,10 +29,13 @@ exports.AssignmentExpression = function (node, parent, file, scope) {
|
||||
// we need to return `node.right`
|
||||
if (!t.isExpressionStatement(parent)) {
|
||||
// `node.right` isn't a simple identifier so we need to reference it
|
||||
if (!t.isIdentifier(value)) {
|
||||
if (t.isDynamic(value)) {
|
||||
var tempName = file.generateUid("temp");
|
||||
temp = value = t.identifier(tempName);
|
||||
scope.push(tempName, temp);
|
||||
scope.push({
|
||||
key: tempName,
|
||||
id: temp
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,11 +73,14 @@ exports.CallExpression = function (node, parent, file, scope) {
|
||||
if (!t.isVirtualPropertyExpression(callee)) return;
|
||||
|
||||
var temp;
|
||||
if (!t.isIdentifier(callee.object)) {
|
||||
if (t.isDynamic(callee.object)) {
|
||||
// we need to save `callee.object` so we can call it again
|
||||
var tempName = file.generateUid("temp");
|
||||
temp = t.identifier(tempName);
|
||||
scope.push(tempName, temp);
|
||||
scope.push({
|
||||
key: tempName,
|
||||
id: temp
|
||||
});
|
||||
}
|
||||
|
||||
var call = util.template("abstract-expression-call", {
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
var t = require("../../types");
|
||||
|
||||
var isMemo = function (node) {
|
||||
var is = t.isAssignmentExpression(node) && node.operator === "?=";
|
||||
if (is) t.assertMemberExpression(node.left);
|
||||
return is;
|
||||
};
|
||||
|
||||
var getPropRef = function (nodes, prop, file, scope) {
|
||||
if (t.isIdentifier(prop)) {
|
||||
return t.literal(prop.name);
|
||||
} else {
|
||||
var temp = file.generateUidIdentifier("propKey", scope);
|
||||
nodes.push(t.variableDeclaration("var", [
|
||||
t.variableDeclarator(temp, prop)
|
||||
]));
|
||||
return temp;
|
||||
}
|
||||
};
|
||||
|
||||
var getObjRef = function (nodes, obj, file, scope) {
|
||||
if (t.isDynamic(obj)) {
|
||||
var temp = file.generateUidIdentifier("obj", scope);
|
||||
nodes.push(t.variableDeclaration("var", [
|
||||
t.variableDeclarator(temp, obj)
|
||||
]));
|
||||
return temp;
|
||||
} else {
|
||||
return obj;
|
||||
}
|
||||
};
|
||||
|
||||
var buildHasOwn = function (obj, prop, file) {
|
||||
return t.unaryExpression(
|
||||
"!",
|
||||
t.callExpression(
|
||||
t.memberExpression(file.addDeclaration("has-own"), t.identifier("call")),
|
||||
[obj, prop]
|
||||
),
|
||||
true
|
||||
);
|
||||
};
|
||||
|
||||
var buildAbsoluteRef = function (left, obj, prop) {
|
||||
var computed = left.computed || t.isLiteral(prop);
|
||||
return t.memberExpression(obj, prop, computed);
|
||||
};
|
||||
|
||||
var buildAssignment = function (expr, obj, prop) {
|
||||
return t.assignmentExpression("=", buildAbsoluteRef(expr.left, obj, prop), expr.right);
|
||||
};
|
||||
|
||||
exports.ExpressionStatement = function (node, parent, file, scope) {
|
||||
var expr = node.expression;
|
||||
if (!isMemo(expr)) return;
|
||||
|
||||
var nodes = [];
|
||||
|
||||
var left = expr.left;
|
||||
var obj = getObjRef(nodes, left.object, file, scope);
|
||||
var prop = getPropRef(nodes, left.property, file, scope);
|
||||
|
||||
nodes.push(t.ifStatement(
|
||||
buildHasOwn(obj, prop, file),
|
||||
t.expressionStatement(buildAssignment(expr, obj, prop))
|
||||
));
|
||||
|
||||
return nodes;
|
||||
};
|
||||
|
||||
exports.AssignmentExpression = function (node, parent, file, scope) {
|
||||
if (t.isExpressionStatement(parent)) return;
|
||||
if (!isMemo(node)) return;
|
||||
|
||||
var nodes = [];
|
||||
|
||||
var left = node.left;
|
||||
var obj = getObjRef(nodes, left.object, file, scope);
|
||||
var prop = getPropRef(nodes, left.property, file, scope);
|
||||
|
||||
nodes.push(t.logicalExpression(
|
||||
"&&",
|
||||
buildHasOwn(obj, prop, file),
|
||||
buildAssignment(node, obj, prop)
|
||||
));
|
||||
|
||||
nodes.push(buildAbsoluteRef(left, obj, prop));
|
||||
|
||||
return t.toSequenceExpression(nodes, scope);
|
||||
};
|
||||
@@ -0,0 +1,30 @@
|
||||
var t = require("../../types");
|
||||
|
||||
exports.BindMemberExpression = function (node, parent, file, scope) {
|
||||
var object = node.object;
|
||||
var prop = node.property;
|
||||
|
||||
var temp;
|
||||
if (t.isDynamic(object)) {
|
||||
var tempName = file.generateUid("temp", scope);
|
||||
temp = object = t.identifier(tempName);
|
||||
scope.push({
|
||||
key: tempName,
|
||||
id: temp
|
||||
});
|
||||
}
|
||||
|
||||
var call = t.callExpression(
|
||||
t.memberExpression(t.memberExpression(object, prop), t.identifier("bind")),
|
||||
[object].concat(node.arguments)
|
||||
);
|
||||
|
||||
if (temp) {
|
||||
return t.sequenceExpression([
|
||||
t.assignmentExpression("=", temp, node.object),
|
||||
call
|
||||
]);
|
||||
} else {
|
||||
return call;
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,29 @@
|
||||
var t = require("../../types");
|
||||
var _ = require("lodash");
|
||||
|
||||
exports.PretzelMapExpression = function (node, parent, file, scope) {
|
||||
var buildCall = function (args) {
|
||||
var param = file.generateUidIdentifier("val", scope);
|
||||
return t.functionExpression(null, [param], t.blockStatement([
|
||||
t.returnStatement(t.callExpression(t.memberExpression(param, node.callee), args))
|
||||
]));
|
||||
};
|
||||
|
||||
if (_.find(node.arguments, t.isDynamic)) {
|
||||
var argsIdName = file.generateUid("args", scope);
|
||||
var argsId = t.identifier(argsIdName);
|
||||
scope.push({
|
||||
key: argsIdName,
|
||||
id: argsId
|
||||
});
|
||||
|
||||
return t.sequenceExpression([
|
||||
t.assignmentExpression("=", argsId, t.arrayExpression(node.arguments)),
|
||||
buildCall(node.arguments.map(function (node, i) {
|
||||
return t.memberExpression(argsId, t.literal(i), true);
|
||||
}))
|
||||
]);
|
||||
} else {
|
||||
return buildCall(node.arguments);
|
||||
}
|
||||
};
|
||||
@@ -132,6 +132,8 @@ exports.XJSElement = {
|
||||
}
|
||||
});
|
||||
|
||||
return;
|
||||
} else if (t.isXJSEmptyExpression(child)) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -21,15 +21,19 @@ function Scope(block, parent) {
|
||||
this.references = this.getReferences();
|
||||
}
|
||||
|
||||
Scope.add = function (node, references) {
|
||||
if (!node) return;
|
||||
_.merge(references, t.getIds(node, true));
|
||||
};
|
||||
|
||||
Scope.prototype.getReferences = function () {
|
||||
var block = this.block;
|
||||
if (block._scopeReferences) return block._scopeReferences;
|
||||
|
||||
var self = this;
|
||||
var references = block._scopeReferences = {};
|
||||
|
||||
var add = function (node) {
|
||||
self.add(node, references);
|
||||
Scope.add(node, references);
|
||||
};
|
||||
|
||||
// ForStatement - left, init
|
||||
@@ -97,7 +101,7 @@ Scope.prototype.getReferences = function () {
|
||||
return references;
|
||||
};
|
||||
|
||||
Scope.prototype.push = function (name, id, init) {
|
||||
Scope.prototype.push = function (opts) {
|
||||
var block = this.block;
|
||||
|
||||
if (t.isFor(block) || t.isCatchClause(block) || t.isFunction(block)) {
|
||||
@@ -107,18 +111,18 @@ Scope.prototype.push = function (name, id, init) {
|
||||
|
||||
if (t.isBlockStatement(block) || t.isProgram(block)) {
|
||||
block._declarations = block._declarations || {};
|
||||
block._declarations[name] = {
|
||||
id: id,
|
||||
init: init
|
||||
block._declarations[opts.key] = {
|
||||
kind: opts.kind,
|
||||
id: opts.id,
|
||||
init: opts.init
|
||||
};
|
||||
} else {
|
||||
throw new Error("wtf");
|
||||
throw new TypeError("cannot add a declaration here in node type " + block.type);
|
||||
}
|
||||
};
|
||||
|
||||
Scope.prototype.add = function (node, references) {
|
||||
if (!node) return;
|
||||
_.merge(references || this.references, t.getIds(node, true));
|
||||
Scope.prototype.add = function (node) {
|
||||
Scope.add(node, this.references);
|
||||
};
|
||||
|
||||
Scope.prototype.get = function (id) {
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
"Identifier": ["name"],
|
||||
"IfStatement": ["test", "consequent", "alternate"],
|
||||
"Literal": ["value"],
|
||||
"LogicalExpression": ["operator", "left", "right"],
|
||||
"MemberExpression": ["object", "property", "computed"],
|
||||
"NewExpression": ["callee", "arguments"],
|
||||
"ObjectExpression": ["properties"],
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
var _ = require("lodash");
|
||||
var esutils = require("esutils");
|
||||
var _ = require("lodash");
|
||||
|
||||
var t = exports;
|
||||
|
||||
@@ -71,6 +72,31 @@ addAssert("Expression", t.isExpression);
|
||||
|
||||
//
|
||||
|
||||
t.toSequenceExpression = function (nodes, scope) {
|
||||
var exprs = [];
|
||||
|
||||
_.each(nodes, function (node) {
|
||||
if (t.isExpression(node)) {
|
||||
exprs.push(node);
|
||||
} if (t.isExpressionStatement(node)) {
|
||||
exprs.push(node.expression);
|
||||
} else if (t.isVariableDeclaration(node)) {
|
||||
_.each(node.declarations, function (declar) {
|
||||
scope.push({
|
||||
kind: node.kind,
|
||||
key: declar.id.name,
|
||||
id: declar.id
|
||||
});
|
||||
exprs.push(t.assignmentExpression("=", declar.id, declar.init));
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
return t.sequenceExpression(exprs);
|
||||
};
|
||||
|
||||
//
|
||||
|
||||
t.shallowEqual = function (actual, expected) {
|
||||
var same = true;
|
||||
|
||||
@@ -87,6 +113,18 @@ t.shallowEqual = function (actual, expected) {
|
||||
|
||||
//
|
||||
|
||||
t.isDynamic = function (node) {
|
||||
if (t.isParenthesizedExpression(node)) {
|
||||
return t.isDynamic(node.expression);
|
||||
} else if (t.isIdentifier(node) || t.isLiteral(node) || t.isThisExpression(node)) {
|
||||
return false;
|
||||
} else if (t.isMemberExpression(node)) {
|
||||
return t.isDynamic(node.object) || t.isDynamic(node.property);
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
t.isReferenced = function (node, parent) {
|
||||
// we're a property key so we aren't referenced
|
||||
if (t.isProperty(parent) && parent.key === node) return false;
|
||||
@@ -125,6 +163,10 @@ t.toIdentifier = function (name) {
|
||||
return name;
|
||||
};
|
||||
|
||||
t.isValidIdentifier = function (name) {
|
||||
return _.isString(name) && esutils.keyword.isIdentifierName(name) && !esutils.keyword.isKeywordES6(name, true);
|
||||
};
|
||||
|
||||
t.ensureBlock = function (node) {
|
||||
node.body = t.toBlock(node.body, node);
|
||||
};
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
"AssignmentExpression": ["left", "right"],
|
||||
"AwaitExpression": ["argument"],
|
||||
"BinaryExpression": ["left", "right"],
|
||||
"BindMemberExpression": ["object", "property", "arguments"],
|
||||
"BlockStatement": ["body"],
|
||||
"BreakStatement": ["label"],
|
||||
"CallExpression": ["callee", "arguments"],
|
||||
@@ -43,6 +44,7 @@
|
||||
"ObjectExpression": ["properties"],
|
||||
"ObjectPattern": ["properties"],
|
||||
"ParenthesizedExpression": ["expression"],
|
||||
"PretzelMapExpression": ["callee", "arguments"],
|
||||
"Program": ["body"],
|
||||
"Property": ["key", "value"],
|
||||
"ReturnStatement": ["argument"],
|
||||
|
||||
@@ -38,9 +38,9 @@ exports.list = function (val) {
|
||||
};
|
||||
|
||||
exports.regexify = function (val) {
|
||||
if (!val) return new RegExp;
|
||||
if (!val) return new RegExp(/.^/);
|
||||
if (_.isArray(val)) val = val.join("|");
|
||||
if (_.isString(val)) return new RegExp(val || "");
|
||||
if (_.isString(val)) return new RegExp(val);
|
||||
if (_.isRegExp(val)) return val;
|
||||
throw new TypeError("illegal type for regexify");
|
||||
};
|
||||
@@ -219,6 +219,7 @@ exports.parse = function (opts, code, callback) {
|
||||
allowReturnOutsideFunction: true,
|
||||
preserveParens: true,
|
||||
ecmaVersion: opts.experimental ? 7 : 6,
|
||||
playground: opts.playground,
|
||||
strictMode: true,
|
||||
onComment: comments,
|
||||
locations: true,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "6to5",
|
||||
"description": "Turn ES6 code into readable vanilla ES5 with source maps",
|
||||
"version": "1.13.9",
|
||||
"version": "1.14.3",
|
||||
"author": "Sebastian McKenzie <sebmck@gmail.com>",
|
||||
"homepage": "https://github.com/6to5/6to5",
|
||||
"repository": {
|
||||
@@ -47,7 +47,7 @@
|
||||
"chokidar": "0.11.1",
|
||||
"source-map-support": "0.2.8",
|
||||
"esutils": "1.1.6",
|
||||
"acorn-6to5": "0.9.1-7",
|
||||
"acorn-6to5": "0.9.1-9",
|
||||
"estraverse": "1.8.0",
|
||||
"private": "0.1.6"
|
||||
},
|
||||
|
||||
8
test/fixtures/transformation/.es6-modules-system/exports-default/actual.js
vendored
Normal file
8
test/fixtures/transformation/.es6-modules-system/exports-default/actual.js
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
export default 42;
|
||||
export default {};
|
||||
export default [];
|
||||
export default foo;
|
||||
export default function () {}
|
||||
export default class {}
|
||||
export default function foo () {}
|
||||
export default class Foo {}
|
||||
32
test/fixtures/transformation/.es6-modules-system/exports-default/expected.js
vendored
Normal file
32
test/fixtures/transformation/.es6-modules-system/exports-default/expected.js
vendored
Normal file
@@ -0,0 +1,32 @@
|
||||
System.register("actual", [], function (_export) {
|
||||
"use strict";
|
||||
|
||||
var __moduleName = "actual";
|
||||
|
||||
function _anonymous() {}
|
||||
var _anonymous2;
|
||||
|
||||
function foo() {}
|
||||
var Foo;
|
||||
|
||||
return {
|
||||
setters: [],
|
||||
execute: function () {
|
||||
_export("default", 42);
|
||||
|
||||
_export("default", {});
|
||||
|
||||
_export("default", []);
|
||||
|
||||
_export("default", foo);
|
||||
|
||||
_export("default", _anonymous);
|
||||
|
||||
_export("default", _anonymous2 = function _anonymous2() {});
|
||||
|
||||
_export("default", foo);
|
||||
|
||||
_export("default", Foo = function Foo() {});
|
||||
}
|
||||
};
|
||||
});
|
||||
6
test/fixtures/transformation/.es6-modules-system/exports-from/actual.js
vendored
Normal file
6
test/fixtures/transformation/.es6-modules-system/exports-from/actual.js
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
export * from "foo";
|
||||
export {foo} from "foo";
|
||||
export {foo, bar} from "foo";
|
||||
export {foo as bar} from "foo";
|
||||
export {foo as default} from "foo";
|
||||
export {foo as default, bar} from "foo";
|
||||
34
test/fixtures/transformation/.es6-modules-system/exports-from/expected.js
vendored
Normal file
34
test/fixtures/transformation/.es6-modules-system/exports-from/expected.js
vendored
Normal file
@@ -0,0 +1,34 @@
|
||||
System.register("actual", ["foo"], function (_export) {
|
||||
"use strict";
|
||||
|
||||
var __moduleName = "actual";
|
||||
|
||||
var _localExports = ['foo', 'bar', 'default'];
|
||||
|
||||
return {
|
||||
setters: [
|
||||
function(m) {
|
||||
_export("foo", m.foo);
|
||||
|
||||
_export("foo", m.foo);
|
||||
|
||||
_export("bar", m.bar);
|
||||
|
||||
_export("bar", m.foo);
|
||||
|
||||
_export("default", m.foo);
|
||||
|
||||
_export("default", m.foo);
|
||||
|
||||
_export("bar", m.bar);
|
||||
|
||||
for (var p in m) {
|
||||
if (_localExports.indexOf(i) == -1)
|
||||
_export(p, m[p]);
|
||||
}
|
||||
}
|
||||
],
|
||||
execute: function () {
|
||||
}
|
||||
};
|
||||
});
|
||||
5
test/fixtures/transformation/.es6-modules-system/exports-named/actual.js
vendored
Normal file
5
test/fixtures/transformation/.es6-modules-system/exports-named/actual.js
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
export {foo};
|
||||
export {foo, bar};
|
||||
export {foo as bar};
|
||||
export {foo as default};
|
||||
export {foo as default, bar};
|
||||
26
test/fixtures/transformation/.es6-modules-system/exports-named/expected.js
vendored
Normal file
26
test/fixtures/transformation/.es6-modules-system/exports-named/expected.js
vendored
Normal file
@@ -0,0 +1,26 @@
|
||||
System.register("actual", [], function (_export) {
|
||||
"use strict";
|
||||
|
||||
var __moduleName = "actual";
|
||||
|
||||
return {
|
||||
setters: [
|
||||
function(m) {
|
||||
_export("foo", m.foo);
|
||||
|
||||
_export("foo", m.foo);
|
||||
|
||||
_export("bar", m.bar);
|
||||
|
||||
_export("bar", m.foo);
|
||||
|
||||
_export("default", m.foo);
|
||||
|
||||
_export("default", m.foo);
|
||||
|
||||
_export("bar", m.bar);
|
||||
}],
|
||||
execute: function () {
|
||||
}
|
||||
};
|
||||
});
|
||||
8
test/fixtures/transformation/.es6-modules-system/exports-variable/actual.js
vendored
Normal file
8
test/fixtures/transformation/.es6-modules-system/exports-variable/actual.js
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
export var foo = 1;
|
||||
export var foo2 = function () {};
|
||||
export var foo3;
|
||||
export let foo4 = 2;
|
||||
export let foo5;
|
||||
export const foo6 = 3;
|
||||
export function foo7 () {}
|
||||
export class foo8 {}
|
||||
37
test/fixtures/transformation/.es6-modules-system/exports-variable/expected.js
vendored
Normal file
37
test/fixtures/transformation/.es6-modules-system/exports-variable/expected.js
vendored
Normal file
@@ -0,0 +1,37 @@
|
||||
System.register("actual", [], function (_export) {
|
||||
"use strict";
|
||||
|
||||
var __moduleName = "actual";
|
||||
|
||||
var foo;
|
||||
var foo2;
|
||||
var foo3;
|
||||
var foo4;
|
||||
var foo5;
|
||||
var foo6;
|
||||
function foo7() {}
|
||||
_export("foo7", foo7);
|
||||
|
||||
var foo8;
|
||||
|
||||
return {
|
||||
setters: [],
|
||||
execute: function () {
|
||||
_export("foo", foo = 1);
|
||||
|
||||
_export("foo2", foo2 = function () {});
|
||||
|
||||
_export("foo3", foo3);
|
||||
|
||||
_export("foo4", foo4 = 2);
|
||||
|
||||
_export("foo5", foo5);
|
||||
|
||||
_export("foo6", foo6 = 3);
|
||||
|
||||
_export("foo7", foo7);
|
||||
|
||||
_export("foo8", foo8 = function foo8() {});
|
||||
}
|
||||
};
|
||||
});
|
||||
11
test/fixtures/transformation/.es6-modules-system/hoist-function-exports/actual.js
vendored
Normal file
11
test/fixtures/transformation/.es6-modules-system/hoist-function-exports/actual.js
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
import { isEven } from "./evens";
|
||||
|
||||
export function nextOdd(n) {
|
||||
return isEven(n) ? n + 1 : n + 2;
|
||||
}
|
||||
|
||||
export var isOdd = (function (isEven) {
|
||||
return function (n) {
|
||||
return !isEven(n);
|
||||
};
|
||||
})(isEven);
|
||||
25
test/fixtures/transformation/.es6-modules-system/hoist-function-exports/expected.js
vendored
Normal file
25
test/fixtures/transformation/.es6-modules-system/hoist-function-exports/expected.js
vendored
Normal file
@@ -0,0 +1,25 @@
|
||||
System.register("actual", ["./evens"], function (_export) {
|
||||
"use strict";
|
||||
|
||||
var __moduleName = "actual";
|
||||
|
||||
var isEven;
|
||||
function nextOdd(n) {
|
||||
return isEven(n) ? n + 1 : n + 2;
|
||||
}
|
||||
_export("nextOdd", nextOdd);
|
||||
|
||||
var isOdd;
|
||||
return {
|
||||
setters: [function (m) {
|
||||
isEven = m.isEven;
|
||||
}],
|
||||
execute: function () {
|
||||
_export("isOdd", isOdd = (function (isEven) {
|
||||
return function (n) {
|
||||
return !isEven(n);
|
||||
};
|
||||
})(isEven));
|
||||
}
|
||||
};
|
||||
});
|
||||
2
test/fixtures/transformation/.es6-modules-system/imports-default/actual.js
vendored
Normal file
2
test/fixtures/transformation/.es6-modules-system/imports-default/actual.js
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
import foo from "foo";
|
||||
import {default as foo} from "foo";
|
||||
14
test/fixtures/transformation/.es6-modules-system/imports-default/expected.js
vendored
Normal file
14
test/fixtures/transformation/.es6-modules-system/imports-default/expected.js
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
System.register("actual", ["foo"], function (_export) {
|
||||
"use strict";
|
||||
|
||||
var __moduleName = "actual";
|
||||
|
||||
var foo;
|
||||
return {
|
||||
setters: [function (m) {
|
||||
foo = m.default;
|
||||
foo = m.default;
|
||||
}],
|
||||
execute: function () {}
|
||||
};
|
||||
});
|
||||
1
test/fixtures/transformation/.es6-modules-system/imports-glob/actual.js
vendored
Normal file
1
test/fixtures/transformation/.es6-modules-system/imports-glob/actual.js
vendored
Normal file
@@ -0,0 +1 @@
|
||||
import * as foo from "foo";
|
||||
13
test/fixtures/transformation/.es6-modules-system/imports-glob/expected.js
vendored
Normal file
13
test/fixtures/transformation/.es6-modules-system/imports-glob/expected.js
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
System.register("actual", ["foo"], function (_export) {
|
||||
"use strict";
|
||||
|
||||
var __moduleName = "actual";
|
||||
|
||||
var foo;
|
||||
return {
|
||||
setters: [function (m) {
|
||||
foo = m;
|
||||
}],
|
||||
execute: function () {}
|
||||
};
|
||||
});
|
||||
1
test/fixtures/transformation/.es6-modules-system/imports-mixing/actual.js
vendored
Normal file
1
test/fixtures/transformation/.es6-modules-system/imports-mixing/actual.js
vendored
Normal file
@@ -0,0 +1 @@
|
||||
import foo, {baz as xyz} from "foo";
|
||||
14
test/fixtures/transformation/.es6-modules-system/imports-mixing/expected.js
vendored
Normal file
14
test/fixtures/transformation/.es6-modules-system/imports-mixing/expected.js
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
System.register("actual", ["foo"], function (_export) {
|
||||
"use strict";
|
||||
|
||||
var __moduleName = "actual";
|
||||
|
||||
var foo, xyz;
|
||||
return {
|
||||
setters: [function (m) {
|
||||
foo = m.default;
|
||||
xyz = m.baz;
|
||||
}],
|
||||
execute: function () {}
|
||||
};
|
||||
});
|
||||
4
test/fixtures/transformation/.es6-modules-system/imports-named/actual.js
vendored
Normal file
4
test/fixtures/transformation/.es6-modules-system/imports-named/actual.js
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
import {bar} from "foo";
|
||||
import {bar, baz} from "foo";
|
||||
import {bar as baz} from "foo";
|
||||
import {bar as baz, xyz} from "foo";
|
||||
18
test/fixtures/transformation/.es6-modules-system/imports-named/expected.js
vendored
Normal file
18
test/fixtures/transformation/.es6-modules-system/imports-named/expected.js
vendored
Normal file
@@ -0,0 +1,18 @@
|
||||
System.register("actual", ["foo"], function (_export) {
|
||||
"use strict";
|
||||
|
||||
var __moduleName = "actual";
|
||||
|
||||
var bar, baz, xyz;
|
||||
return {
|
||||
setters: [function (m) {
|
||||
bar = m.bar;
|
||||
bar = m.bar;
|
||||
baz = m.baz;
|
||||
baz = m.bar;
|
||||
baz = m.bar;
|
||||
xyz = m.xyz;
|
||||
}],
|
||||
execute: function () {}
|
||||
};
|
||||
});
|
||||
3
test/fixtures/transformation/.es6-modules-system/imports/actual.js
vendored
Normal file
3
test/fixtures/transformation/.es6-modules-system/imports/actual.js
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
import "foo";
|
||||
import "foo-bar";
|
||||
import "./directory/foo-bar";
|
||||
10
test/fixtures/transformation/.es6-modules-system/imports/expected.js
vendored
Normal file
10
test/fixtures/transformation/.es6-modules-system/imports/expected.js
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
System.register("actual", ["foo", "foo-bar", "./directory/foo-bar"], function (_export) {
|
||||
"use strict";
|
||||
|
||||
var __moduleName = "actual";
|
||||
|
||||
return {
|
||||
setters: [null, null, null],
|
||||
execute: function () {}
|
||||
};
|
||||
});
|
||||
3
test/fixtures/transformation/.es6-modules-system/options.json
vendored
Normal file
3
test/fixtures/transformation/.es6-modules-system/options.json
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"modules": "system"
|
||||
}
|
||||
12
test/fixtures/transformation/.es6-modules-system/overview/actual.js
vendored
Normal file
12
test/fixtures/transformation/.es6-modules-system/overview/actual.js
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
import "foo";
|
||||
import "foo-bar";
|
||||
import "./directory/foo-bar";
|
||||
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;
|
||||
24
test/fixtures/transformation/.es6-modules-system/overview/expected.js
vendored
Normal file
24
test/fixtures/transformation/.es6-modules-system/overview/expected.js
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
System.register("actual", ["foo", "foo-bar", "./directory/foo-bar"], function (_export) {
|
||||
"use strict";
|
||||
|
||||
var __moduleName = "actual";
|
||||
|
||||
var foo, bar;
|
||||
var test;
|
||||
|
||||
return {
|
||||
setters: [function (m) {
|
||||
foo = m.default;
|
||||
foo = m;
|
||||
bar = m.bar;
|
||||
bar = m.foo;
|
||||
}, null, null],
|
||||
execute: function () {
|
||||
_export("test", test);
|
||||
|
||||
_export("test", test = 5);
|
||||
|
||||
_export("default", test);
|
||||
}
|
||||
};
|
||||
});
|
||||
@@ -1,5 +1,5 @@
|
||||
class Test {
|
||||
constructor() {
|
||||
arr.map(x => x * x);
|
||||
arr.map(x => x * x);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,6 @@
|
||||
"use strict";
|
||||
|
||||
var _argumentsToArray = function (args) {
|
||||
var target = new Array(args.length);
|
||||
for (var i = 0; i < args.length; i++) {
|
||||
target[i] = args[i];
|
||||
}
|
||||
|
||||
return target;
|
||||
};
|
||||
|
||||
var _slice = Array.prototype.slice;
|
||||
var _classProps = function (child, staticProps, instanceProps) {
|
||||
if (staticProps) Object.defineProperties(child, staticProps);
|
||||
if (instanceProps) Object.defineProperties(child.prototype, instanceProps);
|
||||
@@ -33,11 +25,11 @@ var Test = (function (Foo) {
|
||||
Foo.prototype.test.call(this);
|
||||
foob(Foo);
|
||||
|
||||
Foo.call.apply(Foo, [this].concat(_argumentsToArray(arguments)));
|
||||
Foo.call.apply(Foo, [this, "test"].concat(_argumentsToArray(arguments)));
|
||||
Foo.call.apply(Foo, [this].concat(_slice.call(arguments)));
|
||||
Foo.call.apply(Foo, [this, "test"].concat(_slice.call(arguments)));
|
||||
|
||||
Foo.prototype.test.call.apply(Foo.prototype, [this].concat(_argumentsToArray(arguments)));
|
||||
Foo.prototype.test.call.apply(Foo.prototype, [this, "test"].concat(_argumentsToArray(arguments)));
|
||||
Foo.prototype.test.call.apply(Foo.prototype, [this].concat(_slice.call(arguments)));
|
||||
Foo.prototype.test.call.apply(Foo.prototype, [this, "test"].concat(_slice.call(arguments)));
|
||||
};
|
||||
|
||||
_extends(Test, Foo);
|
||||
@@ -47,8 +39,8 @@ var Test = (function (Foo) {
|
||||
writable: true,
|
||||
value: function () {
|
||||
Foo.foo.call(this);
|
||||
Foo.foo.call.apply(Foo.foo, [this].concat(_argumentsToArray(arguments)));
|
||||
Foo.foo.call.apply(Foo.foo, [this, "test"].concat(_argumentsToArray(arguments)));
|
||||
Foo.foo.call.apply(Foo.foo, [this].concat(_slice.call(arguments)));
|
||||
Foo.foo.call.apply(Foo.foo, [this, "test"].concat(_slice.call(arguments)));
|
||||
}
|
||||
}
|
||||
}, {
|
||||
@@ -56,8 +48,8 @@ var Test = (function (Foo) {
|
||||
writable: true,
|
||||
value: function () {
|
||||
Foo.prototype.test.call(this);
|
||||
Foo.prototype.test.call.apply(Foo.prototype.test, [this].concat(_argumentsToArray(arguments)));
|
||||
Foo.prototype.test.call.apply(Foo.prototype.test, [this, "test"].concat(_argumentsToArray(arguments)));
|
||||
Foo.prototype.test.call.apply(Foo.prototype.test, [this].concat(_slice.call(arguments)));
|
||||
Foo.prototype.test.call.apply(Foo.prototype.test, [this, "test"].concat(_slice.call(arguments)));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
console.log([x] = [1]);
|
||||
@@ -1,3 +0,0 @@
|
||||
{
|
||||
"throws": "AssignmentExpression destructuring outside of a ExpressionStatement is forbidden due to current 6to5 limitations"
|
||||
}
|
||||
8
test/fixtures/transformation/es6-destructuring/assignment-expression/expected.js
vendored
Normal file
8
test/fixtures/transformation/es6-destructuring/assignment-expression/expected.js
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
"use strict";
|
||||
|
||||
var _temp, _ref;
|
||||
var _toArray = function (arr) {
|
||||
return Array.isArray(arr) ? arr : Array.from(arr);
|
||||
};
|
||||
|
||||
console.log((_temp = [123], _ref = _toArray(_temp), x = _ref[0], _temp));
|
||||
@@ -1,3 +0,0 @@
|
||||
{
|
||||
"throws": "AssignmentExpression destructuring outside of a ExpressionStatement is forbidden due to current 6to5 limitations"
|
||||
}
|
||||
@@ -1,13 +1,13 @@
|
||||
"use strict";
|
||||
|
||||
exports["default"] = 42;
|
||||
exports["default"] = {};
|
||||
exports["default"] = [];
|
||||
exports["default"] = foo;
|
||||
exports["default"] = function () {};
|
||||
exports["default"] = function () {};
|
||||
module.exports = foo;
|
||||
module.exports = 42;
|
||||
module.exports = {};
|
||||
module.exports = [];
|
||||
module.exports = foo;
|
||||
module.exports = function () {};
|
||||
module.exports = function () {};
|
||||
function foo() {}
|
||||
exports["default"] = foo;
|
||||
var Foo = function Foo() {};
|
||||
|
||||
exports["default"] = Foo;
|
||||
module.exports = Foo;
|
||||
|
||||
@@ -1,15 +1,6 @@
|
||||
"use strict";
|
||||
|
||||
var _arguments = arguments;
|
||||
var _argumentsToArray = function (args) {
|
||||
var target = new Array(args.length);
|
||||
for (var i = 0; i < args.length; i++) {
|
||||
target[i] = args[i];
|
||||
}
|
||||
|
||||
return target;
|
||||
};
|
||||
|
||||
var _slice = Array.prototype.slice;
|
||||
var concat = function () {
|
||||
var arrs = _argumentsToArray(_arguments);
|
||||
var arrs = _slice.call(arguments);
|
||||
};
|
||||
|
||||
@@ -1,18 +1,10 @@
|
||||
"use strict";
|
||||
|
||||
var _argumentsToArray = function (args) {
|
||||
var target = new Array(args.length);
|
||||
for (var i = 0; i < args.length; i++) {
|
||||
target[i] = args[i];
|
||||
}
|
||||
|
||||
return target;
|
||||
};
|
||||
|
||||
var _slice = Array.prototype.slice;
|
||||
var t = function (f) {
|
||||
var items = _argumentsToArray(arguments).slice(1);
|
||||
var items = _slice.call(arguments, 1);
|
||||
};
|
||||
|
||||
function t(f) {
|
||||
var items = _argumentsToArray(arguments).slice(1);
|
||||
var items = _slice.call(arguments, 1);
|
||||
}
|
||||
|
||||
@@ -1,18 +1,10 @@
|
||||
"use strict";
|
||||
|
||||
var _argumentsToArray = function (args) {
|
||||
var target = new Array(args.length);
|
||||
for (var i = 0; i < args.length; i++) {
|
||||
target[i] = args[i];
|
||||
}
|
||||
|
||||
return target;
|
||||
};
|
||||
|
||||
var _slice = Array.prototype.slice;
|
||||
var t = function () {
|
||||
var items = _argumentsToArray(arguments);
|
||||
var items = _slice.call(arguments);
|
||||
};
|
||||
|
||||
function t() {
|
||||
var items = _argumentsToArray(arguments);
|
||||
var items = _slice.call(arguments);
|
||||
}
|
||||
|
||||
@@ -1,16 +1,8 @@
|
||||
"use strict";
|
||||
|
||||
var _argumentsToArray = function (args) {
|
||||
var target = new Array(args.length);
|
||||
for (var i = 0; i < args.length; i++) {
|
||||
target[i] = args[i];
|
||||
}
|
||||
|
||||
return target;
|
||||
};
|
||||
|
||||
var _slice = Array.prototype.slice;
|
||||
function foo() {
|
||||
return bar.apply(null, ["test"].concat(_argumentsToArray(arguments)));
|
||||
return bar.apply(null, ["test"].concat(_slice.call(arguments)));
|
||||
}
|
||||
|
||||
function bar(one, two, three) {
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
"use strict";
|
||||
|
||||
test["catch"];
|
||||
test["catch"]["foo"];
|
||||
test["catch"];
|
||||
test["catch"]["foo"];
|
||||
@@ -1,3 +1,5 @@
|
||||
obj["x"] = 2;
|
||||
|
||||
test.catch;
|
||||
test.catch["foo"];
|
||||
test["catch"];
|
||||
8
test/fixtures/transformation/misc/_member-expression-literals/expected.js
vendored
Normal file
8
test/fixtures/transformation/misc/_member-expression-literals/expected.js
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
"use strict";
|
||||
|
||||
obj.x = 2;
|
||||
|
||||
test["catch"];
|
||||
test["catch"].foo;
|
||||
test["catch"];
|
||||
test["catch"].foo;
|
||||
@@ -1,5 +1,5 @@
|
||||
"use strict";
|
||||
|
||||
function foo() {
|
||||
var test = customNamespace.argumentsToArray(arguments);
|
||||
var test = customNamespace.slice.call(arguments);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
"use strict";
|
||||
|
||||
function foo() {
|
||||
var test = to5Runtime.argumentsToArray(arguments);
|
||||
var test = to5Runtime.slice.call(arguments);
|
||||
}
|
||||
|
||||
13
test/fixtures/transformation/playground/memoization-assignment-operator/actual.js
vendored
Normal file
13
test/fixtures/transformation/playground/memoization-assignment-operator/actual.js
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
var obj = {};
|
||||
|
||||
obj.x ?= 2;
|
||||
|
||||
console.log(obj.x ?= 2);
|
||||
|
||||
obj[x()] ?= 2;
|
||||
|
||||
console.log(obj[x()] ?= 2);
|
||||
|
||||
obj[y()][x()] ?= 2;
|
||||
|
||||
console.log(obj[y()][x()] ?= 2);
|
||||
20
test/fixtures/transformation/playground/memoization-assignment-operator/exec.js
vendored
Normal file
20
test/fixtures/transformation/playground/memoization-assignment-operator/exec.js
vendored
Normal file
@@ -0,0 +1,20 @@
|
||||
var obj = {};
|
||||
obj.x ?= 2;
|
||||
assert.equal(obj.x, 2);
|
||||
|
||||
obj = {};
|
||||
assert.equal(obj.x ?= 2, 2);
|
||||
|
||||
obj = { x: 1 };
|
||||
obj.x ?= 2;
|
||||
assert.equal(obj.x, 1);
|
||||
|
||||
obj = { x: 1 };
|
||||
assert.equal(obj.x ?= 2, 1);
|
||||
|
||||
obj = { x: undefined }
|
||||
obj.x ?= 2;
|
||||
assert.equal(obj.x, undefined);
|
||||
|
||||
obj = { x: undefined }
|
||||
assert.equal(obj.x ?= 2, undefined);
|
||||
25
test/fixtures/transformation/playground/memoization-assignment-operator/expected.js
vendored
Normal file
25
test/fixtures/transformation/playground/memoization-assignment-operator/expected.js
vendored
Normal file
@@ -0,0 +1,25 @@
|
||||
"use strict";
|
||||
|
||||
var _propKey2, _obj2, _propKey4;
|
||||
var _hasOwn = Object.prototype.hasOwnProperty;
|
||||
var obj = {};
|
||||
|
||||
if (!_hasOwn.call(obj, "x")) obj.x = 2;
|
||||
|
||||
|
||||
console.log((!_hasOwn.call(obj, "x") && (obj.x = 2), obj.x));
|
||||
|
||||
var _propKey = x();
|
||||
|
||||
if (!_hasOwn.call(obj, _propKey)) obj[_propKey] = 2;
|
||||
|
||||
|
||||
console.log((_propKey2 = x(), !_hasOwn.call(obj, _propKey2) && (obj[_propKey2] = 2), obj[_propKey2]));
|
||||
|
||||
var _obj = obj[y()];
|
||||
var _propKey3 = x();
|
||||
|
||||
if (!_hasOwn.call(_obj, _propKey3)) _obj[_propKey3] = 2;
|
||||
|
||||
|
||||
console.log((_obj2 = obj[y()], _propKey4 = x(), !_hasOwn.call(_obj2, _propKey4) && (_obj2[_propKey4] = 2), _obj2[_propKey4]));
|
||||
5
test/fixtures/transformation/playground/method-binding/actual.js
vendored
Normal file
5
test/fixtures/transformation/playground/method-binding/actual.js
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
var fn = obj:method;
|
||||
var fn = obj:method("foob");
|
||||
var fn = obj[foo]:method;
|
||||
var fn = obj.foo:method;
|
||||
var fn = obj[foo()]:method;
|
||||
22
test/fixtures/transformation/playground/method-binding/exec.js
vendored
Normal file
22
test/fixtures/transformation/playground/method-binding/exec.js
vendored
Normal file
@@ -0,0 +1,22 @@
|
||||
var obj = {
|
||||
foo: "foo",
|
||||
bar: "bar",
|
||||
getFoo: function () {
|
||||
return this.foo;
|
||||
},
|
||||
getBar: function (arg) {
|
||||
return arg + " " + this.bar;
|
||||
},
|
||||
getZoo: function (a, b) {
|
||||
return this.foo + " " + this.bar + " " + a + " " + b;
|
||||
}
|
||||
};
|
||||
|
||||
var foo = obj:getFoo;
|
||||
assert.equal(foo(), "foo");
|
||||
|
||||
var bar = obj:getBar("foo");
|
||||
assert.equal(bar(), "foo bar");
|
||||
|
||||
var zoo = obj:getZoo("foo");
|
||||
assert.equal(zoo("bar"), "foo bar foo bar");
|
||||
8
test/fixtures/transformation/playground/method-binding/expected.js
vendored
Normal file
8
test/fixtures/transformation/playground/method-binding/expected.js
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
"use strict";
|
||||
|
||||
var _temp;
|
||||
var fn = obj.method.bind(obj);
|
||||
var fn = obj.method.bind(obj, "foob");
|
||||
var fn = obj[foo].method.bind(obj[foo]);
|
||||
var fn = obj.foo.method.bind(obj.foo);
|
||||
var fn = (_temp = obj[foo()], _temp.method.bind(_temp));
|
||||
3
test/fixtures/transformation/playground/options.json
vendored
Normal file
3
test/fixtures/transformation/playground/options.json
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"playground": true
|
||||
}
|
||||
7
test/fixtures/transformation/playground/pretzel-map/actual.js
vendored
Normal file
7
test/fixtures/transformation/playground/pretzel-map/actual.js
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
["foo", "bar"].map(:toUpperCase);
|
||||
[1.1234, 23.53245, 3].map(:toFixed(2));
|
||||
|
||||
var get = function () {
|
||||
return 2;
|
||||
};
|
||||
[1.1234, 23.53245, 3].map(:toFixed(get()));
|
||||
7
test/fixtures/transformation/playground/pretzel-map/exec.js
vendored
Normal file
7
test/fixtures/transformation/playground/pretzel-map/exec.js
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
assert.deepEqual(["foo", "bar"].map(:toUpperCase), ["FOO", "BAR"]);
|
||||
assert.deepEqual([1.1234, 23.53245, 3].map(:toFixed(2)), ["1.12", "23.53", "3.00"]);
|
||||
|
||||
var get = function () {
|
||||
return 2;
|
||||
}
|
||||
assert.deepEqual([1.1234, 23.53245, 3].map(:toFixed(get())), ["1.12", "23.53", "3.00"]);
|
||||
16
test/fixtures/transformation/playground/pretzel-map/expected.js
vendored
Normal file
16
test/fixtures/transformation/playground/pretzel-map/expected.js
vendored
Normal file
@@ -0,0 +1,16 @@
|
||||
"use strict";
|
||||
|
||||
var _args;
|
||||
["foo", "bar"].map(function (_val) {
|
||||
return _val.toUpperCase();
|
||||
});
|
||||
[1.1234, 23.53245, 3].map(function (_val2) {
|
||||
return _val2.toFixed(2);
|
||||
});
|
||||
|
||||
var get = function () {
|
||||
return 2;
|
||||
};
|
||||
[1.1234, 23.53245, 3].map((_args = [get()], function (_val3) {
|
||||
return _val3.toFixed(_args[0]);
|
||||
}));
|
||||
@@ -37,10 +37,11 @@ var run = function (task, done) {
|
||||
err.message += util.codeFrame(execCode);
|
||||
throw err;
|
||||
}
|
||||
} else {
|
||||
var actualCode = actual.code;
|
||||
var expectCode = expect.code;
|
||||
}
|
||||
|
||||
var actualCode = actual.code;
|
||||
var expectCode = expect.code;
|
||||
if (!execCode || actualCode) {
|
||||
result = transform(actualCode, getOpts(actual));
|
||||
actualCode = result.code;
|
||||
|
||||
|
||||
10
test/util.js
10
test/util.js
@@ -58,10 +58,10 @@ suite("util", function () {
|
||||
});
|
||||
|
||||
test("regexify", function () {
|
||||
assert.deepEqual(util.regexify(undefined), /(?:)/);
|
||||
assert.deepEqual(util.regexify(false), /(?:)/);
|
||||
assert.deepEqual(util.regexify(null), /(?:)/);
|
||||
assert.deepEqual(util.regexify(""), /(?:)/);
|
||||
assert.deepEqual(util.regexify(undefined), /.^/);
|
||||
assert.deepEqual(util.regexify(false), /.^/);
|
||||
assert.deepEqual(util.regexify(null), /.^/);
|
||||
assert.deepEqual(util.regexify(""), /.^/);
|
||||
assert.deepEqual(util.regexify(["foo", "bar"]), /foo|bar/);
|
||||
assert.deepEqual(util.regexify("foobar"), /foobar/);
|
||||
assert.deepEqual(util.regexify(/foobar/), /foobar/);
|
||||
@@ -82,6 +82,8 @@ suite("util", function () {
|
||||
assert.equal(t.toIdentifier("swag-lord"), "swagLord");
|
||||
});
|
||||
|
||||
test("isDynamic");
|
||||
|
||||
test("isReferenced");
|
||||
|
||||
test("removeProperties");
|
||||
|
||||
Reference in New Issue
Block a user