Compare commits
116 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4763b95a0d | ||
|
|
9fe1e37ca7 | ||
|
|
8a9aac3e68 | ||
|
|
27138abd29 | ||
|
|
dcf91db475 | ||
|
|
ab63345764 | ||
|
|
a35e63fb29 | ||
|
|
3fe7df9a48 | ||
|
|
5288f3afda | ||
|
|
25566a24f6 | ||
|
|
2ff6dee0ec | ||
|
|
491d1238c2 | ||
|
|
234414c2f2 | ||
|
|
3ff544bbab | ||
|
|
416c4cbb84 | ||
|
|
db5bf1749b | ||
|
|
41349afea3 | ||
|
|
27da6de723 | ||
|
|
2cdb4e3343 | ||
|
|
981d3e40f8 | ||
|
|
c7a616730c | ||
|
|
5aa8ece242 | ||
|
|
8c7ba20f86 | ||
|
|
1cc9027fcf | ||
|
|
e63dbaa646 | ||
|
|
4a720625d9 | ||
|
|
ad428b107a | ||
|
|
c0299320f0 | ||
|
|
efaee3d5d9 | ||
|
|
12bee73070 | ||
|
|
82c18a837d | ||
|
|
fed51e8246 | ||
|
|
71f17e464f | ||
|
|
026fd7eddb | ||
|
|
456b2d3910 | ||
|
|
4208bf3f4b | ||
|
|
a81678d327 | ||
|
|
be55f42f80 | ||
|
|
b1b326cf9c | ||
|
|
a9ff73b4f6 | ||
|
|
f5ccb9c0ba | ||
|
|
1cbbe00b7a | ||
|
|
055dd5d2c3 | ||
|
|
0fea437536 | ||
|
|
f325d3b065 | ||
|
|
e514660fbd | ||
|
|
a5d8144cce | ||
|
|
8145840264 | ||
|
|
a02eafedfd | ||
|
|
3f3cd2bb3a | ||
|
|
503a3f2e3a | ||
|
|
48a8723fdb | ||
|
|
92c297b1be | ||
|
|
56e26378ff | ||
|
|
f3f69ab4fa | ||
|
|
2a488b951d | ||
|
|
ac01caacd3 | ||
|
|
cbdf746474 | ||
|
|
ad0a1ae66d | ||
|
|
3b783979d8 | ||
|
|
b2ec15accc | ||
|
|
3b7cfc908e | ||
|
|
010ca83d0d | ||
|
|
161c895a90 | ||
|
|
47a45f3731 | ||
|
|
7bb3cfd932 | ||
|
|
16a94a769a | ||
|
|
f7c7918efe | ||
|
|
bf393c025f | ||
|
|
bbbc9c0c5e | ||
|
|
579db9107f | ||
|
|
d1d30e9ec9 | ||
|
|
ee782f93c4 | ||
|
|
9ed6aa48a0 | ||
|
|
ecebedd5a2 | ||
|
|
31df576d26 | ||
|
|
63d6335d99 | ||
|
|
9e21994c01 | ||
|
|
b8ee654ac2 | ||
|
|
00b22b1231 | ||
|
|
2e589904c7 | ||
|
|
1cd475d118 | ||
|
|
fbdea41399 | ||
|
|
5d720dbc43 | ||
|
|
3940149517 | ||
|
|
e963798067 | ||
|
|
36acfd16ea | ||
|
|
dda372f93e | ||
|
|
62bc815f81 | ||
|
|
4e68f94023 | ||
|
|
2687b435e0 | ||
|
|
9ddf411f2c | ||
|
|
c2fc89af6c | ||
|
|
a5eda35ff4 | ||
|
|
b5e1221c54 | ||
|
|
03942da57a | ||
|
|
e469c864bc | ||
|
|
9d50cae9fd | ||
|
|
249d749580 | ||
|
|
ea860ac5a5 | ||
|
|
30259f3a99 | ||
|
|
a3d7a29961 | ||
|
|
8ce762846e | ||
|
|
8092716b79 | ||
|
|
b7ba54724c | ||
|
|
7f7ee41315 | ||
|
|
b33f05bd3d | ||
|
|
1ae6eabedd | ||
|
|
973be9ad96 | ||
|
|
979ce93499 | ||
|
|
642e36c259 | ||
|
|
efaf56c6de | ||
|
|
a0c7950d8a | ||
|
|
d87b70b57e | ||
|
|
bbfb297d73 | ||
|
|
9b68d08604 |
3
.gitmodules
vendored
3
.gitmodules
vendored
@@ -7,3 +7,6 @@
|
||||
[submodule "vendor/test262"]
|
||||
path = vendor/test262
|
||||
url = https://github.com/tc39/test262
|
||||
[submodule "vendor/compat-table"]
|
||||
path = vendor/compat-table
|
||||
url = https://github.com/kangax/compat-table
|
||||
|
||||
2
.istanbul.yml
Normal file
2
.istanbul.yml
Normal file
@@ -0,0 +1,2 @@
|
||||
instrumentation:
|
||||
root: lib
|
||||
86
CHANGELOG.md
86
CHANGELOG.md
@@ -11,6 +11,80 @@
|
||||
|
||||
_Note: Gaps between patch versions are faulty/broken releases._
|
||||
|
||||
## 3.3.1
|
||||
|
||||
* **Bug Fix**
|
||||
* Block hoist assignment pattern destructuring.
|
||||
|
||||
## 3.3.0
|
||||
|
||||
* **Bug Fix**
|
||||
* Do all transforms before the regenerator transform is ran.
|
||||
* **New Feature**
|
||||
* Added back the 2.x optional runtime.
|
||||
|
||||
## 3.2.1
|
||||
|
||||
* **Bug Fix**
|
||||
* Fix block scoping transformer rewriting breaks and continues to inner labels.
|
||||
|
||||
## 3.2.0
|
||||
|
||||
* **Bug Fix**
|
||||
* Fixed scope tracking for default parameters IIFE.
|
||||
* Fixed block scoped functions.
|
||||
* Improved `bin/6to5` path resolution.
|
||||
* **New Feature**
|
||||
* You can now trigger super setters in classes. Thanks [@kruppel](https://github.com/kruppel)!
|
||||
* Add `resolveSourceMap` option.
|
||||
* Better support and output for block scoping loops with labels.
|
||||
|
||||
## 3.1.1
|
||||
|
||||
* **Polish**
|
||||
* Drop `enumerable: false` clause from class method definitions as `enumerable` already defaults to `false`.
|
||||
* **Bug Fix**
|
||||
* Properly transform `XJSIdentifier` nodes referencing `this` into a `ThisExpression`.
|
||||
|
||||
## 3.1.0
|
||||
|
||||
* **Breaking Change**
|
||||
* [Make class methods unenumerable](https://esdiscuss.org/topic/classes-and-enumerability#content-61).
|
||||
|
||||
## 3.0.16
|
||||
|
||||
* **Bug Fix**
|
||||
* Don't ignore dynamic import specifiers.
|
||||
* **Internal**
|
||||
* Upgrade `regenerator-6to5`.
|
||||
* **Polish**
|
||||
* Use `Object.assign` in place of extends helper if it exists.
|
||||
* Require individual `lodash` methods. Thanks [@stefanpenner](https://github.com/stefanpenner)!
|
||||
|
||||
## 3.0.15
|
||||
|
||||
* **Bug Fix**
|
||||
* Use `debug/node` instead of `debug` to avoid browserify using the browser version that references `window`.
|
||||
|
||||
## 3.0.14
|
||||
|
||||
* **New Feature**
|
||||
* Add `--optional` argument to `6to5-node`.
|
||||
* **Bug Fix**
|
||||
* Fix bug in `asyncToGenerator` helper where it was incorrectly calling generator iterator functions.
|
||||
|
||||
## 3.0.13
|
||||
|
||||
* **Bug Fix**
|
||||
* Fix modules loose mode using `modules` instead of `es6.modules`.
|
||||
|
||||
## 3.0.12
|
||||
|
||||
* **Internal**
|
||||
* Add internal debug messages.
|
||||
* **Bug Fix**
|
||||
* Add `noScope` option to `traverse.clearProperties`.
|
||||
|
||||
## 3.0.11
|
||||
|
||||
* **Bug Fix**
|
||||
@@ -105,13 +179,13 @@ _Note: Gaps between patch versions are faulty/broken releases._
|
||||
* This shorthand has been removed from the playground.
|
||||
* `6to5/polyfill` can now only be required **once**.
|
||||
* **CLI**
|
||||
* `--indent` option has been removed.
|
||||
* `--include-regenerator` option has been removed.
|
||||
* `--amd-modules-id` option has been removed, use `--module-ids` instead.
|
||||
* `--indent` option has been removed.
|
||||
* `--include-regenerator` option has been removed.
|
||||
* `--amd-modules-id` option has been removed, use `--module-ids` instead.
|
||||
* **Options**
|
||||
* `amdModuleIds` option has been removed, use `moduleIds` instead.
|
||||
* `includeRegenerator` has been removed.
|
||||
* `ignoreRegex` fallback has now been dropped from the require hook. `register(/foo/);`, `register({ ignoreRegex: /foo/ })` -> `register({ ignore: /foo/ })`.
|
||||
* `amdModuleIds` option has been removed, use `moduleIds` instead.
|
||||
* `includeRegenerator` has been removed.
|
||||
* `ignoreRegex` fallback has now been dropped from the require hook. `register(/foo/);`, `register({ ignoreRegex: /foo/ })` -> `register({ ignore: /foo/ })`.
|
||||
* **Modules**
|
||||
* Module interop now only collapses to `module.exports` when there's a **single** export that's default.
|
||||
* Imports and exports are now illegal anywhere except the root level by default. Set `modules` to [loose mode](http://6to5.org/docs/usage/loose) to allow them everywhere.
|
||||
|
||||
5
Makefile
5
Makefile
@@ -21,6 +21,9 @@ build:
|
||||
node $(BROWSERIFY_CMD) lib/6to5/browser.js -s to5 >dist/6to5.js
|
||||
node $(UGLIFY_CMD) dist/6to5.js >dist/6to5.min.js
|
||||
|
||||
node bin/6to5-runtime >dist/runtime.js
|
||||
node $(UGLIFY_CMD) dist/runtime.js >dist/runtime.min.js
|
||||
|
||||
rm -rf templates.json
|
||||
|
||||
clean:
|
||||
@@ -77,6 +80,7 @@ publish:
|
||||
make build
|
||||
cp dist/6to5.min.js browser.js
|
||||
cp dist/polyfill.min.js browser-polyfill.js
|
||||
cp dist/runtime.min.js runtime.js
|
||||
|
||||
node tools/cache-templates
|
||||
test -f templates.json
|
||||
@@ -110,3 +114,4 @@ bootstrap:
|
||||
npm install
|
||||
git submodule update --init
|
||||
cd vendor/regenerator; npm install
|
||||
cd vendor/compat-table; npm install object-assign
|
||||
|
||||
4
bin/6to5-runtime
Executable file
4
bin/6to5-runtime
Executable file
@@ -0,0 +1,4 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
var runtime = require("../lib/6to5/build-runtime");
|
||||
console.log(runtime());
|
||||
@@ -53,10 +53,7 @@ module.exports = function (commander, filenames, opts) {
|
||||
|
||||
_.each(["add", "change"], function (type) {
|
||||
watcher.on(type, function (filename) {
|
||||
// chop off the dirname plus the path separator
|
||||
var relative = filename.slice(dirname.length + 1);
|
||||
|
||||
console.log(type, filename);
|
||||
var relative = path.relative(dirname, filename) || filename;
|
||||
write(filename, relative);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,15 +1,17 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
var commander = require("commander");
|
||||
var transform = require("../../lib/6to5/transformation/transform");
|
||||
var transform = require("../../lib/6to5/transformation");
|
||||
var util = require("../../lib/6to5/util");
|
||||
var fs = require("fs");
|
||||
var _ = require("lodash");
|
||||
var each = require("lodash/collection/each");
|
||||
var keys = require("lodash/object/keys");
|
||||
|
||||
commander.option("-t, --source-maps-inline", "Append sourceMappingURL comment to bottom of code");
|
||||
commander.option("-s, --source-maps", "Save source map alongside the compiled code");
|
||||
commander.option("-f, --filename [filename]", "Filename to use when reading from stdin - this will be used in source-maps, errors etc [stdin]", "stdin");
|
||||
commander.option("-w, --watch", "Recompile files on changes");
|
||||
commander.option("-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");
|
||||
|
||||
@@ -30,7 +32,7 @@ commander.on("--help", function () {
|
||||
console.log(" " + title + ":");
|
||||
console.log();
|
||||
|
||||
_.each(_.keys(obj).sort(), function (key) {
|
||||
each(keys(obj).sort(), function (key) {
|
||||
if (key[0] === "_") return;
|
||||
|
||||
if (obj[key].optional) {
|
||||
@@ -58,7 +60,7 @@ var errors = [];
|
||||
|
||||
var filenames = commander.args;
|
||||
|
||||
_.each(filenames, function (filename) {
|
||||
each(filenames, function (filename) {
|
||||
if (!fs.existsSync(filename)) {
|
||||
errors.push(filename + " doesn't exist");
|
||||
}
|
||||
@@ -107,6 +109,7 @@ exports.opts = {
|
||||
sourceMap: commander.sourceMaps || commander.sourceMapsInline,
|
||||
optional: commander.optional,
|
||||
comments: !commander.removeComments,
|
||||
runtime: commander.runtime,
|
||||
modules: commander.modules,
|
||||
loose: commander.loose
|
||||
};
|
||||
|
||||
@@ -19,6 +19,7 @@ program.option("-r, --experimental", "Enable experimental support for proposed E
|
||||
program.option("-g, --playground", "Enable playground support");
|
||||
program.option("-w, --whitelist [whitelist]", "Whitelist of transformers to ONLY use", util.list);
|
||||
program.option("-b, --blacklist [blacklist]", "Blacklist of transformers to NOT use", util.list);
|
||||
program.option("-o, --optional [optional]", "List of optional transformers to enable", util.list);
|
||||
|
||||
var pkg = require("../package.json");
|
||||
program.version(pkg.version);
|
||||
@@ -33,6 +34,7 @@ to5.register({
|
||||
playground: program.playground,
|
||||
blacklist: program.blacklist,
|
||||
whitelist: program.whitelist,
|
||||
optional: program.optional,
|
||||
ignore: program.ignore
|
||||
});
|
||||
|
||||
@@ -43,6 +45,7 @@ var _eval = function (code, filename) {
|
||||
filename: filename,
|
||||
blacklist: ["useStrict"].concat(program.blacklist || []),
|
||||
whitelist: program.whitelist,
|
||||
optional: program.optional,
|
||||
experimental: program.experimental,
|
||||
playground: program.playground
|
||||
}).code;
|
||||
@@ -61,8 +64,18 @@ if (program.eval || program.print) {
|
||||
var args = process.argv.slice(2);
|
||||
|
||||
var i = 0;
|
||||
var ignoreNext = false;
|
||||
_.each(args, function (arg, i2) {
|
||||
if (arg[0] !== "-") {
|
||||
if (ignoreNext) {
|
||||
ignoreNext = false;
|
||||
return;
|
||||
}
|
||||
|
||||
if (arg[0] === "-") {
|
||||
if (program[arg.slice(2)]) {
|
||||
ignoreNext = true;
|
||||
}
|
||||
} else {
|
||||
i = i2;
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
"use strict";
|
||||
|
||||
var transform = module.exports = require("./transformation/transform");
|
||||
var transform = module.exports = require("./transformation");
|
||||
|
||||
transform.version = require("../../package").version;
|
||||
|
||||
|
||||
13
lib/6to5/build-helpers.js
Normal file
13
lib/6to5/build-helpers.js
Normal file
@@ -0,0 +1,13 @@
|
||||
var File = require("./file");
|
||||
var util = require("./util");
|
||||
var each = require("lodash/collection/each");
|
||||
var t = require("./types");
|
||||
|
||||
module.exports = function (body, namespace) {
|
||||
each(File.helpers, function (name) {
|
||||
var key = t.identifier(t.toIdentifier(name));
|
||||
body.push(t.expressionStatement(
|
||||
t.assignmentExpression("=", t.memberExpression(namespace, key), util.template(name))
|
||||
));
|
||||
});
|
||||
};
|
||||
25
lib/6to5/build-runtime.js
Normal file
25
lib/6to5/build-runtime.js
Normal file
@@ -0,0 +1,25 @@
|
||||
"use strict";
|
||||
|
||||
var buildHelpers = require("./build-helpers");
|
||||
var generator = require("./generation");
|
||||
var util = require("./util");
|
||||
var t = require("./types");
|
||||
|
||||
module.exports = function () {
|
||||
var namespace = t.identifier("to5Runtime");
|
||||
|
||||
var body = [];
|
||||
var container = t.functionExpression(null, [t.identifier("global")], t.blockStatement(body));
|
||||
var tree = t.program([t.expressionStatement(t.callExpression(container, [util.template("self-global")]))]);
|
||||
|
||||
body.push(t.variableDeclaration("var", [
|
||||
t.variableDeclarator(
|
||||
namespace,
|
||||
t.assignmentExpression("=", t.memberExpression(t.identifier("global"), namespace), t.objectExpression([]))
|
||||
)
|
||||
]));
|
||||
|
||||
buildHelpers(body, namespace);
|
||||
|
||||
return generator(tree).code;
|
||||
};
|
||||
29
lib/6to5/detection/index.js
Normal file
29
lib/6to5/detection/index.js
Normal file
@@ -0,0 +1,29 @@
|
||||
module.exports = detect;
|
||||
|
||||
var SYNTAX_KEYS = require("./syntax-keys");
|
||||
var traverse = require("../traverse");
|
||||
var visitors = traverse.explode(require("./visitors"));
|
||||
|
||||
function detect(ast) {
|
||||
var stats = {
|
||||
syntax: {},
|
||||
builtins: {}
|
||||
};
|
||||
|
||||
var detectedSyntax = function (name) {
|
||||
stats.syntax[name] = true;
|
||||
};
|
||||
|
||||
traverse(ast, {
|
||||
enter: function (node, parent) {
|
||||
if (SYNTAX_KEYS[node.type]) {
|
||||
detectedSyntax(SYNTAX_KEYS[node.type]);
|
||||
}
|
||||
|
||||
var visitor = visitors[node.type];
|
||||
if (visitor) visitor(node, parent, detectedSyntax);
|
||||
}
|
||||
});
|
||||
|
||||
return stats;
|
||||
}
|
||||
84
lib/6to5/detection/syntax-keys.json
Normal file
84
lib/6to5/detection/syntax-keys.json
Normal file
@@ -0,0 +1,84 @@
|
||||
{
|
||||
"ArrowFunctionExpression": "es6.arrowFunctions",
|
||||
|
||||
"AwaitExpression": "es7.asyncFunctions",
|
||||
|
||||
"ClassBody": "es6.classes",
|
||||
"ClassDeclaration": "es6.classes",
|
||||
"ClassExpression": "es6.classes",
|
||||
"MethodDefinition": "es6.classes",
|
||||
|
||||
"ComprehensionBlock": "es7.comprehensions",
|
||||
"ComprehensionExpression": "es7.comprehensions",
|
||||
|
||||
"ForOfStatement": "es6.forOf",
|
||||
|
||||
"ExportBatchSpecifier": "es6.modules",
|
||||
"ExportDeclaration": "es6.modules",
|
||||
"ExportSpecifier": "es6.modules",
|
||||
"ImportBatchSpecifier": "es6.modules",
|
||||
"ImportDeclaration": "es6.modules",
|
||||
"ImportSpecifier": "es6.modules",
|
||||
|
||||
"ArrayPattern": "es6.destructuring",
|
||||
"AssignmentPattern": "es6.destructuring",
|
||||
"ObjectPattern": "es6.destructuring",
|
||||
|
||||
"RestElement": "es6.parameters.rest",
|
||||
|
||||
"SpreadElement": "es6.spread",
|
||||
|
||||
"SpreadProperty": "es7.objectSpread",
|
||||
|
||||
"TaggedTemplateExpression": "es6.templateLiterals",
|
||||
"TemplateElement": "es6.templateLiterals",
|
||||
"TemplateLiteral": "es6.templateLiterals",
|
||||
|
||||
"VirtualPropertyExpression": "es7.abstractReferences",
|
||||
"PrivateDeclaration": "es7.abstractReferences",
|
||||
|
||||
"YieldExpression": "es6.generators",
|
||||
|
||||
"AnyTypeAnnotation": "flow",
|
||||
"ArrayTypeAnnotation": "flow",
|
||||
"BooleanTypeAnnotation": "flow",
|
||||
"ClassProperty": "flow",
|
||||
"DeclareClass": "flow",
|
||||
"DeclareFunction": "flow",
|
||||
"DeclareModule": "flow",
|
||||
"DeclareVariable": "flow",
|
||||
"FunctionTypeAnnotation": "flow",
|
||||
"FunctionTypeParam": "flow",
|
||||
"GenericTypeAnnotation": "flow",
|
||||
"InterfaceExtends": "flow",
|
||||
"InterfaceDeclaration": "flow",
|
||||
"IntersectionTypeAnnotation": "flow",
|
||||
"NullableTypeAnnotation": "flow",
|
||||
"NumberTypeAnnotation": "flow",
|
||||
"StringLiteralTypeAnnotation": "flow",
|
||||
"StringTypeAnnotation": "flow",
|
||||
"TupleTypeAnnotation": "flow",
|
||||
"TypeofTypeAnnotation": "flow",
|
||||
"TypeAlias": "flow",
|
||||
"TypeAnnotation": "flow",
|
||||
"TypeParameterDeclaration": "flow",
|
||||
"TypeParameterInstantiation": "flow",
|
||||
"ObjectTypeAnnotation": "flow",
|
||||
"ObjectTypeCallProperty": "flow",
|
||||
"ObjectTypeIndexer": "flow",
|
||||
"ObjectTypeProperty": "flow",
|
||||
"QualifiedTypeIdentifier": "flow",
|
||||
"UnionTypeAnnotation": "flow",
|
||||
"VoidTypeAnnotation": "flow",
|
||||
|
||||
"JSXAttribute": "jsx",
|
||||
"JSXClosingElement": "jsx",
|
||||
"JSXElement": "jsx",
|
||||
"JSXEmptyExpression": "jsx",
|
||||
"JSXExpressionContainer": "jsx",
|
||||
"JSXIdentifier": "jsx",
|
||||
"JSXMemberExpression": "jsx",
|
||||
"JSXNamespacedName": "jsx",
|
||||
"JSXOpeningElement": "jsx",
|
||||
"JSXSpreadAttribute": "jsx"
|
||||
}
|
||||
54
lib/6to5/detection/visitors.js
Normal file
54
lib/6to5/detection/visitors.js
Normal file
@@ -0,0 +1,54 @@
|
||||
var t = require("../types");
|
||||
var _ = require("lodash");
|
||||
|
||||
exports.AssignmentExpression = function (node, parent, detected) {
|
||||
if (node.operator === "**=") {
|
||||
detected("es6.exponentation");
|
||||
}
|
||||
};
|
||||
|
||||
exports.BinaryExpression = function (node, parent, detected) {
|
||||
if (node.operator === "**") {
|
||||
detected("es6.exponentation");
|
||||
}
|
||||
};
|
||||
|
||||
exports.VariableDeclaration = function (node, parent, detected) {
|
||||
if (node.kind === "let" || node.kind === "const") {
|
||||
detected("es6.blockScoping");
|
||||
}
|
||||
|
||||
if (node.kind === "const") {
|
||||
detected("es6.constants");
|
||||
}
|
||||
};
|
||||
|
||||
exports.Property = function (node, parent, detected) {
|
||||
if (node.shorthand || node.method) {
|
||||
detected("es6.properties.shorthand");
|
||||
}
|
||||
|
||||
if (node.kind === "set" || node.kind === "get") {
|
||||
detected("es5.properties.mutators");
|
||||
}
|
||||
|
||||
if (node.computed) {
|
||||
detected("es6.properties.computed");
|
||||
}
|
||||
};
|
||||
|
||||
exports.AssignmentPattern = function (node, parent, detected) {
|
||||
if (t.isFunction(parent) && _.contains(parent.params, node)) {
|
||||
detected("es6.parameters.default");
|
||||
}
|
||||
};
|
||||
|
||||
exports.Function = function (node, parent, detected) {
|
||||
if (node.generator) {
|
||||
detected("es6.generators");
|
||||
}
|
||||
|
||||
if (node.async) {
|
||||
detected("es7.asyncFunctions");
|
||||
}
|
||||
};
|
||||
@@ -4,13 +4,16 @@ module.exports = File;
|
||||
|
||||
var SHEBANG_REGEX = /^\#\!.*/;
|
||||
|
||||
var transform = require("./transformation/transform");
|
||||
var generate = require("./generation/generator");
|
||||
var clone = require("./helpers/clone");
|
||||
var Scope = require("./traverse/scope");
|
||||
var util = require("./util");
|
||||
var t = require("./types");
|
||||
var _ = require("lodash");
|
||||
var transform = require("./transformation");
|
||||
var generate = require("./generation");
|
||||
var clone = require("./helpers/clone");
|
||||
var Scope = require("./traverse/scope");
|
||||
var util = require("./util");
|
||||
var t = require("./types");
|
||||
var contains = require("lodash/collection/contains");
|
||||
var each = require("lodash/collection/each");
|
||||
var defaults = require("lodash/object/defaults");
|
||||
var isFunction = require("lodash/lang/isFunction");
|
||||
|
||||
function File(opts) {
|
||||
this.dynamicImportIds = {};
|
||||
@@ -21,7 +24,7 @@ function File(opts) {
|
||||
this.data = {};
|
||||
|
||||
this.lastStatements = [];
|
||||
this.opts = File.normaliseOptions(opts);
|
||||
this.opts = this.normaliseOptions(opts);
|
||||
this.ast = {};
|
||||
|
||||
this.buildTransformers();
|
||||
@@ -46,7 +49,8 @@ File.helpers = [
|
||||
"interop-require-wildcard",
|
||||
"typeof",
|
||||
"extends",
|
||||
"get"
|
||||
"get",
|
||||
"set"
|
||||
];
|
||||
|
||||
File.validOptions = [
|
||||
@@ -71,14 +75,17 @@ File.validOptions = [
|
||||
"format",
|
||||
"playground",
|
||||
"experimental",
|
||||
"resolveModuleSource",
|
||||
"runtime",
|
||||
|
||||
// these are used by plugins
|
||||
"ignore",
|
||||
"only",
|
||||
"extensions"
|
||||
"extensions",
|
||||
"accept"
|
||||
];
|
||||
|
||||
File.normaliseOptions = function (opts) {
|
||||
File.prototype.normaliseOptions = function (opts) {
|
||||
opts = clone(opts);
|
||||
|
||||
for (var key in opts) {
|
||||
@@ -87,8 +94,9 @@ File.normaliseOptions = function (opts) {
|
||||
}
|
||||
}
|
||||
|
||||
_.defaults(opts, {
|
||||
defaults(opts, {
|
||||
keepModuleIdExtensions: false,
|
||||
resolveModuleSource: null,
|
||||
experimental: false,
|
||||
reactCompat: false,
|
||||
playground: false,
|
||||
@@ -101,6 +109,7 @@ File.normaliseOptions = function (opts) {
|
||||
comments: true,
|
||||
filename: "unknown",
|
||||
modules: "common",
|
||||
runtime: false,
|
||||
loose: [],
|
||||
code: true,
|
||||
ast: true
|
||||
@@ -114,23 +123,23 @@ File.normaliseOptions = function (opts) {
|
||||
opts.optional = util.arrayify(opts.optional);
|
||||
opts.loose = util.arrayify(opts.loose);
|
||||
|
||||
if (_.contains(opts.loose, "all")) {
|
||||
if (contains(opts.loose, "all")) {
|
||||
opts.loose = Object.keys(transform.transformers);
|
||||
}
|
||||
|
||||
_.defaults(opts, {
|
||||
defaults(opts, {
|
||||
moduleRoot: opts.sourceRoot
|
||||
});
|
||||
|
||||
_.defaults(opts, {
|
||||
defaults(opts, {
|
||||
sourceRoot: opts.moduleRoot
|
||||
});
|
||||
|
||||
_.defaults(opts, {
|
||||
defaults(opts, {
|
||||
filenameRelative: opts.filename
|
||||
});
|
||||
|
||||
_.defaults(opts, {
|
||||
defaults(opts, {
|
||||
sourceFileName: opts.filenameRelative,
|
||||
sourceMapName: opts.filenameRelative
|
||||
});
|
||||
@@ -139,6 +148,10 @@ File.normaliseOptions = function (opts) {
|
||||
opts.experimental = true;
|
||||
}
|
||||
|
||||
if (opts.runtime) {
|
||||
this.set("runtimeIdentifier", t.identifier("to5Runtime"));
|
||||
}
|
||||
|
||||
opts.blacklist = transform._ensureTransformerNames("blacklist", opts.blacklist);
|
||||
opts.whitelist = transform._ensureTransformerNames("whitelist", opts.whitelist);
|
||||
opts.optional = transform._ensureTransformerNames("optional", opts.optional);
|
||||
@@ -148,7 +161,7 @@ File.normaliseOptions = function (opts) {
|
||||
};
|
||||
|
||||
File.prototype.isLoose = function (key) {
|
||||
return _.contains(this.opts.loose, key);
|
||||
return contains(this.opts.loose, key);
|
||||
};
|
||||
|
||||
File.prototype.buildTransformers = function () {
|
||||
@@ -159,7 +172,7 @@ File.prototype.buildTransformers = function () {
|
||||
var secondaryStack = [];
|
||||
var stack = [];
|
||||
|
||||
_.each(transform.transformers, function (transformer, key) {
|
||||
each(transform.transformers, function (transformer, key) {
|
||||
var pass = transformers[key] = transformer.buildPass(file);
|
||||
|
||||
if (pass.canRun(file)) {
|
||||
@@ -195,7 +208,7 @@ File.prototype.toArray = function (node, i) {
|
||||
};
|
||||
|
||||
File.prototype.getModuleFormatter = function (type) {
|
||||
var ModuleFormatter = _.isFunction(type) ? type : transform.moduleFormatters[type];
|
||||
var ModuleFormatter = isFunction(type) ? type : transform.moduleFormatters[type];
|
||||
|
||||
if (!ModuleFormatter) {
|
||||
var loc = util.resolve(type);
|
||||
@@ -264,7 +277,7 @@ File.prototype.isConsequenceExpressionStatement = function (node) {
|
||||
};
|
||||
|
||||
File.prototype.addHelper = function (name) {
|
||||
if (!_.contains(File.helpers, name)) {
|
||||
if (!contains(File.helpers, name)) {
|
||||
throw new ReferenceError("unknown declaration " + name);
|
||||
}
|
||||
|
||||
@@ -312,7 +325,8 @@ File.prototype.parse = function (code) {
|
||||
|
||||
var opts = this.opts;
|
||||
|
||||
opts.allowImportExportEverywhere = this.isLoose("modules");
|
||||
opts.allowImportExportEverywhere = this.isLoose("es6.modules");
|
||||
//opts.strictMode = this.transformers.useStrict.canRun();
|
||||
|
||||
return util.parse(opts, code, function (tree) {
|
||||
self.transform(tree);
|
||||
@@ -323,20 +337,26 @@ File.prototype.parse = function (code) {
|
||||
File.prototype.transform = function (ast) {
|
||||
var self = this;
|
||||
|
||||
util.debug(this.opts.filename);
|
||||
|
||||
this.ast = ast;
|
||||
this.lastStatements = t.getLastStatements(ast.program);
|
||||
this.scope = new Scope(ast.program, ast, null, this);
|
||||
this.moduleFormatter = this.getModuleFormatter(this.opts.modules);
|
||||
|
||||
var modFormatter = this.moduleFormatter = this.getModuleFormatter(this.opts.modules);
|
||||
if (modFormatter.init && this.transformers["es6.modules"].canRun()) {
|
||||
modFormatter.init();
|
||||
}
|
||||
|
||||
var astRun = function (key) {
|
||||
_.each(self.transformerStack, function (pass) {
|
||||
each(self.transformerStack, function (pass) {
|
||||
pass.astRun(key);
|
||||
});
|
||||
};
|
||||
|
||||
astRun("enter");
|
||||
|
||||
_.each(this.transformerStack, function (pass) {
|
||||
each(this.transformerStack, function (pass) {
|
||||
pass.transform();
|
||||
});
|
||||
|
||||
|
||||
@@ -2,8 +2,10 @@
|
||||
|
||||
module.exports = Buffer;
|
||||
|
||||
var util = require("../util");
|
||||
var _ = require("lodash");
|
||||
var util = require("../util");
|
||||
var isNumber = require("lodash/lang/isNumber");
|
||||
var isBoolean = require("lodash/lang/isBoolean");
|
||||
var contains = require("lodash/collection/contains");
|
||||
|
||||
function Buffer(position, format) {
|
||||
this.position = position;
|
||||
@@ -75,7 +77,7 @@ Buffer.prototype.newline = function (i, removeLast) {
|
||||
|
||||
removeLast = removeLast || false;
|
||||
|
||||
if (_.isNumber(i)) {
|
||||
if (isNumber(i)) {
|
||||
if (this.endsWith("{\n")) i--;
|
||||
if (this.endsWith(util.repeat(i, "\n"))) return;
|
||||
|
||||
@@ -85,7 +87,7 @@ Buffer.prototype.newline = function (i, removeLast) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (_.isBoolean(i)) {
|
||||
if (isBoolean(i)) {
|
||||
removeLast = i;
|
||||
}
|
||||
|
||||
@@ -155,7 +157,7 @@ Buffer.prototype.isLast = function (cha, trimRight) {
|
||||
var last = buf[buf.length - 1];
|
||||
|
||||
if (Array.isArray(cha)) {
|
||||
return _.contains(cha, last);
|
||||
return contains(cha, last);
|
||||
} else {
|
||||
return cha === last;
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
"use strict";
|
||||
|
||||
var util = require("../../util");
|
||||
var t = require("../../types");
|
||||
var _ = require("lodash");
|
||||
var util = require("../../util");
|
||||
var t = require("../../types");
|
||||
var isNumber = require("lodash/lang/isNumber");
|
||||
|
||||
exports.UnaryExpression = function (node, print) {
|
||||
var hasSpace = /[a-z]$/.test(node.operator);
|
||||
@@ -87,7 +87,11 @@ exports.CallExpression = function (node, print) {
|
||||
var buildYieldAwait = function (keyword) {
|
||||
return function (node, print) {
|
||||
this.push(keyword);
|
||||
if (node.delegate) this.push("*");
|
||||
|
||||
if (node.delegate || node.all) {
|
||||
this.push("*");
|
||||
}
|
||||
|
||||
if (node.argument) {
|
||||
this.space();
|
||||
print(node.argument);
|
||||
@@ -129,7 +133,7 @@ exports.MemberExpression = function (node, print) {
|
||||
}
|
||||
|
||||
var computed = node.computed;
|
||||
if (t.isLiteral(node.property) && _.isNumber(node.property.value)) {
|
||||
if (t.isLiteral(node.property) && isNumber(node.property.value)) {
|
||||
computed = true;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
"use strict";
|
||||
|
||||
var t = require("../../types");
|
||||
var _ = require("lodash");
|
||||
var t = require("../../types");
|
||||
var each = require("lodash/collection/each");
|
||||
|
||||
exports.JSXAttribute = function (node, print) {
|
||||
print(node.name);
|
||||
@@ -47,7 +47,7 @@ exports.JSXElement = function (node, print) {
|
||||
if (open.selfClosing) return;
|
||||
|
||||
this.indent();
|
||||
_.each(node.children, function (child) {
|
||||
each(node.children, function (child) {
|
||||
if (t.isLiteral(child)) {
|
||||
self.push(child.value);
|
||||
} else {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
"use strict";
|
||||
|
||||
var t = require("../../types");
|
||||
var _ = require("lodash");
|
||||
var t = require("../../types");
|
||||
var each = require("lodash/collection/each");
|
||||
|
||||
exports.ImportSpecifier = function (node, print) {
|
||||
if (t.isSpecifierDefault(node)) {
|
||||
@@ -66,7 +66,7 @@ exports.ImportDeclaration = function (node, print) {
|
||||
if (specfiers && specfiers.length) {
|
||||
var foundImportSpecifier = false;
|
||||
|
||||
_.each(node.specifiers, function (spec, i) {
|
||||
each(node.specifiers, function (spec, i) {
|
||||
if (+i > 0) {
|
||||
self.push(", ");
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
"use strict";
|
||||
|
||||
var _ = require("lodash");
|
||||
var each = require("lodash/collection/each");
|
||||
|
||||
_.each(["BindMemberExpression", "BindFunctionExpression"], function (type) {
|
||||
each(["BindMemberExpression", "BindFunctionExpression"], function (type) {
|
||||
exports[type] = function () {
|
||||
throw new ReferenceError("Trying to render non-standard playground node " + JSON.stringify(type));
|
||||
};
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
"use strict";
|
||||
|
||||
var _ = require("lodash");
|
||||
var each = require("lodash/collection/each");
|
||||
|
||||
exports.TaggedTemplateExpression = function (node, print) {
|
||||
print(node.tag);
|
||||
@@ -18,7 +18,7 @@ exports.TemplateLiteral = function (node, print) {
|
||||
var self = this;
|
||||
var len = quasis.length;
|
||||
|
||||
_.each(quasis, function (quasi, i) {
|
||||
each(quasis, function (quasi, i) {
|
||||
print(quasi);
|
||||
|
||||
if (i + 1 < len) {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
"use strict";
|
||||
|
||||
var _ = require("lodash");
|
||||
var each = require("lodash/collection/each");
|
||||
|
||||
exports.Identifier = function (node) {
|
||||
this.push(node.name);
|
||||
@@ -62,7 +62,7 @@ exports.ArrayPattern = function (node, print) {
|
||||
|
||||
this.push("[");
|
||||
|
||||
_.each(elems, function (elem, i) {
|
||||
each(elems, function (elem, i) {
|
||||
if (!elem) {
|
||||
// If the array expression ends with a hole, that hole
|
||||
// will be ignored by the interpreter, but if it ends with
|
||||
|
||||
@@ -15,7 +15,9 @@ var Buffer = require("./buffer");
|
||||
var util = require("../util");
|
||||
var n = require("./node");
|
||||
var t = require("../types");
|
||||
var _ = require("lodash");
|
||||
var each = require("lodash/collection/each");
|
||||
var extend = require("lodash/object/extend");
|
||||
var merge = require("lodash/object/merge");
|
||||
|
||||
function CodeGenerator(ast, opts, code) {
|
||||
opts = opts || {};
|
||||
@@ -31,18 +33,20 @@ function CodeGenerator(ast, opts, code) {
|
||||
this.buffer = new Buffer(this.position, this.format);
|
||||
}
|
||||
|
||||
_.each(Buffer.prototype, function (fn, key) {
|
||||
each(Buffer.prototype, function (fn, key) {
|
||||
CodeGenerator.prototype[key] = function () {
|
||||
return fn.apply(this.buffer, arguments);
|
||||
};
|
||||
});
|
||||
|
||||
CodeGenerator.normaliseOptions = function (code, opts) {
|
||||
var indent = detectIndent(code);
|
||||
var style = indent.indent;
|
||||
if (!style || style === " ") style = " ";
|
||||
var style = " ";
|
||||
if (code) {
|
||||
var indent = detectIndent(code).indent;
|
||||
if (indent && indent !== " ") style = indent;
|
||||
}
|
||||
|
||||
return _.merge({
|
||||
return merge({
|
||||
parentheses: true,
|
||||
comments: opts.comments == null || opts.comments,
|
||||
compact: false,
|
||||
@@ -69,8 +73,8 @@ CodeGenerator.generators = {
|
||||
jsx: require("./generators/jsx")
|
||||
};
|
||||
|
||||
_.each(CodeGenerator.generators, function (generator) {
|
||||
_.extend(CodeGenerator.prototype, generator);
|
||||
each(CodeGenerator.generators, function (generator) {
|
||||
extend(CodeGenerator.prototype, generator);
|
||||
});
|
||||
|
||||
CodeGenerator.prototype.generate = function () {
|
||||
@@ -79,7 +83,7 @@ CodeGenerator.prototype.generate = function () {
|
||||
this.print(ast);
|
||||
|
||||
var comments = [];
|
||||
_.each(ast.comments, function (comment) {
|
||||
each(ast.comments, function (comment) {
|
||||
if (!comment._displayed) comments.push(comment);
|
||||
});
|
||||
this._printComments(comments);
|
||||
@@ -208,7 +212,7 @@ CodeGenerator.prototype.printJoin = function (print, nodes, opts) {
|
||||
|
||||
if (opts.indent) self.indent();
|
||||
|
||||
_.each(nodes, function (node, i) {
|
||||
each(nodes, function (node, i) {
|
||||
print(node, {
|
||||
statement: opts.statement,
|
||||
after: function () {
|
||||
@@ -273,7 +277,7 @@ CodeGenerator.prototype.getComments = function (key, node, parent) {
|
||||
nodes.push(node.argument);
|
||||
}
|
||||
|
||||
_.each(nodes, function (node) {
|
||||
each(nodes, function (node) {
|
||||
comments = comments.concat(self._getComments(key, node));
|
||||
});
|
||||
|
||||
@@ -291,11 +295,11 @@ CodeGenerator.prototype._printComments = function (comments) {
|
||||
|
||||
var self = this;
|
||||
|
||||
_.each(comments, function (comment) {
|
||||
each(comments, function (comment) {
|
||||
var skip = false;
|
||||
|
||||
// find the original comment in the ast and set it as displayed
|
||||
_.each(self.ast.comments, function (origComment) {
|
||||
each(self.ast.comments, function (origComment) {
|
||||
if (origComment.start === comment.start) {
|
||||
// comment has already been output
|
||||
if (origComment._displayed) skip = true;
|
||||
@@ -5,7 +5,8 @@ module.exports = Node;
|
||||
var whitespace = require("./whitespace");
|
||||
var parens = require("./parentheses");
|
||||
var t = require("../../types");
|
||||
var _ = require("lodash");
|
||||
var each = require("lodash/collection/each");
|
||||
var some = require("lodash/collection/some");
|
||||
|
||||
var find = function (obj, node, parent) {
|
||||
if (!obj) return;
|
||||
@@ -44,7 +45,7 @@ Node.needsWhitespace = function (node, parent, type) {
|
||||
var lines = find(whitespace[type].nodes, node, parent);
|
||||
if (lines) return lines;
|
||||
|
||||
_.each(find(whitespace[type].list, node, parent), function (expr) {
|
||||
each(find(whitespace[type].list, node, parent), function (expr) {
|
||||
lines = Node.needsWhitespace(expr, node, type);
|
||||
if (lines) return false;
|
||||
});
|
||||
@@ -65,7 +66,7 @@ Node.needsParens = function (node, parent) {
|
||||
if (t.isNewExpression(parent) && parent.callee === node) {
|
||||
if (t.isCallExpression(node)) return true;
|
||||
|
||||
var hasCall = _.some(node, function (val) {
|
||||
var hasCall = some(node, function (val) {
|
||||
return t.isCallExpression(val);
|
||||
});
|
||||
if (hasCall) return true;
|
||||
@@ -94,7 +95,7 @@ Node.needsParensNoLineTerminator = function (node, parent) {
|
||||
return false;
|
||||
};
|
||||
|
||||
_.each(Node, function (fn, key) {
|
||||
each(Node, function (fn, key) {
|
||||
Node.prototype[key] = function () {
|
||||
// Avoid leaking arguments to prevent deoptimization
|
||||
var args = new Array(arguments.length + 2);
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
"use strict";
|
||||
|
||||
var t = require("../../types");
|
||||
var _ = require("lodash");
|
||||
var t = require("../../types");
|
||||
var each = require("lodash/collection/each");
|
||||
|
||||
var PRECEDENCE = {};
|
||||
|
||||
_.each([
|
||||
each([
|
||||
["||"],
|
||||
["&&"],
|
||||
["|"],
|
||||
@@ -18,7 +18,7 @@ _.each([
|
||||
["*", "/", "%"],
|
||||
["**"]
|
||||
], function (tier, i) {
|
||||
_.each(tier, function (op) {
|
||||
each(tier, function (op) {
|
||||
PRECEDENCE[op] = i;
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
"use strict";
|
||||
|
||||
var _ = require("lodash");
|
||||
var t = require("../../types");
|
||||
var t = require("../../types");
|
||||
var each = require("lodash/collection/each");
|
||||
var map = require("lodash/collection/map");
|
||||
var isNumber = require("lodash/lang/isNumber");
|
||||
|
||||
exports.before = {
|
||||
nodes: {
|
||||
@@ -31,6 +33,10 @@ exports.before = {
|
||||
|
||||
exports.after = {
|
||||
nodes: {
|
||||
LogicalExpression: function (node) {
|
||||
return t.isFunction(node.left) || t.isFunction(node.right);
|
||||
},
|
||||
|
||||
AssignmentExpression: function (node) {
|
||||
if (t.isFunction(node.right)) {
|
||||
return 1;
|
||||
@@ -40,7 +46,7 @@ exports.after = {
|
||||
|
||||
list: {
|
||||
VariableDeclaration: function (node) {
|
||||
return _.map(node.declarations, "init");
|
||||
return map(node.declarations, "init");
|
||||
},
|
||||
|
||||
ArrayExpression: function (node) {
|
||||
@@ -53,7 +59,7 @@ exports.after = {
|
||||
}
|
||||
};
|
||||
|
||||
_.each({
|
||||
each({
|
||||
Function: 1,
|
||||
Class: 1,
|
||||
For: 1,
|
||||
@@ -64,12 +70,12 @@ _.each({
|
||||
CallExpression: { after: 1 },
|
||||
Literal: { after: 1 }
|
||||
}, function (amounts, type) {
|
||||
if (_.isNumber(amounts)) {
|
||||
if (isNumber(amounts)) {
|
||||
amounts = { after: amounts, before: amounts };
|
||||
}
|
||||
|
||||
_.each([type].concat(t.FLIPPED_ALIAS_KEYS[type] || []), function (type) {
|
||||
_.each(amounts, function (amount, key) {
|
||||
each([type].concat(t.FLIPPED_ALIAS_KEYS[type] || []), function (type) {
|
||||
each(amounts, function (amount, key) {
|
||||
exports[key].nodes[type] = function () {
|
||||
return amount;
|
||||
};
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
module.exports = Whitespace;
|
||||
|
||||
var _ = require("lodash");
|
||||
var sortBy = require("lodash/collection/sortBy");
|
||||
|
||||
/**
|
||||
* Returns `i`th number from `base`, continuing from 0 when `max` is reached.
|
||||
@@ -23,7 +23,7 @@ function getLookupIndex(i, base, max) {
|
||||
}
|
||||
|
||||
function Whitespace(tokens, comments) {
|
||||
this.tokens = _.sortBy(tokens.concat(comments), "start");
|
||||
this.tokens = sortBy(tokens.concat(comments), "start");
|
||||
this.used = {};
|
||||
|
||||
// Profiling this code shows that while generator passes over it, indexes
|
||||
|
||||
@@ -47,7 +47,7 @@ module.exports = function (lines, lineNumber, colNumber) {
|
||||
lines = highlight(lines);
|
||||
}
|
||||
|
||||
lines = lines.split("\n");
|
||||
lines = lines.split(/\r\n|[\n\r\u2028\u2029]/);
|
||||
|
||||
var start = Math.max(lineNumber - 3, 0);
|
||||
var end = Math.min(lines.length, lineNumber + 3);
|
||||
|
||||
38
lib/6to5/helpers/levenshtein.js
Normal file
38
lib/6to5/helpers/levenshtein.js
Normal file
@@ -0,0 +1,38 @@
|
||||
// taken from stackoverflow, it's crap i know.
|
||||
|
||||
module.exports = function (a, b) {
|
||||
if (a.length === 0) return b.length;
|
||||
if (b.length === 0) return a.length;
|
||||
|
||||
var matrix = [];
|
||||
|
||||
// increment along the first column of each row
|
||||
var i;
|
||||
for (i = 0; i <= b.length; i++) {
|
||||
matrix[i] = [i];
|
||||
}
|
||||
|
||||
// increment each column in the first row
|
||||
var j;
|
||||
for (j = 0; j <= a.length; j++) {
|
||||
matrix[0][j] = j;
|
||||
}
|
||||
|
||||
// Fill in the rest of the matrix
|
||||
for (i = 1; i <= b.length; i++) {
|
||||
for (j = 1; j <= a.length; j++) {
|
||||
if (b.charAt(i - 1) == a.charAt(j - 1)) {
|
||||
matrix[i][j] = matrix[i - 1][j - 1];
|
||||
} else {
|
||||
matrix[i][j] = Math.min(
|
||||
matrix[i - 1][j - 1] + 1, // substitution
|
||||
Math.min(
|
||||
matrix[i][j - 1] + 1, // insertion
|
||||
matrix[i - 1][j] + 1) // deletion
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return matrix[b.length][a.length];
|
||||
};
|
||||
@@ -1,12 +1,14 @@
|
||||
"use strict";
|
||||
|
||||
var transform = require("./transformation/transform");
|
||||
var util = require("./util");
|
||||
var fs = require("fs");
|
||||
var _ = require("lodash");
|
||||
var transform = require("./transformation");
|
||||
var util = require("./util");
|
||||
var fs = require("fs");
|
||||
var isFunction = require("lodash/lang/isFunction");
|
||||
|
||||
exports.version = require("../../package").version;
|
||||
|
||||
exports.runtime = require("./build-runtime");
|
||||
|
||||
exports.types = require("./types");
|
||||
|
||||
exports.register = function (opts) {
|
||||
@@ -27,7 +29,7 @@ exports._util = util;
|
||||
exports.transform = transform;
|
||||
|
||||
exports.transformFile = function (filename, opts, callback) {
|
||||
if (_.isFunction(opts)) {
|
||||
if (isFunction(opts)) {
|
||||
callback = opts;
|
||||
opts = {};
|
||||
}
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
"use strict";
|
||||
|
||||
var t = require("./types");
|
||||
var _ = require("lodash");
|
||||
var t = require("./types");
|
||||
var extend = require("lodash/object/extend");
|
||||
|
||||
require("./types/node");
|
||||
|
||||
// estraverse
|
||||
|
||||
var estraverse = require("estraverse");
|
||||
_.extend(estraverse.VisitorKeys, t.VISITOR_KEYS);
|
||||
extend(estraverse.VisitorKeys, t.VISITOR_KEYS);
|
||||
|
||||
// regenerator-6to5/ast-types
|
||||
|
||||
|
||||
@@ -7,7 +7,8 @@ var registerCache = require("./register-cache");
|
||||
var util = require("./util");
|
||||
var to5 = require("./index");
|
||||
var fs = require("fs");
|
||||
var _ = require("lodash");
|
||||
var extend = require("lodash/object/extend");
|
||||
var each = require("lodash/collection/each");
|
||||
|
||||
sourceMapSupport.install({
|
||||
retrieveSourceMap: function (source) {
|
||||
@@ -56,7 +57,7 @@ var loader = function (m, filename) {
|
||||
}
|
||||
}
|
||||
|
||||
result = result || to5.transformFileSync(filename, _.extend({
|
||||
result = result || to5.transformFileSync(filename, extend({
|
||||
whitelist: whitelist,
|
||||
sourceMap: true,
|
||||
ast: false
|
||||
@@ -73,13 +74,13 @@ var loader = function (m, filename) {
|
||||
};
|
||||
|
||||
var hookExtensions = function (_exts) {
|
||||
_.each(exts, function (old, ext) {
|
||||
each(exts, function (old, ext) {
|
||||
require.extensions[ext] = old;
|
||||
});
|
||||
|
||||
exts = {};
|
||||
|
||||
_.each(_exts, function (ext) {
|
||||
each(_exts, function (ext) {
|
||||
exts[ext] = require.extensions[ext];
|
||||
require.extensions[ext] = loader;
|
||||
});
|
||||
@@ -103,5 +104,5 @@ module.exports = function (opts) {
|
||||
delete opts.cache;
|
||||
delete opts.only;
|
||||
|
||||
_.extend(transformOpts, opts);
|
||||
extend(transformOpts, opts);
|
||||
};
|
||||
|
||||
22
lib/6to5/transformation/helpers/react.js
vendored
Normal file
22
lib/6to5/transformation/helpers/react.js
vendored
Normal file
@@ -0,0 +1,22 @@
|
||||
var t = require("../../types");
|
||||
|
||||
var isCreateClassCallExpression = t.buildMatchMemberExpression("React.createClass");
|
||||
|
||||
exports.isCreateClass = function (node) {
|
||||
if (!node || !t.isCallExpression(node)) return false;
|
||||
|
||||
// not React.createClass call member object
|
||||
if (!isCreateClassCallExpression(node.callee)) return false;
|
||||
|
||||
// no call arguments
|
||||
var args = node.arguments;
|
||||
if (args.length !== 1) return false;
|
||||
|
||||
// first node arg is not an object
|
||||
var first = args[0];
|
||||
if (!t.isObjectExpression(first)) return false;
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
exports.isReactComponent = t.buildMatchMemberExpression("React.Component");
|
||||
@@ -9,6 +9,12 @@ var visitor = {
|
||||
|
||||
if (t.isAwaitExpression(node)) {
|
||||
node.type = "YieldExpression";
|
||||
|
||||
if (node.all) {
|
||||
// await* foo; -> yield Promise.all(foo);
|
||||
node.all = false;
|
||||
node.argument = t.callExpression(t.memberExpression(t.identifier("Promise"), t.identifier("all")), [node.argument]);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -21,6 +21,36 @@ function ReplaceSupers(opts) {
|
||||
this.file = opts.file;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a super class value of the named property.
|
||||
*
|
||||
* @example
|
||||
*
|
||||
* _set(Object.getPrototypeOf(CLASS.prototype), "METHOD", "VALUE", this)
|
||||
*
|
||||
* @param {Node} property
|
||||
* @param {boolean} isStatic
|
||||
* @param {boolean} isComputed
|
||||
*
|
||||
* @returns {Node}
|
||||
*/
|
||||
ReplaceSupers.prototype.setSuperProperty = function (property, value, isStatic, isComputed, thisExpression) {
|
||||
return t.callExpression(
|
||||
this.file.addHelper("set"),
|
||||
[
|
||||
t.callExpression(
|
||||
t.memberExpression(t.identifier("Object"), t.identifier("getPrototypeOf")),
|
||||
[
|
||||
isStatic ? this.className : t.memberExpression(this.className, t.identifier("prototype"))
|
||||
]
|
||||
),
|
||||
isComputed ? property : t.literal(property.name),
|
||||
value,
|
||||
thisExpression
|
||||
]
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* Gets a node representing the super class value of the named property.
|
||||
*
|
||||
@@ -191,6 +221,7 @@ ReplaceSupers.prototype.specHandle = function (getThisReference, node, parent) {
|
||||
var property;
|
||||
var computed;
|
||||
var args;
|
||||
var thisReference;
|
||||
|
||||
if (t.isIdentifier(node, { name: "super" })) {
|
||||
if (!(t.isMemberExpression(parent) && !parent.computed && parent.property === node)) {
|
||||
@@ -226,11 +257,18 @@ ReplaceSupers.prototype.specHandle = function (getThisReference, node, parent) {
|
||||
// super.name; -> _get(Object.getPrototypeOf(ClassName.prototype), "name", this);
|
||||
property = node.property;
|
||||
computed = node.computed;
|
||||
} else if (t.isAssignmentExpression(node)) {
|
||||
if (!t.isIdentifier(node.left.object, { name: "super" })) return;
|
||||
if (methodNode.kind !== "set") return;
|
||||
|
||||
thisReference = getThisReference();
|
||||
// super.name = "val"; -> _set(Object.getPrototypeOf(ClassName.prototype), "name", this);
|
||||
return this.setSuperProperty(node.left.property, node.right, methodNode.static, node.left.computed, thisReference);
|
||||
}
|
||||
|
||||
if (!property) return;
|
||||
|
||||
var thisReference = getThisReference();
|
||||
thisReference = getThisReference();
|
||||
var superProperty = this.superProperty(property, methodNode.static, computed, thisReference);
|
||||
if (args) {
|
||||
if (args.length === 1 && t.isSpreadElement(args[0])) {
|
||||
|
||||
@@ -6,7 +6,7 @@ var Transformer = require("./transformer");
|
||||
var object = require("../helpers/object");
|
||||
var File = require("../file");
|
||||
var util = require("../util");
|
||||
var _ = require("lodash");
|
||||
var each = require("lodash/collection/each");
|
||||
|
||||
function transform(code, opts) {
|
||||
var file = new File(opts);
|
||||
@@ -64,7 +64,7 @@ transform.moduleFormatters = require("./modules");
|
||||
|
||||
var rawTransformers = require("./transformers");
|
||||
|
||||
_.each(rawTransformers, function (transformer, key) {
|
||||
each(rawTransformers, function (transformer, key) {
|
||||
var namespace = key.split(".")[0];
|
||||
transform.namespaces[namespace] = transform.namespaces[namespace] || [];
|
||||
transform.namespaces[namespace].push(key);
|
||||
@@ -6,7 +6,7 @@ var traverse = require("../../traverse");
|
||||
var object = require("../../helpers/object");
|
||||
var util = require("../../util");
|
||||
var t = require("../../types");
|
||||
var _ = require("lodash");
|
||||
var extend = require("lodash/object/extend");
|
||||
|
||||
function DefaultFormatter(file) {
|
||||
this.file = file;
|
||||
@@ -45,7 +45,7 @@ var exportsVisitor = {
|
||||
formatter.hasLocalImports = true;
|
||||
|
||||
if (declar && t.isStatement(declar)) {
|
||||
_.extend(formatter.localExports, t.getIds(declar, true));
|
||||
extend(formatter.localExports, t.getDeclarations(declar));
|
||||
}
|
||||
|
||||
if (!node.default) {
|
||||
@@ -67,7 +67,7 @@ var importsVisitor = {
|
||||
enter: function (node, parent, scope, context, formatter) {
|
||||
if (t.isImportDeclaration(node)) {
|
||||
formatter.hasLocalImports = true;
|
||||
_.extend(formatter.localImports, t.getIds(node, true));
|
||||
extend(formatter.localImports, t.getDeclarations(node));
|
||||
formatter.bumpImportOccurences(node);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,8 @@ var DefaultFormatter = require("./_default");
|
||||
var CommonFormatter = require("./common");
|
||||
var util = require("../../util");
|
||||
var t = require("../../types");
|
||||
var _ = require("lodash");
|
||||
var contains = require("lodash/collection/contains");
|
||||
var values = require("lodash/object/values");
|
||||
|
||||
function AMDFormatter() {
|
||||
CommonFormatter.apply(this, arguments);
|
||||
@@ -14,6 +15,8 @@ function AMDFormatter() {
|
||||
|
||||
util.inherits(AMDFormatter, DefaultFormatter);
|
||||
|
||||
AMDFormatter.prototype.init = CommonFormatter.prototype.init;
|
||||
|
||||
AMDFormatter.prototype.buildDependencyLiterals = function () {
|
||||
var names = [];
|
||||
for (var name in this.ids) {
|
||||
@@ -39,7 +42,7 @@ AMDFormatter.prototype.transform = function (ast) {
|
||||
|
||||
// build up define container
|
||||
|
||||
var params = _.values(this.ids);
|
||||
var params = values(this.ids);
|
||||
if (this.passModuleArg) params.unshift(t.identifier("module"));
|
||||
params.unshift(t.identifier("exports"));
|
||||
|
||||
@@ -79,10 +82,9 @@ AMDFormatter.prototype.importSpecifier = function (specifier, node, nodes) {
|
||||
var key = t.getSpecifierName(specifier);
|
||||
var ref = this.getExternalReference(node);
|
||||
|
||||
if (_.contains(this.file.dynamicImported, node)) {
|
||||
if (contains(this.file.dynamicImported, node)) {
|
||||
// Prevent unnecessary renaming of dynamic imports.
|
||||
this.ids[node.source.value] = key;
|
||||
return;
|
||||
this.ids[node.source.value] = ref;
|
||||
} else if (t.isImportBatchSpecifier(specifier)) {
|
||||
// import * as bar from "foo";
|
||||
} else if (t.isSpecifierDefault(specifier) && !this.noInteropRequire) {
|
||||
|
||||
@@ -3,20 +3,22 @@
|
||||
module.exports = CommonJSFormatter;
|
||||
|
||||
var DefaultFormatter = require("./_default");
|
||||
var contains = require("lodash/collection/contains");
|
||||
var util = require("../../util");
|
||||
var t = require("../../types");
|
||||
var _ = require("lodash");
|
||||
|
||||
function CommonJSFormatter(file) {
|
||||
function CommonJSFormatter() {
|
||||
DefaultFormatter.apply(this, arguments);
|
||||
|
||||
if (this.hasNonDefaultExports) {
|
||||
file.ast.program.body.push(util.template("exports-module-declaration", true));
|
||||
}
|
||||
}
|
||||
|
||||
util.inherits(CommonJSFormatter, DefaultFormatter);
|
||||
|
||||
CommonJSFormatter.prototype.init = function () {
|
||||
if (this.hasNonDefaultExports) {
|
||||
this.file.ast.program.body.push(util.template("exports-module-declaration", true));
|
||||
}
|
||||
};
|
||||
|
||||
CommonJSFormatter.prototype.importSpecifier = function (specifier, node, nodes) {
|
||||
var variableName = t.getSpecifierName(specifier);
|
||||
|
||||
@@ -24,7 +26,7 @@ CommonJSFormatter.prototype.importSpecifier = function (specifier, node, nodes)
|
||||
|
||||
// import foo from "foo";
|
||||
if (t.isSpecifierDefault(specifier)) {
|
||||
if (!_.contains(this.file.dynamicImported, node)) {
|
||||
if (!contains(this.file.dynamicImported, node)) {
|
||||
ref = t.callExpression(this.file.addHelper("interop-require"), [ref]);
|
||||
}
|
||||
nodes.push(t.variableDeclaration("var", [t.variableDeclarator(variableName, ref)]));
|
||||
|
||||
@@ -8,7 +8,9 @@ var useStrict = require("../helpers/use-strict");
|
||||
var traverse = require("../../traverse");
|
||||
var util = require("../../util");
|
||||
var t = require("../../types");
|
||||
var _ = require("lodash");
|
||||
var last = require("lodash/array/last");
|
||||
var each = require("lodash/collection/each");
|
||||
var map = require("lodash/collection/map");
|
||||
|
||||
function SystemFormatter(file) {
|
||||
this.exportIdentifier = file.generateUidIdentifier("export");
|
||||
@@ -19,6 +21,8 @@ function SystemFormatter(file) {
|
||||
|
||||
util.inherits(SystemFormatter, AMDFormatter);
|
||||
|
||||
SystemFormatter.prototype.init = function () {};
|
||||
|
||||
SystemFormatter.prototype._addImportSource = function (node, exportNode) {
|
||||
node._importSource = exportNode.source && exportNode.source.value;
|
||||
return node;
|
||||
@@ -61,14 +65,14 @@ SystemFormatter.prototype.buildExportCall = function (id, init, isStatement) {
|
||||
|
||||
SystemFormatter.prototype.importSpecifier = function (specifier, node, nodes) {
|
||||
AMDFormatter.prototype.importSpecifier.apply(this, arguments);
|
||||
this._addImportSource(_.last(nodes), node);
|
||||
this._addImportSource(last(nodes), node);
|
||||
};
|
||||
|
||||
var runnerSettersVisitor = {
|
||||
enter: function (node, parent, scope, context, state) {
|
||||
if (node._importSource === state.source) {
|
||||
if (t.isVariableDeclaration(node)) {
|
||||
_.each(node.declarations, function (declar) {
|
||||
each(node.declarations, function (declar) {
|
||||
state.hoistDeclarators.push(t.variableDeclarator(declar.id));
|
||||
state.nodes.push(t.expressionStatement(
|
||||
t.assignmentExpression("=", declar.id, declar.init)
|
||||
@@ -86,7 +90,7 @@ var runnerSettersVisitor = {
|
||||
SystemFormatter.prototype.buildRunnerSetters = function (block, hoistDeclarators) {
|
||||
var scope = this.file.scope;
|
||||
|
||||
return t.arrayExpression(_.map(this.ids, function (uid, source) {
|
||||
return t.arrayExpression(map(this.ids, function (uid, source) {
|
||||
var state = {
|
||||
source: source,
|
||||
nodes: [],
|
||||
|
||||
@@ -5,7 +5,7 @@ module.exports = UMDFormatter;
|
||||
var AMDFormatter = require("./amd");
|
||||
var util = require("../../util");
|
||||
var t = require("../../types");
|
||||
var _ = require("lodash");
|
||||
var values = require("lodash/object/values");
|
||||
|
||||
function UMDFormatter() {
|
||||
AMDFormatter.apply(this, arguments);
|
||||
@@ -26,7 +26,7 @@ UMDFormatter.prototype.transform = function (ast) {
|
||||
|
||||
// factory
|
||||
|
||||
var ids = _.values(this.ids);
|
||||
var ids = values(this.ids);
|
||||
var args = [t.identifier("exports")];
|
||||
if (this.passModuleArg) args.push(t.identifier("module"));
|
||||
args = args.concat(ids);
|
||||
|
||||
@@ -3,12 +3,12 @@
|
||||
var gen = fn.apply(this, arguments);
|
||||
|
||||
return new Promise(function (resolve, reject) {
|
||||
var callNext = step.bind(gen.next);
|
||||
var callThrow = step.bind(gen.throw);
|
||||
var callNext = step.bind(null, "next");
|
||||
var callThrow = step.bind(null, "throw");
|
||||
|
||||
function step(arg) {
|
||||
function step(key, arg) {
|
||||
try {
|
||||
var info = this(arg);
|
||||
var info = gen[key](arg);
|
||||
var value = info.value;
|
||||
} catch (error) {
|
||||
reject(error);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
(function (target) {
|
||||
Object.assign || (function (target) {
|
||||
for (var i = 1; i < arguments.length; i++) {
|
||||
var source = arguments[i];
|
||||
for (var key in source) {
|
||||
|
||||
24
lib/6to5/transformation/templates/set.js
Normal file
24
lib/6to5/transformation/templates/set.js
Normal file
@@ -0,0 +1,24 @@
|
||||
(function set(object, property, value, receiver) {
|
||||
var desc = Object.getOwnPropertyDescriptor(object, property);
|
||||
|
||||
if (desc === undefined) {
|
||||
var parent = Object.getPrototypeOf(object);
|
||||
|
||||
if (parent === null) {
|
||||
return;
|
||||
} else {
|
||||
return set(parent, property, value, receiver);
|
||||
}
|
||||
} else if ("value" in desc && desc.writable) {
|
||||
desc.value = value;
|
||||
return;
|
||||
} else {
|
||||
var setter = desc.set;
|
||||
|
||||
if (setter === undefined) {
|
||||
return;
|
||||
}
|
||||
|
||||
return setter.call(receiver, value);
|
||||
}
|
||||
});
|
||||
@@ -1,7 +1,8 @@
|
||||
module.exports = TransformerPass;
|
||||
|
||||
var traverse = require("../traverse");
|
||||
var _ = require("lodash");
|
||||
var util = require("../util");
|
||||
var contains = require("lodash/collection/contains");
|
||||
|
||||
/**
|
||||
* This class is responsible for traversing over the provided `File`s
|
||||
@@ -31,15 +32,17 @@ TransformerPass.prototype.canRun = function () {
|
||||
if (key[0] === "_") return true;
|
||||
|
||||
var blacklist = opts.blacklist;
|
||||
if (blacklist.length && _.contains(blacklist, key)) return false;
|
||||
if (blacklist.length && contains(blacklist, key)) return false;
|
||||
|
||||
var whitelist = opts.whitelist;
|
||||
if (whitelist.length && !_.contains(whitelist, key)) return false;
|
||||
if (whitelist.length && !contains(whitelist, key)) return false;
|
||||
|
||||
if (transformer.optional && !_.contains(opts.optional, key)) return false;
|
||||
if (transformer.optional && !contains(opts.optional, key)) return false;
|
||||
|
||||
if (transformer.experimental && !opts.experimental) return false;
|
||||
|
||||
if (transformer.playground && !opts.playground) return false;
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
@@ -60,6 +63,8 @@ var transformVisitor = {
|
||||
TransformerPass.prototype.transform = function () {
|
||||
var file = this.file;
|
||||
|
||||
util.debug(file.opts.filename + ": Running transformer " + this.transformer.key);
|
||||
|
||||
this.astRun("before");
|
||||
|
||||
var state = { file: file, handlers: this.handlers, pass: this };
|
||||
|
||||
@@ -3,8 +3,10 @@
|
||||
module.exports = Transformer;
|
||||
|
||||
var TransformerPass = require("./transformer-pass");
|
||||
var t = require("../types");
|
||||
var _ = require("lodash");
|
||||
var isFunction = require("lodash/lang/isFunction");
|
||||
var traverse = require("../traverse");
|
||||
var isObject = require("lodash/lang/isObject");
|
||||
var each = require("lodash/collection/each");
|
||||
|
||||
/**
|
||||
* This is the class responsible for normalising a transformers handlers
|
||||
@@ -15,6 +17,7 @@ var _ = require("lodash");
|
||||
function Transformer(key, transformer, opts) {
|
||||
this.manipulateOptions = transformer.manipulateOptions;
|
||||
this.experimental = !!transformer.experimental;
|
||||
this.playground = !!transformer.playground;
|
||||
this.secondPass = !!transformer.secondPass;
|
||||
this.optional = !!transformer.optional;
|
||||
this.handlers = this.normalise(transformer);
|
||||
@@ -25,32 +28,27 @@ function Transformer(key, transformer, opts) {
|
||||
Transformer.prototype.normalise = function (transformer) {
|
||||
var self = this;
|
||||
|
||||
if (_.isFunction(transformer)) {
|
||||
if (isFunction(transformer)) {
|
||||
transformer = { ast: transformer };
|
||||
}
|
||||
|
||||
_.each(transformer, function (fns, type) {
|
||||
traverse.explode(transformer);
|
||||
|
||||
each(transformer, function (fns, type) {
|
||||
// hidden property
|
||||
if (type[0] === "_") {
|
||||
self[type] = fns;
|
||||
return;
|
||||
}
|
||||
|
||||
if (_.isFunction(fns)) fns = { enter: fns };
|
||||
if (isFunction(fns)) fns = { enter: fns };
|
||||
|
||||
if (!_.isObject(fns)) return;
|
||||
if (!isObject(fns)) return;
|
||||
|
||||
if (!fns.enter) fns.enter = _.noop;
|
||||
if (!fns.exit) fns.exit = _.noop;
|
||||
if (!fns.enter) fns.enter = function () { };
|
||||
if (!fns.exit) fns.exit = function () { };
|
||||
|
||||
transformer[type] = fns;
|
||||
|
||||
var aliases = t.FLIPPED_ALIAS_KEYS[type];
|
||||
if (aliases) {
|
||||
_.each(aliases, function (alias) {
|
||||
transformer[alias] = fns;
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
return transformer;
|
||||
|
||||
@@ -4,7 +4,8 @@ var traverse = require("../../../traverse");
|
||||
var object = require("../../../helpers/object");
|
||||
var util = require("../../../util");
|
||||
var t = require("../../../types");
|
||||
var _ = require("lodash");
|
||||
var values = require("lodash/object/values");
|
||||
var extend = require("lodash/object/extend");
|
||||
|
||||
var isLet = function (node, parent) {
|
||||
if (!t.isVariableDeclaration(node)) return false;
|
||||
@@ -44,19 +45,8 @@ exports.Loop = function (node, parent, scope, context, file) {
|
||||
t.ensureBlock(node);
|
||||
node.body._letDeclarators = [init];
|
||||
}
|
||||
|
||||
if (t.isLabeledStatement(parent)) {
|
||||
// set label so `run` has access to it
|
||||
node.label = parent.label;
|
||||
}
|
||||
|
||||
var letScoping = new LetScoping(node, node.body, parent, scope, file);
|
||||
letScoping.run();
|
||||
|
||||
if (node.label && !t.isLabeledStatement(parent)) {
|
||||
// we've been given a label so let's wrap ourselves
|
||||
return t.labeledStatement(node.label, node);
|
||||
}
|
||||
};
|
||||
|
||||
exports.Program =
|
||||
@@ -200,7 +190,7 @@ LetScoping.prototype.needsClosure = function () {
|
||||
this.hoistVarDeclarations();
|
||||
|
||||
// turn outsideLetReferences into an array
|
||||
var params = _.values(this.outsideLetReferences);
|
||||
var params = values(this.outsideLetReferences);
|
||||
|
||||
// build the closure that we're going to wrap the block with
|
||||
var fn = t.functionExpression(null, params, t.blockStatement(block.body));
|
||||
@@ -268,7 +258,7 @@ LetScoping.prototype.getLetReferences = function () {
|
||||
//
|
||||
for (var i = 0; i < declarators.length; i++) {
|
||||
declar = declarators[i];
|
||||
_.extend(this.outsideLetReferences, t.getIds(declar, true));
|
||||
extend(this.outsideLetReferences, t.getDeclarations(declar));
|
||||
}
|
||||
|
||||
//
|
||||
@@ -284,8 +274,8 @@ LetScoping.prototype.getLetReferences = function () {
|
||||
//
|
||||
for (i = 0; i < declarators.length; i++) {
|
||||
declar = declarators[i];
|
||||
var keys = t.getIds(declar, true);
|
||||
_.extend(this.letReferences, keys);
|
||||
var keys = t.getDeclarations(declar);
|
||||
extend(this.letReferences, keys);
|
||||
this.hasLetReferences = true;
|
||||
}
|
||||
|
||||
@@ -307,34 +297,71 @@ LetScoping.prototype.getLetReferences = function () {
|
||||
return state.closurify;
|
||||
};
|
||||
|
||||
var loopNodeTo = function (node) {
|
||||
if (t.isBreakStatement(node)) {
|
||||
return "break";
|
||||
} else if (t.isContinueStatement(node)) {
|
||||
return "continue";
|
||||
}
|
||||
};
|
||||
|
||||
var loopVisitor = {
|
||||
enter: function (node, parent, scope, context, state) {
|
||||
var replace;
|
||||
|
||||
if (t.isLoop(node)) {
|
||||
state.ignoreLabeless = true;
|
||||
traverse(node, loopVisitor, scope, state);
|
||||
state.ignoreLabeless = false;
|
||||
}
|
||||
|
||||
if (t.isFunction(node) || t.isLoop(node)) {
|
||||
return context.skip();
|
||||
}
|
||||
|
||||
if (node && !node.label && state.isLoop) {
|
||||
if (t.isBreakStatement(node)) {
|
||||
if (t.isSwitchCase(parent)) return;
|
||||
var loopText = loopNodeTo(node);
|
||||
|
||||
state.hasBreak = true;
|
||||
replace = t.returnStatement(t.literal("break"));
|
||||
} else if (t.isContinueStatement(node)) {
|
||||
state.hasContinue = true;
|
||||
replace = t.returnStatement(t.literal("continue"));
|
||||
if (loopText) {
|
||||
if (node.label) {
|
||||
// we shouldn't be transforming this because it exists somewhere inside
|
||||
if (state.innerLabels.indexOf(node.label.name) >= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
loopText = loopText + "|" + node.label.name;
|
||||
} else {
|
||||
// we shouldn't be transforming these statements because
|
||||
// they don't refer to the actual loop we're scopifying
|
||||
if (state.ignoreLabeless) return;
|
||||
|
||||
// break statements mean something different in this context
|
||||
if (t.isBreakStatement(node) && t.isSwitchCase(parent)) return;
|
||||
}
|
||||
|
||||
state.hasBreakContinue = true;
|
||||
state.map[loopText] = node;
|
||||
replace = t.literal(loopText);
|
||||
}
|
||||
|
||||
if (t.isReturnStatement(node)) {
|
||||
state.hasReturn = true;
|
||||
replace = t.returnStatement(t.objectExpression([
|
||||
replace = t.objectExpression([
|
||||
t.property("init", t.identifier("v"), node.argument || t.identifier("undefined"))
|
||||
]));
|
||||
]);
|
||||
}
|
||||
|
||||
if (replace) return t.inherits(replace, node);
|
||||
if (replace) {
|
||||
replace = t.returnStatement(replace);
|
||||
return t.inherits(replace, node);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var loopLabelVisitor = {
|
||||
enter: function (node, parent, scope, context, state) {
|
||||
if (t.isLabeledStatement(node)) {
|
||||
state.innerLabels.push(node.label.name);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -349,13 +376,17 @@ var loopVisitor = {
|
||||
|
||||
LetScoping.prototype.checkLoop = function () {
|
||||
var state = {
|
||||
hasContinue: false,
|
||||
hasReturn: false,
|
||||
hasBreak: false,
|
||||
isLoop: !!this.loopParent
|
||||
hasBreakContinue: false,
|
||||
ignoreLabeless: false,
|
||||
innerLabels: [],
|
||||
hasReturn: false,
|
||||
isLoop: !!this.loopParent,
|
||||
map: {}
|
||||
};
|
||||
|
||||
traverse(this.block, loopLabelVisitor, this.scope, state);
|
||||
traverse(this.block, loopVisitor, this.scope, state);
|
||||
|
||||
return state;
|
||||
};
|
||||
|
||||
@@ -421,7 +452,7 @@ LetScoping.prototype.pushDeclar = function (node) {
|
||||
|
||||
LetScoping.prototype.build = function (ret, call) {
|
||||
var has = this.has;
|
||||
if (has.hasReturn || has.hasBreak || has.hasContinue) {
|
||||
if (has.hasReturn || has.hasBreakContinue) {
|
||||
this.buildHas(ret, call);
|
||||
} else {
|
||||
this.body.push(t.expressionStatement(call));
|
||||
@@ -454,22 +485,14 @@ LetScoping.prototype.buildHas = function (ret, call) {
|
||||
});
|
||||
}
|
||||
|
||||
if (has.hasBreak || has.hasContinue) {
|
||||
if (has.hasBreakContinue) {
|
||||
if (!loopParent) {
|
||||
throw new Error("Has no loop parent but we're trying to reassign breaks " +
|
||||
"and continues, something is going wrong here.");
|
||||
}
|
||||
|
||||
// ensure that the parent has a label as we're building a switch and we
|
||||
// need to be able to access it
|
||||
var label = loopParent.label = loopParent.label || this.scope.generateUidIdentifier("loop");
|
||||
|
||||
if (has.hasBreak) {
|
||||
cases.push(t.switchCase(t.literal("break"), [t.breakStatement(label)]));
|
||||
}
|
||||
|
||||
if (has.hasContinue) {
|
||||
cases.push(t.switchCase(t.literal("continue"), [t.continueStatement(label)]));
|
||||
for (var key in has.map) {
|
||||
cases.push(t.switchCase(t.literal(key), [has.map[key]]));
|
||||
}
|
||||
|
||||
if (has.hasReturn) {
|
||||
|
||||
@@ -231,6 +231,7 @@ Class.prototype.pushMethod = function (node) {
|
||||
}
|
||||
|
||||
util.pushMutatorMap(mutatorMap, methodName, kind, node.computed, node);
|
||||
util.pushMutatorMap(mutatorMap, methodName, "enumerable", node.computed, false);
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@@ -6,7 +6,7 @@ var t = require("../../../types");
|
||||
var visitor = {
|
||||
enter: function (node, parent, scope, context, state) {
|
||||
if (t.isDeclaration(node) || t.isAssignmentExpression(node)) {
|
||||
var ids = t.getIds(node, true);
|
||||
var ids = t.getDeclarations(node);
|
||||
|
||||
for (var key in ids) {
|
||||
var id = ids[key];
|
||||
@@ -31,7 +31,7 @@ var visitor = {
|
||||
|
||||
exports.Scope = function (node, parent, scope, context, file) {
|
||||
traverse(node, visitor, scope, {
|
||||
constants: scope.getAllOfKind("const"),
|
||||
constants: scope.getAllDeclarationsOfKind("const"),
|
||||
file: file
|
||||
});
|
||||
};
|
||||
|
||||
@@ -46,9 +46,11 @@ var push = function (opts, nodes, elem, parentId) {
|
||||
var pushAssignmentPattern = function (opts, nodes, pattern, parentId) {
|
||||
var tempParentId = opts.scope.generateUidBasedOnNode(parentId, opts.file);
|
||||
|
||||
nodes.push(t.variableDeclaration("var", [
|
||||
var declar = t.variableDeclaration("var", [
|
||||
t.variableDeclarator(tempParentId, parentId)
|
||||
]));
|
||||
]);
|
||||
declar._blockHoist = opts.blockHoist;
|
||||
nodes.push(declar);
|
||||
|
||||
nodes.push(buildVariableAssign(
|
||||
opts,
|
||||
|
||||
@@ -13,7 +13,7 @@ var hasDefaults = function (node) {
|
||||
|
||||
var iifeVisitor = {
|
||||
enter: function (node, parent, scope, context, state) {
|
||||
if (t.isReferencedIdentifier(node, parent) && scope.hasOwn(node.name)) {
|
||||
if (t.isReferencedIdentifier(node, parent) && state.scope.hasOwn(node.name)) {
|
||||
state.iife = true;
|
||||
context.stop();
|
||||
}
|
||||
|
||||
@@ -58,15 +58,15 @@ exports.Function = function (node, parent, scope) {
|
||||
node.body.body.unshift(restDeclar);
|
||||
}
|
||||
|
||||
node.body.body.unshift(
|
||||
util.template("rest", {
|
||||
ARGUMENTS: argsId,
|
||||
ARRAY_KEY: arrKey,
|
||||
ARRAY_LEN: arrLen,
|
||||
START: start,
|
||||
ARRAY: rest,
|
||||
KEY: key,
|
||||
LEN: len,
|
||||
})
|
||||
);
|
||||
var loop = util.template("rest", {
|
||||
ARGUMENTS: argsId,
|
||||
ARRAY_KEY: arrKey,
|
||||
ARRAY_LEN: arrLen,
|
||||
START: start,
|
||||
ARRAY: rest,
|
||||
KEY: key,
|
||||
LEN: len,
|
||||
});
|
||||
loop._blockHoist = node.params.length + 1;
|
||||
node.body.body.unshift(loop);
|
||||
};
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
var nameMethod = require("../../helpers/name-method");
|
||||
var t = require("../../../types");
|
||||
var _ = require("lodash");
|
||||
var clone = require("lodash/lang/clone");
|
||||
|
||||
exports.Property = function (node, parent, scope, context, file) {
|
||||
if (node.method) {
|
||||
@@ -12,6 +12,6 @@ exports.Property = function (node, parent, scope, context, file) {
|
||||
|
||||
if (node.shorthand) {
|
||||
node.shorthand = false;
|
||||
node.key = t.removeComments(_.clone(node.key));
|
||||
node.key = t.removeComments(clone(node.key));
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
"use strict";
|
||||
|
||||
var t = require("../../../types");
|
||||
var _ = require("lodash");
|
||||
var contains = require("lodash/collection/contains");
|
||||
|
||||
var getSpreadLiteral = function (spread, file) {
|
||||
return file.toArray(spread.argument);
|
||||
@@ -101,7 +101,7 @@ exports.NewExpression = function (node, parent, scope, context, file) {
|
||||
var args = node.arguments;
|
||||
if (!hasSpread(args)) return;
|
||||
|
||||
var nativeType = t.isIdentifier(node.callee) && _.contains(t.NATIVE_TYPE_NAMES, node.callee.name);
|
||||
var nativeType = t.isIdentifier(node.callee) && contains(t.NATIVE_TYPE_NAMES, node.callee.name);
|
||||
|
||||
var nodes = build(args, file);
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
"use strict";
|
||||
|
||||
var rewritePattern = require("regexpu/rewrite-pattern");
|
||||
var _ = require("lodash");
|
||||
var pull = require("lodash/array/pull");
|
||||
|
||||
exports.Literal = function (node) {
|
||||
var regex = node.regex;
|
||||
@@ -9,7 +9,7 @@ exports.Literal = function (node) {
|
||||
|
||||
var flags = regex.flags.split("");
|
||||
if (regex.flags.indexOf("u") < 0) return;
|
||||
_.pull(flags, "u");
|
||||
pull(flags, "u");
|
||||
|
||||
regex.pattern = rewritePattern(regex.pattern, regex.flags);
|
||||
regex.flags = flags.join("");
|
||||
|
||||
@@ -12,8 +12,9 @@ module.exports = {
|
||||
"playground.objectGetterMemoization": require("./playground/object-getter-memoization"),
|
||||
|
||||
react: require("./other/react"),
|
||||
"optimisation.react": require("./optimisation/react"),
|
||||
|
||||
_modulesSplit: require("./internal/modules-split"),
|
||||
_modules: require("./internal/modules"),
|
||||
|
||||
// needs to be before `regenerator` due to generator comprehensions
|
||||
// needs to be before `_aliasFunction`
|
||||
@@ -52,15 +53,13 @@ module.exports = {
|
||||
// needs to be after `es6.blockScoping` due to needing `letReferences` set on blocks
|
||||
"es6.blockScopingTDZ": require("./es6/block-scoping-tdz"),
|
||||
|
||||
// needs to be after block scoping since regenerator doesn't support it
|
||||
regenerator: require("./other/regenerator"),
|
||||
|
||||
"es6.parameters.default": require("./es6/parameters.default"),
|
||||
"es6.parameters.rest": require("./es6/parameters.rest"),
|
||||
|
||||
// needs to be before regenerator since regenerator doesn't know how to handle destructuring patterns
|
||||
"es6.destructuring": require("./es6/destructuring"),
|
||||
|
||||
regenerator: require("./other/regenerator"),
|
||||
|
||||
// needs to be after `regenerator` due to needing `regeneratorRuntime` references
|
||||
// needs to be after `es6.forOf` due to needing `Symbol.iterator` references
|
||||
// needs to be before `es6.modules` due to dynamic imports
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
"use strict";
|
||||
|
||||
var useStrict = require("../../helpers/use-strict");
|
||||
var _ = require("lodash");
|
||||
var groupBy = require("lodash/collection/groupBy");
|
||||
var flatten = require("lodash/array/flatten");
|
||||
var values = require("lodash/object/values");
|
||||
|
||||
// Priority:
|
||||
//
|
||||
@@ -21,14 +23,14 @@ exports.Program = {
|
||||
if (!hasChange) return;
|
||||
|
||||
useStrict.wrap(node, function () {
|
||||
var nodePriorities = _.groupBy(node.body, function (bodyNode) {
|
||||
var nodePriorities = groupBy(node.body, function (bodyNode) {
|
||||
var priority = bodyNode._blockHoist;
|
||||
if (priority == null) priority = 1;
|
||||
if (priority === true) priority = 2;
|
||||
return priority;
|
||||
});
|
||||
|
||||
node.body = _.flatten(_.values(nodePriorities).reverse());
|
||||
node.body = flatten(values(nodePriorities).reverse());
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
@@ -8,7 +8,18 @@
|
||||
|
||||
var t = require("../../../types");
|
||||
|
||||
var resolveModuleSource = function (node, parent, scope, context, file) {
|
||||
var resolveModuleSource = file.opts.resolveModuleSource;
|
||||
if (node.source && resolveModuleSource) {
|
||||
node.source.value = resolveModuleSource(node.source.value);
|
||||
}
|
||||
};
|
||||
|
||||
exports.ImportDeclaration = resolveModuleSource;
|
||||
|
||||
exports.ExportDeclaration = function (node, parent, scope) {
|
||||
resolveModuleSource.apply(null, arguments);
|
||||
|
||||
var declar = node.declaration;
|
||||
|
||||
if (node.default) {
|
||||
@@ -0,0 +1,55 @@
|
||||
module.exports = BaseOptimiser;
|
||||
|
||||
var object = require("../../../../helpers/object");
|
||||
|
||||
/**
|
||||
* Description
|
||||
*
|
||||
* @param {Node} node
|
||||
*/
|
||||
|
||||
function BaseOptimiser(node) {
|
||||
this.methods = object();
|
||||
this.types = object();
|
||||
|
||||
this.node = node;
|
||||
}
|
||||
|
||||
/**
|
||||
* Description
|
||||
*/
|
||||
|
||||
BaseOptimiser.prototype.run = function () {
|
||||
this.getMethods();
|
||||
this.getTypes();
|
||||
};
|
||||
|
||||
/**
|
||||
* Add an `ObjectExpression` `node` that contains `propTypes`.
|
||||
*
|
||||
* Search it and match it against the types that we can optimise
|
||||
* and register it for consumption later.
|
||||
*
|
||||
* @param {Node} node
|
||||
*/
|
||||
|
||||
BaseOptimiser.prototype.addPropTypes = function (node) {
|
||||
var props = node.properties;
|
||||
|
||||
for (var i = 0; i < props.length; i++) {
|
||||
this.addPropType(props[i]);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Register a `Property` node as a prop type.
|
||||
*
|
||||
* We'll try and resolve it to a known type if we can and normalise
|
||||
* it for consumption later.
|
||||
*
|
||||
* @param {Node} prop
|
||||
*/
|
||||
|
||||
BaseOptimiser.prototype.addPropType = function () {
|
||||
|
||||
};
|
||||
@@ -0,0 +1,51 @@
|
||||
module.exports = CreateClassOptimiser;
|
||||
|
||||
var BaseOptimiser = require("./base");
|
||||
var util = require("../../../../util");
|
||||
var t = require("../../../../types");
|
||||
|
||||
function CreateClassOptimiser() {
|
||||
BaseOptimiser.apply(this, arguments);
|
||||
}
|
||||
|
||||
util.inherits(CreateClassOptimiser, BaseOptimiser);
|
||||
|
||||
/**
|
||||
* Get all function expressions.
|
||||
*/
|
||||
|
||||
CreateClassOptimiser.prototype.getMethods = function () {
|
||||
var props = this.node.properties;
|
||||
|
||||
for (var i = 0; i < props.length; i++) {
|
||||
var prop = props[i];
|
||||
|
||||
// irrelevant
|
||||
if (!t.isFunctionExpression(prop.value)) continue;
|
||||
|
||||
// deopt
|
||||
if (prop.computed) continue;
|
||||
|
||||
this.methods[prop.key.name] = prop;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Find a `propTypes` property.
|
||||
*/
|
||||
|
||||
CreateClassOptimiser.prototype.getTypes = function () {
|
||||
var props = this.node.properties;
|
||||
|
||||
for (var i = 0; i < props.length; i++) {
|
||||
var prop = props[i];
|
||||
var key = t.toComputedKey(prop, prop.key);
|
||||
|
||||
if (t.isLiteral(key, { value: "propTypes" }) && t.isObjectExpression(prop.value)) {
|
||||
this.addPropTypes(prop.value);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// not found
|
||||
};
|
||||
@@ -0,0 +1,17 @@
|
||||
var CreateClassOptimiser = require("./create-class");
|
||||
var NativeClassOptimiser = require("./native-class");
|
||||
var react = require("../../../helpers/react");
|
||||
|
||||
exports.optional = true;
|
||||
|
||||
exports.CallExpression = function (node) {
|
||||
if (react.isCreateClass(node)) {
|
||||
new CreateClassOptimiser(node.arguments[0]).run();
|
||||
}
|
||||
};
|
||||
|
||||
exports.CallExpression = function (node) {
|
||||
if (react.isReactComponent(node.superClass)) {
|
||||
new NativeClassOptimiser(node).run();
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,42 @@
|
||||
module.exports = NativeClassOptimiser;
|
||||
|
||||
var BaseOptimiser = require("./base");
|
||||
var util = require("../../../../util");
|
||||
var t = require("../../../../types");
|
||||
|
||||
function NativeClassOptimiser() {
|
||||
BaseOptimiser.apply(this, arguments);
|
||||
}
|
||||
|
||||
util.inherits(NativeClassOptimiser, BaseOptimiser);
|
||||
|
||||
/**
|
||||
* Get all instance methods.
|
||||
*/
|
||||
|
||||
NativeClassOptimiser.prototype.getMethods = function () {
|
||||
var body = this.node.body;
|
||||
|
||||
for (var i = 0; i < body.length; i++) {
|
||||
var node = body[i];
|
||||
|
||||
// PrivateDeclaration etc
|
||||
if (!t.isMethodDefinition(node)) continue;
|
||||
|
||||
// deopt
|
||||
if (node.computed) continue;
|
||||
|
||||
// irrelevant
|
||||
if (node.static) continue;
|
||||
|
||||
this.methods[node.key.name] = node;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Description
|
||||
*/
|
||||
|
||||
NativeClassOptimiser.prototype.getTypes = function () {
|
||||
|
||||
};
|
||||
@@ -6,10 +6,13 @@
|
||||
// jsx
|
||||
|
||||
var esutils = require("esutils");
|
||||
var react = require("../../helpers/react");
|
||||
var t = require("../../../types");
|
||||
|
||||
exports.JSXIdentifier = function (node) {
|
||||
if (esutils.keyword.isIdentifierName(node.name)) {
|
||||
exports.JSXIdentifier = function (node, parent) {
|
||||
if (node.name === "this" && t.isReferenced(node, parent)) {
|
||||
return t.thisExpression();
|
||||
} else if (esutils.keyword.isIdentifierName(node.name)) {
|
||||
node.type = "Identifier";
|
||||
} else {
|
||||
return t.literal(node.name);
|
||||
@@ -195,33 +198,8 @@ var cleanJSXElementLiteralChild = function (child, args) {
|
||||
|
||||
// display names
|
||||
|
||||
var isCreateClass = function (call) {
|
||||
if (!call || !t.isCallExpression(call)) return false;
|
||||
|
||||
var callee = call.callee;
|
||||
if (!t.isMemberExpression(callee)) return false;
|
||||
|
||||
// not React call member object
|
||||
var obj = callee.object;
|
||||
if (!t.isIdentifier(obj, { name: "React" })) return false;
|
||||
|
||||
// not createClass call member property
|
||||
var prop = callee.property;
|
||||
if (!t.isIdentifier(prop, { name: "createClass" })) return false;
|
||||
|
||||
// no call arguments
|
||||
var args = call.arguments;
|
||||
if (args.length !== 1) return false;
|
||||
|
||||
// first call arg is not an object
|
||||
var first = args[0];
|
||||
if (!t.isObjectExpression(first)) return;
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
var addDisplayName = function (id, call) {
|
||||
if (!isCreateClass(call)) return;
|
||||
if (!react.isCreateClass(call)) return;
|
||||
|
||||
var props = call.arguments[0].properties;
|
||||
var safe = true;
|
||||
|
||||
@@ -4,10 +4,11 @@ var traverse = require("../../../traverse");
|
||||
var util = require("../../../util");
|
||||
var core = require("core-js/library");
|
||||
var t = require("../../../types");
|
||||
var _ = require("lodash");
|
||||
var has = require("lodash/object/has");
|
||||
var contains = require("lodash/collection/contains");
|
||||
|
||||
var coreHas = function (node) {
|
||||
return node.name !== "_" && _.has(core, node.name);
|
||||
return node.name !== "_" && has(core, node.name);
|
||||
};
|
||||
|
||||
var ALIASABLE_CONSTRUCTORS = [
|
||||
@@ -30,11 +31,11 @@ var astVisitor = {
|
||||
|
||||
if (!t.isReferenced(obj, node)) return;
|
||||
|
||||
if (!node.computed && coreHas(obj) && _.has(core[obj.name], prop.name)) {
|
||||
if (!node.computed && coreHas(obj) && has(core[obj.name], prop.name)) {
|
||||
context.skip();
|
||||
return t.prependToMemberExpression(node, file.get("coreIdentifier"));
|
||||
}
|
||||
} else if (t.isReferencedIdentifier(node, parent) && !t.isMemberExpression(parent) && _.contains(ALIASABLE_CONSTRUCTORS, node.name) && !scope.get(node.name, true)) {
|
||||
} else if (t.isReferencedIdentifier(node, parent) && !t.isMemberExpression(parent) && contains(ALIASABLE_CONSTRUCTORS, node.name) && !scope.get(node.name, true)) {
|
||||
// Symbol() -> _core.Symbol(); new Promise -> new _core.Promise
|
||||
return t.memberExpression(file.get("coreIdentifier"), node);
|
||||
} else if (t.isCallExpression(node)) {
|
||||
|
||||
@@ -17,5 +17,5 @@ exports.FunctionExpression = function (node, parent, scope, context) {
|
||||
};
|
||||
|
||||
exports.ThisExpression = function (node, parent, scope, context, file) {
|
||||
throw file.errorWithNode(node, "Top level `this` is not allowed", ReferenceError);
|
||||
throw file.errorWithNode(node, "Top level `this` is `undefined` in strict mode", ReferenceError);
|
||||
};
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
"use strict";
|
||||
|
||||
var build = require("../../helpers/build-conditional-assignment-operator-transformer");
|
||||
var t = require("../../../types");
|
||||
var t = require("../../../types");
|
||||
|
||||
exports.playground = true;
|
||||
|
||||
build(exports, {
|
||||
is: function (node, file) {
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
var build = require("../../helpers/build-conditional-assignment-operator-transformer");
|
||||
var t = require("../../../types");
|
||||
|
||||
exports.playground = true;
|
||||
|
||||
build(exports, {
|
||||
is: function (node) {
|
||||
var is = t.isAssignmentExpression(node) && node.operator === "?=";
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
var t = require("../../../types");
|
||||
|
||||
exports.playground = true;
|
||||
|
||||
exports.BindMemberExpression = function (node, parent, scope) {
|
||||
var object = node.object;
|
||||
var prop = node.property;
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
var traverse = require("../../../traverse");
|
||||
var t = require("../../../types");
|
||||
|
||||
exports.playground = true;
|
||||
|
||||
var visitor = {
|
||||
enter: function (node, parent, scope, context, state) {
|
||||
if (t.isFunction(node)) return;
|
||||
|
||||
@@ -3,13 +3,13 @@
|
||||
var t = require("../../../types");
|
||||
|
||||
exports.BlockStatement = function (node, parent) {
|
||||
if (t.isFunction(parent) || t.isExportDeclaration(parent)) {
|
||||
if ((t.isFunction(parent) && parent.body === node) || t.isExportDeclaration(parent)) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (var i = 0; i < node.body.length; i++) {
|
||||
var func = node.body[i];
|
||||
if (!t.isFunctionDeclaration(i)) continue;
|
||||
if (!t.isFunctionDeclaration(func)) continue;
|
||||
|
||||
var declar = t.variableDeclaration("let", [
|
||||
t.variableDeclarator(func.id, t.toExpression(func))
|
||||
@@ -21,6 +21,6 @@ exports.BlockStatement = function (node, parent) {
|
||||
// todo: name this
|
||||
func.id = null;
|
||||
|
||||
func.body[i] = declar;
|
||||
node.body[i] = declar;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
"use strict";
|
||||
|
||||
var t = require("../../../types");
|
||||
var _ = require("lodash");
|
||||
var t = require("../../../types");
|
||||
var pull = require("lodash/array/pull");
|
||||
|
||||
var isProtoKey = function (node) {
|
||||
return t.isLiteral(t.toComputedKey(node, node.key), { value: "__proto__" });
|
||||
@@ -50,7 +50,7 @@ exports.ObjectExpression = function (node, parent, scope, context, file) {
|
||||
|
||||
if (isProtoKey(prop)) {
|
||||
proto = prop.value;
|
||||
_.pull(node.properties, prop);
|
||||
pull(node.properties, prop);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,9 +1,38 @@
|
||||
"use strict";
|
||||
|
||||
var levenshtein = require("../../../helpers/levenshtein");
|
||||
var t = require("../../../types");
|
||||
|
||||
exports.optional = true;
|
||||
|
||||
exports.Identifier = function (node, parent, scope, context, file) {
|
||||
if (!scope.has(node.name, true)) {
|
||||
throw file.errorWithNode(node, "Reference to undeclared variable", ReferenceError);
|
||||
if (!t.isReferenced(node, parent)) return;
|
||||
if (scope.has(node.name, true)) return;
|
||||
|
||||
var msg = "Reference to undeclared variable";
|
||||
|
||||
// get the closest declaration to offer as a suggestion
|
||||
// the variable name may have just been mistyped
|
||||
|
||||
var declarations = scope.getAllDeclarations();
|
||||
|
||||
var closest;
|
||||
var shortest = -1;
|
||||
|
||||
for (var name in declarations) {
|
||||
var distance = levenshtein(node.name, name);
|
||||
if (distance <= 0 || distance > 3) continue;
|
||||
if (distance <= shortest) continue;
|
||||
|
||||
closest = name;
|
||||
shortest = distance;
|
||||
}
|
||||
|
||||
if (closest) {
|
||||
msg += " - Did you mean " + closest + "?";
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
throw file.errorWithNode(node, msg, ReferenceError);
|
||||
};
|
||||
|
||||
@@ -1,64 +0,0 @@
|
||||
[
|
||||
"Set",
|
||||
"Map",
|
||||
"WeakMap",
|
||||
"WeakSet",
|
||||
"Proxy",
|
||||
"Promise",
|
||||
"Reflect",
|
||||
"Symbol",
|
||||
"System",
|
||||
"__filename",
|
||||
"__dirname",
|
||||
"GLOBAL",
|
||||
"global",
|
||||
"module",
|
||||
"require",
|
||||
"Buffer",
|
||||
"console",
|
||||
"exports",
|
||||
"process",
|
||||
"setTimeout",
|
||||
"clearTimeout",
|
||||
"setInterval",
|
||||
"clearInterval",
|
||||
"setImmediate",
|
||||
"clearImmediate",
|
||||
"Array",
|
||||
"Boolean",
|
||||
"Date",
|
||||
"decodeURI",
|
||||
"decodeURIComponent",
|
||||
"encodeURI",
|
||||
"encodeURIComponent",
|
||||
"Error",
|
||||
"eval",
|
||||
"EvalError",
|
||||
"Function",
|
||||
"hasOwnProperty",
|
||||
"isFinite",
|
||||
"isNaN",
|
||||
"JSON",
|
||||
"Map",
|
||||
"Math",
|
||||
"Number",
|
||||
"Object",
|
||||
"Proxy",
|
||||
"Promise",
|
||||
"parseInt",
|
||||
"parseFloat",
|
||||
"RangeError",
|
||||
"ReferenceError",
|
||||
"RegExp",
|
||||
"Set",
|
||||
"String",
|
||||
"SyntaxError",
|
||||
"TypeError",
|
||||
"URIError",
|
||||
"WeakMap",
|
||||
"WeakSet",
|
||||
"arguments",
|
||||
"NaN",
|
||||
"window",
|
||||
"self"
|
||||
]
|
||||
@@ -4,9 +4,11 @@ module.exports = traverse;
|
||||
|
||||
/* jshint maxparams:7 */
|
||||
|
||||
var Scope = require("./scope");
|
||||
var t = require("../types");
|
||||
var _ = require("lodash");
|
||||
var Scope = require("./scope");
|
||||
var t = require("../types");
|
||||
var contains = require("lodash/collection/contains");
|
||||
var flatten = require("lodash/array/flatten");
|
||||
var compact = require("lodash/array/compact");
|
||||
|
||||
function TraversalContext() {
|
||||
this.shouldFlatten = false;
|
||||
@@ -52,7 +54,7 @@ function replaceNode(obj, key, node, result) {
|
||||
|
||||
// we're replacing a statement or block node with an array of statements so we better
|
||||
// ensure that it's a block
|
||||
if (isArray && _.contains(t.STATEMENT_OR_BLOCK_KEYS, key) && !t.isBlockStatement(obj)) {
|
||||
if (isArray && contains(t.STATEMENT_OR_BLOCK_KEYS, key) && !t.isBlockStatement(obj)) {
|
||||
t.ensureBlock(obj, key);
|
||||
}
|
||||
|
||||
@@ -110,7 +112,7 @@ TraversalContext.prototype.visitNode = function (obj, key, opts, scope, parent,
|
||||
|
||||
var ourScope = scope;
|
||||
// we're entering a new scope so let's construct it!
|
||||
if (t.isScope(node)) {
|
||||
if (!opts.noScope && t.isScope(node)) {
|
||||
ourScope = new Scope(node, parent, scope);
|
||||
}
|
||||
|
||||
@@ -153,11 +155,11 @@ TraversalContext.prototype.visit = function (node, key, opts, scope, state) {
|
||||
}
|
||||
|
||||
if (this.shouldFlatten) {
|
||||
node[key] = _.flatten(node[key]);
|
||||
node[key] = flatten(node[key]);
|
||||
|
||||
if (key === "body") {
|
||||
// we can safely compact this
|
||||
node[key] = _.compact(node[key]);
|
||||
node[key] = compact(node[key]);
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -177,15 +179,15 @@ function traverseNode(node, opts, scope, state) {
|
||||
function traverse(parent, opts, scope, state) {
|
||||
if (!parent) return;
|
||||
|
||||
if (!scope) {
|
||||
if (!opts.noScope && !scope) {
|
||||
if (parent.type !== "Program" && parent.type !== "File") {
|
||||
throw new Error("Must pass a scope unless traversing a Program/File got a " + parent.type + " node");
|
||||
}
|
||||
}
|
||||
|
||||
if (!opts) opts = {};
|
||||
if (!opts.enter) opts.enter = _.noop;
|
||||
if (!opts.exit) opts.exit = _.noop;
|
||||
if (!opts.enter) opts.enter = function () { };
|
||||
if (!opts.exit) opts.exit = function () { };
|
||||
|
||||
// array of nodes
|
||||
if (Array.isArray(parent)) {
|
||||
@@ -217,7 +219,10 @@ function clearNode(node) {
|
||||
}
|
||||
}
|
||||
|
||||
var clearVisitor = { enter: clearNode };
|
||||
var clearVisitor = {
|
||||
noScope: true,
|
||||
enter: clearNode
|
||||
};
|
||||
|
||||
function clearComments(comments) {
|
||||
for (var i = 0; i < comments.length; i++) {
|
||||
@@ -232,6 +237,20 @@ traverse.removeProperties = function (tree) {
|
||||
return tree;
|
||||
};
|
||||
|
||||
traverse.explode = function (obj) {
|
||||
for (var type in obj) {
|
||||
var fns = obj[type];
|
||||
|
||||
var aliases = t.FLIPPED_ALIAS_KEYS[type];
|
||||
if (aliases) {
|
||||
for (var i = 0; i < aliases.length; i++) {
|
||||
obj[aliases[i]] = fns;
|
||||
}
|
||||
}
|
||||
}
|
||||
return obj;
|
||||
};
|
||||
|
||||
function hasBlacklistedType(node, parent, scope, context, state) {
|
||||
if (node.type === state.type) {
|
||||
state.has = true;
|
||||
@@ -241,7 +260,7 @@ function hasBlacklistedType(node, parent, scope, context, state) {
|
||||
|
||||
traverse.hasType = function (tree, scope, type, blacklistTypes) {
|
||||
// the node we're searching in is blacklisted
|
||||
if (_.contains(blacklistTypes, tree.type)) return false;
|
||||
if (contains(blacklistTypes, tree.type)) return false;
|
||||
|
||||
// the type we're looking for is the same as the passed node
|
||||
if (tree.type === type) return true;
|
||||
|
||||
@@ -3,9 +3,14 @@
|
||||
module.exports = Scope;
|
||||
|
||||
var traverse = require("./index");
|
||||
var globals = require("globals");
|
||||
var object = require("../helpers/object");
|
||||
var t = require("../types");
|
||||
var _ = require("lodash");
|
||||
var each = require("lodash/collection/each");
|
||||
var has = require("lodash/object/has");
|
||||
var contains = require("lodash/collection/contains");
|
||||
var flatten = require("lodash/array/flatten");
|
||||
var defaults = require("lodash/object/defaults");
|
||||
|
||||
var FOR_KEYS = ["left", "init"];
|
||||
|
||||
@@ -32,12 +37,12 @@ function Scope(block, parentBlock, parent, file) {
|
||||
this.declarationKinds = info.declarationKinds;
|
||||
}
|
||||
|
||||
Scope.defaultDeclarations = require("./global-keys");
|
||||
Scope.defaultDeclarations = flatten([globals.builtin, globals.browser, globals.node].map(Object.keys));
|
||||
|
||||
Scope.prototype._add = function (node, references, throwOnDuplicate) {
|
||||
if (!node) return;
|
||||
|
||||
var ids = t.getIds(node, true);
|
||||
var ids = t.getDeclarations(node);
|
||||
|
||||
for (var key in ids) {
|
||||
var id = ids[key];
|
||||
@@ -140,7 +145,7 @@ Scope.prototype.generateTempBasedOnNode = function (node) {
|
||||
var functionVariableVisitor = {
|
||||
enter: function (node, parent, scope, context, state) {
|
||||
if (t.isFor(node)) {
|
||||
_.each(FOR_KEYS, function (key) {
|
||||
each(FOR_KEYS, function (key) {
|
||||
var declar = node[key];
|
||||
if (t.isVar(declar)) state.add(declar);
|
||||
});
|
||||
@@ -207,7 +212,7 @@ Scope.prototype.getInfo = function () {
|
||||
// ForStatement - left, init
|
||||
|
||||
if (t.isFor(block)) {
|
||||
_.each(FOR_KEYS, function (key) {
|
||||
each(FOR_KEYS, function (key) {
|
||||
var node = block[key];
|
||||
if (t.isBlockScoped(node)) add(node, false, true);
|
||||
});
|
||||
@@ -261,7 +266,7 @@ Scope.prototype.getInfo = function () {
|
||||
// Function - params, rest
|
||||
|
||||
if (t.isFunction(block)) {
|
||||
_.each(block.params, function (param) {
|
||||
each(block.params, function (param) {
|
||||
add(param);
|
||||
});
|
||||
}
|
||||
@@ -321,17 +326,36 @@ Scope.prototype.getFunctionParent = function () {
|
||||
};
|
||||
|
||||
/**
|
||||
* Walks the scope tree and gathers all declarations of `kind`.
|
||||
* Walks the scope tree and gathers **all** declarations.
|
||||
*
|
||||
* @returns {Object}
|
||||
*/
|
||||
|
||||
Scope.prototype.getAllOfKind = function (kind) {
|
||||
Scope.prototype.getAllDeclarations = function () {
|
||||
var ids = object();
|
||||
|
||||
var scope = this;
|
||||
do {
|
||||
_.defaults(ids, scope.declarationKinds[kind]);
|
||||
defaults(ids, scope.declarations);
|
||||
scope = scope.parent;
|
||||
} while (scope);
|
||||
|
||||
return ids;
|
||||
};
|
||||
|
||||
/**
|
||||
* Walks the scope tree and gathers all declarations of `kind`.
|
||||
*
|
||||
* @param {String} kind
|
||||
* @returns {Object}
|
||||
*/
|
||||
|
||||
Scope.prototype.getAllDeclarationsOfKind = function (kind) {
|
||||
var ids = object();
|
||||
|
||||
var scope = this;
|
||||
do {
|
||||
defaults(ids, scope.declarationKinds[kind]);
|
||||
scope = scope.parent;
|
||||
} while (scope);
|
||||
|
||||
@@ -359,7 +383,7 @@ Scope.prototype.get = function (id, decl) {
|
||||
Scope.prototype.getOwn = function (id, decl) {
|
||||
var refs = this.references;
|
||||
if (decl) refs = this.declarations;
|
||||
return _.has(refs, id) && refs[id];
|
||||
return has(refs, id) && refs[id];
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -382,8 +406,11 @@ Scope.prototype.parentGet = function (id, decl) {
|
||||
*/
|
||||
|
||||
Scope.prototype.has = function (id, decl) {
|
||||
return (id && (this.hasOwn(id, decl) || this.parentHas(id, decl))) ||
|
||||
_.contains(Scope.defaultDeclarations, id);
|
||||
if (!id) return false;
|
||||
if (this.hasOwn(id, decl)) return true;
|
||||
if (this.parentHas(id, decl)) return true;
|
||||
if (contains(Scope.defaultDeclarations, id)) return true;
|
||||
return false;
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@@ -4,14 +4,14 @@ var toFastProperties = require("../helpers/to-fast-properties");
|
||||
var esutils = require("esutils");
|
||||
var object = require("../helpers/object");
|
||||
var Node = require("./node");
|
||||
var _ = require("lodash");
|
||||
var each = require("lodash/collection/each");
|
||||
var uniq = require("lodash/array/uniq");
|
||||
var compact = require("lodash/array/compact");
|
||||
var defaults = require("lodash/object/defaults");
|
||||
var isString = require("lodash/lang/isString");
|
||||
|
||||
var t = exports;
|
||||
|
||||
t.NATIVE_TYPE_NAMES = ["Array", "Object", "Number", "Boolean", "Date", "Array", "String"];
|
||||
|
||||
//
|
||||
|
||||
/**
|
||||
* Registers `is[Type]` and `assert[Type]` generated functions for a given `type`.
|
||||
* Pass `skipAliasCheck` to force it to directly compare `node.type` with `type`.
|
||||
@@ -34,8 +34,7 @@ function registerType(type, skipAliasCheck) {
|
||||
}
|
||||
|
||||
t.STATEMENT_OR_BLOCK_KEYS = ["consequent", "body"];
|
||||
|
||||
//
|
||||
t.NATIVE_TYPE_NAMES = ["Array", "Object", "Number", "Boolean", "Date", "Array", "String"];
|
||||
|
||||
t.VISITOR_KEYS = require("./visitor-keys");
|
||||
|
||||
@@ -43,24 +42,25 @@ t.ALIAS_KEYS = require("./alias-keys");
|
||||
|
||||
t.FLIPPED_ALIAS_KEYS = {};
|
||||
|
||||
_.each(t.VISITOR_KEYS, function (keys, type) {
|
||||
each(t.VISITOR_KEYS, function (keys, type) {
|
||||
registerType(type, true);
|
||||
});
|
||||
|
||||
_.each(t.ALIAS_KEYS, function (aliases, type) {
|
||||
_.each(aliases, function (alias) {
|
||||
each(t.ALIAS_KEYS, function (aliases, type) {
|
||||
each(aliases, function (alias) {
|
||||
var types = t.FLIPPED_ALIAS_KEYS[alias] = t.FLIPPED_ALIAS_KEYS[alias] || [];
|
||||
types.push(type);
|
||||
});
|
||||
});
|
||||
|
||||
_.each(t.FLIPPED_ALIAS_KEYS, function (types, type) {
|
||||
each(t.FLIPPED_ALIAS_KEYS, function (types, type) {
|
||||
t[type.toUpperCase() + "_TYPES"] = types;
|
||||
registerType(type, false);
|
||||
});
|
||||
|
||||
/**
|
||||
* Returns whether `node` is of given `type`.
|
||||
*
|
||||
* For better performance, use this instead of `is[Type]` when `type` is unknown.
|
||||
* Optionally, pass `skipAliasCheck` to directly compare `node.type` with `type`.
|
||||
*
|
||||
@@ -97,15 +97,15 @@ t.is = function (type, node, opts, skipAliasCheck) {
|
||||
|
||||
//
|
||||
|
||||
t.BUILDER_KEYS = _.defaults(require("./builder-keys"), t.VISITOR_KEYS);
|
||||
t.BUILDER_KEYS = defaults(require("./builder-keys"), t.VISITOR_KEYS);
|
||||
|
||||
_.each(t.BUILDER_KEYS, function (keys, type) {
|
||||
each(t.BUILDER_KEYS, function (keys, type) {
|
||||
t[type[0].toLowerCase() + type.slice(1)] = function () {
|
||||
var args = arguments;
|
||||
var node = new Node;
|
||||
node.start = null;
|
||||
node.type = type;
|
||||
_.each(keys, function (key, i) {
|
||||
each(keys, function (key, i) {
|
||||
node[key] = args[i];
|
||||
});
|
||||
return node;
|
||||
@@ -151,18 +151,21 @@ t.isFalsyExpression = function (node) {
|
||||
* declarations hoisted to the top of the current scope.
|
||||
*
|
||||
* Expression statements are just resolved to their standard expression.
|
||||
*
|
||||
* @param {Array} nodes
|
||||
* @param {Scope} scope
|
||||
*/
|
||||
|
||||
t.toSequenceExpression = function (nodes, scope) {
|
||||
var exprs = [];
|
||||
|
||||
_.each(nodes, function (node) {
|
||||
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) {
|
||||
each(node.declarations, function (declar) {
|
||||
scope.push({
|
||||
kind: node.kind,
|
||||
key: declar.id.name,
|
||||
@@ -180,17 +183,23 @@ t.toSequenceExpression = function (nodes, scope) {
|
||||
}
|
||||
};
|
||||
|
||||
//
|
||||
/*
|
||||
* Description
|
||||
*
|
||||
* @param {Object} actual
|
||||
* @param {Object} expected
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
|
||||
t.shallowEqual = function (actual, expected) {
|
||||
var keys = Object.keys(expected);
|
||||
var key;
|
||||
|
||||
for (var i = 0; i < keys.length; i++) {
|
||||
key = keys[i];
|
||||
var key = keys[i];
|
||||
|
||||
if (actual[key] !== expected[key])
|
||||
if (actual[key] !== expected[key]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
@@ -325,7 +334,7 @@ t.isReferenced = function (node, parent) {
|
||||
};
|
||||
|
||||
/**
|
||||
* Description
|
||||
* Check if the input `node` is an `Identifier` and `isReferenced`.
|
||||
*
|
||||
* @param {Object} node
|
||||
* @param {Object} parent
|
||||
@@ -337,14 +346,15 @@ t.isReferencedIdentifier = function (node, parent) {
|
||||
};
|
||||
|
||||
/**
|
||||
* Description
|
||||
* Check if the input `name` is a valid identifier name
|
||||
* and isn't a reserved word.
|
||||
*
|
||||
* @param {String} name
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
|
||||
t.isValidIdentifier = function (name) {
|
||||
return _.isString(name) && esutils.keyword.isIdentifierName(name) && !esutils.keyword.isReservedWordES6(name, true);
|
||||
return isString(name) && esutils.keyword.isIdentifierName(name) && !esutils.keyword.isReservedWordES6(name, true);
|
||||
};
|
||||
|
||||
/*
|
||||
@@ -389,6 +399,60 @@ t.ensureBlock = function (node, key) {
|
||||
node[key] = t.toBlock(node[key], node);
|
||||
};
|
||||
|
||||
/**
|
||||
* Build a function that when called will return whether or not the
|
||||
* input `node` `MemberExpression` matches the input `match`.
|
||||
*
|
||||
* For example, given the match `React.createClass` it would match the
|
||||
* parsed nodes of `React.createClass` and `React["createClass"]`.
|
||||
*
|
||||
* @param {String} match Dot delimetered string
|
||||
* @returns {Function}
|
||||
*/
|
||||
|
||||
t.buildMatchMemberExpression = function (match) {
|
||||
var parts = match.split(".");
|
||||
|
||||
return function (member) {
|
||||
// not a member expression
|
||||
if (!t.isMemberExpression(member)) return false;
|
||||
|
||||
var search = [member];
|
||||
var i = 0;
|
||||
|
||||
while (search.length) {
|
||||
var node = search.shift();
|
||||
|
||||
if (t.isIdentifier(node)) {
|
||||
// this part doesn't match
|
||||
if (parts[i] !== node.name) return false;
|
||||
} else if (t.isLiteral(node)) {
|
||||
// this part doesn't match
|
||||
if (parts[i] !== node.value) return false;
|
||||
} else if (t.isMemberExpression(node)) {
|
||||
if (node.computed && !t.isLiteral(node.property)) {
|
||||
// we can't deal with this
|
||||
return false;
|
||||
} else {
|
||||
search.push(node.object);
|
||||
search.push(node.property);
|
||||
continue;
|
||||
}
|
||||
} else {
|
||||
// we can't deal with this
|
||||
return false;
|
||||
}
|
||||
|
||||
// too many parts
|
||||
if (++i > parts.length) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* Description
|
||||
*
|
||||
@@ -497,12 +561,10 @@ t.toBlock = function (node, parent) {
|
||||
* declaration then it's assumed to be an assignable.
|
||||
*
|
||||
* @param {Object} node
|
||||
* @param {Boolean} [map]
|
||||
* @param {Array} [ignoreTypes]
|
||||
* @returns {Array|Object}
|
||||
*/
|
||||
|
||||
t.getIds = function (node, map, ignoreTypes) {
|
||||
t.getDeclarations = function (node) {
|
||||
var search = [].concat(node);
|
||||
var ids = object();
|
||||
|
||||
@@ -510,13 +572,7 @@ t.getIds = function (node, map, ignoreTypes) {
|
||||
var id = search.shift();
|
||||
if (!id) continue;
|
||||
|
||||
// blacklist types
|
||||
if (ignoreTypes && ignoreTypes.indexOf(id.type) >= 0) continue;
|
||||
|
||||
var nodeKeys = t.getIds.nodes[id.type];
|
||||
var arrKeys = t.getIds.arrays[id.type];
|
||||
|
||||
var i, key;
|
||||
var keys = t.getDeclarations.keys[id.type];
|
||||
|
||||
if (t.isIdentifier(id)) {
|
||||
ids[id.name] = id;
|
||||
@@ -524,27 +580,18 @@ t.getIds = function (node, map, ignoreTypes) {
|
||||
if (t.isDeclaration(node.declaration)) {
|
||||
search.push(node.declaration);
|
||||
}
|
||||
} else if (nodeKeys) {
|
||||
for (i = 0; i < nodeKeys.length; i++) {
|
||||
key = nodeKeys[i];
|
||||
if (id[key]) {
|
||||
search.push(id[key]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else if (arrKeys) {
|
||||
for (i = 0; i < arrKeys.length; i++) {
|
||||
key = arrKeys[i];
|
||||
} else if (keys) {
|
||||
for (var i = 0; i < keys.length; i++) {
|
||||
var key = keys[i];
|
||||
search = search.concat(id[key] || []);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!map) ids = _.keys(ids);
|
||||
return ids;
|
||||
};
|
||||
|
||||
t.getIds.nodes = {
|
||||
t.getDeclarations.keys = {
|
||||
AssignmentExpression: ["left"],
|
||||
ImportBatchSpecifier: ["name"],
|
||||
ImportSpecifier: ["name", "id"],
|
||||
@@ -557,10 +604,7 @@ t.getIds.nodes = {
|
||||
RestElement: ["argument"],
|
||||
Property: ["value"],
|
||||
ComprehensionBlock: ["left"],
|
||||
AssignmentPattern: ["left"]
|
||||
};
|
||||
|
||||
t.getIds.arrays = {
|
||||
AssignmentPattern: ["left"],
|
||||
PrivateDeclaration: ["declarations"],
|
||||
ComprehensionExpression: ["blocks"],
|
||||
ImportDeclaration: ["specifiers"],
|
||||
@@ -614,7 +658,7 @@ t.COMMENT_KEYS = ["leadingComments", "trailingComments"];
|
||||
*/
|
||||
|
||||
t.removeComments = function (child) {
|
||||
_.each(t.COMMENT_KEYS, function (key) {
|
||||
each(t.COMMENT_KEYS, function (key) {
|
||||
delete child[key];
|
||||
});
|
||||
return child;
|
||||
@@ -629,8 +673,8 @@ t.removeComments = function (child) {
|
||||
*/
|
||||
|
||||
t.inheritsComments = function (child, parent) {
|
||||
_.each(t.COMMENT_KEYS, function (key) {
|
||||
child[key] = _.uniq(_.compact([].concat(child[key], parent[key])));
|
||||
each(t.COMMENT_KEYS, function (key) {
|
||||
child[key] = uniq(compact([].concat(child[key], parent[key])));
|
||||
});
|
||||
return child;
|
||||
};
|
||||
|
||||
@@ -5,25 +5,36 @@ require("./patch");
|
||||
var estraverse = require("estraverse");
|
||||
var codeFrame = require("./helpers/code-frame");
|
||||
var traverse = require("./traverse");
|
||||
var debug = require("debug/node");
|
||||
var acorn = require("acorn-6to5");
|
||||
var path = require("path");
|
||||
var util = require("util");
|
||||
var fs = require("fs");
|
||||
var t = require("./types");
|
||||
var _ = require("lodash");
|
||||
var each = require("lodash/collection/each");
|
||||
var isNumber = require("lodash/lang/isNumber");
|
||||
var isString = require("lodash/lang/isString");
|
||||
var isRegExp = require("lodash/lang/isRegExp");
|
||||
var isEmpty = require("lodash/lang/isEmpty");
|
||||
var clone = require("lodash/lang/clone");
|
||||
var cloneDeep = require("lodash/lang/cloneDeep");
|
||||
var has = require("lodash/object/has");
|
||||
var contains = require("lodash/collection/contains");
|
||||
|
||||
exports.inherits = util.inherits;
|
||||
|
||||
exports.debug = debug("6to5");
|
||||
|
||||
exports.canCompile = function (filename, altExts) {
|
||||
var exts = altExts || exports.canCompile.EXTENSIONS;
|
||||
var ext = path.extname(filename);
|
||||
return _.contains(exts, ext);
|
||||
return contains(exts, ext);
|
||||
};
|
||||
|
||||
exports.canCompile.EXTENSIONS = [".js", ".jsx", ".es6", ".es"];
|
||||
|
||||
exports.isInteger = function (i) {
|
||||
return _.isNumber(i) && i % 1 === 0;
|
||||
return isNumber(i) && i % 1 === 0;
|
||||
};
|
||||
|
||||
exports.resolve = function (loc) {
|
||||
@@ -45,14 +56,14 @@ exports.list = function (val) {
|
||||
exports.regexify = function (val) {
|
||||
if (!val) return new RegExp(/.^/);
|
||||
if (Array.isArray(val)) val = val.join("|");
|
||||
if (_.isString(val)) return new RegExp(val);
|
||||
if (_.isRegExp(val)) return val;
|
||||
if (isString(val)) return new RegExp(val);
|
||||
if (isRegExp(val)) return val;
|
||||
throw new TypeError("illegal type for regexify");
|
||||
};
|
||||
|
||||
exports.arrayify = function (val) {
|
||||
if (!val) return [];
|
||||
if (_.isString(val)) return exports.list(val);
|
||||
if (isString(val)) return exports.list(val);
|
||||
if (Array.isArray(val)) return val;
|
||||
throw new TypeError("illegal type for arrayify");
|
||||
};
|
||||
@@ -70,7 +81,7 @@ exports.sourceMapToComment = function (map) {
|
||||
return "//# sourceMappingURL=data:application/json;base64," + base64;
|
||||
};
|
||||
|
||||
exports.pushMutatorMap = function (mutatorMap, key, kind, computed, method) {
|
||||
exports.pushMutatorMap = function (mutatorMap, key, kind, computed, value) {
|
||||
var alias;
|
||||
|
||||
if (t.isIdentifier(key)) {
|
||||
@@ -79,11 +90,11 @@ exports.pushMutatorMap = function (mutatorMap, key, kind, computed, method) {
|
||||
} else if (t.isLiteral(key)) {
|
||||
alias = String(key.value);
|
||||
} else {
|
||||
alias = JSON.stringify(traverse.removeProperties(_.cloneDeep(key)));
|
||||
alias = JSON.stringify(traverse.removeProperties(cloneDeep(key)));
|
||||
}
|
||||
|
||||
var map;
|
||||
if (_.has(mutatorMap, alias)) {
|
||||
if (has(mutatorMap, alias)) {
|
||||
map = mutatorMap[alias];
|
||||
} else {
|
||||
map = {};
|
||||
@@ -95,13 +106,13 @@ exports.pushMutatorMap = function (mutatorMap, key, kind, computed, method) {
|
||||
map._computed = true;
|
||||
}
|
||||
|
||||
map[kind] = method;
|
||||
map[kind] = value;
|
||||
};
|
||||
|
||||
exports.buildDefineProperties = function (mutatorMap) {
|
||||
var objExpr = t.objectExpression([]);
|
||||
|
||||
_.each(mutatorMap, function (map) {
|
||||
each(mutatorMap, function (map) {
|
||||
var mapNode = t.objectExpression([]);
|
||||
|
||||
var propNode = t.property("init", map._key, mapNode, map._computed);
|
||||
@@ -110,13 +121,18 @@ exports.buildDefineProperties = function (mutatorMap) {
|
||||
map.writable = t.literal(true);
|
||||
}
|
||||
|
||||
map.enumerable = t.literal(true);
|
||||
if (map.enumerable === false) {
|
||||
delete map.enumerable;
|
||||
} else {
|
||||
map.enumerable = t.literal(true);
|
||||
}
|
||||
|
||||
map.configurable = t.literal(true);
|
||||
|
||||
_.each(map, function (node, key) {
|
||||
each(map, function (node, key) {
|
||||
if (key[0] === "_") return;
|
||||
|
||||
node = _.clone(node);
|
||||
node = clone(node);
|
||||
var inheritNode = node;
|
||||
if (t.isMethodDefinition(node)) node = node.value;
|
||||
|
||||
@@ -134,7 +150,7 @@ exports.buildDefineProperties = function (mutatorMap) {
|
||||
|
||||
var templateVisitor = {
|
||||
enter: function (node, parent, scope, context, nodes) {
|
||||
if (t.isIdentifier(node) && _.has(nodes, node.name)) {
|
||||
if (t.isIdentifier(node) && has(nodes, node.name)) {
|
||||
return nodes[node.name];
|
||||
}
|
||||
}
|
||||
@@ -149,9 +165,9 @@ exports.template = function (name, nodes, keepExpression) {
|
||||
nodes = null;
|
||||
}
|
||||
|
||||
template = _.cloneDeep(template);
|
||||
template = cloneDeep(template);
|
||||
|
||||
if (!_.isEmpty(nodes)) {
|
||||
if (!isEmpty(nodes)) {
|
||||
traverse(template, templateVisitor, null, nodes);
|
||||
}
|
||||
|
||||
@@ -193,7 +209,7 @@ exports.parse = function (opts, code, callback) {
|
||||
allowReturnOutsideFunction: true,
|
||||
ecmaVersion: opts.experimental ? 7 : 6,
|
||||
playground: opts.playground,
|
||||
strictMode: true,
|
||||
strictMode: opts.strictMode,
|
||||
onComment: comments,
|
||||
locations: true,
|
||||
onToken: tokens,
|
||||
@@ -243,7 +259,7 @@ var loadTemplates = function () {
|
||||
"https://github.com/6to5/6to5/issues");
|
||||
}
|
||||
|
||||
_.each(fs.readdirSync(templatesLoc), function (name) {
|
||||
each(fs.readdirSync(templatesLoc), function (name) {
|
||||
if (name[0] === ".") return;
|
||||
|
||||
var key = path.basename(name, path.extname(name));
|
||||
|
||||
14
package.json
14
package.json
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "6to5",
|
||||
"description": "Turn ES6 code into readable vanilla ES5 with source maps",
|
||||
"version": "3.0.11",
|
||||
"version": "3.3.2",
|
||||
"author": "Sebastian McKenzie <sebmck@gmail.com>",
|
||||
"homepage": "https://6to5.org/",
|
||||
"repository": "6to5/6to5",
|
||||
@@ -9,7 +9,8 @@
|
||||
"main": "lib/6to5/index.js",
|
||||
"bin": {
|
||||
"6to5": "./bin/6to5/index.js",
|
||||
"6to5-node": "./bin/6to5-node"
|
||||
"6to5-node": "./bin/6to5-node",
|
||||
"6to5-runtime": "./bin/6to5-runtime"
|
||||
},
|
||||
"browser": {
|
||||
"./lib/6to5/index.js": "./lib/6to5/browser.js",
|
||||
@@ -38,25 +39,28 @@
|
||||
"chokidar": "0.12.6",
|
||||
"commander": "2.6.0",
|
||||
"core-js": "^0.4.9",
|
||||
"debug": "^2.1.1",
|
||||
"detect-indent": "3.0.0",
|
||||
"estraverse": "1.9.1",
|
||||
"esutils": "1.1.6",
|
||||
"esvalid": "1.1.0",
|
||||
"fs-readdir-recursive": "0.1.0",
|
||||
"globals": "^5.1.0",
|
||||
"js-tokenizer": "1.3.3",
|
||||
"lodash": "3.0.0",
|
||||
"output-file-sync": "1.1.0",
|
||||
"private": "0.1.6",
|
||||
"regenerator-6to5": "0.8.9-6",
|
||||
"regenerator-6to5": "0.8.9-8",
|
||||
"regexpu": "1.1.0",
|
||||
"roadrunner": "1.0.4",
|
||||
"source-map": "0.1.43",
|
||||
"source-map-support": "0.2.9",
|
||||
"supports-color": "1.2.0"
|
||||
"supports-color": "1.2.0",
|
||||
"useragent": "^2.1.5"
|
||||
},
|
||||
"devDependencies": {
|
||||
"browserify": "8.1.1",
|
||||
"chai": "1.10.0",
|
||||
"esvalid": "1.1.0",
|
||||
"istanbul": "0.3.5",
|
||||
"jscs": "1.10.0",
|
||||
"jshint": "2.6.0",
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "6to5-runtime",
|
||||
"description": "6to5 selfContained runtime",
|
||||
"version": "3.0.10",
|
||||
"version": "3.3.1",
|
||||
"repository": "6to5/6to5",
|
||||
"author": "Sebastian McKenzie <sebmck@gmail.com>"
|
||||
}
|
||||
@@ -1,11 +1,10 @@
|
||||
"use strict";
|
||||
|
||||
var transform = require("../lib/6to5/transformation/transform");
|
||||
var File = require("../lib/6to5/file");
|
||||
var util = require("../lib/6to5/util");
|
||||
var fs = require("fs");
|
||||
var t = require("../lib/6to5/types");
|
||||
var _ = require("lodash");
|
||||
var buildHelpers = require("../lib/6to5/build-helpers");
|
||||
var transform = require("../lib/6to5/transformation");
|
||||
var fs = require("fs");
|
||||
var t = require("../lib/6to5/types");
|
||||
var _ = require("lodash");
|
||||
|
||||
var relative = function (filename) {
|
||||
return __dirname + "/6to5-runtime/" + filename;
|
||||
@@ -31,23 +30,18 @@ var updatePackage = function () {
|
||||
writeFile("package.json", JSON.stringify(pkg, null, 2));
|
||||
};
|
||||
|
||||
var buildHelpers = function () {
|
||||
var buildHelpers2 = function () {
|
||||
var body = [];
|
||||
var tree = t.program(body);
|
||||
|
||||
_.each(File.helpers, function (name) {
|
||||
var key = t.identifier(t.toIdentifier(name));
|
||||
body.push(t.expressionStatement(
|
||||
t.assignmentExpression("=", t.memberExpression(t.identifier("exports"), key), util.template(name))
|
||||
));
|
||||
});
|
||||
buildHelpers(body, t.identifier("exports"));
|
||||
|
||||
return transform.fromAst(tree, null, {
|
||||
optional: ["selfContained"]
|
||||
}).code;
|
||||
};
|
||||
|
||||
writeFile("helpers.js", buildHelpers());
|
||||
writeFile("helpers.js", buildHelpers2());
|
||||
writeFile("core-js.js", readFile("core-js/library"));
|
||||
writeFile("regenerator/index.js", readFile("regenerator-6to5/runtime-module"));
|
||||
writeFile("regenerator/runtime.js", readFile("regenerator-6to5/runtime"));
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
var util = require("../lib/6to5/util");
|
||||
var path = require("path");
|
||||
var fs = require("fs");
|
||||
var _ = require("lodash");
|
||||
@@ -17,6 +18,13 @@ var readFile = exports.readFile = function (filename) {
|
||||
}
|
||||
};
|
||||
|
||||
exports.assertVendor = function (name) {
|
||||
if (!fs.existsSync(__dirname + "/../vendor/" + name)) {
|
||||
console.error("No vendor/" + name + " - run `make bootstrap`");
|
||||
process.exit(1);
|
||||
}
|
||||
};
|
||||
|
||||
exports.get = function (entryName, entryLoc) {
|
||||
if (exports.cache[entryName]) return exports.cache[entryName];
|
||||
|
||||
@@ -34,8 +42,8 @@ exports.get = function (entryName, entryLoc) {
|
||||
};
|
||||
suites.push(suite);
|
||||
|
||||
var suiteOptsLoc = suite.filename + "/options.json";
|
||||
if (fs.existsSync(suiteOptsLoc)) suite.options = require(suiteOptsLoc);
|
||||
var suiteOptsLoc = util.resolve(suite.filename + "/options");
|
||||
if (suiteOptsLoc) suite.options = require(suiteOptsLoc);
|
||||
|
||||
if (fs.statSync(suite.filename).isFile()) {
|
||||
push(suiteName, suite.filename);
|
||||
@@ -71,8 +79,8 @@ exports.get = function (entryName, entryLoc) {
|
||||
sourceMapName: expectLocAlias
|
||||
}, _.cloneDeep(suite.options));
|
||||
|
||||
var taskOptsLoc = taskDir + "/options.json";
|
||||
if (fs.existsSync(taskOptsLoc)) _.merge(taskOpts, require(taskOptsLoc));
|
||||
var taskOptsLoc = util.resolve(taskDir + "/options");
|
||||
if (taskOptsLoc) _.merge(taskOpts, require(taskOptsLoc));
|
||||
|
||||
var test = {
|
||||
title: humanise(taskName, true),
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
var genHelpers = require("./_generator-helpers");
|
||||
var transform = require("../lib/6to5/transformation/transform");
|
||||
var transform = require("../lib/6to5/transformation");
|
||||
var sourceMap = require("source-map");
|
||||
var codeFrame = require("../lib/6to5/helpers/code-frame");
|
||||
var esvalid = require("esvalid");
|
||||
@@ -125,12 +125,14 @@ var run = function (task, done) {
|
||||
module.exports = function (suiteOpts, taskOpts, dynamicOpts) {
|
||||
taskOpts = taskOpts || {};
|
||||
|
||||
require("../register")(taskOpts);
|
||||
|
||||
_.each(helper.get(suiteOpts.name, suiteOpts.loc), function (testSuite) {
|
||||
if (_.contains(suiteOpts.ignoreSuites, testSuite.title)) return;
|
||||
|
||||
suite(suiteOpts.name + "/" + testSuite.title, function () {
|
||||
setup(function () {
|
||||
require("../register")(taskOpts);
|
||||
});
|
||||
|
||||
_.each(testSuite.tests, function (task) {
|
||||
if (_.contains(suiteOpts.ignoreTasks, task.title) || _.contains(suiteOpts.ignoreTasks, testSuite.title + "/" + task.title)) return;
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
var transform = require("../lib/6to5/transformation/transform");
|
||||
var transform = require("../lib/6to5/transformation");
|
||||
var assert = require("assert");
|
||||
var File = require("../lib/6to5/file");
|
||||
|
||||
|
||||
140
test/detection.js
Normal file
140
test/detection.js
Normal file
@@ -0,0 +1,140 @@
|
||||
var detect = require("../lib/6to5/detection");
|
||||
var assert = require("assert");
|
||||
var util = require("../lib/6to5/util");
|
||||
|
||||
suite("detection", function () {
|
||||
var checkSyntax = function (code, name) {
|
||||
var ast = util.parse({
|
||||
experimental: true
|
||||
}, code);
|
||||
|
||||
assert.ok(detect(ast).syntax[name]);
|
||||
};
|
||||
|
||||
test("es5.properties.mutators", function () {
|
||||
checkSyntax("var obj = { get foo() {} };", "es5.properties.mutators");
|
||||
checkSyntax("var obj = { set foo() {} };", "es5.properties.mutators");
|
||||
});
|
||||
|
||||
test("es6.exponentation", function () {
|
||||
checkSyntax("x ** 2;", "es6.exponentation");
|
||||
checkSyntax("x **= 2;", "es6.exponentation");
|
||||
});
|
||||
|
||||
test("es6.blockScoping", function () {
|
||||
checkSyntax("let foo;", "es6.blockScoping");
|
||||
checkSyntax("let foo = bar;", "es6.blockScoping");
|
||||
checkSyntax("const foo = bar;", "es6.blockScoping");
|
||||
});
|
||||
|
||||
test("es6.constants", function () {
|
||||
checkSyntax("const foo = bar;", "es6.constants");
|
||||
});
|
||||
|
||||
test("es6.properties.shorthand", function () {
|
||||
checkSyntax("var obj = { foo };", "es6.properties.shorthand");
|
||||
checkSyntax("var obj = { foo };", "es6.properties.shorthand");
|
||||
});
|
||||
|
||||
test("es6.properties.computed", function () {
|
||||
checkSyntax("var obj = { [foo]: bar };", "es6.properties.computed");
|
||||
checkSyntax("var obj = { ['foo']: bar };", "es6.properties.computed");
|
||||
});
|
||||
|
||||
test("es6.parameters.default", function () {
|
||||
checkSyntax("var obj = (foo = bar) => {};", "es6.parameters.default");
|
||||
checkSyntax("var obj = function (foo = bar) {};", "es6.parameters.default");
|
||||
checkSyntax("function foo(foo = bar) {}", "es6.parameters.default");
|
||||
});
|
||||
|
||||
test("es6.arrowFunctions", function () {
|
||||
checkSyntax("var foo = x => x;", "es6.arrowFunctions");
|
||||
checkSyntax("var foo = x => { return x * x };", "es6.arrowFunctions");
|
||||
checkSyntax("var foo = (x) => x;", "es6.arrowFunctions");
|
||||
checkSyntax("var foo = (a, b) => { return a * b };", "es6.arrowFunctions");
|
||||
});
|
||||
|
||||
test("es6.classes", function () {
|
||||
checkSyntax("class Foo {}", "es6.classes");
|
||||
checkSyntax("var Foo = class {};", "es6.classes");
|
||||
});
|
||||
|
||||
test("es6.forOf", function () {
|
||||
checkSyntax("for (var val of foo);", "es6.forOf");
|
||||
checkSyntax("for (val of foo);", "es6.forOf");
|
||||
});
|
||||
|
||||
test("es6.modules", function () {
|
||||
checkSyntax("import 'foo';", "es6.modules");
|
||||
checkSyntax("import foo from 'foo';", "es6.modules");
|
||||
checkSyntax("import * as foo from 'foo';", "es6.modules");
|
||||
checkSyntax("import { foo } from 'foo';", "es6.modules");
|
||||
checkSyntax("export { foo } from 'foo';", "es6.modules");
|
||||
checkSyntax("export var foo = 5;", "es6.modules");
|
||||
checkSyntax("export class Foo {}", "es6.modules");
|
||||
checkSyntax("export function foo() {}", "es6.modules");
|
||||
checkSyntax("export default class Foo {}", "es6.modules");
|
||||
checkSyntax("export default function foo() {}", "es6.modules");
|
||||
});
|
||||
|
||||
test("es6.destructuring", function () {
|
||||
checkSyntax("[a, b] = [];", "es6.destructuring");
|
||||
checkSyntax("var [a, b] = [];", "es6.destructuring");
|
||||
checkSyntax("({ a, b }) = {};", "es6.destructuring");
|
||||
checkSyntax("var { a, b } = {};", "es6.destructuring");
|
||||
checkSyntax("function foo(foo = bar) {}", "es6.destructuring");
|
||||
});
|
||||
|
||||
test("es6.parameters.rest", function () {
|
||||
checkSyntax("function foo(...items) {}", "es6.parameters.rest");
|
||||
checkSyntax("var foo = (...items) => {}", "es6.parameters.rest");
|
||||
});
|
||||
|
||||
test("es6.spread", function () {
|
||||
checkSyntax("new Foo(...items);", "es6.spread");
|
||||
checkSyntax("foo(...items);", "es6.spread");
|
||||
checkSyntax("[...items];", "es6.spread");
|
||||
});
|
||||
|
||||
test("es6.templateLiterals", function () {
|
||||
checkSyntax("`foobar`;", "es6.templateLiterals");
|
||||
checkSyntax("foobar`foobar`;", "es6.templateLiterals");
|
||||
});
|
||||
|
||||
test("es6.generators", function () {
|
||||
checkSyntax("function* foo() {}", "es6.generators");
|
||||
checkSyntax("var foo = function* () {};", "es6.generators");
|
||||
});
|
||||
|
||||
test("es7.asyncFunctions", function () {
|
||||
checkSyntax("async function foo() {}", "es7.asyncFunctions");
|
||||
checkSyntax("var foo = async function() {};", "es7.asyncFunctions");
|
||||
checkSyntax("var foo = async () => {};", "es7.asyncFunctions");
|
||||
});
|
||||
|
||||
test("es7.comprehensions", function () {
|
||||
checkSyntax("[for (i of test) i]", "es7.comprehensions");
|
||||
checkSyntax("(for (i of test) i)", "es7.comprehensions");
|
||||
});
|
||||
|
||||
test("es7.objectSpread", function () {
|
||||
checkSyntax("var foo = { ...bar };", "es7.objectSpread");
|
||||
});
|
||||
|
||||
test("es7.abstractReferences", function () {
|
||||
checkSyntax("class Foo { private A; }", "es7.abstractReferences");
|
||||
checkSyntax("foo::bar();", "es7.abstractReferences");
|
||||
checkSyntax("delete foo::bar;", "es7.abstractReferences");
|
||||
checkSyntax("foo::bar;", "es7.abstractReferences");
|
||||
checkSyntax("foo::bar = baz;", "es7.abstractReferences");
|
||||
});
|
||||
|
||||
test("flow", function () {
|
||||
|
||||
});
|
||||
|
||||
test("jsx", function () {
|
||||
checkSyntax("<div />", "jsx");
|
||||
checkSyntax("<Element />", "jsx");
|
||||
});
|
||||
});
|
||||
@@ -17,4 +17,4 @@ for (var key in point) {
|
||||
}
|
||||
|
||||
assert.equal(point.toString(), '(1, 2)');
|
||||
assert.deepEqual(keys.sort(), ['toString', 'x', 'y']);
|
||||
assert.deepEqual(keys.sort(), ['x', 'y']);
|
||||
|
||||
@@ -2,6 +2,14 @@ class Base {
|
||||
get sound() {
|
||||
return 'I am a ' + this.type + '.';
|
||||
}
|
||||
|
||||
get name() {
|
||||
return this._name;
|
||||
}
|
||||
|
||||
set name(val) {
|
||||
this._name = val;
|
||||
}
|
||||
}
|
||||
|
||||
class Animal extends Base {}
|
||||
@@ -12,6 +20,18 @@ class Cat extends Animal {
|
||||
get sound() {
|
||||
return super.sound + ' MEOW!';
|
||||
}
|
||||
|
||||
get name() {
|
||||
return super.name;
|
||||
}
|
||||
|
||||
set name(val) {
|
||||
super.name = val + ' Cat';
|
||||
}
|
||||
}
|
||||
|
||||
assert.equal(new Cat().sound, 'I am a cat. MEOW!');
|
||||
var cat = new Cat();
|
||||
|
||||
assert.equal(cat.sound, 'I am a cat. MEOW!');
|
||||
cat.name = 'Nyan';
|
||||
assert.equal(cat.name, 'Nyan Cat');
|
||||
|
||||
@@ -25,4 +25,4 @@ var forLoopProperties = [];
|
||||
for (var key in mazer) {
|
||||
forLoopProperties.push(key);
|
||||
}
|
||||
assert.ok(forLoopProperties.indexOf('name') >= 0, 'getters/setters are enumerable');
|
||||
assert.ok(forLoopProperties.indexOf('name') === -1, 'getters/setters should be unenumerable');
|
||||
|
||||
3
test/fixtures/transformation/api/resolve-module-source/actual.js
vendored
Normal file
3
test/fixtures/transformation/api/resolve-module-source/actual.js
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
export { foo } from "foo-export-named";
|
||||
import foo from "foo-import-default";
|
||||
import "foo-import-bare";
|
||||
5
test/fixtures/transformation/api/resolve-module-source/expected.js
vendored
Normal file
5
test/fixtures/transformation/api/resolve-module-source/expected.js
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
"use strict";
|
||||
|
||||
export { foo } from "resolved/foo-export-named";
|
||||
import foo from "resolved/foo-import-default";
|
||||
import "resolved/foo-import-bare";
|
||||
5
test/fixtures/transformation/api/resolve-module-source/options.js
vendored
Normal file
5
test/fixtures/transformation/api/resolve-module-source/options.js
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
exports.blacklist = ["es6.modules"];
|
||||
|
||||
exports.resolveModuleSource = function (originalSource) {
|
||||
return "resolved/" + originalSource;
|
||||
};
|
||||
@@ -1,6 +1,6 @@
|
||||
"use strict";
|
||||
|
||||
var _asyncToGenerator = function (fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { var callNext = step.bind(gen.next); var callThrow = step.bind(gen["throw"]); function step(arg) { try { var info = this(arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(callNext, callThrow); } } callNext(); }); }; };
|
||||
var _asyncToGenerator = function (fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { var callNext = step.bind(null, "next"); var callThrow = step.bind(null, "throw"); function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(callNext, callThrow); } } callNext(); }); }; };
|
||||
|
||||
var _prototypeProperties = function (child, staticProps, instanceProps) { if (staticProps) Object.defineProperties(child, staticProps); if (instanceProps) Object.defineProperties(child.prototype, instanceProps); };
|
||||
|
||||
@@ -13,10 +13,9 @@ var Foo = (function () {
|
||||
var wat = yield bar();
|
||||
}),
|
||||
writable: true,
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
}
|
||||
});
|
||||
|
||||
return Foo;
|
||||
})();
|
||||
})();
|
||||
@@ -1,7 +1,7 @@
|
||||
"use strict";
|
||||
|
||||
var _asyncToGenerator = function (fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { var callNext = step.bind(gen.next); var callThrow = step.bind(gen["throw"]); function step(arg) { try { var info = this(arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(callNext, callThrow); } } callNext(); }); }; };
|
||||
var _asyncToGenerator = function (fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { var callNext = step.bind(null, "next"); var callThrow = step.bind(null, "throw"); function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(callNext, callThrow); } } callNext(); }); }; };
|
||||
|
||||
var foo = _asyncToGenerator(function* () {
|
||||
var wat = yield bar();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
"use strict";
|
||||
|
||||
var _asyncToGenerator = function (fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { var callNext = step.bind(gen.next); var callThrow = step.bind(gen["throw"]); function step(arg) { try { var info = this(arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(callNext, callThrow); } } callNext(); }); }; };
|
||||
var _asyncToGenerator = function (fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { var callNext = step.bind(null, "next"); var callThrow = step.bind(null, "throw"); function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(callNext, callThrow); } } callNext(); }); }; };
|
||||
|
||||
var foo = _asyncToGenerator(function* () {
|
||||
var wat = yield bar();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -13,10 +13,9 @@ var Foo = (function () {
|
||||
var wat = yield bar();
|
||||
}),
|
||||
writable: true,
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
}
|
||||
});
|
||||
|
||||
return Foo;
|
||||
})();
|
||||
})();
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user