add corejs aliasing transformer and support for optional transformers
This commit is contained in:
parent
31fff092b6
commit
9ee7b07cbf
1
lib/6to5/transformation/templates/corejs-iterator.js
Normal file
1
lib/6to5/transformation/templates/corejs-iterator.js
Normal file
@ -0,0 +1 @@
|
||||
CORE_ID.$for.getIterator(VALUE);
|
||||
@ -80,6 +80,8 @@ _.each({
|
||||
|
||||
_declarations: require("./transformers/_declarations"),
|
||||
|
||||
coreAliasing: require("./transformers/optional-core-aliasing"),
|
||||
|
||||
// wrap up
|
||||
_aliasFunctions: require("./transformers/_alias-functions"),
|
||||
useStrict: require("./transformers/use-strict"),
|
||||
|
||||
@ -5,7 +5,9 @@ var t = require("../types");
|
||||
var _ = require("lodash");
|
||||
|
||||
function Transformer(key, transformer, opts) {
|
||||
this.experimental = !!transformer.experimental;
|
||||
this.transformer = Transformer.normalise(transformer);
|
||||
this.optional = !!transformer.optional;
|
||||
this.opts = opts || {};
|
||||
this.key = key;
|
||||
}
|
||||
@ -16,8 +18,13 @@ Transformer.normalise = function (transformer) {
|
||||
}
|
||||
|
||||
_.each(transformer, function (fns, type) {
|
||||
// hidden property
|
||||
if (type[0] === "_") return;
|
||||
|
||||
if (_.isFunction(fns)) fns = { enter: fns };
|
||||
|
||||
if (!_.isObject(fns)) return;
|
||||
|
||||
transformer[type] = fns;
|
||||
|
||||
var aliases = t.FLIPPED_ALIAS_KEYS[type];
|
||||
@ -41,13 +48,9 @@ Transformer.prototype.astRun = function (file, key) {
|
||||
};
|
||||
|
||||
Transformer.prototype.transform = function (file) {
|
||||
if (!this.canRun(file)) return;
|
||||
|
||||
var transformer = this.transformer;
|
||||
var ast = file.ast;
|
||||
|
||||
this.astRun(file, "enter");
|
||||
|
||||
var build = function (exit) {
|
||||
return function (node, parent, scope) {
|
||||
var fns = transformer[node.type];
|
||||
@ -61,12 +64,14 @@ Transformer.prototype.transform = function (file) {
|
||||
};
|
||||
};
|
||||
|
||||
this.astRun(file, "before");
|
||||
|
||||
traverse(ast, {
|
||||
enter: build(),
|
||||
exit: build(true)
|
||||
});
|
||||
|
||||
this.astRun(file, "exit");
|
||||
this.astRun(file, "after");
|
||||
};
|
||||
|
||||
Transformer.prototype.canRun = function (file) {
|
||||
@ -80,5 +85,9 @@ Transformer.prototype.canRun = function (file) {
|
||||
var whitelist = opts.whitelist;
|
||||
if (whitelist.length && !_.contains(whitelist, key)) return false;
|
||||
|
||||
if (this.optional && !_.contains(opts.optional, key)) return false;
|
||||
|
||||
if (this.experimental && !opts.experimental) return false;
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
@ -11,6 +11,8 @@
|
||||
"FunctionExpression": ["id", "params", "body", "generator"],
|
||||
"Identifier": ["name"],
|
||||
"IfStatement": ["test", "consequent", "alternate"],
|
||||
"ImportDeclaration": ["specifiers", "source"],
|
||||
"ImportSpecifier": ["id", "name"],
|
||||
"Literal": ["value"],
|
||||
"LogicalExpression": ["operator", "left", "right"],
|
||||
"MemberExpression": ["object", "property", "computed"],
|
||||
|
||||
1
test/fixtures/transformation/optional-core-aliasing/es6-destructuring/actual.js
vendored
Normal file
1
test/fixtures/transformation/optional-core-aliasing/es6-destructuring/actual.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
var [a, [b], [c], d] = ["hello", [", ", "junk"], ["world"]];
|
||||
35
test/fixtures/transformation/optional-core-aliasing/es6-destructuring/expected.js
vendored
Normal file
35
test/fixtures/transformation/optional-core-aliasing/es6-destructuring/expected.js
vendored
Normal file
@ -0,0 +1,35 @@
|
||||
"use strict";
|
||||
|
||||
var _slicedToArray = function (arr, i) {
|
||||
if (Array.isArray(arr)) {
|
||||
return arr;
|
||||
} else {
|
||||
var _arr = [];
|
||||
for (var _iterator = _core.$for.getIterator(arr), _step; !(_step = _iterator.next()).done;) {
|
||||
_arr.push(_step.value);
|
||||
|
||||
if (i && _arr.length === i) break;
|
||||
}
|
||||
|
||||
return _arr;
|
||||
}
|
||||
};
|
||||
|
||||
var _interopRequire = function (obj) {
|
||||
return obj && (obj["default"] || obj);
|
||||
};
|
||||
|
||||
var _core = _interopRequire(require("core-js/library"));
|
||||
|
||||
var _ref = ["hello", [", ", "junk"], ["world"]];
|
||||
|
||||
var _ref2 = _slicedToArray(_ref, 4);
|
||||
|
||||
var a = _ref2[0];
|
||||
var _ref3 = _slicedToArray(_ref2[1], 1);
|
||||
|
||||
var b = _ref3[0];
|
||||
var _ref4 = _slicedToArray(_ref2[2], 1);
|
||||
|
||||
var c = _ref4[0];
|
||||
var d = _ref2[3];
|
||||
3
test/fixtures/transformation/optional-core-aliasing/es6-for-of/actual.js
vendored
Normal file
3
test/fixtures/transformation/optional-core-aliasing/es6-for-of/actual.js
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
for (var i of arr) {
|
||||
|
||||
}
|
||||
11
test/fixtures/transformation/optional-core-aliasing/es6-for-of/expected.js
vendored
Normal file
11
test/fixtures/transformation/optional-core-aliasing/es6-for-of/expected.js
vendored
Normal file
@ -0,0 +1,11 @@
|
||||
"use strict";
|
||||
|
||||
var _interopRequire = function (obj) {
|
||||
return obj && (obj["default"] || obj);
|
||||
};
|
||||
|
||||
var _core = _interopRequire(require("core-js/library"));
|
||||
|
||||
for (var _iterator = _core.$for.getIterator(arr), _step; !(_step = _iterator.next()).done;) {
|
||||
var i = _step.value;
|
||||
}
|
||||
1
test/fixtures/transformation/optional-core-aliasing/es6-spread/actual.js
vendored
Normal file
1
test/fixtures/transformation/optional-core-aliasing/es6-spread/actual.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
var lyrics = ["head", "and", "toes", ...parts];
|
||||
13
test/fixtures/transformation/optional-core-aliasing/es6-spread/expected.js
vendored
Normal file
13
test/fixtures/transformation/optional-core-aliasing/es6-spread/expected.js
vendored
Normal file
@ -0,0 +1,13 @@
|
||||
"use strict";
|
||||
|
||||
var _toArray = function (arr) {
|
||||
return Array.isArray(arr) ? arr : _core.Array.from(arr);
|
||||
};
|
||||
|
||||
var _interopRequire = function (obj) {
|
||||
return obj && (obj["default"] || obj);
|
||||
};
|
||||
|
||||
var _core = _interopRequire(require("core-js/library"));
|
||||
|
||||
var lyrics = ["head", "and", "toes"].concat(_toArray(parts));
|
||||
1
test/fixtures/transformation/optional-core-aliasing/es7-array-comprehensions/actual.js
vendored
Normal file
1
test/fixtures/transformation/optional-core-aliasing/es7-array-comprehensions/actual.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
var arr = [for (i of nums) i * i];
|
||||
15
test/fixtures/transformation/optional-core-aliasing/es7-array-comprehensions/expected.js
vendored
Normal file
15
test/fixtures/transformation/optional-core-aliasing/es7-array-comprehensions/expected.js
vendored
Normal file
@ -0,0 +1,15 @@
|
||||
"use strict";
|
||||
|
||||
var _toArray = function (arr) {
|
||||
return Array.isArray(arr) ? arr : _core.Array.from(arr);
|
||||
};
|
||||
|
||||
var _interopRequire = function (obj) {
|
||||
return obj && (obj["default"] || obj);
|
||||
};
|
||||
|
||||
var _core = _interopRequire(require("core-js/library"));
|
||||
|
||||
var arr = _toArray(nums).map(function (i) {
|
||||
return i * i;
|
||||
});
|
||||
1
test/fixtures/transformation/optional-core-aliasing/es7-object-spread-rest/actual.js
vendored
Normal file
1
test/fixtures/transformation/optional-core-aliasing/es7-object-spread-rest/actual.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
var z = { ...x };
|
||||
9
test/fixtures/transformation/optional-core-aliasing/es7-object-spread-rest/expected.js
vendored
Normal file
9
test/fixtures/transformation/optional-core-aliasing/es7-object-spread-rest/expected.js
vendored
Normal file
@ -0,0 +1,9 @@
|
||||
"use strict";
|
||||
|
||||
var _interopRequire = function (obj) {
|
||||
return obj && (obj["default"] || obj);
|
||||
};
|
||||
|
||||
var _core = _interopRequire(require("core-js/library"));
|
||||
|
||||
var z = _core.Object.assign({}, x);
|
||||
4
test/fixtures/transformation/optional-core-aliasing/options.json
vendored
Normal file
4
test/fixtures/transformation/optional-core-aliasing/options.json
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
{
|
||||
"optional": ["coreAliasing"],
|
||||
"experimental": true
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user