Compare commits
14 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f21743012b | ||
|
|
22dc33f93c | ||
|
|
6df6652629 | ||
|
|
4942ba6dd7 | ||
|
|
b63bd8cba0 | ||
|
|
cd1eb11171 | ||
|
|
6e1c12fd78 | ||
|
|
a4ea71b85d | ||
|
|
1df69bdbeb | ||
|
|
84c2766756 | ||
|
|
1f000ac83d | ||
|
|
322aa246b8 | ||
|
|
601bbb86cd | ||
|
|
5c39685650 |
15
CHANGELOG.md
15
CHANGELOG.md
@@ -2,6 +2,21 @@
|
||||
|
||||
Gaps between patch versions are faulty/broken releases.
|
||||
|
||||
## 2.0.3
|
||||
|
||||
* Hoist function declarations in system module formatter for circular references.
|
||||
* Hoist default function declarations in umd and amd module formatters for circular references.
|
||||
|
||||
## 2.0.2
|
||||
|
||||
* Inherit comments in `for-of` transformer.
|
||||
* Remove `interopRequire` from `system` module formatter.
|
||||
|
||||
## 2.0.1
|
||||
|
||||
* Remap `UpdateExpression` module export binding.
|
||||
* Fix automatic closure on `PrivateDeclaration` in classes.
|
||||
|
||||
## 2.0.0
|
||||
|
||||
* Make string literal generation only escapes unicode that it has to.
|
||||
|
||||
@@ -44,16 +44,42 @@ DefaultFormatter.prototype.remapAssignments = function () {
|
||||
var self = this;
|
||||
|
||||
var isLocalReference = function (node, scope) {
|
||||
var left = node.left;
|
||||
var name = left.name;
|
||||
return t.isIdentifier(left) && localExports[name] && localExports[name] === scope.get(name, true);
|
||||
var name = node.name;
|
||||
return t.isIdentifier(node) && localExports[name] && localExports[name] === scope.get(name, true);
|
||||
};
|
||||
|
||||
traverse(this.file.ast, {
|
||||
enter: function (node, parent, scope) {
|
||||
if (t.isExportDeclaration(node)) return false;
|
||||
if (t.isUpdateExpression(node) && isLocalReference(node.argument, scope)) {
|
||||
this.stop();
|
||||
|
||||
if (t.isAssignmentExpression(node) && isLocalReference(node, scope)) {
|
||||
// expand to long file assignment expression
|
||||
var assign = t.assignmentExpression(node.operator[0] + "=", node.argument, t.literal(1));
|
||||
|
||||
// remap this assignment expression
|
||||
var remapped = self.remapExportAssignment(assign);
|
||||
|
||||
// we don't need to change the result
|
||||
if (t.isExpressionStatement(parent) || node.prefix) {
|
||||
return remapped;
|
||||
}
|
||||
|
||||
var nodes = [];
|
||||
nodes.push(remapped);
|
||||
|
||||
var operator;
|
||||
if (node.operator === "--") {
|
||||
operator = "+";
|
||||
} else { // "++"
|
||||
operator = "-";
|
||||
}
|
||||
nodes.push(t.binaryExpression(operator, node.argument, t.literal(1)));
|
||||
|
||||
return t.sequenceExpression(nodes);
|
||||
}
|
||||
|
||||
if (t.isAssignmentExpression(node) && isLocalReference(node.left, scope)) {
|
||||
this.stop();
|
||||
return self.remapExportAssignment(node);
|
||||
}
|
||||
}
|
||||
@@ -143,30 +169,36 @@ DefaultFormatter.prototype._exportsAssign = function (id, init) {
|
||||
DefaultFormatter.prototype.exportDeclaration = function (node, nodes) {
|
||||
var declar = node.declaration;
|
||||
|
||||
var id = declar.id;
|
||||
|
||||
if (node.default) {
|
||||
nodes.push(
|
||||
this._exportsAssign(t.identifier("default"), this._pushStatement(declar, nodes))
|
||||
);
|
||||
} else {
|
||||
var assign;
|
||||
id = t.identifier("default");
|
||||
}
|
||||
|
||||
if (t.isVariableDeclaration(declar)) {
|
||||
for (var i in declar.declarations) {
|
||||
var decl = declar.declarations[i];
|
||||
var assign;
|
||||
|
||||
decl.init = this._exportsAssign(decl.id, decl.init).expression;
|
||||
if (t.isVariableDeclaration(declar)) {
|
||||
for (var i in declar.declarations) {
|
||||
var decl = declar.declarations[i];
|
||||
|
||||
var newDeclar = t.variableDeclaration(declar.kind, [decl]);
|
||||
if (i === "0") t.inherits(newDeclar, declar);
|
||||
nodes.push(newDeclar);
|
||||
}
|
||||
} else {
|
||||
assign = this._exportsAssign(declar.id, declar.id);
|
||||
decl.init = this._exportsAssign(decl.id, decl.init).expression;
|
||||
|
||||
nodes.push(t.toStatement(declar));
|
||||
nodes.push(assign);
|
||||
|
||||
this._hoistExport(declar, assign);
|
||||
var newDeclar = t.variableDeclaration(declar.kind, [decl]);
|
||||
if (i === "0") t.inherits(newDeclar, declar);
|
||||
nodes.push(newDeclar);
|
||||
}
|
||||
} else {
|
||||
var ref = declar;
|
||||
|
||||
if (t.isFunctionDeclaration(declar) || t.isClassDeclaration(declar)) {
|
||||
ref = declar.id;
|
||||
nodes.push(declar);
|
||||
}
|
||||
|
||||
assign = this._exportsAssign(id, ref);
|
||||
|
||||
nodes.push(assign);
|
||||
|
||||
this._hoistExport(declar, assign);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -5,8 +5,8 @@ var util = require("../../util");
|
||||
var t = require("../../types");
|
||||
var _ = require("lodash");
|
||||
|
||||
function AMDFormatter(file) {
|
||||
this.file = file;
|
||||
function AMDFormatter() {
|
||||
DefaultFormatter.apply(this, arguments);
|
||||
this.ids = {};
|
||||
}
|
||||
|
||||
@@ -83,7 +83,7 @@ AMDFormatter.prototype.importSpecifier = function (specifier, node, nodes) {
|
||||
|
||||
if (t.isImportBatchSpecifier(specifier)) {
|
||||
// import * as bar from "foo";
|
||||
} else if (t.isSpecifierDefault(specifier)) {
|
||||
} else if (t.isSpecifierDefault(specifier) && !this.noInteropRequire) {
|
||||
// import foo from "foo";
|
||||
ref = t.callExpression(this.file.addDeclaration("interop-require"), [ref]);
|
||||
} else {
|
||||
|
||||
@@ -2,15 +2,18 @@ module.exports = SystemFormatter;
|
||||
|
||||
var DefaultFormatter = require("./_default");
|
||||
var AMDFormatter = require("./amd");
|
||||
var traverse = require("../../traverse");
|
||||
var util = require("../../util");
|
||||
var t = require("../../types");
|
||||
var _ = require("lodash");
|
||||
|
||||
function SystemFormatter(file) {
|
||||
this.exportIdentifier = file.generateUidIdentifier("export");
|
||||
this.noInteropRequire = true;
|
||||
|
||||
AMDFormatter.apply(this, arguments);
|
||||
|
||||
this.moduleNameLiteral = t.literal(this.getModuleName());
|
||||
this.exportIdentifier = file.generateUidIdentifier("export");
|
||||
}
|
||||
|
||||
util.inherits(SystemFormatter, AMDFormatter);
|
||||
@@ -72,12 +75,28 @@ SystemFormatter.prototype.transform = function (ast) {
|
||||
EXECUTE: t.functionExpression(null, [], t.blockStatement(program.body))
|
||||
}, true);
|
||||
|
||||
var handlerBody = runner.expression.arguments[2].body.body;
|
||||
var returnStatement = handlerBody.pop();
|
||||
|
||||
// hoist up function declarations for circular references
|
||||
traverse(program, {
|
||||
enter: function (node) {
|
||||
if (t.isFunction(node)) this.stop();
|
||||
|
||||
if (t.isFunctionDeclaration(node) || node._blockHoist) {
|
||||
handlerBody.push(node);
|
||||
this.remove();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (!_.isEmpty(this.ids)) {
|
||||
var handlerBody = runner.expression.arguments[2].body.body;
|
||||
handlerBody.unshift(t.variableDeclaration("var", _.map(this.ids, function (uid) {
|
||||
handlerBody.push(t.variableDeclaration("var", _.map(this.ids, function (uid) {
|
||||
return t.variableDeclarator(uid);
|
||||
})));
|
||||
}
|
||||
|
||||
handlerBody.push(returnStatement);
|
||||
|
||||
program.body = [runner];
|
||||
};
|
||||
|
||||
@@ -8,16 +8,24 @@ exports.ClassDeclaration = function (node, parent, file, scope) {
|
||||
closure = false;
|
||||
}
|
||||
|
||||
var newNode = new Class(node, file, scope, closure).run();
|
||||
if (closure) {
|
||||
// declaration in an expression context...
|
||||
// export default class Foo {}
|
||||
scope.push({
|
||||
kind: "var",
|
||||
key: node.id.key,
|
||||
id: node.id
|
||||
});
|
||||
return t.assignmentExpression("=", node.id, newNode);
|
||||
var factory = new Class(node, file, scope, closure);
|
||||
var newNode = factory.run();
|
||||
if (factory.closure) {
|
||||
if (closure) {
|
||||
// declaration in an expression context...
|
||||
// export default class Foo {}
|
||||
scope.push({
|
||||
kind: "var",
|
||||
key: node.id.key,
|
||||
id: node.id
|
||||
});
|
||||
return t.assignmentExpression("=", node.id, newNode);
|
||||
} else {
|
||||
// likely has a PrivateDeclaration etc
|
||||
return t.variableDeclaration("let", [
|
||||
t.variableDeclarator(node.id, newNode)
|
||||
]);
|
||||
}
|
||||
} else {
|
||||
return newNode;
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ exports.ForOfStatement = function (node, parent, file, scope) {
|
||||
OBJECT: node.right
|
||||
});
|
||||
|
||||
t.inheritsComments(node2, node);
|
||||
t.ensureBlock(node);
|
||||
|
||||
var block = node2.body;
|
||||
|
||||
@@ -50,17 +50,35 @@ function traverse(parent, opts, scope) {
|
||||
}
|
||||
};
|
||||
|
||||
var stopped = false;
|
||||
var removed = false;
|
||||
|
||||
var context = {
|
||||
stop: function () {
|
||||
stopped = true;
|
||||
},
|
||||
|
||||
remove: function () {
|
||||
stopped = removed = true;
|
||||
}
|
||||
};
|
||||
|
||||
//
|
||||
var ourScope = scope;
|
||||
if (t.isScope(node)) ourScope = new Scope(node, scope);
|
||||
|
||||
// enter
|
||||
if (opts.enter) {
|
||||
var result = opts.enter(node, parent, ourScope);
|
||||
var result = opts.enter.call(context, node, parent, ourScope);
|
||||
maybeReplace(result);
|
||||
|
||||
if (removed) {
|
||||
delete obj[key];
|
||||
updated = true;
|
||||
}
|
||||
|
||||
// stop iteration
|
||||
if (result === false) return;
|
||||
if (stopped || result === false) return;
|
||||
}
|
||||
|
||||
// traverse node
|
||||
@@ -68,7 +86,7 @@ function traverse(parent, opts, scope) {
|
||||
|
||||
// exit
|
||||
if (opts.exit) {
|
||||
maybeReplace(opts.exit(node, parent, ourScope));
|
||||
maybeReplace(opts.exit.call(context, node, parent, ourScope));
|
||||
}
|
||||
};
|
||||
|
||||
@@ -89,7 +107,6 @@ traverse.removeProperties = function (tree) {
|
||||
delete node._declarations;
|
||||
delete node.extendedRange;
|
||||
delete node._scopeInfo;
|
||||
delete node._scope;
|
||||
delete node.tokens;
|
||||
delete node.range;
|
||||
delete node.start;
|
||||
|
||||
@@ -240,14 +240,17 @@ t.getIds = function (node, map, ignoreTypes) {
|
||||
if (_.contains(ignoreTypes, id.type)) continue;
|
||||
|
||||
var nodeKey = t.getIds.nodes[id.type];
|
||||
var arrKey = t.getIds.arrays[id.type];
|
||||
var arrKeys = t.getIds.arrays[id.type];
|
||||
|
||||
if (t.isIdentifier(id)) {
|
||||
ids[id.name] = id;
|
||||
} else if (nodeKey) {
|
||||
if (id[nodeKey]) search.push(id[nodeKey]);
|
||||
} else if (arrKey) {
|
||||
search = search.concat(id[arrKey] || []);
|
||||
} else if (arrKeys) {
|
||||
for (var i in arrKeys) {
|
||||
var key = arrKeys[i];
|
||||
search = search.concat(id[key] || []);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -268,11 +271,11 @@ t.getIds.nodes = {
|
||||
};
|
||||
|
||||
t.getIds.arrays = {
|
||||
ExportDeclaration: "specifiers",
|
||||
ImportDeclaration: "specifiers",
|
||||
VariableDeclaration: "declarations",
|
||||
ArrayPattern: "elements",
|
||||
ObjectPattern: "properties"
|
||||
ExportDeclaration: ["specifiers", "declaration"],
|
||||
ImportDeclaration: ["specifiers"],
|
||||
VariableDeclaration: ["declarations"],
|
||||
ArrayPattern: ["elements"],
|
||||
ObjectPattern: ["properties"]
|
||||
};
|
||||
|
||||
t.isLet = function (node) {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "6to5",
|
||||
"description": "Turn ES6 code into readable vanilla ES5 with source maps",
|
||||
"version": "2.0.0",
|
||||
"version": "2.0.3",
|
||||
"author": "Sebastian McKenzie <sebmck@gmail.com>",
|
||||
"homepage": "https://github.com/6to5/6to5",
|
||||
"repository": {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
define(["exports"], function (exports) {
|
||||
"use strict";
|
||||
|
||||
exports["default"] = foo;
|
||||
exports["default"] = 42;
|
||||
exports["default"] = {};
|
||||
exports["default"] = [];
|
||||
@@ -10,7 +11,6 @@ define(["exports"], function (exports) {
|
||||
exports["default"] = function () {};
|
||||
|
||||
function foo() {}
|
||||
exports["default"] = foo;
|
||||
var Foo = function Foo() {};
|
||||
|
||||
exports["default"] = Foo;
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
export var test = 2;
|
||||
test = 5;
|
||||
test++;
|
||||
|
||||
(function () {
|
||||
var test = 2;
|
||||
test = 3;
|
||||
test++;
|
||||
})();
|
||||
|
||||
@@ -2,10 +2,12 @@ define(["exports"], function (exports) {
|
||||
"use strict";
|
||||
|
||||
var test = exports.test = 2;
|
||||
test = 5;
|
||||
test = exports.test = 5;
|
||||
test = exports.test += 1;
|
||||
|
||||
(function () {
|
||||
var test = 2;
|
||||
test = 3;
|
||||
test++;
|
||||
})();
|
||||
});
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
export var test = 2;
|
||||
test = 5;
|
||||
test++;
|
||||
|
||||
(function () {
|
||||
var test = 2;
|
||||
test = 3;
|
||||
test++;
|
||||
})();
|
||||
|
||||
@@ -2,8 +2,10 @@
|
||||
|
||||
var test = exports.test = 2;
|
||||
test = exports.test = 5;
|
||||
test = exports.test += 1;
|
||||
|
||||
(function () {
|
||||
var test = 2;
|
||||
test = 3;
|
||||
test++;
|
||||
})();
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
export var test = 2;
|
||||
test = 5;
|
||||
test++;
|
||||
|
||||
(function () {
|
||||
var test = 2;
|
||||
test = 3;
|
||||
test++;
|
||||
})();
|
||||
|
||||
@@ -2,8 +2,10 @@
|
||||
|
||||
var test = 2;
|
||||
test = 5;
|
||||
test++;
|
||||
|
||||
(function () {
|
||||
var test = 2;
|
||||
test = 3;
|
||||
test++;
|
||||
})();
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
System.register("es6-modules-system/exports-default/expected", [], function (_export) {
|
||||
_export("default", foo);
|
||||
|
||||
function foo() {}
|
||||
return {
|
||||
setters: [],
|
||||
execute: function () {
|
||||
@@ -16,12 +19,9 @@ System.register("es6-modules-system/exports-default/expected", [], function (_ex
|
||||
|
||||
_export("default", function () {});
|
||||
|
||||
function foo() {}
|
||||
_export("default", foo);
|
||||
|
||||
var Foo = function Foo() {};
|
||||
|
||||
_export("default", Foo);
|
||||
}
|
||||
};
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,21 +1,21 @@
|
||||
System.register("es6-modules-system/exports-variable/expected", [], function (_export) {
|
||||
_export("foo7", foo7);
|
||||
|
||||
function foo7() {}
|
||||
return {
|
||||
setters: [],
|
||||
execute: function () {
|
||||
"use strict";
|
||||
|
||||
_export("foo7", foo7);
|
||||
|
||||
var foo = _export("foo", 1);
|
||||
var foo2 = _export("foo2", function () {});
|
||||
var foo3 = _export("foo3", undefined);
|
||||
var foo4 = _export("foo4", 2);
|
||||
var foo5 = _export("foo5", undefined);
|
||||
var foo6 = _export("foo6", 3);
|
||||
function foo7() {}
|
||||
var foo8 = function foo8() {};
|
||||
|
||||
_export("foo8", foo8);
|
||||
}
|
||||
};
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,4 +1,10 @@
|
||||
System.register("es6-modules-system/hoist-function-exports/expected", ["./evens"], function (_export) {
|
||||
_export("nextOdd", nextOdd);
|
||||
|
||||
function nextOdd(n) {
|
||||
return isEven(n) ? n + 1 : n + 2;
|
||||
}
|
||||
|
||||
var _evens;
|
||||
return {
|
||||
setters: [function (m) {
|
||||
@@ -7,13 +13,7 @@ System.register("es6-modules-system/hoist-function-exports/expected", ["./evens"
|
||||
execute: function () {
|
||||
"use strict";
|
||||
|
||||
_export("nextOdd", nextOdd);
|
||||
|
||||
var isEven = _evens.isEven;
|
||||
function nextOdd(n) {
|
||||
return isEven(n) ? n + 1 : n + 2;
|
||||
}
|
||||
|
||||
var isOdd = _export("isOdd", (function (isEven) {
|
||||
return function (n) {
|
||||
return !isEven(n);
|
||||
@@ -21,4 +21,4 @@ System.register("es6-modules-system/hoist-function-exports/expected", ["./evens"
|
||||
})(isEven));
|
||||
}
|
||||
};
|
||||
});
|
||||
});
|
||||
|
||||
@@ -7,13 +7,8 @@ System.register("es6-modules-system/imports-default/expected", ["foo"], function
|
||||
execute: function () {
|
||||
"use strict";
|
||||
|
||||
var _interopRequire = function (obj) {
|
||||
return obj && (obj["default"] || obj);
|
||||
};
|
||||
|
||||
var foo = _interopRequire(_foo);
|
||||
|
||||
var foo = _interopRequire(_foo);
|
||||
var foo = _foo["default"];
|
||||
var foo = _foo["default"];
|
||||
}
|
||||
};
|
||||
});
|
||||
});
|
||||
|
||||
@@ -7,13 +7,8 @@ System.register("es6-modules-system/imports-mixing/expected", ["foo"], function
|
||||
execute: function () {
|
||||
"use strict";
|
||||
|
||||
var _interopRequire = function (obj) {
|
||||
return obj && (obj["default"] || obj);
|
||||
};
|
||||
|
||||
var foo = _interopRequire(_foo);
|
||||
|
||||
var foo = _foo["default"];
|
||||
var xyz = _foo.baz;
|
||||
}
|
||||
};
|
||||
});
|
||||
});
|
||||
|
||||
@@ -11,12 +11,7 @@ System.register("es6-modules-system/overview/expected", ["foo", "foo-bar", "./di
|
||||
execute: function () {
|
||||
"use strict";
|
||||
|
||||
var _interopRequire = function (obj) {
|
||||
return obj && (obj["default"] || obj);
|
||||
};
|
||||
|
||||
var foo = _interopRequire(_foo);
|
||||
|
||||
var foo = _foo["default"];
|
||||
var foo = _foo;
|
||||
var bar = _foo.bar;
|
||||
var bar = _foo.foo;
|
||||
@@ -27,4 +22,4 @@ System.register("es6-modules-system/overview/expected", ["foo", "foo-bar", "./di
|
||||
_export("default", test);
|
||||
}
|
||||
};
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
export var test = 2;
|
||||
test = 5;
|
||||
test++;
|
||||
|
||||
(function () {
|
||||
var test = 2;
|
||||
test = 3;
|
||||
test++;
|
||||
})();
|
||||
|
||||
@@ -5,12 +5,14 @@ System.register("es6-modules-system/remap/expected", [], function (_export) {
|
||||
"use strict";
|
||||
|
||||
var test = _export("test", 2);
|
||||
test = 5;
|
||||
_export("test", test = 5);
|
||||
_export("test", test += 1);
|
||||
|
||||
(function () {
|
||||
var test = 2;
|
||||
test = 3;
|
||||
test++;
|
||||
})();
|
||||
}
|
||||
};
|
||||
});
|
||||
});
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
})(function (exports) {
|
||||
"use strict";
|
||||
|
||||
exports["default"] = foo;
|
||||
exports["default"] = 42;
|
||||
exports["default"] = {};
|
||||
exports["default"] = [];
|
||||
@@ -16,7 +17,6 @@
|
||||
exports["default"] = function () {};
|
||||
|
||||
function foo() {}
|
||||
exports["default"] = foo;
|
||||
var Foo = function Foo() {};
|
||||
|
||||
exports["default"] = Foo;
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
export var test = 2;
|
||||
test = 5;
|
||||
test++;
|
||||
|
||||
(function () {
|
||||
var test = 2;
|
||||
test = 3;
|
||||
test++;
|
||||
})();
|
||||
|
||||
@@ -8,10 +8,12 @@
|
||||
"use strict";
|
||||
|
||||
var test = exports.test = 2;
|
||||
test = 5;
|
||||
test = exports.test = 5;
|
||||
test = exports.test += 1;
|
||||
|
||||
(function () {
|
||||
var test = 2;
|
||||
test = 3;
|
||||
test++;
|
||||
})();
|
||||
});
|
||||
|
||||
@@ -2,13 +2,13 @@
|
||||
|
||||
var A = new WeakMap();
|
||||
var B = new WeakMap(), C = new WeakMap();
|
||||
(function () {
|
||||
var D = (function () {
|
||||
var F = new WeakMap(), G = new WeakMap();
|
||||
var E = new WeakMap();
|
||||
var D = function D() {};
|
||||
|
||||
return D;
|
||||
})()
|
||||
})();
|
||||
|
||||
var H = (function () {
|
||||
var J = new WeakMap(), K = new WeakMap();
|
||||
|
||||
Reference in New Issue
Block a user