Compare commits
135 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0953c48620 | ||
|
|
056b90831d | ||
|
|
8ba276b209 | ||
|
|
8eaa9d29e3 | ||
|
|
6212791356 | ||
|
|
d436d32d82 | ||
|
|
2a29f70bba | ||
|
|
6ccb8957bd | ||
|
|
dc45415ee0 | ||
|
|
98ca541fde | ||
|
|
8328f638c2 | ||
|
|
5586ce280f | ||
|
|
0ca71f5e15 | ||
|
|
212776e220 | ||
|
|
4a95a9ec8f | ||
|
|
45953ffc8a | ||
|
|
9c79290bb2 | ||
|
|
cfdf6b7385 | ||
|
|
907e0e0f86 | ||
|
|
f3bd9cbcb8 | ||
|
|
3bd14f9e07 | ||
|
|
102b4b3d1e | ||
|
|
6564f1ff76 | ||
|
|
58d7a5e069 | ||
|
|
f5db53cebe | ||
|
|
2da010fcae | ||
|
|
52b99bdf93 | ||
|
|
9d7e953451 | ||
|
|
b1252b865b | ||
|
|
068b1341d9 | ||
|
|
20eb143915 | ||
|
|
1302a86bf2 | ||
|
|
b7831f1d7b | ||
|
|
e46cabb21f | ||
|
|
fff06a047e | ||
|
|
6b0320fc83 | ||
|
|
5dde63fa0a | ||
|
|
672118149a | ||
|
|
5e0236d9a7 | ||
|
|
5101664e7d | ||
|
|
2d684a06d3 | ||
|
|
e31bad8f42 | ||
|
|
644b4373fc | ||
|
|
62f37c1e62 | ||
|
|
fc0e89463b | ||
|
|
56b6a795a4 | ||
|
|
17b34a2959 | ||
|
|
c4da0253c5 | ||
|
|
5f2df40f6d | ||
|
|
7d20a9b882 | ||
|
|
ee0ac9f149 | ||
|
|
c5d3f42d8a | ||
|
|
d162919a53 | ||
|
|
d1f712344d | ||
|
|
e985912b54 | ||
|
|
97a21e4a34 | ||
|
|
d8fb7812f7 | ||
|
|
bfe63f5885 | ||
|
|
44ca6873a3 | ||
|
|
2f66ea7338 | ||
|
|
70967cf53c | ||
|
|
dc117a74ae | ||
|
|
b0f32a822d | ||
|
|
fd99ea1749 | ||
|
|
572129542d | ||
|
|
6f53980bfc | ||
|
|
348af1990d | ||
|
|
bd5f350728 | ||
|
|
958fa282f4 | ||
|
|
31e6b18346 | ||
|
|
d17ac92a3f | ||
|
|
188bcb70f7 | ||
|
|
b8bd11a0e7 | ||
|
|
45bc74efe7 | ||
|
|
7eb169a894 | ||
|
|
eed4f312d8 | ||
|
|
44e4dc970f | ||
|
|
5d32432e67 | ||
|
|
9c9af6dbbd | ||
|
|
70cd650e10 | ||
|
|
c7bb00d58d | ||
|
|
623be068c4 | ||
|
|
71f5c9791d | ||
|
|
3e6e86d073 | ||
|
|
a0fb398ca2 | ||
|
|
71b9f19e6a | ||
|
|
c45ce58f0c | ||
|
|
d32f587e3c | ||
|
|
9e1874ba89 | ||
|
|
35fdc5c5d8 | ||
|
|
a4035fc257 | ||
|
|
42e3dc7a70 | ||
|
|
cdccf24515 | ||
|
|
c8cd5c108b | ||
|
|
2b95b876e1 | ||
|
|
f801772fc2 | ||
|
|
8ad678e5bc | ||
|
|
35c49dbef7 | ||
|
|
119314df74 | ||
|
|
51e336b037 | ||
|
|
7d446807a9 | ||
|
|
8afdeaf557 | ||
|
|
4df50954a2 | ||
|
|
70b6317865 | ||
|
|
5ebdc44297 | ||
|
|
a4659fd239 | ||
|
|
19bfa4a35b | ||
|
|
f7fff7d35e | ||
|
|
130e0ebe6b | ||
|
|
69c836fc8d | ||
|
|
f62a3ef394 | ||
|
|
7a6e568940 | ||
|
|
65998c3437 | ||
|
|
160de340b0 | ||
|
|
76ca40f698 | ||
|
|
01ed824b5c | ||
|
|
6b0dbc4486 | ||
|
|
2648268f30 | ||
|
|
8cf5bf7037 | ||
|
|
60961bc3ff | ||
|
|
df16bc17e7 | ||
|
|
5337ab5a08 | ||
|
|
2eaeebcce4 | ||
|
|
ad71010144 | ||
|
|
864169c1eb | ||
|
|
930d7f9aa6 | ||
|
|
ed40ec03d1 | ||
|
|
73062ae240 | ||
|
|
bafa3e0d4c | ||
|
|
f3d2b2bb81 | ||
|
|
a74b67d4eb | ||
|
|
625416862f | ||
|
|
8dda8838ba | ||
|
|
96d3a25213 | ||
|
|
727208f376 |
@@ -4,7 +4,6 @@ root = true
|
||||
insert_final_newline = true
|
||||
charset = utf-8
|
||||
trim_trailing_whitespace = true
|
||||
insert_final_newline = true
|
||||
end_of_line = lf
|
||||
|
||||
[*.{js,json}]
|
||||
|
||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -12,5 +12,6 @@ coverage
|
||||
dist
|
||||
.package.json
|
||||
packages/babel-runtime/*.js
|
||||
packages/babel-runtime/helpers/*.js
|
||||
packages/babel-runtime/regenerator/*.js
|
||||
lib
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
sudo: false
|
||||
language: node_js
|
||||
node_js:
|
||||
- "0.10"
|
||||
- "0.12"
|
||||
- "iojs"
|
||||
|
||||
|
||||
60
CHANGELOG.md
60
CHANGELOG.md
@@ -13,6 +13,66 @@ _Note: Gaps between patch versions are faulty/broken releases._
|
||||
|
||||
See [CHANGELOG - 6to5](CHANGELOG-6to5.md) for the pre-4.0.0 version changelog.
|
||||
|
||||
## 4.7.7
|
||||
|
||||
* **Bug Fix**
|
||||
* Fix `types` API exposure.
|
||||
|
||||
## 4.7.6
|
||||
|
||||
* **Bug Fix**
|
||||
* Fix non-Identifier/Literal computed class methods.
|
||||
* **Polish**
|
||||
* Add a fallback if `stack` on an error is unconfigurable.
|
||||
* Hoist `esModule` module declarations to the top of the file to handle circular dependencies better.
|
||||
|
||||
## 4.7.5
|
||||
|
||||
* **Bug Fix**
|
||||
* Don't remap` break`s to call the iterator return.
|
||||
* **Polish**
|
||||
* Use a different helper for computed classes for much nicer output. Also fixes a bug in symbols being non-enumerable so they wouldn't be set on the class.
|
||||
|
||||
## 4.7.4
|
||||
|
||||
* **Bug Fix**
|
||||
* Rewrite named function expressions in optional async function transformers.
|
||||
* Hoist directives.
|
||||
* Remove `Number` from the list of valid `runtime` constructors.
|
||||
* **Internal**
|
||||
* `spec.typeofSymbol` transformer has been renamed to `es6.symbols`.
|
||||
|
||||
## 4.7.2
|
||||
|
||||
* **New Feature**
|
||||
* `"both"` option for `sourceMap`.
|
||||
* Add output types to external helpers. Thanks [@neVERberleRfellerER](https://github.com/neVERberleRfellerER)!
|
||||
* **Bug Fix**
|
||||
* Fix node duplication sometimes resulting in a recursion error.
|
||||
* Ignore `break`s within cases inside `for...of`.
|
||||
* **Polish**
|
||||
* Split up variable declarations and export declarations to allow easier transformation.
|
||||
|
||||
## 4.7.0
|
||||
|
||||
* **Bug Fix**
|
||||
* Add `alternate` to list of `STATEMENT_OR_BLOCK` keys.
|
||||
* Add support for module specifiers to `t.isReferenced`.
|
||||
* **New Feature**
|
||||
* Add `inputSourceMap` option.
|
||||
* **Polish**
|
||||
* Throw an error on different `babel` and `babel-runtime` versions.
|
||||
* Replicate module environment for `babel-node` eval.
|
||||
* Clean up classes output.
|
||||
* **Spec Compliancy**
|
||||
* Make it illegal to use a rest parameter on a setter.
|
||||
|
||||
## 4.6.6
|
||||
|
||||
* **Bug Fix**
|
||||
* Fix incorrect method call in `utility.deadCodeElimination` transformer.
|
||||
* Fix `es6.blockScopingTDZ` transformer duplicating binding nodes.
|
||||
|
||||
## 4.6.5
|
||||
|
||||
* **Internal**
|
||||
|
||||
@@ -53,7 +53,6 @@ $ mocha test/transformation.js
|
||||
#### Code Standards
|
||||
|
||||
* **General**
|
||||
* No ES6 syntax features or methods, exclusively ES5.
|
||||
* Max of five arguments for functions
|
||||
* Max depth of four nested blocks
|
||||
* 2-spaced soft tabs
|
||||
|
||||
18
Makefile
18
Makefile
@@ -8,11 +8,14 @@ BABEL_CMD = node_modules/babel/bin/babel
|
||||
|
||||
export NODE_ENV = test
|
||||
|
||||
.PHONY: clean test test-cov test-clean test-travis test-simple test-all test-browser publish build bootstrap publish-core publish-runtime build-core watch-core
|
||||
.PHONY: clean test test-cov test-clean test-travis test-simple test-all test-browser publish build bootstrap publish-core publish-runtime build-core watch-core build-core-test
|
||||
|
||||
build-core:
|
||||
node $(BABEL_CMD) src --out-dir lib --copy-files
|
||||
|
||||
build-core-test:
|
||||
node $(BABEL_CMD) src --out-dir lib --copy-files --auxiliary-comment "istanbul ignore next"
|
||||
|
||||
watch-core:
|
||||
node $(BABEL_CMD) src --out-dir lib --watch --copy-files
|
||||
|
||||
@@ -40,26 +43,19 @@ test-clean:
|
||||
rm -rf test/tmp
|
||||
|
||||
test:
|
||||
$(MOCHA_CMD)
|
||||
node $(MOCHA_CMD)
|
||||
make test-clean
|
||||
|
||||
test-simple:
|
||||
# excludes test262
|
||||
export SIMPLE_BABEL_TESTS=1; \
|
||||
make test
|
||||
|
||||
test-all:
|
||||
export ALL_BABEL_TESTS=1; \
|
||||
make test
|
||||
|
||||
test-cov:
|
||||
rm -rf coverage
|
||||
export SIMPLE_BABEL_TESTS=1; \
|
||||
make build-core-test
|
||||
node $(ISTANBUL_CMD) $(MOCHA_CMD) --
|
||||
|
||||
test-travis: bootstrap build
|
||||
node $(ISTANBUL_CMD) $(MOCHA_CMD) --
|
||||
if test -n "$$CODECLIMATE_REPO_TOKEN"; then codeclimate < coverage/lcov.info; fi
|
||||
test-travis: bootstrap build test
|
||||
|
||||
test-browser:
|
||||
mkdir -p dist
|
||||
|
||||
@@ -60,7 +60,18 @@ if (program.eval || program.print) {
|
||||
var code = program.eval;
|
||||
if (!code || code === true) code = program.print;
|
||||
|
||||
var result = _eval(code, "eval");
|
||||
global.__filename = "[eval]";
|
||||
global.__dirname = process.cwd();
|
||||
|
||||
var module = new Module(global.__filename);
|
||||
module.filename = global.__filename;
|
||||
module.paths = Module._nodeModulePaths(global.__dirname);
|
||||
|
||||
global.exports = module.exports;
|
||||
global.module = module;
|
||||
global.require = module.require.bind(module);
|
||||
|
||||
var result = _eval(code, global.__filename);
|
||||
if (program.print) console.log(result);
|
||||
} else {
|
||||
if (program.args.length) {
|
||||
@@ -88,7 +99,7 @@ if (program.eval || program.print) {
|
||||
args = args.slice(i);
|
||||
|
||||
// make the filename absolute
|
||||
var filename = args[0]
|
||||
var filename = args[0];
|
||||
if (!pathIsAbsolute(filename)) args[0] = path.join(process.cwd(), filename);
|
||||
|
||||
// add back on node and concat the sliced args
|
||||
|
||||
@@ -1,4 +1,13 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
var runtime = require("../lib/babel/build-external-helpers");
|
||||
console.log(runtime());
|
||||
var commander = require("commander");
|
||||
var util = require("../lib/babel/util");
|
||||
var runtime = require("../lib/babel/tools/build-external-helpers");
|
||||
|
||||
commander.option("-l, --whitelist [whitelist]", "Whitelist of helpers to ONLY include", util.list);
|
||||
commander.option("-t, --output-type [type]", "Type of output (global|umd|var)", "global");
|
||||
|
||||
commander.usage("[options]");
|
||||
commander.parse(process.argv);
|
||||
|
||||
console.log(runtime(commander.whitelist, commander.outputType));
|
||||
|
||||
@@ -7,7 +7,17 @@
|
||||
|
||||
var args = [__dirname + "/_babel-node"];
|
||||
|
||||
process.argv.slice(2).forEach(function(arg){
|
||||
var babelArgs = process.argv.slice(2);
|
||||
var userArgs;
|
||||
|
||||
// separate node arguments from script arguments
|
||||
var argSeparator = babelArgs.indexOf("--");
|
||||
if (argSeparator > -1) {
|
||||
userArgs = babelArgs.slice(argSeparator); // including the --
|
||||
babelArgs = babelArgs.slice(0, argSeparator);
|
||||
}
|
||||
|
||||
babelArgs.forEach(function(arg){
|
||||
var flag = arg.split("=")[0];
|
||||
|
||||
switch (flag) {
|
||||
@@ -49,6 +59,11 @@ process.argv.slice(2).forEach(function(arg){
|
||||
}
|
||||
});
|
||||
|
||||
// append arguments passed after --
|
||||
if (argSeparator > -1) {
|
||||
args = args.concat(userArgs);
|
||||
}
|
||||
|
||||
try {
|
||||
var kexec = require("kexec");
|
||||
kexec(process.argv[0], args);
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
var sourceMapToComment = require("source-map-to-comment");
|
||||
var sourceMap = require("source-map");
|
||||
var chokidar = require("chokidar");
|
||||
var path = require("path");
|
||||
var util = require("./util");
|
||||
var fs = require("fs");
|
||||
var _ = require("lodash");
|
||||
var convertSourceMap = require("convert-source-map");
|
||||
var sourceMap = require("source-map");
|
||||
var chokidar = require("chokidar");
|
||||
var path = require("path");
|
||||
var util = require("./util");
|
||||
var fs = require("fs");
|
||||
var _ = require("lodash");
|
||||
|
||||
module.exports = function (commander, filenames) {
|
||||
var results = [];
|
||||
@@ -42,7 +42,7 @@ module.exports = function (commander, filenames) {
|
||||
});
|
||||
|
||||
if (commander.sourceMapsInline || (!commander.outFile && commander.sourceMaps)) {
|
||||
code += "\n" + sourceMapToComment(map);
|
||||
code += "\n" + convertSourceMap.fromObject(map).toComment();
|
||||
}
|
||||
|
||||
return {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "babel",
|
||||
"description": "Turn ES6 code into readable vanilla ES5 with source maps",
|
||||
"version": "4.6.5",
|
||||
"version": "4.7.8",
|
||||
"author": "Sebastian McKenzie <sebmck@gmail.com>",
|
||||
"homepage": "https://babeljs.io/",
|
||||
"repository": "babel/babel",
|
||||
@@ -37,10 +37,11 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"acorn-babel": "0.11.1-37",
|
||||
"ast-types": "~0.6.1",
|
||||
"ast-types": "~0.7.0",
|
||||
"chalk": "^1.0.0",
|
||||
"chokidar": "^0.12.6",
|
||||
"commander": "^2.6.0",
|
||||
"convert-source-map": "^0.5.0",
|
||||
"core-js": "^0.6.1",
|
||||
"debug": "^2.1.1",
|
||||
"detect-indent": "^3.0.0",
|
||||
@@ -56,14 +57,14 @@
|
||||
"output-file-sync": "^1.1.0",
|
||||
"path-is-absolute": "^1.0.0",
|
||||
"private": "^0.1.6",
|
||||
"regenerator-babel": "0.8.13-1",
|
||||
"regenerator-babel": "0.8.13-2",
|
||||
"regexpu": "^1.1.2",
|
||||
"repeating": "^1.1.2",
|
||||
"shebang-regex": "^1.0.0",
|
||||
"slash": "^1.0.0",
|
||||
"source-map": "^0.4.0",
|
||||
"source-map-support": "^0.2.9",
|
||||
"source-map-to-comment": "^1.0.0",
|
||||
"to-fast-properties": "^1.0.0",
|
||||
"trim-right": "^1.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
||||
0
packages/babel-runtime/helpers/.gitkeep
Normal file
0
packages/babel-runtime/helpers/.gitkeep
Normal file
@@ -1,7 +1,10 @@
|
||||
{
|
||||
"name": "babel-runtime",
|
||||
"description": "babel selfContained runtime",
|
||||
"version": "4.6.4",
|
||||
"version": "4.7.7",
|
||||
"repository": "babel/babel",
|
||||
"author": "Sebastian McKenzie <sebmck@gmail.com>"
|
||||
"author": "Sebastian McKenzie <sebmck@gmail.com>",
|
||||
"dependencies": {
|
||||
"core-js": "^0.6.1"
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,12 @@
|
||||
"use strict";
|
||||
|
||||
var transform = require("../lib/babel/transformation");
|
||||
var buildHelpers = require("../lib/babel/build-helpers");
|
||||
var util = require("../lib/babel/util");
|
||||
var fs = require("fs");
|
||||
var t = require("../lib/babel/types");
|
||||
var _ = require("lodash");
|
||||
var transform = require("../lib/babel/transformation");
|
||||
var each = require("lodash/collection/each");
|
||||
var File = require("../lib/babel/transformation/file");
|
||||
var util = require("../lib/babel/util");
|
||||
var fs = require("fs");
|
||||
var t = require("../lib/babel/types");
|
||||
var _ = require("lodash");
|
||||
|
||||
var relative = function (filename) {
|
||||
return __dirname + "/babel-runtime/" + filename;
|
||||
@@ -39,22 +40,26 @@ var updatePackage = function () {
|
||||
|
||||
var selfContainify = function (code) {
|
||||
return transform(code, {
|
||||
optional: ["selfContained"]
|
||||
optional: ["runtime"]
|
||||
}).code;
|
||||
};
|
||||
|
||||
var buildHelpers2 = function () {
|
||||
var body = util.template("self-contained-helpers-head");
|
||||
var tree = t.program(body);
|
||||
|
||||
buildHelpers(body, t.identifier("helpers"));
|
||||
var buildHelper = function (helperName) {
|
||||
var tree = t.program(
|
||||
util.template("self-contained-helpers-head", {
|
||||
HELPER: util.template(helperName)
|
||||
})
|
||||
);
|
||||
|
||||
return transform.fromAst(tree, null, {
|
||||
optional: ["selfContained"]
|
||||
optional: ["runtime"]
|
||||
}).code;
|
||||
};
|
||||
|
||||
writeFile("helpers.js", buildHelpers2());
|
||||
each(File.helpers, function (helperName) {
|
||||
writeFile("helpers/" + helperName + ".js", buildHelper(helperName));
|
||||
});
|
||||
|
||||
writeFile("core-js.js", readFile("core-js/library", true));
|
||||
writeFile("regenerator/index.js", readFile("regenerator-babel/runtime-module", true));
|
||||
writeFile("regenerator/runtime.js", selfContainify(readFile("regenerator-babel/runtime")));
|
||||
|
||||
@@ -25,7 +25,7 @@ transform.load = function (url, callback, opts = {}, hold) {
|
||||
if (!hold) transform.run.apply(transform, param);
|
||||
if (callback) callback(param);
|
||||
} else {
|
||||
throw new Error("Could not load " + url);
|
||||
throw new Error(`Could not load ${url}`);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -9,10 +9,12 @@ export { canCompile } from "../util";
|
||||
export { default as acorn } from "acorn-babel";
|
||||
export { default as transform } from "../transformation";
|
||||
export { default as traverse } from "../traversal";
|
||||
export { default as buildExternalHelpers } from "../build-external-helpers";
|
||||
export { default as types } from "../types";
|
||||
export { default as buildExternalHelpers } from "../tools/build-external-helpers";
|
||||
export { version } from "../../../package";
|
||||
|
||||
import * as t from "../types";
|
||||
export { t as types };
|
||||
|
||||
export function register(opts) {
|
||||
var callback = require("./register/node");
|
||||
if (opts != null) callback(opts);
|
||||
|
||||
@@ -46,7 +46,7 @@ var compile = function (filename) {
|
||||
var opts = extend({}, transformOpts);
|
||||
resolveRc(filename, opts);
|
||||
|
||||
var cacheKey = filename + ":" + JSON.stringify(opts);
|
||||
var cacheKey = `${filename}:${JSON.stringify(opts)}:${babel.version}`;
|
||||
|
||||
if (cache) {
|
||||
var cached = cache[cacheKey];
|
||||
@@ -57,7 +57,7 @@ var compile = function (filename) {
|
||||
|
||||
if (!result) {
|
||||
result = babel.transformFileSync(filename, extend(opts, {
|
||||
sourceMap: true,
|
||||
sourceMap: "both",
|
||||
ast: false
|
||||
}));
|
||||
}
|
||||
@@ -153,3 +153,16 @@ export default function (opts = {}) {
|
||||
|
||||
extend(transformOpts, opts);
|
||||
};
|
||||
|
||||
//
|
||||
|
||||
try {
|
||||
var runtimePackage = require("babel-runtime/package");
|
||||
var version = require("../../package").version;
|
||||
if (runtimePackage.version !== version) {
|
||||
throw new ReferenceError(`The verison of babel-runtime of ${runtimePackage.runtime} that you have installed does not match the babel verison of ${version}`);
|
||||
}
|
||||
} catch (err) {
|
||||
if (err.code !== "MODULE_NOT_FOUND") throw err;
|
||||
}
|
||||
|
||||
|
||||
@@ -23,7 +23,7 @@ export default function (loc, opts = {}) {
|
||||
try {
|
||||
json = JSON.parse(content);
|
||||
} catch (err) {
|
||||
err.message = file + ": " + err.message;
|
||||
err.message = `${file}: ${err.message}`;
|
||||
throw err;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,23 +0,0 @@
|
||||
import buildHelpers from "./build-helpers";
|
||||
import generator from "./generation";
|
||||
import * as util from "./util";
|
||||
import t from "./types";
|
||||
|
||||
export default function (whitelist) {
|
||||
var namespace = t.identifier("babelHelpers");
|
||||
|
||||
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, whitelist);
|
||||
|
||||
return generator(tree).code;
|
||||
};
|
||||
@@ -1,15 +0,0 @@
|
||||
import File from "./transformation/file";
|
||||
import * as util from "./util";
|
||||
import each from "lodash/collection/each";
|
||||
import t from "./types";
|
||||
|
||||
export default function (body, namespace, whitelist = []) {
|
||||
each(File.helpers, function (name) {
|
||||
if (whitelist.length && whitelist.indexOf(name) === -1) return;
|
||||
|
||||
var key = t.identifier(t.toIdentifier(name));
|
||||
body.push(t.expressionStatement(
|
||||
t.assignmentExpression("=", t.memberExpression(namespace, key), util.template(name))
|
||||
));
|
||||
});
|
||||
};
|
||||
@@ -1,28 +0,0 @@
|
||||
import SYNTAX_KEYS from "./syntax-keys";
|
||||
import traverse from "../traversal";
|
||||
|
||||
var visitors = traverse.explode(require("./visitors"));
|
||||
|
||||
export default function (ast) {
|
||||
var stats = {
|
||||
syntax: {},
|
||||
builtins: {}
|
||||
};
|
||||
|
||||
var detectedSyntax = function (name) {
|
||||
stats.syntax[name] = true;
|
||||
};
|
||||
|
||||
traverse(ast, {
|
||||
enter(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;
|
||||
};
|
||||
@@ -1,84 +0,0 @@
|
||||
{
|
||||
"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"
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
import includes from "lodash/collection/includes";
|
||||
import t from "../types";
|
||||
|
||||
export function AssignmentExpression(node, parent, detected) {
|
||||
if (node.operator === "**=") {
|
||||
detected("es6.exponentation");
|
||||
}
|
||||
}
|
||||
|
||||
export function BinaryExpression(node, parent, detected) {
|
||||
if (node.operator === "**") {
|
||||
detected("es6.exponentation");
|
||||
}
|
||||
}
|
||||
|
||||
export function VariableDeclaration(node, parent, detected) {
|
||||
if (node.kind === "let" || node.kind === "const") {
|
||||
detected("es6.blockScoping");
|
||||
}
|
||||
|
||||
if (node.kind === "const") {
|
||||
detected("es6.constants");
|
||||
}
|
||||
}
|
||||
|
||||
export function Property(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");
|
||||
}
|
||||
}
|
||||
|
||||
export function AssignmentPattern(node, parent, detected) {
|
||||
if (t.isFunction(parent) && includes(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");
|
||||
}
|
||||
};
|
||||
@@ -140,7 +140,7 @@ export default class Buffer {
|
||||
var indent = this.getIndent();
|
||||
|
||||
// replace all newlines with newlines with the indentation
|
||||
str = str.replace(/\n/g, "\n" + indent);
|
||||
str = str.replace(/\n/g, `\n${indent}`);
|
||||
|
||||
// we've got a newline before us so prepend on the indentation
|
||||
if (this.isLast("\n")) this._push(indent);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import isInteger from "is-integer";
|
||||
import isNumber from "lodash/lang/isNumber";
|
||||
import t from "../../types";
|
||||
import * as t from "../../types";
|
||||
|
||||
export function UnaryExpression(node, print) {
|
||||
var hasSpace = /[a-z]$/.test(node.operator);
|
||||
@@ -109,16 +109,19 @@ export function ExpressionStatement(node, print) {
|
||||
this.semicolon();
|
||||
}
|
||||
|
||||
exports.BinaryExpression =
|
||||
exports.LogicalExpression =
|
||||
exports.AssignmentPattern =
|
||||
exports.AssignmentExpression = function (node, print) {
|
||||
export function AssignmentExpression(node, print) {
|
||||
// todo: add cases where the spaces can be dropped when in compact mode
|
||||
print(node.left);
|
||||
this.push(" ");
|
||||
this.push(node.operator);
|
||||
this.push(" ");
|
||||
print(node.right);
|
||||
}
|
||||
|
||||
export {
|
||||
AssignmentExpression as BinaryExpression,
|
||||
AssignmentExpression as LogicalExpression,
|
||||
AssignmentExpression as AssignmentPattern
|
||||
};
|
||||
|
||||
var SCIENTIFIC_NOTATION = /e/i;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import t from "../../types";
|
||||
import * as t from "../../types";
|
||||
|
||||
export function AnyTypeAnnotation() {
|
||||
this.push("any");
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import each from "lodash/collection/each";
|
||||
import t from "../../types";
|
||||
import * as t from "../../types";
|
||||
|
||||
export function JSXAttribute(node, print) {
|
||||
print(node.name);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import t from "../../types";
|
||||
import * as t from "../../types";
|
||||
|
||||
export function _params(node, print) {
|
||||
print(node.typeParameters);
|
||||
@@ -44,8 +44,7 @@ export function _method(node, print) {
|
||||
print(value.body);
|
||||
}
|
||||
|
||||
exports.FunctionDeclaration =
|
||||
exports.FunctionExpression = function (node, print) {
|
||||
export function FunctionExpression(node, print) {
|
||||
if (node.async) this.push("async ");
|
||||
this.push("function");
|
||||
if (node.generator) this.push("*");
|
||||
@@ -60,7 +59,9 @@ exports.FunctionExpression = function (node, print) {
|
||||
this._params(node, print);
|
||||
this.space();
|
||||
print(node.body);
|
||||
};
|
||||
}
|
||||
|
||||
export { FunctionExpression as FunctionDeclaration };
|
||||
|
||||
export function ArrowFunctionExpression(node, print) {
|
||||
if (node.async) this.push("async ");
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import each from "lodash/collection/each";
|
||||
import t from "../../types";
|
||||
import * as t from "../../types";
|
||||
|
||||
export function ImportSpecifier(node, print) {
|
||||
if (t.isSpecifierDefault(node)) {
|
||||
print(t.getSpecifierName(node));
|
||||
} else {
|
||||
return exports.ExportSpecifier.apply(this, arguments);
|
||||
return ExportSpecifier.apply(this, arguments);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,6 @@ import each from "lodash/collection/each";
|
||||
|
||||
each(["BindMemberExpression", "BindFunctionExpression"], function (type) {
|
||||
exports[type] = function () {
|
||||
throw new ReferenceError("Trying to render non-standard playground node " + JSON.stringify(type));
|
||||
throw new ReferenceError(`Trying to render non-standard playground node ${JSON.stringify(type)}`);
|
||||
};
|
||||
});
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import repeating from "repeating";
|
||||
import t from "../../types";
|
||||
import * as t from "../../types";
|
||||
|
||||
export function WithStatement(node, print) {
|
||||
this.keyword("with");
|
||||
@@ -60,7 +60,7 @@ var buildForXStatement = function (op) {
|
||||
this.keyword("for");
|
||||
this.push("(");
|
||||
print(node.left);
|
||||
this.push(" " + op + " ");
|
||||
this.push(` ${op} `);
|
||||
print(node.right);
|
||||
this.push(")");
|
||||
print.block(node.body);
|
||||
@@ -192,7 +192,7 @@ export function VariableDeclaration(node, print, parent) {
|
||||
|
||||
var sep = ",";
|
||||
if (!this.format.compact && hasInits) {
|
||||
sep += "\n" + repeating(" ", node.kind.length + 1);
|
||||
sep += `\n${repeating(" ", node.kind.length + 1)}`;
|
||||
} else {
|
||||
sep += " ";
|
||||
}
|
||||
|
||||
@@ -4,12 +4,12 @@ export function Identifier(node) {
|
||||
this.push(node.name);
|
||||
}
|
||||
|
||||
exports.RestElement =
|
||||
exports.SpreadElement =
|
||||
exports.SpreadProperty = function (node, print) {
|
||||
export function RestElement(node, print) {
|
||||
this.push("...");
|
||||
print(node.argument);
|
||||
};
|
||||
}
|
||||
|
||||
export { RestElement as SpreadElement, RestElement as SpreadProperty };
|
||||
|
||||
export function VirtualPropertyExpression(node, print) {
|
||||
print(node.object);
|
||||
@@ -17,8 +17,7 @@ export function VirtualPropertyExpression(node, print) {
|
||||
print(node.property);
|
||||
}
|
||||
|
||||
exports.ObjectExpression =
|
||||
exports.ObjectPattern = function (node, print) {
|
||||
export function ObjectExpression(node, print) {
|
||||
var props = node.properties;
|
||||
|
||||
if (props.length) {
|
||||
@@ -32,7 +31,9 @@ exports.ObjectPattern = function (node, print) {
|
||||
} else {
|
||||
this.push("{}");
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export { ObjectExpression as ObjectPattern };
|
||||
|
||||
export function Property(node, print) {
|
||||
if (node.method || node.kind === "get" || node.kind === "set") {
|
||||
@@ -53,8 +54,7 @@ export function Property(node, print) {
|
||||
}
|
||||
}
|
||||
|
||||
exports.ArrayExpression =
|
||||
exports.ArrayPattern = function (node, print) {
|
||||
export function ArrayExpression(node, print) {
|
||||
var elems = node.elements;
|
||||
var len = elems.length;
|
||||
|
||||
@@ -76,7 +76,9 @@ exports.ArrayPattern = function (node, print) {
|
||||
});
|
||||
|
||||
this.push("]");
|
||||
};
|
||||
}
|
||||
|
||||
export { ArrayExpression as ArrayPattern };
|
||||
|
||||
export function Literal(node) {
|
||||
var val = node.value;
|
||||
@@ -89,7 +91,7 @@ export function Literal(node) {
|
||||
} else if (type === "boolean") {
|
||||
this.push(val ? "true" : "false");
|
||||
} else if (node.regex) {
|
||||
this.push("/" + node.regex.pattern + "/" + node.regex.flags);
|
||||
this.push(`/${node.regex.pattern}/${node.regex.flags}`);
|
||||
} else if (val === null) {
|
||||
this.push("null");
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ import Buffer from "./buffer";
|
||||
import extend from "lodash/object/extend";
|
||||
import each from "lodash/collection/each";
|
||||
import n from "./node";
|
||||
import t from "../types";
|
||||
import * as t from "../types";
|
||||
|
||||
class CodeGenerator {
|
||||
constructor(ast, opts, code) {
|
||||
@@ -187,7 +187,7 @@ class CodeGenerator {
|
||||
|
||||
this.printTrailingComments(node, parent);
|
||||
} else {
|
||||
throw new ReferenceError("unknown node of type " + JSON.stringify(node.type) + " with constructor " + JSON.stringify(node && node.constructor.name));
|
||||
throw new ReferenceError(`unknown node of type ${JSON.stringify(node.type)} with constructor ${JSON.stringify(node && node.constructor.name)}`);
|
||||
}
|
||||
|
||||
this.format.concise = oldConcise;
|
||||
@@ -238,9 +238,9 @@ class CodeGenerator {
|
||||
generateComment(comment) {
|
||||
var val = comment.value;
|
||||
if (comment.type === "Line") {
|
||||
val = "//" + val;
|
||||
val = `//${val}`;
|
||||
} else {
|
||||
val = "/*" + val + "*/";
|
||||
val = `/*${val}*/`;
|
||||
}
|
||||
return val;
|
||||
}
|
||||
@@ -319,7 +319,7 @@ class CodeGenerator {
|
||||
}
|
||||
|
||||
var indent = Math.max(this.indentSize(), column);
|
||||
val = val.replace(/\n/g, "\n" + repeating(" ", indent));
|
||||
val = val.replace(/\n/g, `\n${repeating(" ", indent)}`);
|
||||
}
|
||||
|
||||
if (column === 0) {
|
||||
|
||||
@@ -2,7 +2,7 @@ import whitespace from "./whitespace";
|
||||
import * as parens from "./parentheses";
|
||||
import each from "lodash/collection/each";
|
||||
import some from "lodash/collection/some";
|
||||
import t from "../../types";
|
||||
import * as t from "../../types";
|
||||
|
||||
var find = function (obj, node, parent) {
|
||||
if (!obj) return;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import each from "lodash/collection/each";
|
||||
import t from "../../types";
|
||||
import * as t from "../../types";
|
||||
|
||||
var PRECEDENCE = {};
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import isBoolean from "lodash/lang/isBoolean";
|
||||
import each from "lodash/collection/each";
|
||||
import map from "lodash/collection/map";
|
||||
import t from "../../types";
|
||||
import * as t from "../../types";
|
||||
|
||||
function crawl(node, state = {}) {
|
||||
if (t.isMemberExpression(node)) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import sourceMap from "source-map";
|
||||
import t from "../types";
|
||||
import * as t from "../types";
|
||||
|
||||
export default class SourceMap {
|
||||
constructor(position, opts, code) {
|
||||
|
||||
@@ -55,7 +55,7 @@ var highlight = function (text) {
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = function (lines, lineNumber, colNumber) {
|
||||
export default function (lines, lineNumber, colNumber) {
|
||||
colNumber = Math.max(colNumber, 0);
|
||||
|
||||
if (chalk.supportsColor) {
|
||||
@@ -81,8 +81,7 @@ module.exports = function (lines, lineNumber, colNumber) {
|
||||
return;
|
||||
}
|
||||
if (colNumber) {
|
||||
params.line += "\n" + params.before + repeating(" ", params.width) +
|
||||
params.after + repeating(" ", colNumber - 1) + "^";
|
||||
params.line += `\n${params.before}${repeating(" ", params.width)}${params.after}${repeating(" ", colNumber - 1)}^`;
|
||||
}
|
||||
params.before = params.before.replace(/^./, ">");
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import t from "../types";
|
||||
import * as t from "../types";
|
||||
|
||||
module.exports = function (ast, comments, tokens) {
|
||||
export default function (ast, comments, tokens) {
|
||||
if (ast && ast.type === "Program") {
|
||||
return t.file(ast, comments || [], tokens || []);
|
||||
} else {
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
module.exports = function () {
|
||||
export default function () {
|
||||
return Object.create(null);
|
||||
};
|
||||
|
||||
@@ -3,7 +3,7 @@ import estraverse from "estraverse";
|
||||
import codeFrame from "./code-frame";
|
||||
import acorn from "acorn-babel";
|
||||
|
||||
module.exports = function (opts, code, callback) {
|
||||
export default function (opts, code, callback) {
|
||||
try {
|
||||
var comments = [];
|
||||
var tokens = [];
|
||||
@@ -32,7 +32,7 @@ module.exports = function (opts, code, callback) {
|
||||
} catch (err) {
|
||||
if (!err._babel) {
|
||||
err._babel = true;
|
||||
var message = opts.filename + ": " + err.message;
|
||||
var message = `${opts.filename}: ${err.message}`;
|
||||
|
||||
var loc = err.loc;
|
||||
if (loc) {
|
||||
@@ -40,7 +40,15 @@ module.exports = function (opts, code, callback) {
|
||||
message += frame;
|
||||
}
|
||||
|
||||
if (err.stack) err.stack = err.stack.replace(err.message, message);
|
||||
if (err.stack) {
|
||||
var newStack = err.stack.replace(err.message, message);
|
||||
try {
|
||||
err.stack = newStack;
|
||||
} catch (e) {
|
||||
// `err.stack` may be a readonly property in some environments
|
||||
}
|
||||
}
|
||||
|
||||
err.message = message;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
/**
|
||||
* A trick from Bluebird to force V8 to use fast properties for an object.
|
||||
* Read more: http://stackoverflow.com/questions/24987896/
|
||||
*
|
||||
* Use %HasFastProperties(obj) and --allow-natives-syntax to check whether
|
||||
* a particular object already has fast properties.
|
||||
*/
|
||||
|
||||
module.exports = function toFastProperties(obj) {
|
||||
/*jshint -W027*/
|
||||
function f() {}
|
||||
f.prototype = obj;
|
||||
return f;
|
||||
eval(obj);
|
||||
};
|
||||
@@ -10,6 +10,7 @@ export var messages = {
|
||||
undeclaredVariable: "Reference to undeclared variable $1",
|
||||
undeclaredVariableSuggestion: "Reference to undeclared variable $1 - did you mean $2?",
|
||||
settersInvalidParamLength: "Setters must have exactly one parameter",
|
||||
settersNoRest: "Setters aren't allowed to have a rest",
|
||||
noAssignmentsInForHead: "No assignments allowed in for-in/of head",
|
||||
expectedMemberExpressionOrIdentifier: "Expected type MemeberExpression or Identifier",
|
||||
invalidParentForThisNode: "We don't know how to handle this node within the current parent - please open an issue",
|
||||
@@ -18,25 +19,23 @@ export var messages = {
|
||||
unknownForHead: "Unknown node type $1 in ForStatement",
|
||||
didYouMean: "Did you mean $1?",
|
||||
evalInStrictMode: "eval is not allowed in strict mode",
|
||||
codeGeneratorDeopt: "Note: The code generator has deoptimised the styling of $1 as it exceeds the max of $2."
|
||||
codeGeneratorDeopt: "Note: The code generator has deoptimised the styling of $1 as it exceeds the max of $2.",
|
||||
missingTemplatesDirectory: "no templates directory - this is most likely the result of a broken `npm publish`. Please report to https://github.com/babel/babel/issues",
|
||||
unsupportedOutputType: "Unsupported output type $1"
|
||||
};
|
||||
|
||||
export function get(key) {
|
||||
var msg = exports.messages[key];
|
||||
if (!msg) throw new ReferenceError("Unknown message `" + key + "`");
|
||||
export function get(key: String, ...args) {
|
||||
var msg = messages[key];
|
||||
if (!msg) throw new ReferenceError(`Unknown message ${JSON.stringify(key)}`);
|
||||
|
||||
var args = [];
|
||||
for (var i = 1; i < arguments.length; i++) {
|
||||
args.push(arguments[i]);
|
||||
}
|
||||
args = exports.parseArgs(args);
|
||||
args = parseArgs(args);
|
||||
|
||||
return msg.replace(/\$(\d+)/g, function (str, i) {
|
||||
return args[--i];
|
||||
});
|
||||
}
|
||||
|
||||
export function parseArgs(args) {
|
||||
export function parseArgs(args: Array<any>) {
|
||||
return args.map(function (val) {
|
||||
if (val != null && val.inspect) {
|
||||
return val.inspect();
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import estraverse from "estraverse";
|
||||
import extend from "lodash/object/extend";
|
||||
import types from "ast-types";
|
||||
import t from "./types";
|
||||
import * as t from "./types";
|
||||
|
||||
// estraverse
|
||||
|
||||
@@ -9,8 +9,8 @@ extend(estraverse.VisitorKeys, t.VISITOR_KEYS);
|
||||
|
||||
// regenerator-babel/ast-types
|
||||
|
||||
var def = types.Type.def;
|
||||
var or = types.Type.or;
|
||||
var def = types.Type.def;
|
||||
var or = types.Type.or;
|
||||
|
||||
def("File")
|
||||
.bases("Node")
|
||||
|
||||
86
src/babel/tools/build-external-helpers.js
Normal file
86
src/babel/tools/build-external-helpers.js
Normal file
@@ -0,0 +1,86 @@
|
||||
import generator from "../generation";
|
||||
import * as messages from "../messages";
|
||||
import * as util from "../util";
|
||||
import File from "../transformation/file";
|
||||
import each from "lodash/collection/each";
|
||||
import * as t from "../types";
|
||||
|
||||
function buildGlobal(namespace, builder) {
|
||||
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([]))
|
||||
)
|
||||
]));
|
||||
|
||||
builder(body);
|
||||
|
||||
return tree;
|
||||
}
|
||||
|
||||
function buildUmd(namespace, builder) {
|
||||
var body = [];
|
||||
body.push(t.variableDeclaration("var", [
|
||||
t.variableDeclarator(namespace, t.identifier("global"))
|
||||
]));
|
||||
|
||||
builder(body);
|
||||
|
||||
var container = util.template("umd-commonjs-strict", {
|
||||
FACTORY_PARAMETERS: t.identifier("global"),
|
||||
BROWSER_ARGUMENTS: t.assignmentExpression("=", t.memberExpression(t.identifier("root"), namespace), t.objectExpression({})),
|
||||
COMMON_ARGUMENTS: t.identifier("exports"),
|
||||
AMD_ARGUMENTS: t.arrayExpression([t.literal("exports")]),
|
||||
FACTORY_BODY: body,
|
||||
UMD_ROOT: t.identifier("this")
|
||||
});
|
||||
return t.program([container]);
|
||||
}
|
||||
|
||||
function buildVar(namespace, builder) {
|
||||
var body = [];
|
||||
body.push(t.variableDeclaration("var", [
|
||||
t.variableDeclarator(namespace, t.objectExpression({}))
|
||||
]));
|
||||
builder(body);
|
||||
return t.program(body);
|
||||
}
|
||||
|
||||
function buildHelpers(body, namespace, whitelist = []) {
|
||||
each(File.helpers, function (name) {
|
||||
if (whitelist.length && whitelist.indexOf(name) === -1) return;
|
||||
|
||||
var key = t.identifier(t.toIdentifier(name));
|
||||
body.push(t.expressionStatement(
|
||||
t.assignmentExpression("=", t.memberExpression(namespace, key), util.template(name))
|
||||
));
|
||||
});
|
||||
}
|
||||
|
||||
export default function (whitelist, outputType = "global") {
|
||||
var namespace = t.identifier("babelHelpers");
|
||||
|
||||
var builder = function (body) {
|
||||
return buildHelpers(body, namespace, whitelist);
|
||||
};
|
||||
|
||||
var tree;
|
||||
|
||||
var build = {
|
||||
global: buildGlobal,
|
||||
umd: buildUmd,
|
||||
var: buildVar
|
||||
}[outputType];
|
||||
|
||||
if (build) {
|
||||
tree = build(namespace, builder);
|
||||
} else {
|
||||
throw new Error(messages.get("unsupportedOutputType", outputType));
|
||||
}
|
||||
|
||||
return generator(tree).code;
|
||||
};
|
||||
@@ -1,18 +1,20 @@
|
||||
import sourceMapToComment from "source-map-to-comment";
|
||||
import convertSourceMap from "convert-source-map";
|
||||
import shebangRegex from "shebang-regex";
|
||||
import isFunction from "lodash/lang/isFunction";
|
||||
import sourceMap from "source-map";
|
||||
import transform from "./index";
|
||||
import generate from "../generation";
|
||||
import defaults from "lodash/object/defaults";
|
||||
import includes from "lodash/collection/includes";
|
||||
import assign from "lodash/object/assign";
|
||||
import Logger from "./logger";
|
||||
import parse from "../helpers/parse";
|
||||
import Scope from "../traversal/scope";
|
||||
import slash from "slash";
|
||||
import * as util from "../util";
|
||||
import path from "path";
|
||||
import each from "lodash/collection/each";
|
||||
import t from "../types";
|
||||
import * as t from "../types";
|
||||
|
||||
var checkTransformerVisitor = {
|
||||
enter(node, parent, scope, state) {
|
||||
@@ -22,7 +24,7 @@ var checkTransformerVisitor = {
|
||||
|
||||
function checkNode(stack, node, scope) {
|
||||
each(stack, function (pass) {
|
||||
if (pass.shouldRun) return;
|
||||
if (pass.shouldRun || pass.ran) return;
|
||||
pass.checkNode(node, scope);
|
||||
});
|
||||
}
|
||||
@@ -40,6 +42,7 @@ export default class File {
|
||||
|
||||
this.lastStatements = [];
|
||||
this.opts = this.normalizeOptions(opts);
|
||||
this.log = new Logger(this);
|
||||
this.ast = {};
|
||||
|
||||
this.buildTransformers();
|
||||
@@ -48,7 +51,8 @@ export default class File {
|
||||
static helpers = [
|
||||
"inherits",
|
||||
"defaults",
|
||||
"prototype-properties",
|
||||
"create-class",
|
||||
"create-computed-class",
|
||||
"apply-constructor",
|
||||
"tagged-template-literal",
|
||||
"tagged-template-literal-loose",
|
||||
@@ -77,34 +81,41 @@ export default class File {
|
||||
static validOptions = [
|
||||
"filename",
|
||||
"filenameRelative",
|
||||
|
||||
"blacklist",
|
||||
"whitelist",
|
||||
"loose",
|
||||
"optional",
|
||||
|
||||
"loose",
|
||||
"playground",
|
||||
"experimental",
|
||||
|
||||
"modules",
|
||||
"moduleIds",
|
||||
"moduleId",
|
||||
"resolveModuleSource",
|
||||
"keepModuleIdExtensions",
|
||||
|
||||
"code",
|
||||
"ast",
|
||||
|
||||
"comments",
|
||||
"compact",
|
||||
|
||||
"auxiliaryComment",
|
||||
"externalHelpers",
|
||||
"returnUsedHelpers",
|
||||
|
||||
"inputSourceMap",
|
||||
"sourceMap",
|
||||
"sourceMapName",
|
||||
"sourceFileName",
|
||||
"sourceRoot",
|
||||
"moduleRoot",
|
||||
"moduleIds",
|
||||
"comments",
|
||||
"reactCompat",
|
||||
"keepModuleIdExtensions",
|
||||
"code",
|
||||
"ast",
|
||||
"playground",
|
||||
"experimental",
|
||||
"externalHelpers",
|
||||
"auxiliaryComment",
|
||||
"compact",
|
||||
"returnUsedHelpers",
|
||||
|
||||
"resolveModuleSource",
|
||||
"moduleId",
|
||||
|
||||
// legacy
|
||||
"format",
|
||||
"reactCompat",
|
||||
|
||||
// these are used by plugins
|
||||
"ignore",
|
||||
@@ -113,12 +124,12 @@ export default class File {
|
||||
"accept"
|
||||
];
|
||||
|
||||
normalizeOptions(opts) {
|
||||
normalizeOptions(opts: Object) {
|
||||
opts = assign({}, opts);
|
||||
|
||||
for (var key in opts) {
|
||||
if (key[0] !== "_" && File.validOptions.indexOf(key) < 0) {
|
||||
throw new ReferenceError("Unknown option: " + key);
|
||||
throw new ReferenceError(`Unknown option: ${key}`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -128,6 +139,7 @@ export default class File {
|
||||
returnUsedHelpers: false,
|
||||
externalHelpers: false,
|
||||
auxilaryComment: "",
|
||||
inputSourceMap: false,
|
||||
experimental: false,
|
||||
reactCompat: false,
|
||||
playground: false,
|
||||
@@ -145,6 +157,10 @@ export default class File {
|
||||
ast: true
|
||||
});
|
||||
|
||||
if (opts.inputSourceMap) {
|
||||
opts.sourceMap = true;
|
||||
}
|
||||
|
||||
// normalize windows path separators to unix
|
||||
opts.filename = slash(opts.filename);
|
||||
if (opts.sourceRoot) {
|
||||
@@ -214,7 +230,7 @@ export default class File {
|
||||
return opts;
|
||||
};
|
||||
|
||||
isLoose(key) {
|
||||
isLoose(key: string) {
|
||||
return includes(this.opts.loose, key);
|
||||
}
|
||||
|
||||
@@ -246,13 +262,7 @@ export default class File {
|
||||
this.transformers = transformers;
|
||||
}
|
||||
|
||||
debug(msg) {
|
||||
var parts = this.opts.filename;
|
||||
if (msg) parts += ": " + msg;
|
||||
util.debug(parts);
|
||||
}
|
||||
|
||||
getModuleFormatter(type) {
|
||||
getModuleFormatter(type: string) {
|
||||
var ModuleFormatter = isFunction(type) ? type : transform.moduleFormatters[type];
|
||||
|
||||
if (!ModuleFormatter) {
|
||||
@@ -261,13 +271,25 @@ export default class File {
|
||||
}
|
||||
|
||||
if (!ModuleFormatter) {
|
||||
throw new ReferenceError("Unknown module formatter type " + JSON.stringify(type));
|
||||
throw new ReferenceError(`Unknown module formatter type ${JSON.stringify(type)}`);
|
||||
}
|
||||
|
||||
return new ModuleFormatter(this);
|
||||
}
|
||||
|
||||
parseShebang(code) {
|
||||
parseInputSourceMap(code: string) {
|
||||
var opts = this.opts;
|
||||
|
||||
var inputMap = convertSourceMap.fromSource(code);
|
||||
if (inputMap) {
|
||||
opts.inputSourceMap = inputMap.toObject();
|
||||
code = convertSourceMap.removeComments(code);
|
||||
}
|
||||
|
||||
return code;
|
||||
}
|
||||
|
||||
parseShebang(code: string) {
|
||||
var shebangMatch = shebangRegex.exec(code);
|
||||
|
||||
if (shebangMatch) {
|
||||
@@ -280,15 +302,15 @@ export default class File {
|
||||
return code;
|
||||
}
|
||||
|
||||
set(key, val) {
|
||||
set(key: string, val): any {
|
||||
return this.data[key] = val;
|
||||
};
|
||||
|
||||
setDynamic(key, fn) {
|
||||
setDynamic(key: string, fn: Function) {
|
||||
this.dynamicData[key] = fn;
|
||||
}
|
||||
|
||||
get(key) {
|
||||
get(key: string): any {
|
||||
var data = this.data[key];
|
||||
if (data) {
|
||||
return data;
|
||||
@@ -300,7 +322,7 @@ export default class File {
|
||||
}
|
||||
}
|
||||
|
||||
addImport(source, name, noDefault) {
|
||||
addImport(source: string, name?: string, noDefault?: boolean): Object {
|
||||
name ||= source;
|
||||
var id = this.dynamicImportIds[name];
|
||||
|
||||
@@ -314,17 +336,21 @@ export default class File {
|
||||
this.dynamicImported.push(declar);
|
||||
if (noDefault) this.dynamicImportedNoDefault.push(declar);
|
||||
|
||||
this.moduleFormatter.importSpecifier(specifiers[0], declar, this.dynamicImports);
|
||||
if (this.transformers["es6.modules"].canRun()) {
|
||||
this.moduleFormatter.importSpecifier(specifiers[0], declar, this.dynamicImports);
|
||||
} else {
|
||||
this.dynamicImports.push(declar);
|
||||
}
|
||||
}
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
isConsequenceExpressionStatement(node) {
|
||||
isConsequenceExpressionStatement(node: Object): boolean {
|
||||
return t.isExpressionStatement(node) && this.lastStatements.indexOf(node) >= 0;
|
||||
}
|
||||
|
||||
attachAuxiliaryComment(node) {
|
||||
attachAuxiliaryComment(node: Object): Object {
|
||||
var comment = this.opts.auxiliaryComment;
|
||||
if (comment) {
|
||||
node.leadingComments ||= [];
|
||||
@@ -336,9 +362,9 @@ export default class File {
|
||||
return node;
|
||||
}
|
||||
|
||||
addHelper(name) {
|
||||
addHelper(name: string): Object {
|
||||
if (!includes(File.helpers, name)) {
|
||||
throw new ReferenceError("Unknown helper " + name);
|
||||
throw new ReferenceError(`Unknown helper ${name}`);
|
||||
}
|
||||
|
||||
var program = this.ast.program;
|
||||
@@ -348,10 +374,13 @@ export default class File {
|
||||
|
||||
this.usedHelpers[name] = true;
|
||||
|
||||
var generator = this.get("helperGenerator");
|
||||
var runtime = this.get("helpersNamespace");
|
||||
if (runtime) {
|
||||
name = t.identifier(t.toIdentifier(name));
|
||||
return t.memberExpression(runtime, name);
|
||||
if (generator) {
|
||||
return generator(name);
|
||||
} else if (runtime) {
|
||||
var id = t.identifier(t.toIdentifier(name));
|
||||
return t.memberExpression(runtime, id);
|
||||
} else {
|
||||
var ref = util.template(name);
|
||||
ref._compact = true;
|
||||
@@ -365,24 +394,21 @@ export default class File {
|
||||
}
|
||||
}
|
||||
|
||||
logDeopt() {
|
||||
// todo, (node, msg)
|
||||
}
|
||||
|
||||
errorWithNode(node, msg, Error = SyntaxError) {
|
||||
var loc = node.loc.start;
|
||||
var err = new Error("Line " + loc.line + ": " + msg);
|
||||
var err = new Error(`Line ${loc.line}: ${msg}`);
|
||||
err.loc = loc;
|
||||
return err;
|
||||
}
|
||||
|
||||
addCode(code) {
|
||||
addCode(code: string) {
|
||||
code = (code || "") + "";
|
||||
code = this.parseInputSourceMap(code);
|
||||
this.code = code;
|
||||
return this.parseShebang(code);
|
||||
}
|
||||
|
||||
parse(code) {
|
||||
parse(code: string) {
|
||||
code = this.addCode(code);
|
||||
|
||||
var opts = this.opts;
|
||||
@@ -397,7 +423,7 @@ export default class File {
|
||||
}
|
||||
|
||||
transform(ast) {
|
||||
this.debug();
|
||||
this.log.debug();
|
||||
|
||||
this.ast = ast;
|
||||
this.lastStatements = t.getLastStatements(ast.program);
|
||||
@@ -419,7 +445,7 @@ export default class File {
|
||||
this.call("post");
|
||||
}
|
||||
|
||||
call(key) {
|
||||
call(key: string) {
|
||||
var stack = this.transformerStack;
|
||||
for (var i = 0; i < stack.length; i++) {
|
||||
var transformer = stack[i].transformer;
|
||||
@@ -430,6 +456,13 @@ export default class File {
|
||||
}
|
||||
|
||||
checkNode(node, scope) {
|
||||
if (Array.isArray(node)) {
|
||||
for (var i = 0; i < node.length; i++) {
|
||||
this.checkNode(node[i], scope);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
var stack = this.transformerStack;
|
||||
scope ||= this.scope;
|
||||
|
||||
@@ -440,7 +473,33 @@ export default class File {
|
||||
});
|
||||
}
|
||||
|
||||
generate() {
|
||||
mergeSourceMap(map: Object) {
|
||||
var opts = this.opts;
|
||||
|
||||
var inputMap = opts.inputSourceMap;
|
||||
|
||||
if (inputMap) {
|
||||
map.sources[0] = inputMap.file;
|
||||
|
||||
var inputMapConsumer = new sourceMap.SourceMapConsumer(inputMap);
|
||||
var outputMapConsumer = new sourceMap.SourceMapConsumer(map);
|
||||
var outputMapGenerator = sourceMap.SourceMapGenerator.fromSourceMap(outputMapConsumer);
|
||||
outputMapGenerator.applySourceMap(inputMapConsumer);
|
||||
|
||||
var mergedMap = outputMapGenerator.toJSON();
|
||||
mergedMap.sources = inputMap.sources
|
||||
mergedMap.file = inputMap.file;
|
||||
return mergedMap;
|
||||
}
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
generate(): {
|
||||
code: string;
|
||||
map?: Object;
|
||||
ast?: Object;
|
||||
} {
|
||||
var opts = this.opts;
|
||||
var ast = this.ast;
|
||||
|
||||
@@ -463,11 +522,16 @@ export default class File {
|
||||
|
||||
if (this.shebang) {
|
||||
// add back shebang
|
||||
result.code = this.shebang + "\n" + result.code;
|
||||
result.code = `${this.shebang}\n${result.code}`;
|
||||
}
|
||||
|
||||
result.map = this.mergeSourceMap(result.map);
|
||||
|
||||
if (opts.sourceMap === "inline" || opts.sourceMap === "both") {
|
||||
result.code += "\n" + convertSourceMap.fromObject(result.map).toComment();
|
||||
}
|
||||
|
||||
if (opts.sourceMap === "inline") {
|
||||
result.code += "\n" + sourceMapToComment(result.map);
|
||||
result.map = null;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import explode from "./explode-assignable-expression";
|
||||
import t from "../../types";
|
||||
import * as t from "../../types";
|
||||
|
||||
module.exports = function (exports, opts) {
|
||||
export default function (exports, opts) {
|
||||
var isAssignment = function (node) {
|
||||
return node.operator === opts.operator + "=";
|
||||
};
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import t from "../../types";
|
||||
import * as t from "../../types";
|
||||
|
||||
module.exports = function build(node, buildBody) {
|
||||
export default function build(node, buildBody) {
|
||||
var self = node.blocks.shift();
|
||||
if (!self) return;
|
||||
|
||||
@@ -20,4 +20,4 @@ module.exports = function build(node, buildBody) {
|
||||
self.right,
|
||||
t.blockStatement([child])
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import explode from "./explode-assignable-expression";
|
||||
import t from "../../types";
|
||||
import * as t from "../../types";
|
||||
|
||||
module.exports = function (exports, opts) {
|
||||
export default function (exports, opts) {
|
||||
var buildAssignment = function (left, right) {
|
||||
return t.assignmentExpression("=", left, right);
|
||||
};
|
||||
|
||||
@@ -7,9 +7,9 @@ import isString from "lodash/lang/isString";
|
||||
import * as messages from "../../messages";
|
||||
import esutils from "esutils";
|
||||
import * as react from "./react";
|
||||
import t from "../../types";
|
||||
import * as t from "../../types";
|
||||
|
||||
module.exports = function (exports, opts) {
|
||||
export default function (exports, opts) {
|
||||
exports.check = function (node) {
|
||||
if (t.isJSX(node)) return true;
|
||||
if (react.isCreateClass(node)) return true;
|
||||
|
||||
@@ -1,39 +1,45 @@
|
||||
import cloneDeep from "lodash/lang/cloneDeep";
|
||||
import traverse from "../../traversal";
|
||||
import clone from "lodash/lang/clone";
|
||||
import each from "lodash/collection/each";
|
||||
import has from "lodash/object/has";
|
||||
import t from "../../types";
|
||||
import * as t from "../../types";
|
||||
|
||||
export function push(mutatorMap, key, kind, computed, value) {
|
||||
var alias;
|
||||
var alias = t.toKeyAlias({ computed }, key);
|
||||
|
||||
if (t.isIdentifier(key)) {
|
||||
alias = key.name;
|
||||
if (computed) alias = "computed:" + alias;
|
||||
} else if (t.isLiteral(key)) {
|
||||
alias = String(key.value);
|
||||
} else {
|
||||
alias = JSON.stringify(traverse.removeProperties(cloneDeep(key)));
|
||||
}
|
||||
|
||||
var map;
|
||||
if (has(mutatorMap, alias)) {
|
||||
map = mutatorMap[alias];
|
||||
} else {
|
||||
map = {};
|
||||
}
|
||||
var map = {};
|
||||
if (has(mutatorMap, alias)) map = mutatorMap[alias];
|
||||
mutatorMap[alias] = map;
|
||||
|
||||
map._key = key;
|
||||
if (computed) {
|
||||
map._computed = true;
|
||||
}
|
||||
if (computed) map._computed = true;
|
||||
|
||||
map[kind] = value;
|
||||
}
|
||||
|
||||
export function build(mutatorMap) {
|
||||
export function hasComputed(mutatorMap) {
|
||||
for (var key in mutatorMap) {
|
||||
if (mutatorMap[key]._computed) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
export function toComputedObjectFromClass(obj) {
|
||||
var objExpr = t.arrayExpression([]);
|
||||
|
||||
for (var i = 0; i < obj.properties.length; i++) {
|
||||
var prop = obj.properties[i];
|
||||
var val = prop.value;
|
||||
val.properties.unshift(t.property("init", t.identifier("key"), t.toComputedKey(prop)));
|
||||
objExpr.elements.push(val);
|
||||
}
|
||||
|
||||
return objExpr;
|
||||
}
|
||||
|
||||
export function toClassObject(mutatorMap) {
|
||||
var objExpr = t.objectExpression([]);
|
||||
|
||||
each(mutatorMap, function (map) {
|
||||
@@ -41,28 +47,16 @@ export function build(mutatorMap) {
|
||||
|
||||
var propNode = t.property("init", map._key, mapNode, map._computed);
|
||||
|
||||
if (!map.get && !map.set) {
|
||||
map.writable = 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) {
|
||||
if (key[0] === "_") return;
|
||||
|
||||
node = clone(node);
|
||||
var inheritNode = node;
|
||||
if (t.isMethodDefinition(node)) node = node.value;
|
||||
|
||||
var prop = t.property("init", t.identifier(key), node);
|
||||
t.inheritsComments(prop, inheritNode);
|
||||
t.removeComments(inheritNode);
|
||||
|
||||
mapNode.properties.push(prop);
|
||||
});
|
||||
|
||||
@@ -71,3 +65,13 @@ export function build(mutatorMap) {
|
||||
|
||||
return objExpr;
|
||||
}
|
||||
|
||||
export function toDefineObject(mutatorMap) {
|
||||
each(mutatorMap, function (map) {
|
||||
if (map.value) map.writable = t.literal(true);
|
||||
map.configurable = t.literal(true);
|
||||
map.enumerable = t.literal(true);
|
||||
});
|
||||
|
||||
return toClassObject(mutatorMap);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import t from "../../types";
|
||||
import * as t from "../../types";
|
||||
|
||||
var getObjRef = function (node, nodes, file, scope) {
|
||||
var ref;
|
||||
@@ -23,7 +23,7 @@ var getObjRef = function (node, nodes, file, scope) {
|
||||
return ref;
|
||||
}
|
||||
} else {
|
||||
throw new Error("We can't explode this node type " + node.type);
|
||||
throw new Error(`We can't explode this node type ${node.type}`);
|
||||
}
|
||||
|
||||
var temp = scope.generateUidBasedOnNode(ref);
|
||||
@@ -45,7 +45,7 @@ var getPropRef = function (node, nodes, file, scope) {
|
||||
return temp;
|
||||
};
|
||||
|
||||
module.exports = function (node, nodes, file, scope, allowedSingleIdent) {
|
||||
export default function (node, nodes, file, scope, allowedSingleIdent) {
|
||||
var obj;
|
||||
if (t.isIdentifier(node) && allowedSingleIdent) {
|
||||
obj = node;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import t from "../../types";
|
||||
import * as t from "../../types";
|
||||
|
||||
module.exports = function (node) {
|
||||
export default function (node) {
|
||||
var lastNonDefault = 0;
|
||||
for (var i = 0; i < node.params.length; i++) {
|
||||
if (!t.isAssignmentPattern(node.params[i])) lastNonDefault = i + 1;
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
import getFunctionArity from "./get-function-arity";
|
||||
import * as util from "../../util";
|
||||
import t from "../../types";
|
||||
import * as t from "../../types";
|
||||
|
||||
var visitor = {
|
||||
enter(node, parent, scope, state) {
|
||||
// check if this node is a referenced identifier that matches the same as our
|
||||
// function id
|
||||
if (!t.isReferencedIdentifier(node, parent, { name: state.name })) return;
|
||||
if (!this.isReferencedIdentifier({ name: state.name })) return;
|
||||
|
||||
// check that we don't have a local variable declared as that removes the need
|
||||
// for the wrapper
|
||||
@@ -89,6 +89,11 @@ var visit = function (node, name, scope) {
|
||||
return state;
|
||||
};
|
||||
|
||||
export function custom(node, id, scope) {
|
||||
var state = visit(node, id.name, scope);
|
||||
return wrap(state, node, id, scope);
|
||||
}
|
||||
|
||||
export function property(node, file, scope) {
|
||||
var key = t.toComputedKey(node, node.key);
|
||||
if (!t.isLiteral(key)) return node; // we can't set a function id with this
|
||||
@@ -103,7 +108,7 @@ export function property(node, file, scope) {
|
||||
|
||||
export function bare(node, parent, scope) {
|
||||
// has an `id` so we don't need to infer one
|
||||
if (node.id) return;
|
||||
if (node.id) return node;
|
||||
|
||||
var id;
|
||||
if (t.isProperty(parent) && parent.kind === "init" && !parent.computed) {
|
||||
@@ -113,10 +118,10 @@ export function bare(node, parent, scope) {
|
||||
// var foo = function () {};
|
||||
id = parent.id;
|
||||
} else {
|
||||
return;
|
||||
return node;
|
||||
}
|
||||
|
||||
if (!t.isIdentifier(id)) return;
|
||||
if (!t.isIdentifier(id)) return node;
|
||||
|
||||
var name = t.toIdentifier(id.name);
|
||||
id = t.identifier(name);
|
||||
|
||||
2
src/babel/transformation/helpers/react.js
vendored
2
src/babel/transformation/helpers/react.js
vendored
@@ -1,4 +1,4 @@
|
||||
import t from "../../types";
|
||||
import * as t from "../../types";
|
||||
|
||||
var isCreateClassCallExpression = t.buildMatchMemberExpression("React.createClass");
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import pull from "lodash/array/pull";
|
||||
import t from "../../types";
|
||||
import * as t from "../../types";
|
||||
|
||||
export function is(node, flag) {
|
||||
return t.isLiteral(node) && node.regex && node.regex.flags.indexOf(flag) >= 0;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import t from "../../types";
|
||||
import * as t from "../../types";
|
||||
|
||||
var visitor = {
|
||||
enter(node) {
|
||||
var awaitVisitor = {
|
||||
enter(node, parent, scope, state) {
|
||||
if (t.isFunction(node)) this.skip();
|
||||
|
||||
if (t.isAwaitExpression(node)) {
|
||||
@@ -16,15 +16,25 @@ var visitor = {
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = function (node, callId, scope) {
|
||||
var referenceVisitor = {
|
||||
enter(node, parent, scope, state) {
|
||||
var name = state.id.name;
|
||||
if (t.isReferencedIdentifier(node, parent, { name: name }) && scope.bindingIdentifierEquals(name, state.id)) {
|
||||
return state.ref ||= scope.generateUidIdentifier(name);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export default function (node, callId, scope) {
|
||||
node.async = false;
|
||||
node.generator = true;
|
||||
|
||||
scope.traverse(node, visitor);
|
||||
scope.traverse(node, awaitVisitor, state);
|
||||
|
||||
var call = t.callExpression(callId, [node]);
|
||||
|
||||
var id = node.id;
|
||||
delete node.id;
|
||||
node.id = null;
|
||||
|
||||
if (t.isFunctionDeclaration(node)) {
|
||||
var declar = t.variableDeclaration("let", [
|
||||
@@ -33,6 +43,16 @@ module.exports = function (node, callId, scope) {
|
||||
declar._blockHoist = true;
|
||||
return declar;
|
||||
} else {
|
||||
if (id) {
|
||||
var state = { id: id };
|
||||
scope.traverse(node, referenceVisitor, state);
|
||||
|
||||
if (state.ref) {
|
||||
scope.parent.push({ id: state.ref });
|
||||
return t.assignmentExpression("=", state.ref, call);
|
||||
}
|
||||
}
|
||||
|
||||
return call;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
module.exports = ReplaceSupers;
|
||||
|
||||
import * as messages from "../../messages";
|
||||
import t from "../../types";
|
||||
import * as t from "../../types";
|
||||
|
||||
|
||||
function isIllegalBareSuper(node, parent) {
|
||||
@@ -47,22 +47,23 @@ export default class ReplaceSupers {
|
||||
|
||||
/**
|
||||
* Description
|
||||
*
|
||||
* @param {Object} opts
|
||||
* @param {Boolean} [inClass]
|
||||
*/
|
||||
|
||||
constructor(opts, inClass) {
|
||||
constructor(opts: Object, inClass?: boolean = false) {
|
||||
this.topLevelThisReference = opts.topLevelThisReference;
|
||||
this.methodNode = opts.methodNode;
|
||||
this.className = opts.className;
|
||||
this.superName = opts.superName;
|
||||
this.superRef = opts.superRef;
|
||||
this.isStatic = opts.isStatic;
|
||||
this.hasSuper = false;
|
||||
this.inClass = inClass;
|
||||
this.isLoose = opts.isLoose;
|
||||
this.scope = opts.scope;
|
||||
this.file = opts.file;
|
||||
this.opts = opts;
|
||||
}
|
||||
|
||||
getObjectRef() {
|
||||
return this.opts.objectRef || this.opts.getObjectRef();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -72,22 +73,16 @@ export default class ReplaceSupers {
|
||||
*
|
||||
* _set(Object.getPrototypeOf(CLASS.prototype), "METHOD", "VALUE", this)
|
||||
*
|
||||
* @param {Node} property
|
||||
* @param {Node} value
|
||||
* @param {Boolean} isComputed
|
||||
* @param {Node} thisExpression
|
||||
*
|
||||
* @returns {Node}
|
||||
*/
|
||||
|
||||
setSuperProperty(property, value, isComputed, thisExpression) {
|
||||
setSuperProperty(property: Object, value: Object, isComputed: boolean, thisExpression: Object): Object {
|
||||
return t.callExpression(
|
||||
this.file.addHelper("set"),
|
||||
[
|
||||
t.callExpression(
|
||||
t.memberExpression(t.identifier("Object"), t.identifier("getPrototypeOf")),
|
||||
[
|
||||
this.isStatic ? this.className : t.memberExpression(this.className, t.identifier("prototype"))
|
||||
this.isStatic ? this.getObjectRef() : t.memberExpression(this.getObjectRef(), t.identifier("prototype"))
|
||||
]
|
||||
),
|
||||
isComputed ? property : t.literal(property.name),
|
||||
@@ -104,21 +99,16 @@ export default class ReplaceSupers {
|
||||
*
|
||||
* _get(Object.getPrototypeOf(CLASS.prototype), "METHOD", this)
|
||||
*
|
||||
* @param {Node} property
|
||||
* @param {Boolean} isComputed
|
||||
* @param {Node} thisExpression
|
||||
*
|
||||
* @returns {Node}
|
||||
*/
|
||||
|
||||
getSuperProperty(property, isComputed, thisExpression) {
|
||||
getSuperProperty(property: Object, isComputed: boolean, thisExpression: Object): Object {
|
||||
return t.callExpression(
|
||||
this.file.addHelper("get"),
|
||||
[
|
||||
t.callExpression(
|
||||
t.memberExpression(t.identifier("Object"), t.identifier("getPrototypeOf")),
|
||||
[
|
||||
this.isStatic ? this.className : t.memberExpression(this.className, t.identifier("prototype"))
|
||||
this.isStatic ? this.getObjectRef() : t.memberExpression(this.getObjectRef(), t.identifier("prototype"))
|
||||
]
|
||||
),
|
||||
isComputed ? property : t.literal(property.name),
|
||||
@@ -137,12 +127,9 @@ export default class ReplaceSupers {
|
||||
|
||||
/**
|
||||
* Description
|
||||
*
|
||||
* @param {Object} node
|
||||
* @param {Boolean} topLevel
|
||||
*/
|
||||
|
||||
traverseLevel(node, topLevel) {
|
||||
traverseLevel(node: Object, topLevel: boolean) {
|
||||
var state = { self: this, topLevel: topLevel };
|
||||
this.scope.traverse(node, visitor, state);
|
||||
}
|
||||
@@ -165,29 +152,24 @@ export default class ReplaceSupers {
|
||||
|
||||
/**
|
||||
* Description
|
||||
*
|
||||
* @param {Object} node
|
||||
* @param {Object} id
|
||||
* @param {Object} parent
|
||||
* @returns {Object}
|
||||
*/
|
||||
|
||||
getLooseSuperProperty(id, parent) {
|
||||
getLooseSuperProperty(id: Object, parent: Object) {
|
||||
var methodNode = this.methodNode;
|
||||
var methodName = methodNode.key;
|
||||
var superName = this.superName || t.identifier("Function");
|
||||
var superRef = this.superRef || t.identifier("Function");
|
||||
|
||||
if (parent.property === id) {
|
||||
return;
|
||||
} else if (t.isCallExpression(parent, { callee: id })) {
|
||||
// super(); -> ClassName.prototype.MethodName.call(this);
|
||||
// super(); -> objectRef.prototype.MethodName.call(this);
|
||||
parent.arguments.unshift(t.thisExpression());
|
||||
|
||||
if (methodName.name === "constructor") {
|
||||
// constructor() { super(); }
|
||||
return t.memberExpression(superName, t.identifier("call"));
|
||||
return t.memberExpression(superRef, t.identifier("call"));
|
||||
} else {
|
||||
id = superName;
|
||||
id = superRef;
|
||||
|
||||
// foo() { super(); }
|
||||
if (!methodNode.static) {
|
||||
@@ -198,22 +180,18 @@ export default class ReplaceSupers {
|
||||
return t.memberExpression(id, t.identifier("call"));
|
||||
}
|
||||
} else if (t.isMemberExpression(parent) && !methodNode.static) {
|
||||
// super.test -> ClassName.prototype.test
|
||||
return t.memberExpression(superName, t.identifier("prototype"));
|
||||
// super.test -> objectRef.prototype.test
|
||||
return t.memberExpression(superRef, t.identifier("prototype"));
|
||||
} else {
|
||||
return superName;
|
||||
return superRef;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Description
|
||||
*
|
||||
* @param {Function} getThisReference
|
||||
* @param {Object} node
|
||||
* @param {Object} parent
|
||||
*/
|
||||
|
||||
looseHandle(getThisReference, node, parent) {
|
||||
looseHandle(getThisReference: Function, node: Object, parent: Object) {
|
||||
if (t.isIdentifier(node, { name: "super" })) {
|
||||
this.hasSuper = true;
|
||||
return this.getLooseSuperProperty(node, parent);
|
||||
@@ -222,7 +200,7 @@ export default class ReplaceSupers {
|
||||
if (!t.isMemberExpression(callee)) return;
|
||||
if (callee.object.name !== "super") return;
|
||||
|
||||
// super.test(); -> ClassName.prototype.MethodName.call(this);
|
||||
// super.test(); -> objectRef.prototype.MethodName.call(this);
|
||||
this.hasSuper = true;
|
||||
t.appendToMemberExpression(callee, t.identifier("call"));
|
||||
node.arguments.unshift(getThisReference());
|
||||
@@ -231,13 +209,9 @@ export default class ReplaceSupers {
|
||||
|
||||
/**
|
||||
* Description
|
||||
*
|
||||
* @param {Function} getThisReference
|
||||
* @param {Object} node
|
||||
* @param {Object} parent
|
||||
*/
|
||||
|
||||
specHandle(getThisReference, node, parent) {
|
||||
specHandle(getThisReference: Function, node: Object, parent: Object) {
|
||||
var methodNode = this.methodNode;
|
||||
var property;
|
||||
var computed;
|
||||
@@ -251,7 +225,7 @@ export default class ReplaceSupers {
|
||||
if (t.isCallExpression(node)) {
|
||||
var callee = node.callee;
|
||||
if (isSuper(callee, node)) {
|
||||
// super(); -> _get(Object.getPrototypeOf(ClassName), "MethodName", this).call(this);
|
||||
// super(); -> _get(Object.getPrototypeOf(objectRef), "MethodName", this).call(this);
|
||||
property = methodNode.key;
|
||||
computed = methodNode.computed;
|
||||
args = node.arguments;
|
||||
@@ -264,17 +238,17 @@ export default class ReplaceSupers {
|
||||
throw this.file.errorWithNode(node, messages.get("classesIllegalSuperCall", methodName));
|
||||
}
|
||||
} else if (t.isMemberExpression(callee) && isSuper(callee.object, callee)) {
|
||||
// super.test(); -> _get(Object.getPrototypeOf(ClassName.prototype), "test", this).call(this);
|
||||
// super.test(); -> _get(Object.getPrototypeOf(objectRef.prototype), "test", this).call(this);
|
||||
property = callee.property;
|
||||
computed = callee.computed;
|
||||
args = node.arguments;
|
||||
}
|
||||
} else if (t.isMemberExpression(node) && isSuper(node.object, node)) {
|
||||
// super.name; -> _get(Object.getPrototypeOf(ClassName.prototype), "name", this);
|
||||
// super.name; -> _get(Object.getPrototypeOf(objectRef.prototype), "name", this);
|
||||
property = node.property;
|
||||
computed = node.computed;
|
||||
} else if (t.isAssignmentExpression(node) && isSuper(node.left.object, node.left) && methodNode.kind === "set") {
|
||||
// super.name = "val"; -> _set(Object.getPrototypeOf(ClassName.prototype), "name", this);
|
||||
// super.name = "val"; -> _set(Object.getPrototypeOf(objectRef.prototype), "name", this);
|
||||
this.hasSuper = true;
|
||||
return this.setSuperProperty(node.left.property, node.right, node.left.computed, getThisReference());
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import t from "../../types";
|
||||
import * as t from "../../types";
|
||||
|
||||
export function has(node) {
|
||||
var first = node.body[0];
|
||||
@@ -7,7 +7,7 @@ export function has(node) {
|
||||
|
||||
export function wrap(node, callback) {
|
||||
var useStrictNode;
|
||||
if (exports.has(node)) {
|
||||
if (has(node)) {
|
||||
useStrictNode = node.body.shift();
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ import object from "../helpers/object";
|
||||
import File from "./file";
|
||||
import each from "lodash/collection/each";
|
||||
|
||||
export default function transform(code, opts) {
|
||||
export default function transform(code: code, opts?: Object) {
|
||||
var file = new File(opts);
|
||||
return file.parse(code);
|
||||
}
|
||||
@@ -18,7 +18,7 @@ transform.fromAst = function (ast, code, opts) {
|
||||
return file.generate();
|
||||
};
|
||||
|
||||
transform._ensureTransformerNames = function (type, rawKeys) {
|
||||
transform._ensureTransformerNames = function (type: string, rawKeys: Array<string>) {
|
||||
var keys = [];
|
||||
|
||||
for (var i = 0; i < rawKeys.length; i++) {
|
||||
@@ -30,7 +30,7 @@ transform._ensureTransformerNames = function (type, rawKeys) {
|
||||
keys.push(aliasKey);
|
||||
} else if (deprecatedKey) {
|
||||
// deprecated key, remap it to the new one
|
||||
console.error("The transformer " + key + " has been renamed to " + deprecatedKey);
|
||||
console.error(`The transformer ${key} has been renamed to ${deprecatedKey}`);
|
||||
rawKeys.push(deprecatedKey);
|
||||
} else if (transform.transformers[key]) {
|
||||
// valid key
|
||||
@@ -40,7 +40,7 @@ transform._ensureTransformerNames = function (type, rawKeys) {
|
||||
keys = keys.concat(transform.namespaces[key]);
|
||||
} else {
|
||||
// invalid key
|
||||
throw new ReferenceError("Unknown transformer " + key + " specified in " + type);
|
||||
throw new ReferenceError(`Unknown transformer ${key} specified in ${type}`);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
22
src/babel/transformation/logger.js
Normal file
22
src/babel/transformation/logger.js
Normal file
@@ -0,0 +1,22 @@
|
||||
import * as util from "../util";
|
||||
|
||||
export default class Logger {
|
||||
constructor(file: File) {
|
||||
this.filename = file.opts.filename;
|
||||
this.file = file;
|
||||
}
|
||||
|
||||
_buildMessage(msg: string): string {
|
||||
var parts = this.filename;
|
||||
if (msg) parts += `: ${msg}`;
|
||||
return parts;
|
||||
}
|
||||
|
||||
debug(msg: string) {
|
||||
util.debug(this._buildMessage(msg));
|
||||
}
|
||||
|
||||
deopt(node: Object, msg: string) {
|
||||
util.debug(this._buildMessage(msg));
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@ import * as messages from "../../messages";
|
||||
import extend from "lodash/object/extend";
|
||||
import object from "../../helpers/object";
|
||||
import * as util from "../../util";
|
||||
import t from "../../types";
|
||||
import * as t from "../../types";
|
||||
|
||||
var remapVisitor = {
|
||||
enter(node, parent, scope, formatter) {
|
||||
@@ -42,8 +42,8 @@ var remapVisitor = {
|
||||
};
|
||||
|
||||
var importsVisitor = {
|
||||
enter(node, parent, scope, formatter) {
|
||||
if (t.isImportDeclaration(node)) {
|
||||
ImportDeclaration: {
|
||||
enter(node, parent, scope, formatter) {
|
||||
formatter.hasLocalImports = true;
|
||||
extend(formatter.localImports, t.getBindingIdentifiers(node));
|
||||
formatter.bumpImportOccurences(node);
|
||||
@@ -52,9 +52,9 @@ var importsVisitor = {
|
||||
};
|
||||
|
||||
var exportsVisitor = {
|
||||
enter(node, parent, scope, formatter) {
|
||||
var declar = node && node.declaration;
|
||||
if (t.isExportDeclaration(node)) {
|
||||
ExportDeclaration: {
|
||||
enter(node, parent, scope, formatter) {
|
||||
var declar = node.declaration;
|
||||
formatter.hasLocalImports = true;
|
||||
|
||||
if (declar && t.isStatement(declar)) {
|
||||
|
||||
@@ -3,7 +3,7 @@ import CommonFormatter from "./common";
|
||||
import includes from "lodash/collection/includes";
|
||||
import values from "lodash/object/values";
|
||||
import * as util from "../../util";
|
||||
import t from "../../types";
|
||||
import * as t from "../../types";
|
||||
|
||||
export default class AMDFormatter extends DefaultFormatter {
|
||||
init = CommonFormatter.prototype.init;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import DefaultFormatter from "./_default";
|
||||
import includes from "lodash/collection/includes";
|
||||
import * as util from "../../util";
|
||||
import t from "../../types";
|
||||
import * as t from "../../types";
|
||||
|
||||
export default class CommonJSFormatter extends DefaultFormatter {
|
||||
init() {
|
||||
@@ -13,7 +13,7 @@ export default class CommonJSFormatter extends DefaultFormatter {
|
||||
if (!this.noInteropRequireImport && this.hasNonDefaultExports) {
|
||||
var templateName = "exports-module-declaration";
|
||||
if (this.file.isLoose("es6.modules")) templateName += "-loose";
|
||||
file.ast.program.body.push(util.template(templateName, true));
|
||||
file.ast.program.body.unshift(util.template(templateName, true));
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import t from "../../types";
|
||||
import * as t from "../../types";
|
||||
|
||||
export default class IgnoreFormatter {
|
||||
exportDeclaration(node, nodes) {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
module.exports = {
|
||||
export default {
|
||||
commonStrict: require("./common-strict"),
|
||||
amdStrict: require("./amd-strict"),
|
||||
umdStrict: require("./umd-strict"),
|
||||
|
||||
@@ -4,7 +4,7 @@ import * as util from "../../util";
|
||||
import last from "lodash/array/last";
|
||||
import each from "lodash/collection/each";
|
||||
import map from "lodash/collection/map";
|
||||
import t from "../../types";
|
||||
import * as t from "../../types";
|
||||
|
||||
var hoistVariablesVisitor = {
|
||||
enter(node, parent, scope, hoistDeclarators) {
|
||||
@@ -142,9 +142,9 @@ export default class SystemFormatter extends AMDFormatter {
|
||||
|
||||
return t.arrayExpression(map(this.ids, function (uid, source) {
|
||||
var state = {
|
||||
hoistDeclarators: hoistDeclarators,
|
||||
source: source,
|
||||
nodes: [],
|
||||
hoistDeclarators: hoistDeclarators
|
||||
nodes: []
|
||||
};
|
||||
|
||||
scope.traverse(block, runnerSettersVisitor, state);
|
||||
@@ -161,11 +161,11 @@ export default class SystemFormatter extends AMDFormatter {
|
||||
var block = t.blockStatement(program.body);
|
||||
|
||||
var runner = util.template("system", {
|
||||
MODULE_NAME: moduleNameLiteral,
|
||||
MODULE_DEPENDENCIES: t.arrayExpression(this.buildDependencyLiterals()),
|
||||
EXPORT_IDENTIFIER: this.exportIdentifier,
|
||||
SETTERS: this.buildRunnerSetters(block, hoistDeclarators),
|
||||
EXECUTE: t.functionExpression(null, [], block)
|
||||
EXPORT_IDENTIFIER: this.exportIdentifier,
|
||||
MODULE_NAME: moduleNameLiteral,
|
||||
SETTERS: this.buildRunnerSetters(block, hoistDeclarators),
|
||||
EXECUTE: t.functionExpression(null, [], block)
|
||||
}, true);
|
||||
|
||||
var handlerBody = runner.expression.arguments[2].body.body;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import AMDFormatter from "./amd";
|
||||
import values from "lodash/object/values";
|
||||
import * as util from "../../util";
|
||||
import t from "../../types";
|
||||
import * as t from "../../types";
|
||||
|
||||
export default class UMDFormatter extends AMDFormatter {
|
||||
transform(program) {
|
||||
|
||||
16
src/babel/transformation/templates/create-class.js
Normal file
16
src/babel/transformation/templates/create-class.js
Normal file
@@ -0,0 +1,16 @@
|
||||
(function() {
|
||||
function defineProperties(target, props) {
|
||||
for (var key in props) {
|
||||
var prop = props[key];
|
||||
prop.configurable = true;
|
||||
if (prop.value) prop.writable = true;
|
||||
}
|
||||
Object.defineProperties(target, props);
|
||||
}
|
||||
|
||||
return function (Constructor, protoProps, staticProps) {
|
||||
if (protoProps) defineProperties(Constructor.prototype, protoProps);
|
||||
if (staticProps) defineProperties(Constructor, staticProps);
|
||||
return Constructor;
|
||||
};
|
||||
})()
|
||||
16
src/babel/transformation/templates/create-computed-class.js
Normal file
16
src/babel/transformation/templates/create-computed-class.js
Normal file
@@ -0,0 +1,16 @@
|
||||
(function() {
|
||||
function defineProperties(target, props) {
|
||||
for (var i = 0; i < props.length; i ++) {
|
||||
var prop = props[i];
|
||||
prop.configurable = true;
|
||||
if (prop.value) prop.writable = true;
|
||||
Object.defineProperty(target, prop.key, prop);
|
||||
}
|
||||
}
|
||||
|
||||
return function (Constructor, protoProps, staticProps) {
|
||||
if (protoProps) defineProperties(Constructor.prototype, protoProps);
|
||||
if (staticProps) defineProperties(Constructor, staticProps);
|
||||
return Constructor;
|
||||
};
|
||||
})()
|
||||
@@ -1,4 +0,0 @@
|
||||
(function (child, staticProps, instanceProps) {
|
||||
if (staticProps) Object.defineProperties(child, staticProps);
|
||||
if (instanceProps) Object.defineProperties(child.prototype, instanceProps);
|
||||
})
|
||||
@@ -1,2 +1,2 @@
|
||||
var helpers = exports.default = {};
|
||||
exports.default = HELPER;
|
||||
exports.__esModule = true;
|
||||
|
||||
11
src/babel/transformation/templates/umd-commonjs-strict.js
Normal file
11
src/babel/transformation/templates/umd-commonjs-strict.js
Normal file
@@ -0,0 +1,11 @@
|
||||
(function (root, factory) {
|
||||
if (typeof define === "function" && define.amd) {
|
||||
define(AMD_ARGUMENTS, factory);
|
||||
} else if (typeof exports === 'object') {
|
||||
factory(COMMON_ARGUMENTS);
|
||||
} else {
|
||||
factory(BROWSER_ARGUMENTS);
|
||||
}
|
||||
})(UMD_ROOT, function (FACTORY_PARAMETERS) {
|
||||
FACTORY_BODY
|
||||
});
|
||||
@@ -6,14 +6,15 @@ import includes from "lodash/collection/includes";
|
||||
*/
|
||||
|
||||
export default class TransformerPass {
|
||||
constructor(file, transformer) {
|
||||
constructor(file: File, transformer: Transformer) {
|
||||
this.transformer = transformer;
|
||||
this.shouldRun = !transformer.check;
|
||||
this.handlers = transformer.handlers;
|
||||
this.file = file;
|
||||
this.ran = false;
|
||||
}
|
||||
|
||||
canRun() {
|
||||
canRun(): boolean {
|
||||
var transformer = this.transformer;
|
||||
|
||||
var opts = this.file.opts;
|
||||
@@ -42,7 +43,7 @@ export default class TransformerPass {
|
||||
return true;
|
||||
}
|
||||
|
||||
checkNode(node) {
|
||||
checkNode(node: Object): boolean {
|
||||
var check = this.transformer.check;
|
||||
if (check) {
|
||||
return this.shouldRun = check(node);
|
||||
@@ -56,8 +57,10 @@ export default class TransformerPass {
|
||||
|
||||
var file = this.file;
|
||||
|
||||
file.debug("Running transformer " + this.transformer.key);
|
||||
file.log.debug(`Running transformer ${this.transformer.key}`);
|
||||
|
||||
file.scope.traverse(file.ast, this.handlers, file);
|
||||
|
||||
this.ran = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ import each from "lodash/collection/each";
|
||||
*/
|
||||
|
||||
export default class Transformer {
|
||||
constructor(transformerKey, transformer, opts) {
|
||||
constructor(transformerKey: key, transformer: Object, opts: Object) {
|
||||
transformer = assign({}, transformer);
|
||||
|
||||
var take = function (key) {
|
||||
@@ -36,7 +36,7 @@ export default class Transformer {
|
||||
this.key = transformerKey;
|
||||
}
|
||||
|
||||
normalize(transformer) {
|
||||
normalize(transformer: Object): Object {
|
||||
if (isFunction(transformer)) {
|
||||
transformer = { ast: transformer };
|
||||
}
|
||||
@@ -65,7 +65,7 @@ export default class Transformer {
|
||||
return transformer;
|
||||
}
|
||||
|
||||
buildPass(file) {
|
||||
buildPass(file: File): TransformerPass {
|
||||
return new TransformerPass(file, this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
{
|
||||
"selfContained": "runtime",
|
||||
"unicode-regex": "regex.unicode",
|
||||
"spec.typeofSymbol": "es6.symbols",
|
||||
|
||||
"minification.deadCodeElimination": "utility.deadCodeElimination",
|
||||
"minification.removeConsoleCalls": "utility.removeConsole",
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import t from "../../../types";
|
||||
import * as t from "../../../types";
|
||||
|
||||
export function MemberExpression(node) {
|
||||
var prop = node.property;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import t from "../../../types";
|
||||
import * as t from "../../../types";
|
||||
|
||||
export function Property(node) {
|
||||
var key = node.key;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import * as defineMap from "../../helpers/define-map";
|
||||
import t from "../../../types";
|
||||
import * as t from "../../../types";
|
||||
|
||||
export function check(node) {
|
||||
return t.isProperty(node) && (node.kind === "get" || node.kind === "set");
|
||||
@@ -23,6 +23,6 @@ export function ObjectExpression(node) {
|
||||
|
||||
return t.callExpression(
|
||||
t.memberExpression(t.identifier("Object"), t.identifier("defineProperties")),
|
||||
[node, defineMap.build(mutatorMap)]
|
||||
[node, defineMap.toDefineObject(mutatorMap)]
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import t from "../../../types";
|
||||
import * as t from "../../../types";
|
||||
|
||||
export var check = t.isArrowFunctionExpression;
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import t from "../../../types";
|
||||
import * as t from "../../../types";
|
||||
|
||||
var visitor = {
|
||||
enter(node, parent, scope, state) {
|
||||
if (!t.isReferencedIdentifier(node, parent)) return;
|
||||
if (!this.isReferencedIdentifier()) return;
|
||||
|
||||
var declared = state.letRefs[node.name];
|
||||
if (!declared) return;
|
||||
@@ -32,12 +32,10 @@ export function BlockStatement(node, parent, scope, file) {
|
||||
var letRefs = node._letReferences;
|
||||
if (!letRefs) return;
|
||||
|
||||
var state = {
|
||||
scope.traverse(node, visitor, {
|
||||
letRefs: letRefs,
|
||||
file: file
|
||||
};
|
||||
|
||||
scope.traverse(node, visitor, state);
|
||||
});
|
||||
}
|
||||
|
||||
export { BlockStatement as Program, BlockStatement as Loop };
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import traverse from "../../../traversal";
|
||||
import object from "../../../helpers/object";
|
||||
import * as util from "../../../util";
|
||||
import t from "../../../types";
|
||||
import * as t from "../../../types";
|
||||
import values from "lodash/object/values";
|
||||
import extend from "lodash/object/extend";
|
||||
|
||||
@@ -75,7 +75,7 @@ export function Loop(node, parent, scope, file) {
|
||||
|
||||
export function BlockStatement(block, parent, scope, file) {
|
||||
if (!t.isLoop(parent)) {
|
||||
var blockScoping = new BlockScoping(false, block, parent, scope, file);
|
||||
var blockScoping = new BlockScoping(null, block, parent, scope, file);
|
||||
blockScoping.run();
|
||||
}
|
||||
}
|
||||
@@ -109,7 +109,7 @@ function traverseReplace(node, parent, scope, remaps) {
|
||||
|
||||
var letReferenceBlockVisitor = {
|
||||
enter(node, parent, scope, state) {
|
||||
if (t.isFunction(node)) {
|
||||
if (this.isFunction()) {
|
||||
scope.traverse(node, letReferenceFunctionVisitor, state);
|
||||
return this.skip();
|
||||
}
|
||||
@@ -119,7 +119,7 @@ var letReferenceBlockVisitor = {
|
||||
var letReferenceFunctionVisitor = {
|
||||
enter(node, parent, scope, state) {
|
||||
// not a direct reference
|
||||
if (!t.isReferencedIdentifier(node, parent)) return;
|
||||
if (!this.isReferencedIdentifier()) return;
|
||||
|
||||
// this scope has a variable with the same name so it couldn't belong
|
||||
// to our let scope
|
||||
@@ -134,17 +134,17 @@ var letReferenceFunctionVisitor = {
|
||||
|
||||
var hoistVarDeclarationsVisitor = {
|
||||
enter(node, parent, scope, self) {
|
||||
if (t.isForStatement(node)) {
|
||||
if (this.isForStatement()) {
|
||||
if (isVar(node.init, node)) {
|
||||
node.init = t.sequenceExpression(self.pushDeclar(node.init));
|
||||
}
|
||||
} else if (t.isFor(node)) {
|
||||
} else if (this.isFor()) {
|
||||
if (isVar(node.left, node)) {
|
||||
node.left = node.left.declarations[0].id;
|
||||
}
|
||||
} else if (isVar(node, parent)) {
|
||||
return self.pushDeclar(node).map(t.expressionStatement);
|
||||
} else if (t.isFunction(node)) {
|
||||
} else if (this.isFunction()) {
|
||||
return this.skip();
|
||||
}
|
||||
}
|
||||
@@ -152,7 +152,7 @@ var hoistVarDeclarationsVisitor = {
|
||||
|
||||
var loopLabelVisitor = {
|
||||
enter(node, parent, scope, state) {
|
||||
if (t.isLabeledStatement(node)) {
|
||||
if (this.isLabeledStatement()) {
|
||||
state.innerLabels.push(node.label.name);
|
||||
}
|
||||
}
|
||||
@@ -170,13 +170,13 @@ var loopVisitor = {
|
||||
enter(node, parent, scope, state) {
|
||||
var replace;
|
||||
|
||||
if (t.isLoop(node)) {
|
||||
if (this.isLoop()) {
|
||||
state.ignoreLabeless = true;
|
||||
scope.traverse(node, loopVisitor, state);
|
||||
state.ignoreLabeless = false;
|
||||
}
|
||||
|
||||
if (t.isFunction(node) || t.isLoop(node)) {
|
||||
if (this.isFunction() || this.isLoop()) {
|
||||
return this.skip();
|
||||
}
|
||||
|
||||
@@ -189,7 +189,7 @@ var loopVisitor = {
|
||||
return;
|
||||
}
|
||||
|
||||
loopText = loopText + "|" + node.label.name;
|
||||
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
|
||||
@@ -204,7 +204,7 @@ var loopVisitor = {
|
||||
replace = t.literal(loopText);
|
||||
}
|
||||
|
||||
if (t.isReturnStatement(node)) {
|
||||
if (this.isReturnStatement()) {
|
||||
state.hasReturn = true;
|
||||
replace = t.objectExpression([
|
||||
t.property("init", t.identifier("v"), node.argument || t.identifier("undefined"))
|
||||
@@ -222,15 +222,9 @@ class BlockScoping {
|
||||
|
||||
/**
|
||||
* Description
|
||||
*
|
||||
* @param {Boolean|Node} loopParent
|
||||
* @param {Node} block
|
||||
* @param {Node} parent
|
||||
* @param {Scope} scope
|
||||
* @param {File} file
|
||||
*/
|
||||
|
||||
constructor(loopParent, block, parent, scope, file) {
|
||||
constructor(loopParent?: Object, block: Object, parent: Object, scope: Scope, file: File) {
|
||||
this.loopParent = loopParent;
|
||||
this.parent = parent;
|
||||
this.scope = scope;
|
||||
@@ -466,12 +460,9 @@ class BlockScoping {
|
||||
/**
|
||||
* Turn a `VariableDeclaration` into an array of `AssignmentExpressions` with
|
||||
* their declarations hoisted to before the closure wrapper.
|
||||
*
|
||||
* @param {Node} node VariableDeclaration
|
||||
* @returns {Array}
|
||||
*/
|
||||
|
||||
pushDeclar(node) {
|
||||
pushDeclar(node: { type: "VariableDeclaration" }): Array<Object> {
|
||||
this.body.push(t.variableDeclaration(node.kind, node.declarations.map(function (declar) {
|
||||
return t.variableDeclarator(declar.id);
|
||||
})));
|
||||
@@ -491,12 +482,9 @@ class BlockScoping {
|
||||
|
||||
/**
|
||||
* Push the closure to the body.
|
||||
*
|
||||
* @param {Node} ret Identifier
|
||||
* @param {Node} call CallExpression
|
||||
*/
|
||||
|
||||
build(ret, call) {
|
||||
build(ret: { type: "Identifier" }, call: { type: "CallExpression" }) {
|
||||
var has = this.has;
|
||||
if (has.hasReturn || has.hasBreakContinue) {
|
||||
this.buildHas(ret, call);
|
||||
@@ -507,12 +495,9 @@ class BlockScoping {
|
||||
|
||||
/**
|
||||
* Description
|
||||
*
|
||||
* @param {Node} ret Identifier
|
||||
* @param {Node} call CallExpression
|
||||
*/
|
||||
|
||||
buildHas(ret, call) {
|
||||
buildHas(ret: { type: "Identifier" }, call: { type: "CallExpression" }) {
|
||||
var body = this.body;
|
||||
|
||||
body.push(t.variableDeclaration("var", [
|
||||
|
||||
@@ -3,57 +3,81 @@ import * as nameMethod from "../../helpers/name-method";
|
||||
import * as defineMap from "../../helpers/define-map";
|
||||
import * as messages from "../../../messages";
|
||||
import * as util from "../../../util";
|
||||
import t from "../../../types";
|
||||
import traverse from "../../../traversal";
|
||||
import * as t from "../../../types";
|
||||
|
||||
export var check = t.isClass;
|
||||
|
||||
export function ClassDeclaration(node, parent, scope, file) {
|
||||
return new ClassTransformer(node, file, scope, true).run();
|
||||
return t.variableDeclaration("let", [
|
||||
t.variableDeclarator(node.id, t.toExpression(node))
|
||||
]);
|
||||
}
|
||||
|
||||
export function ClassExpression(node, parent, scope, file) {
|
||||
if (!node.id) {
|
||||
if (t.isProperty(parent) && parent.value === node && !parent.computed && t.isIdentifier(parent.key)) {
|
||||
// var o = { foo: class {} };
|
||||
node.id = parent.key;
|
||||
}
|
||||
return new ClassTransformer(node, parent, scope, file).run();
|
||||
}
|
||||
|
||||
if (t.isVariableDeclarator(parent) && t.isIdentifier(parent.id)) {
|
||||
// var foo = class {};
|
||||
node.id = parent.id;
|
||||
var verifyConstructorVisitor = traverse.explode({
|
||||
MethodDefinition: {
|
||||
enter() {
|
||||
this.skip();
|
||||
}
|
||||
},
|
||||
|
||||
Property: {
|
||||
enter(node) {
|
||||
if (node.method) this.skip();
|
||||
}
|
||||
},
|
||||
|
||||
CallExpression: {
|
||||
enter(node, parent, scope, state) {
|
||||
if (t.isIdentifier(node.callee, { name: "super" })) {
|
||||
state.hasBareSuper = true;
|
||||
|
||||
if (!state.hasSuper) {
|
||||
throw state.file.errorWithNode(node, "super call is only allowed in derived constructor");
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
ThisExpression: {
|
||||
enter(node, parent, scope, state) {
|
||||
if (state.hasSuper && !state.hasBareSuper) {
|
||||
throw state.file.errorWithNode(node, "'this' is not allowed before super()");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new ClassTransformer(node, file, scope, false).run();
|
||||
}
|
||||
});
|
||||
|
||||
class ClassTransformer {
|
||||
|
||||
/**
|
||||
* Description
|
||||
*
|
||||
* @param {Node} node
|
||||
* @param {File} file
|
||||
* @param {Scope} scope
|
||||
* @param {Boolean} isStatement
|
||||
*/
|
||||
|
||||
constructor(node, file, scope, isStatement) {
|
||||
this.isStatement = isStatement;
|
||||
this.scope = scope;
|
||||
this.node = node;
|
||||
this.file = file;
|
||||
constructor(node: Object, parent: Object, scope: Scope, file: File) {
|
||||
this.parent = parent;
|
||||
this.scope = scope;
|
||||
this.node = node;
|
||||
this.file = file;
|
||||
|
||||
this.hasInstanceMutators = false;
|
||||
this.hasStaticMutators = false;
|
||||
|
||||
this.instanceMutatorMap = {};
|
||||
this.staticMutatorMap = {};
|
||||
this.hasConstructor = false;
|
||||
this.className = node.id || scope.generateUidIdentifier("class");
|
||||
this.superName = node.superClass || t.identifier("Function");
|
||||
this.hasSuper = !!node.superClass;
|
||||
this.isLoose = file.isLoose("es6.classes");
|
||||
|
||||
this.hasConstructor = false;
|
||||
this.className = node.id;
|
||||
this.classRef = node.id || scope.generateUidIdentifier("class");
|
||||
|
||||
this.superName = node.superClass || t.identifier("Function");
|
||||
this.hasSuper = !!node.superClass;
|
||||
|
||||
this.isLoose = file.isLoose("es6.classes");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -66,39 +90,35 @@ class ClassTransformer {
|
||||
var superName = this.superName;
|
||||
var className = this.className;
|
||||
var classBody = this.node.body.body;
|
||||
var classRef = this.classRef;
|
||||
var file = this.file;
|
||||
|
||||
//
|
||||
|
||||
var body = this.body = [];
|
||||
|
||||
//
|
||||
|
||||
var constructorBody = t.blockStatement([
|
||||
t.expressionStatement(t.callExpression(file.addHelper("class-call-check"), [
|
||||
t.thisExpression(),
|
||||
className
|
||||
classRef
|
||||
]))
|
||||
]);
|
||||
|
||||
var constructor;
|
||||
if (this.node.id) {
|
||||
constructor = t.functionDeclaration(className, [], constructorBody);
|
||||
|
||||
if (this.className) {
|
||||
constructor = t.functionDeclaration(this.className, [], constructorBody);
|
||||
body.push(constructor);
|
||||
} else {
|
||||
var constructorName = null;
|
||||
// when a class has no parent and there is only a constructor or no body
|
||||
// then the constructor is not wrapped in a closure and needs to be named
|
||||
var containsOnlyConstructor = classBody.length === 1 && classBody[0].key.name === "constructor";
|
||||
if (!this.hasSuper && (classBody.length === 0 || containsOnlyConstructor)) {
|
||||
constructorName = className;
|
||||
}
|
||||
|
||||
constructor = t.functionExpression(constructorName, [], constructorBody);
|
||||
body.push(t.variableDeclaration("var", [
|
||||
t.variableDeclarator(className, constructor)
|
||||
]));
|
||||
constructor = t.functionExpression(null, [], constructorBody);
|
||||
}
|
||||
|
||||
this.constructor = constructor;
|
||||
|
||||
//
|
||||
|
||||
var closureParams = [];
|
||||
var closureArgs = [];
|
||||
|
||||
@@ -107,40 +127,40 @@ class ClassTransformer {
|
||||
if (this.hasSuper) {
|
||||
closureArgs.push(superName);
|
||||
|
||||
if (!t.isIdentifier(superName)) {
|
||||
superName = this.scope.generateUidBasedOnNode(superName, this.file);
|
||||
}
|
||||
|
||||
superName = this.scope.generateUidBasedOnNode(superName, this.file);
|
||||
closureParams.push(superName);
|
||||
|
||||
this.superName = superName;
|
||||
body.push(t.expressionStatement(t.callExpression(file.addHelper("inherits"), [className, superName])));
|
||||
body.push(t.expressionStatement(t.callExpression(file.addHelper("inherits"), [classRef, superName])));
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
this.buildBody();
|
||||
|
||||
t.inheritsComments(body[0], this.node);
|
||||
|
||||
var init;
|
||||
|
||||
if (body.length === 1) {
|
||||
// only a constructor so no need for a closure container
|
||||
init = t.toExpression(constructor);
|
||||
if (this.className) {
|
||||
// named class with only a constructor
|
||||
if (body.length === 1) return t.toExpression(body[0]);
|
||||
} else {
|
||||
body.push(t.returnStatement(className));
|
||||
init = t.callExpression(
|
||||
t.functionExpression(null, closureParams, t.blockStatement(body)),
|
||||
closureArgs
|
||||
);
|
||||
// infer class name if this is a nameless class expression
|
||||
constructor = nameMethod.bare(constructor, this.parent, this.scope);
|
||||
|
||||
body.unshift(t.variableDeclaration("var", [
|
||||
t.variableDeclarator(classRef, constructor)
|
||||
]));
|
||||
|
||||
t.inheritsComments(body[0], this.node);
|
||||
}
|
||||
|
||||
if (this.isStatement) {
|
||||
return t.variableDeclaration("let", [
|
||||
t.variableDeclarator(className, init)
|
||||
]);
|
||||
} else {
|
||||
return init;
|
||||
}
|
||||
//
|
||||
|
||||
body.push(t.returnStatement(classRef));
|
||||
|
||||
return t.callExpression(
|
||||
t.functionExpression(null, closureParams, t.blockStatement(body)),
|
||||
closureArgs
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -157,18 +177,22 @@ class ClassTransformer {
|
||||
for (var i = 0; i < classBody.length; i++) {
|
||||
var node = classBody[i];
|
||||
if (t.isMethodDefinition(node)) {
|
||||
var isConstructor = (!node.computed && t.isIdentifier(node.key, { name: "constructor" })) || t.isLiteral(node.key, { value: "constructor" });
|
||||
if (isConstructor) this.verifyConstructor(node);
|
||||
|
||||
var replaceSupers = new ReplaceSupers({
|
||||
methodNode: node,
|
||||
className: this.className,
|
||||
superName: this.superName,
|
||||
objectRef: this.classRef,
|
||||
superRef: this.superName,
|
||||
isStatic: node.static,
|
||||
isLoose: this.isLoose,
|
||||
scope: this.scope,
|
||||
file: this.file
|
||||
}, true);
|
||||
|
||||
replaceSupers.replace();
|
||||
|
||||
if ((!node.computed && t.isIdentifier(node.key, { name: "constructor" })) || t.isLiteral(node.key, { value: "constructor" })) {
|
||||
if (isConstructor) {
|
||||
this.pushConstructor(node);
|
||||
} else {
|
||||
this.pushMethod(node);
|
||||
@@ -182,7 +206,7 @@ class ClassTransformer {
|
||||
}
|
||||
|
||||
// we have no constructor, we have a super, and the super doesn't appear to be falsy
|
||||
if (!this.hasConstructor && this.hasSuper && t.evaluateTruthy(superName) !== false) {
|
||||
if (!this.hasConstructor && this.hasSuper && t.evaluateTruthy(superName, this.scope) !== false) {
|
||||
var helperName = "class-super-constructor-call";
|
||||
if (this.isLoose) helperName += "-loose";
|
||||
constructor.body.body.push(util.template(helperName, {
|
||||
@@ -193,34 +217,59 @@ class ClassTransformer {
|
||||
|
||||
var instanceProps;
|
||||
var staticProps;
|
||||
var classHelper = "create-class";
|
||||
|
||||
if (this.hasInstanceMutators) {
|
||||
instanceProps = defineMap.build(this.instanceMutatorMap);
|
||||
instanceProps = defineMap.toClassObject(this.instanceMutatorMap);
|
||||
}
|
||||
|
||||
if (this.hasStaticMutators) {
|
||||
staticProps = defineMap.build(this.staticMutatorMap);
|
||||
staticProps = defineMap.toClassObject(this.staticMutatorMap);
|
||||
}
|
||||
|
||||
if (instanceProps || staticProps) {
|
||||
staticProps ||= t.literal(null);
|
||||
if (defineMap.hasComputed(this.instanceMutatorMap) || defineMap.hasComputed(this.staticMutatorMap)) {
|
||||
if (instanceProps) instanceProps = defineMap.toComputedObjectFromClass(instanceProps);
|
||||
if (staticProps) staticProps = defineMap.toComputedObjectFromClass(staticProps);
|
||||
classHelper = "create-computed-class";
|
||||
}
|
||||
|
||||
var args = [className, staticProps];
|
||||
if (instanceProps) args.push(instanceProps);
|
||||
instanceProps ||= t.literal(null);
|
||||
|
||||
var args = [this.classRef, instanceProps];
|
||||
if (staticProps) args.push(staticProps);
|
||||
|
||||
body.push(t.expressionStatement(
|
||||
t.callExpression(this.file.addHelper("prototype-properties"), args)
|
||||
t.callExpression(this.file.addHelper(classHelper), args)
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Push a method to its respective mutatorMap.
|
||||
*
|
||||
* @param {Node} node MethodDefinition
|
||||
* Description
|
||||
*/
|
||||
|
||||
pushMethod(node) {
|
||||
verifyConstructor(node: Object) {
|
||||
return; // enable this for the next major
|
||||
|
||||
var state = {
|
||||
hasBareSuper: false,
|
||||
hasSuper: this.hasSuper,
|
||||
file: this.file
|
||||
};
|
||||
|
||||
traverse(node, verifyConstructorVisitor, this.scope, state);
|
||||
|
||||
if (!state.hasBareSuper && this.hasSuper) {
|
||||
throw this.file.errorWithNode(node, "Derived constructor must call super()");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Push a method to its respective mutatorMap.
|
||||
*/
|
||||
|
||||
pushMethod(node: { type: "MethodDefinition" }) {
|
||||
var methodName = node.key;
|
||||
|
||||
var kind = node.kind;
|
||||
@@ -231,9 +280,9 @@ class ClassTransformer {
|
||||
if (this.isLoose) {
|
||||
// use assignments instead of define properties for loose classes
|
||||
|
||||
var className = this.className;
|
||||
if (!node.static) className = t.memberExpression(className, t.identifier("prototype"));
|
||||
methodName = t.memberExpression(className, methodName, node.computed);
|
||||
var classRef = this.classRef;
|
||||
if (!node.static) classRef = t.memberExpression(classRef, t.identifier("prototype"));
|
||||
methodName = t.memberExpression(classRef, methodName, node.computed);
|
||||
|
||||
var expr = t.expressionStatement(t.assignmentExpression("=", methodName, node.value));
|
||||
t.inheritsComments(expr, node);
|
||||
@@ -253,22 +302,19 @@ class ClassTransformer {
|
||||
}
|
||||
|
||||
defineMap.push(mutatorMap, methodName, kind, node.computed, node);
|
||||
defineMap.push(mutatorMap, methodName, "enumerable", node.computed, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Description
|
||||
*
|
||||
* @param {Node} node
|
||||
*/
|
||||
|
||||
pushProperty(node) {
|
||||
pushProperty(node: Object) {
|
||||
if (!node.value) return;
|
||||
|
||||
var key;
|
||||
|
||||
if (node.static) {
|
||||
key = t.memberExpression(this.className, node.key);
|
||||
key = t.memberExpression(this.classRef, node.key);
|
||||
this.body.push(
|
||||
t.expressionStatement(t.assignmentExpression("=", key, node.value))
|
||||
);
|
||||
@@ -282,11 +328,9 @@ class ClassTransformer {
|
||||
|
||||
/**
|
||||
* Replace the constructor body of our class.
|
||||
*
|
||||
* @param {Node} method MethodDefinition
|
||||
*/
|
||||
|
||||
pushConstructor(method) {
|
||||
pushConstructor(method: { type: "MethodDefinition" }) {
|
||||
if (method.kind) {
|
||||
throw this.file.errorWithNode(method, messages.get("classesIllegalConstructorKind"));
|
||||
}
|
||||
@@ -301,6 +345,8 @@ class ClassTransformer {
|
||||
|
||||
construct._ignoreUserWhitespace = true;
|
||||
construct.params = fn.params;
|
||||
construct.body.body = construct.body.body.concat(fn.body.body);
|
||||
|
||||
t.inherits(construct.body, fn.body);
|
||||
construct.body.body = construct.body.body.concat(fn.body.body);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import * as messages from "../../../messages";
|
||||
import t from "../../../types";
|
||||
import * as t from "../../../types";
|
||||
|
||||
export function check(node) {
|
||||
return t.isVariableDeclaration(node, { kind: "const" });
|
||||
@@ -7,8 +7,8 @@ export function check(node) {
|
||||
|
||||
var visitor = {
|
||||
enter(node, parent, scope, state) {
|
||||
if (t.isAssignmentExpression(node) || t.isUpdateExpression(node)) {
|
||||
var ids = t.getBindingIdentifiers(node);
|
||||
if (this.isAssignmentExpression() || this.isUpdateExpression()) {
|
||||
var ids = this.getBindingIdentifiers();
|
||||
|
||||
for (var name in ids) {
|
||||
var id = ids[name];
|
||||
@@ -30,7 +30,7 @@ var visitor = {
|
||||
|
||||
throw state.file.errorWithNode(id, messages.get("readOnly", name));
|
||||
}
|
||||
} else if (t.isScope(node, parent)) {
|
||||
} else if (this.isScope()) {
|
||||
this.skip();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import * as messages from "../../../messages";
|
||||
import t from "../../../types";
|
||||
import * as t from "../../../types";
|
||||
|
||||
export var check = t.isPattern;
|
||||
|
||||
@@ -56,12 +56,12 @@ export { ForOfStatement as ForInStatement };
|
||||
exports.Function = function (node, parent, scope, file) {
|
||||
var nodes = [];
|
||||
|
||||
var hasDestructuringTransformer = false;
|
||||
var hasDestructuring = false;
|
||||
|
||||
node.params = node.params.map(function (pattern, i) {
|
||||
if (!t.isPattern(pattern)) return pattern;
|
||||
|
||||
hasDestructuringTransformer = true;
|
||||
hasDestructuring = true;
|
||||
var ref = scope.generateUidIdentifier("ref");
|
||||
|
||||
var destructuring = new DestructuringTransformer({
|
||||
@@ -69,15 +69,16 @@ exports.Function = function (node, parent, scope, file) {
|
||||
nodes: nodes,
|
||||
scope: scope,
|
||||
file: file,
|
||||
kind: "var",
|
||||
kind: "let"
|
||||
});
|
||||
destructuring.init(pattern, ref);
|
||||
|
||||
return ref;
|
||||
});
|
||||
|
||||
if (!hasDestructuringTransformer) return;
|
||||
if (!hasDestructuring) return;
|
||||
|
||||
file.checkNode(nodes);
|
||||
t.ensureBlock(node);
|
||||
|
||||
var block = node.body;
|
||||
@@ -134,10 +135,7 @@ export function AssignmentExpression(node, parent, scope, file) {
|
||||
if (!t.isPattern(node.left)) return;
|
||||
|
||||
var ref = scope.generateUidIdentifier("temp");
|
||||
scope.push({
|
||||
key: ref.name,
|
||||
id: ref
|
||||
});
|
||||
scope.push({ id: ref });
|
||||
|
||||
var nodes = [];
|
||||
nodes.push(t.assignmentExpression("=", ref, node.right));
|
||||
@@ -218,7 +216,7 @@ export function VariableDeclaration(node, parent, scope, file) {
|
||||
}
|
||||
|
||||
return nodes;
|
||||
};
|
||||
}
|
||||
|
||||
var hasRest = function (pattern) {
|
||||
for (var i = 0; i < pattern.elements.length; i++) {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import * as messages from "../../../messages";
|
||||
import * as util from "../../../util";
|
||||
import t from "../../../types";
|
||||
import * as t from "../../../types";
|
||||
|
||||
export var check = t.isForOfStatement;
|
||||
|
||||
@@ -39,30 +39,6 @@ export function ForOfStatement(node, parent, scope, file) {
|
||||
}
|
||||
}
|
||||
|
||||
var breakVisitor = {
|
||||
enter(node, parent, scope, state) {
|
||||
if (t.isLoop(node)) {
|
||||
state.ignoreLabeless = true;
|
||||
scope.traverse(node, breakVisitor, state);
|
||||
state.ignoreLabeless = false;
|
||||
return this.skip();
|
||||
}
|
||||
|
||||
if (t.isBreakStatement(node)) {
|
||||
if (!node.label && state.ignoreLabeless) return;
|
||||
if (node.label && node.label.name !== state.label) return;
|
||||
|
||||
var ret = t.expressionStatement(
|
||||
t.callExpression(t.memberExpression(state.iteratorKey, t.identifier("return")), [])
|
||||
);
|
||||
ret = state.wrapReturn(ret);
|
||||
|
||||
this.skip();
|
||||
return [ret, node];
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var loose = function (node, parent, scope, file) {
|
||||
var left = node.left;
|
||||
var declar, id;
|
||||
@@ -99,23 +75,6 @@ var loose = function (node, parent, scope, file) {
|
||||
|
||||
//
|
||||
|
||||
scope.traverse(node, breakVisitor, {
|
||||
iteratorKey: iteratorKey,
|
||||
label: t.isLabeledStatement(parent) && parent.label.name,
|
||||
|
||||
wrapReturn: function (node) {
|
||||
return t.ifStatement(
|
||||
t.logicalExpression(
|
||||
"&&",
|
||||
t.unaryExpression("!", isArrayKey, true),
|
||||
t.memberExpression(iteratorKey, t.identifier("return")
|
||||
)
|
||||
), node);
|
||||
}
|
||||
});
|
||||
|
||||
//
|
||||
|
||||
return {
|
||||
declar: declar,
|
||||
node: loop,
|
||||
@@ -124,8 +83,6 @@ var loose = function (node, parent, scope, file) {
|
||||
};
|
||||
|
||||
var spec = function (node, parent, scope, file) {
|
||||
|
||||
|
||||
var left = node.left;
|
||||
var declar;
|
||||
|
||||
@@ -169,17 +126,6 @@ var spec = function (node, parent, scope, file) {
|
||||
|
||||
//
|
||||
|
||||
scope.traverse(node, breakVisitor, {
|
||||
iteratorKey: iteratorKey,
|
||||
label: isLabeledParent && parent.label.name,
|
||||
|
||||
wrapReturn: function (node) {
|
||||
return t.ifStatement(t.memberExpression(iteratorKey, t.identifier("return")), node);
|
||||
}
|
||||
});
|
||||
|
||||
//
|
||||
|
||||
return {
|
||||
replaceParent: isLabeledParent,
|
||||
declar: declar,
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import t from "../../../types";
|
||||
import * as t from "../../../types";
|
||||
|
||||
export { check } from "../internal/modules";
|
||||
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import ReplaceSupers from "../../helpers/replace-supers";
|
||||
import t from "../../../types";
|
||||
import * as t from "../../../types";
|
||||
|
||||
export function check(node) {
|
||||
return t.isIdentifier(node, { name: "super" });
|
||||
}
|
||||
|
||||
export function Property(node, parent, scope, file) {
|
||||
function Property(node, scope, getObjectRef, file) {
|
||||
if (!node.method) return;
|
||||
|
||||
var value = node.value;
|
||||
@@ -13,8 +13,8 @@ export function Property(node, parent, scope, file) {
|
||||
|
||||
var replaceSupers = new ReplaceSupers({
|
||||
topLevelThisReference: thisExpr,
|
||||
getObjectRef: getObjectRef,
|
||||
methodNode: node,
|
||||
className: thisExpr,
|
||||
isStatic: true,
|
||||
scope: scope,
|
||||
file: file
|
||||
@@ -30,3 +30,17 @@ export function Property(node, parent, scope, file) {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export function ObjectExpression(node, parent, scope, file) {
|
||||
var objectRef;
|
||||
var getObjectRef = () => objectRef ||= scope.generateUidIdentifier("obj");
|
||||
|
||||
for (var i = 0; i < node.properties.length; i++) {
|
||||
Property(node.properties[i], scope, getObjectRef, file);
|
||||
}
|
||||
|
||||
if (objectRef) {
|
||||
scope.push({ id: objectRef });
|
||||
return t.assignmentExpression("=", objectRef, node);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import * as util from "../../../util";
|
||||
import t from "../../../types";
|
||||
import * as t from "../../../types";
|
||||
|
||||
export function check(node) {
|
||||
return t.isFunction(node) && hasDefaults(node);
|
||||
@@ -14,7 +14,7 @@ var hasDefaults = function (node) {
|
||||
|
||||
var iifeVisitor = {
|
||||
enter(node, parent, scope, state) {
|
||||
if (!t.isReferencedIdentifier(node, parent)) return;
|
||||
if (!this.isReferencedIdentifier()) return;
|
||||
if (!state.scope.hasOwnBinding(node.name)) return;
|
||||
if (state.scope.bindingIdentifierEquals(node.name, node)) return;
|
||||
|
||||
@@ -61,7 +61,7 @@ exports.Function = function (node, parent, scope, file) {
|
||||
scope.traverse(param, iifeVisitor, state);
|
||||
}
|
||||
|
||||
if (file.transformers["es6.blockScopingTDZ"].canRun()) {
|
||||
if (file.transformers["es6.blockScopingTDZ"].canRun() && t.isIdentifier(param)) {
|
||||
pushDefNode(param, t.identifier("undefined"), i);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,19 +1,19 @@
|
||||
import isNumber from "lodash/lang/isNumber";
|
||||
import * as util from "../../../util";
|
||||
import t from "../../../types";
|
||||
import * as t from "../../../types";
|
||||
|
||||
export var check = t.isRestElement;
|
||||
|
||||
var memberExpressionOptimisationVisitor = {
|
||||
enter(node, parent, scope, state) {
|
||||
// check if this scope has a local binding that will shadow the rest parameter
|
||||
if (t.isScope(node, parent) && !scope.bindingIdentifierEquals(state.name, state.outerBinding)) {
|
||||
if (this.isScope() && !scope.bindingIdentifierEquals(state.name, state.outerBinding)) {
|
||||
return this.skip();
|
||||
}
|
||||
|
||||
// skip over functions as whatever `arguments` we reference inside will refer
|
||||
// to the wrong function
|
||||
if (t.isFunctionDeclaration(node) || t.isFunctionExpression(node)) {
|
||||
if (this.isFunctionDeclaration() || this.isFunctionExpression()) {
|
||||
state.noOptimise = true;
|
||||
scope.traverse(node, memberExpressionOptimisationVisitor, state);
|
||||
state.noOptimise = false;
|
||||
@@ -21,7 +21,7 @@ var memberExpressionOptimisationVisitor = {
|
||||
}
|
||||
|
||||
// is this a referenced identifier and is it referencing the rest parameter?
|
||||
if (!t.isReferencedIdentifier(node, parent, { name: state.name })) return;
|
||||
if (!this.isReferencedIdentifier({ name: state.name })) return;
|
||||
|
||||
if (!state.noOptimise && t.isMemberExpression(parent) && parent.computed) {
|
||||
// if we know that this member expression is referencing a number then we can safely
|
||||
@@ -55,7 +55,7 @@ var hasRest = function (node) {
|
||||
return t.isRestElement(node.params[node.params.length - 1]);
|
||||
};
|
||||
|
||||
exports.Function = function (node, parent, scope) {
|
||||
exports.Function = function (node, parent, scope, file) {
|
||||
if (!hasRest(node)) return;
|
||||
|
||||
var rest = node.params.pop().argument;
|
||||
@@ -69,10 +69,12 @@ exports.Function = function (node, parent, scope) {
|
||||
if (t.isPattern(rest)) {
|
||||
var pattern = rest;
|
||||
rest = scope.generateUidIdentifier("ref");
|
||||
var declar = t.variableDeclaration("var", pattern.elements.map(function (elem, index) {
|
||||
|
||||
var declar = t.variableDeclaration("let", pattern.elements.map(function (elem, index) {
|
||||
var accessExpr = t.memberExpression(rest, t.literal(index), true);
|
||||
return t.variableDeclarator(elem, accessExpr);
|
||||
}));
|
||||
file.checkNode(declar);
|
||||
node.body.body.unshift(declar);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import t from "../../../types";
|
||||
import * as t from "../../../types";
|
||||
|
||||
function loose(node, body, objId) {
|
||||
for (var i = 0; i < node.properties.length; i++) {
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import clone from "lodash/lang/clone";
|
||||
import t from "../../../types";
|
||||
import * as t from "../../../types";
|
||||
|
||||
export function check(node) {
|
||||
return t.isProperty(node) && (node.method || node.shorthand);
|
||||
@@ -12,6 +11,6 @@ export function Property(node) {
|
||||
|
||||
if (node.shorthand) {
|
||||
node.shorthand = false;
|
||||
node.key = t.removeComments(clone(node.key));
|
||||
node.key = t.removeComments(t.clone(node.key));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import * as regex from "../../helpers/regex";
|
||||
import t from "../../../types";
|
||||
import * as t from "../../../types";
|
||||
|
||||
export function check(node) {
|
||||
return regex.is(node, "y");
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import includes from "lodash/collection/includes";
|
||||
import t from "../../../types";
|
||||
import * as t from "../../../types";
|
||||
|
||||
function getSpreadLiteral(spread, scope) {
|
||||
return scope.toArray(spread.argument, true);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import t from "../../../types";
|
||||
import * as t from "../../../types";
|
||||
|
||||
export var optional = true;
|
||||
|
||||
@@ -3,7 +3,7 @@ import * as messages from "../../../messages";
|
||||
import flatten from "lodash/array/flatten";
|
||||
import * as util from "../../../util";
|
||||
import map from "lodash/collection/map";
|
||||
import t from "../../../types";
|
||||
import * as t from "../../../types";
|
||||
|
||||
exports.Function = function (node, parent, scope, file) {
|
||||
var tailCall = new TailCallTransformer(node, scope, file);
|
||||
@@ -17,7 +17,7 @@ function returnBlock(expr) {
|
||||
// looks for and replaces tail recursion calls
|
||||
var firstPass = {
|
||||
enter(node, parent, scope, state) {
|
||||
if (t.isIfStatement(node)) {
|
||||
if (this.isIfStatement()) {
|
||||
if (t.isReturnStatement(node.alternate)) {
|
||||
t.ensureBlock(node, "alternate");
|
||||
}
|
||||
@@ -25,7 +25,7 @@ var firstPass = {
|
||||
if (t.isReturnStatement(node.consequent)) {
|
||||
t.ensureBlock(node, "consequent");
|
||||
}
|
||||
} else if (t.isReturnStatement(node)) {
|
||||
} else if (this.isReturnStatement()) {
|
||||
this.skip();
|
||||
return state.subTransform(node.argument);
|
||||
} else if (t.isTryStatement(parent)) {
|
||||
@@ -34,9 +34,9 @@ var firstPass = {
|
||||
} else if (parent.finalizer && node !== parent.finalizer) {
|
||||
this.skip();
|
||||
}
|
||||
} else if (t.isFunction(node)) {
|
||||
} else if (this.isFunction()) {
|
||||
this.skip();
|
||||
} else if (t.isVariableDeclaration(node)) {
|
||||
} else if (this.isVariableDeclaration()) {
|
||||
this.skip();
|
||||
state.vars.push(node);
|
||||
}
|
||||
@@ -47,15 +47,15 @@ var firstPass = {
|
||||
// them as needed
|
||||
var secondPass = {
|
||||
enter(node, parent, scope, state) {
|
||||
if (t.isThisExpression(node)) {
|
||||
if (this.isThisExpression()) {
|
||||
state.needsThis = true;
|
||||
return state.getThisId();
|
||||
} else if (t.isReferencedIdentifier(node, parent, { name: "arguments" })) {
|
||||
} else if (this.isReferencedIdentifier({ name: "arguments" })) {
|
||||
state.needsArguments = true;
|
||||
return state.getArgumentsId();
|
||||
} else if (t.isFunction(node)) {
|
||||
} else if (this.isFunction()) {
|
||||
this.skip();
|
||||
if (t.isFunctionDeclaration(node)) {
|
||||
if (this.isFunctionDeclaration()) {
|
||||
node = t.variableDeclaration("var", [
|
||||
t.variableDeclarator(node.id, t.toExpression(node))
|
||||
]);
|
||||
@@ -69,7 +69,7 @@ var secondPass = {
|
||||
// optimizes recursion by removing `this` and `arguments` if they aren't used
|
||||
var thirdPass = {
|
||||
enter(node, parent, scope, state) {
|
||||
if (!t.isExpressionStatement(node)) return;
|
||||
if (!this.isExpressionStatement()) return;
|
||||
|
||||
var expr = node.expression;
|
||||
if (!t.isAssignmentExpression(expr)) return;
|
||||
@@ -161,7 +161,7 @@ class TailCallTransformer {
|
||||
if (!this.hasTailRecursion) return;
|
||||
|
||||
if (this.hasDeopt()) {
|
||||
this.file.logDeopt(node, messages.get("tailCallReassignmentDeopt"));
|
||||
this.file.log.deopt(node, messages.get("tailCallReassignmentDeopt"));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -229,7 +229,7 @@ class TailCallTransformer {
|
||||
subTransform(node) {
|
||||
if (!node) return;
|
||||
|
||||
var handler = this["subTransform" + node.type];
|
||||
var handler = this[`subTransform${node.type}`];
|
||||
if (handler) return handler.call(this, node);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import t from "../../../types";
|
||||
import * as t from "../../../types";
|
||||
|
||||
var buildBinaryExpression = function (left, right) {
|
||||
return t.binaryExpression("+", left, right);
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// https://github.com/zenparsing/es-abstract-refs
|
||||
|
||||
import * as util from "../../../util";
|
||||
import t from "../../../types";
|
||||
import * as t from "../../../types";
|
||||
|
||||
export var experimental = true;
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import buildComprehension from "../../helpers/build-comprehension";
|
||||
import traverse from "../../../traversal";
|
||||
import * as util from "../../../util";
|
||||
import t from "../../../types";
|
||||
import * as t from "../../../types";
|
||||
|
||||
export var experimental = true;
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// https://github.com/rwaldron/exponentiation-operator
|
||||
|
||||
import build from "../../helpers/build-binary-assignment-operator-transformer";
|
||||
import t from "../../../types";
|
||||
import * as t from "../../../types";
|
||||
|
||||
export var experimental = true;
|
||||
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user