Compare commits
108 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c31832439a | ||
|
|
b7f19221a9 | ||
|
|
46a37f4672 | ||
|
|
cfe2c19a02 | ||
|
|
649d91ef25 | ||
|
|
4d72bffa30 | ||
|
|
5889233adc | ||
|
|
7bb98352df | ||
|
|
12ebeed7c6 | ||
|
|
7d87e52377 | ||
|
|
2bab285970 | ||
|
|
00651e671e | ||
|
|
37588a6ceb | ||
|
|
a45d6960da | ||
|
|
b13aa41a75 | ||
|
|
20a4ed6140 | ||
|
|
c256e060b3 | ||
|
|
bef315efd6 | ||
|
|
2ec5390b63 | ||
|
|
f4c9dd8768 | ||
|
|
510c7a3e60 | ||
|
|
3ce9508a1c | ||
|
|
717ef280f7 | ||
|
|
8c5e7cf272 | ||
|
|
811a843be9 | ||
|
|
b7c297bb89 | ||
|
|
b7342ef4ea | ||
|
|
1a899f5e77 | ||
|
|
f2eb1643c0 | ||
|
|
9621d1bbeb | ||
|
|
3cf7b2b761 | ||
|
|
ae8b1e242b | ||
|
|
eea48f866d | ||
|
|
84d2d7b7d4 | ||
|
|
ea30a619dd | ||
|
|
87a201db22 | ||
|
|
bcc86c47bc | ||
|
|
b6df9b583b | ||
|
|
52ea7b5f59 | ||
|
|
7bff8239a1 | ||
|
|
04d79c1740 | ||
|
|
e387da7c2b | ||
|
|
53cf453480 | ||
|
|
341528ee4a | ||
|
|
5f6808ba92 | ||
|
|
0b2f1fedcb | ||
|
|
c3c94f0a4a | ||
|
|
f2d60aab9e | ||
|
|
ab6bb35a4f | ||
|
|
e11b943514 | ||
|
|
b44a6eb297 | ||
|
|
f3288ddb1f | ||
|
|
476aa44a90 | ||
|
|
c5589e9336 | ||
|
|
404eb2f972 | ||
|
|
366257915e | ||
|
|
e0cd9bdbe3 | ||
|
|
39854dc088 | ||
|
|
81132aa942 | ||
|
|
5ba2e6254b | ||
|
|
61ea720637 | ||
|
|
55357a331d | ||
|
|
5deaeba3a0 | ||
|
|
a38ae381e2 | ||
|
|
9a2e56f003 | ||
|
|
5bc78b0237 | ||
|
|
c5af8b8694 | ||
|
|
c637575cab | ||
|
|
bc8d49c95c | ||
|
|
59ff0fd9a9 | ||
|
|
b1f794eb84 | ||
|
|
79ab92b5d4 | ||
|
|
f6512b45d5 | ||
|
|
8065c981dc | ||
|
|
7927aa2e18 | ||
|
|
35bd510930 | ||
|
|
74186241f9 | ||
|
|
1effa72a33 | ||
|
|
b03a806d7c | ||
|
|
6220fd9fc3 | ||
|
|
8d92a75190 | ||
|
|
60a7e40140 | ||
|
|
299e462a29 | ||
|
|
62556f6102 | ||
|
|
f2c5accab3 | ||
|
|
15f1978cae | ||
|
|
710ff548cb | ||
|
|
fbb19fc656 | ||
|
|
e87ef80bc5 | ||
|
|
aca750e881 | ||
|
|
672e55082c | ||
|
|
a49f746700 | ||
|
|
83cd3bef92 | ||
|
|
5e4a008a06 | ||
|
|
1594efbc20 | ||
|
|
002be9a05c | ||
|
|
8e06db8f7d | ||
|
|
b1238a1746 | ||
|
|
0874f390ff | ||
|
|
eeff4ac9d1 | ||
|
|
8f0ffc7302 | ||
|
|
bb19649af8 | ||
|
|
692262df1b | ||
|
|
067047da34 | ||
|
|
1c5d169b1e | ||
|
|
3f82b4ec72 | ||
|
|
9c6e632021 | ||
|
|
17e65cc772 |
@@ -5,6 +5,7 @@ insert_final_newline = true
|
||||
charset = utf-8
|
||||
trim_trailing_whitespace = true
|
||||
insert_final_newline = true
|
||||
end_of_line = lf
|
||||
|
||||
[*.{js,json}]
|
||||
indent_style = space
|
||||
|
||||
68
CHANGELOG.md
68
CHANGELOG.md
@@ -13,6 +13,74 @@ _Note: Gaps between patch versions are faulty/broken releases._
|
||||
|
||||
See [CHANGELOG - 6to5](CHANGELOG-6to5.md) for the pre-4.0.0 version changelog.
|
||||
|
||||
## 4.5.0
|
||||
|
||||
* **New Feature**
|
||||
* Add `.babelrc` support.
|
||||
* **Bug Fix**
|
||||
* Move use strict directives to the module formatter bodies.
|
||||
* **Internal**
|
||||
* Make default `bin/babel` behaviour to ignore non-compilable files and add a `--copy-files` flag to revert to the old behaviour.
|
||||
|
||||
## 4.4.6
|
||||
|
||||
* **Bug Fix**
|
||||
* Fix extending a class expression with no methods/only constructor. Thanks [@neVERberleRfellerER](https://github.com/neVERberleRfellerER)!
|
||||
* Allow `MemberExpression` as a valid `left` of `ForOfStatement`.
|
||||
* **Polish**
|
||||
* Throw an error when people try and transpile code with the `@jsx React.DOM` pragma as it conflicts with the custom jsx constructo method detection.
|
||||
* Crawl all comments for `@jsx` pragma.
|
||||
* **Internal**
|
||||
* Upgrade `chalk`.
|
||||
* Upgrade `core-js`.
|
||||
|
||||
## 4.4.5
|
||||
|
||||
* **Internal**
|
||||
* Remove function self reference optimisation.
|
||||
|
||||
## 4.4.4
|
||||
|
||||
* **Bug Fix**
|
||||
* Handle inferred function ids to be reassigned and deopt to a slower but working equivalent.
|
||||
* Don't unpack array patterns that have more elements than their right hand array expression.
|
||||
* **Polish**
|
||||
* Improve syntax highlighting in the code frame. Thanks [@lydell](https://github.com/lydell)!
|
||||
* **Internal**
|
||||
* Upgrade `acorn-babel`.
|
||||
|
||||
## 4.4.3
|
||||
|
||||
* **Bug Fix**
|
||||
* Fix `for...of` iterator break returns being duplicated.
|
||||
* Only call `return` on the iterator if it exists.
|
||||
* **Internal**
|
||||
* Rename `selfContained` transformer to `runtime`.
|
||||
|
||||
## 4.4.2
|
||||
|
||||
* **New Feature**
|
||||
* Add `moduleId` option for specifying a custom module id.
|
||||
|
||||
## 4.4.0
|
||||
|
||||
* **New Feature**
|
||||
* `/*** @jsx NAMESPACE **/` comments are now honored by the `react` transformer.
|
||||
* `getModuleName` option.
|
||||
* Infer function expression names. Thanks [@RReverser](https://github.com/RReverser)!
|
||||
* **Bug Fix**
|
||||
* Add proper control flow for tail recursion optimisation.
|
||||
* **Internal**
|
||||
* Remove useless `format` options and move the `format.compact` option to `format`.
|
||||
* **Polish**
|
||||
* Newline handling of the code generator has been heavily improved.
|
||||
* Code generator now deopts whitespace if the input size is >100KB.
|
||||
|
||||
## 4.3.0
|
||||
|
||||
* **Breaking Change**
|
||||
* Remove `commonStandard` module formatter and make it the default behaviour of all the strict module formatters.
|
||||
|
||||
## 4.2.1
|
||||
|
||||
* **Polish**
|
||||
|
||||
4
Makefile
4
Makefile
@@ -119,5 +119,5 @@ publish-core:
|
||||
bootstrap:
|
||||
npm install
|
||||
git submodule update --init
|
||||
cd vendor/regenerator; npm install
|
||||
cd vendor/compat-table; npm install object-assign
|
||||
cd vendor/regenerator && npm install
|
||||
cd vendor/compat-table && npm install object-assign
|
||||
|
||||
13
README.md
13
README.md
@@ -1,5 +1,7 @@
|
||||
<p align="center">
|
||||
<img alt="babel" src="https://raw.githubusercontent.com/babel/logo/master/logo.png" width="546">
|
||||
<a href="https://babeljs.io/">
|
||||
<img alt="babel" src="https://raw.githubusercontent.com/babel/logo/master/logo.png" width="546">
|
||||
</a>
|
||||
</p>
|
||||
|
||||
<p align="center">
|
||||
@@ -7,6 +9,11 @@
|
||||
</p>
|
||||
|
||||
<p align="center">
|
||||
For more information view the <a href="https://babeljs.io/">documentation</a>. For
|
||||
support visit the <a href="https://gitter.im/babel/babel">gitter room</a>.
|
||||
For more information view the <a href="https://babeljs.io/">documentation</a>. For questions
|
||||
and support please visit the <a href="https://gitter.im/babel/babel">gitter room</a> before
|
||||
creating an issue.
|
||||
</p>
|
||||
|
||||
<p align="center">
|
||||
For documentation and website issues please visit the <a href="https://github.com/babel/babel.github.io">babel.github.io</a> repo.
|
||||
</p>
|
||||
|
||||
@@ -50,7 +50,10 @@ var _eval = function (code, filename) {
|
||||
experimental: program.experimental,
|
||||
playground: program.playground
|
||||
}).code;
|
||||
return vm.runInThisContext(code, filename);
|
||||
|
||||
return vm.runInThisContext(code, {
|
||||
filename: filename
|
||||
});
|
||||
};
|
||||
|
||||
if (program.eval || program.print) {
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
var opts = require("./babel").opts;
|
||||
opts.optional = (opts.optional || []).concat("minification");
|
||||
opts.format = {
|
||||
compact: true
|
||||
};
|
||||
@@ -43,7 +43,7 @@ module.exports = function (commander, filenames, opts) {
|
||||
var src = path.join(dirname, filename);
|
||||
if (util.canCompile(filename)) {
|
||||
write(src, filename);
|
||||
} else {
|
||||
} else if (commander.copyFiles) {
|
||||
outputFileSync(path.join(commander.outDir, filename), fs.readFileSync(src));
|
||||
}
|
||||
});
|
||||
|
||||
@@ -15,6 +15,7 @@ commander.option("-r, --external-helpers", "Replace helpers with references to a
|
||||
commander.option("-e, --experimental", "Enable experimental support for proposed ES7 features");
|
||||
commander.option("-p, --playground", "Enable playground support");
|
||||
|
||||
commander.option("-c, --compact [mode]", "When set to \"auto\" compact is `true` when the input size exceeds 100KB. (auto|true|false)", "auto");
|
||||
commander.option("-m, --modules [modules]", "Module formatter type to use [common]", "common");
|
||||
commander.option("-l, --whitelist [whitelist]", "Whitelist of transformers to ONLY use", util.list);
|
||||
commander.option("-b, --blacklist [blacklist]", "Blacklist of transformers to NOT use", util.list);
|
||||
@@ -27,6 +28,7 @@ commander.option("-M, --module-ids", "Insert module id in modules", false);
|
||||
commander.option("-R, --react-compat", "Makes the react transformer produce pre-v0.12 code");
|
||||
commander.option("--keep-module-id-extensions", "Keep extensions when generating module ids", false);
|
||||
commander.option("-a, --auxiliary-comment [comment]", "Comment text to prepend to all auxiliary code");
|
||||
commander.option("-D, --copy-files", "When compiling a directory copy over non-compilable files");
|
||||
|
||||
commander.on("--help", function () {
|
||||
var outKeys = function (title, obj) {
|
||||
@@ -113,20 +115,16 @@ exports.opts = {
|
||||
optional: commander.optional,
|
||||
comments: !commander.removeComments,
|
||||
modules: commander.modules,
|
||||
compact: commander.compact,
|
||||
loose: commander.loose
|
||||
};
|
||||
|
||||
setTimeout(function () {
|
||||
// this is just a hack to give `babel-minify` and other files including this
|
||||
// time to modify `exports.opts`
|
||||
var fn;
|
||||
|
||||
var fn;
|
||||
if (commander.outDir) {
|
||||
fn = require("./dir");
|
||||
} else {
|
||||
fn = require("./file");
|
||||
}
|
||||
|
||||
if (commander.outDir) {
|
||||
fn = require("./dir");
|
||||
} else {
|
||||
fn = require("./file");
|
||||
}
|
||||
|
||||
fn(commander, filenames, exports.opts);
|
||||
}, 0);
|
||||
fn(commander, filenames, exports.opts);
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
var readdir = require("fs-readdir-recursive");
|
||||
var index = require("./index");
|
||||
var babel = require("../../lib/babel/api/node");
|
||||
var util = require("../../lib/babel/util");
|
||||
var path = require("path");
|
||||
var fs = require("fs");
|
||||
var _ = require("lodash");
|
||||
var resolveRc = require("../../lib/babel/api/register/resolve-rc");
|
||||
var readdir = require("fs-readdir-recursive");
|
||||
var index = require("./index");
|
||||
var babel = require("../../lib/babel/api/node");
|
||||
var util = require("../../lib/babel/util");
|
||||
var path = require("path");
|
||||
var fs = require("fs");
|
||||
var _ = require("lodash");
|
||||
|
||||
exports.readdirFilter = function (filename) {
|
||||
return readdir(filename).filter(function (filename) {
|
||||
@@ -41,6 +42,7 @@ exports.transform = function (filename, code, opts) {
|
||||
};
|
||||
|
||||
exports.compile = function (filename, opts) {
|
||||
resolveRc(filename, opts);
|
||||
var code = fs.readFileSync(filename, "utf8");
|
||||
return exports.transform(filename, code, opts);
|
||||
};
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
console.error("`6to5-minify` has been renamed to `babel-minify`");
|
||||
require("../babel-minify");
|
||||
@@ -4,6 +4,7 @@ require("../../polyfill");
|
||||
|
||||
var sourceMapSupport = require("source-map-support");
|
||||
var registerCache = require("./cache");
|
||||
var resolveRc = require("./resolve-rc");
|
||||
var extend = require("lodash/object/extend");
|
||||
var babel = require("../node");
|
||||
var each = require("lodash/collection/each");
|
||||
@@ -44,7 +45,10 @@ var mtime = function (filename) {
|
||||
var compile = function (filename) {
|
||||
var result;
|
||||
|
||||
var cacheKey = filename + ":" + JSON.stringify(transformOpts);
|
||||
var opts = extend({}, transformOpts);
|
||||
resolveRc(filename, opts);
|
||||
|
||||
var cacheKey = filename + ":" + JSON.stringify(opts);
|
||||
|
||||
if (cache) {
|
||||
var cached = cache[cacheKey];
|
||||
@@ -54,10 +58,10 @@ var compile = function (filename) {
|
||||
}
|
||||
|
||||
if (!result) {
|
||||
result = babel.transformFileSync(filename, extend({
|
||||
result = babel.transformFileSync(filename, extend(opts, {
|
||||
sourceMap: true,
|
||||
ast: false
|
||||
}, transformOpts));
|
||||
}));
|
||||
}
|
||||
|
||||
if (cache) {
|
||||
|
||||
45
lib/babel/api/register/resolve-rc.js
Normal file
45
lib/babel/api/register/resolve-rc.js
Normal file
@@ -0,0 +1,45 @@
|
||||
"use strict";
|
||||
|
||||
var merge = require("lodash/object/merge");
|
||||
var path = require("path");
|
||||
var fs = require("fs");
|
||||
|
||||
var cache = {};
|
||||
|
||||
function exists(filename) {
|
||||
var cached = cache[filename];
|
||||
if (cached != null) return cached;
|
||||
return cache[filename] = fs.existsSync(filename);
|
||||
}
|
||||
|
||||
module.exports = function (loc, opts) {
|
||||
var rel = ".babelrc";
|
||||
opts = opts || {};
|
||||
|
||||
function find(start, rel) {
|
||||
var file = path.join(start, rel);
|
||||
|
||||
if (exists(file)) {
|
||||
var content = fs.readFileSync(file, "utf8");
|
||||
var json;
|
||||
|
||||
try {
|
||||
json = JSON.parse(content);
|
||||
} catch (err) {
|
||||
err.message = file + ": " + err.message;
|
||||
throw err;
|
||||
}
|
||||
|
||||
opts = merge(json, opts);
|
||||
}
|
||||
|
||||
var up = path.dirname(start);
|
||||
if (up !== start) { // root
|
||||
find(up, rel);
|
||||
}
|
||||
}
|
||||
|
||||
find(loc, rel);
|
||||
|
||||
return opts;
|
||||
};
|
||||
@@ -59,13 +59,13 @@ Buffer.prototype.keyword = function (name) {
|
||||
|
||||
Buffer.prototype.space = function () {
|
||||
if (this.format.compact) return;
|
||||
|
||||
if (this.buf && !this.isLast([" ", "\n"])) {
|
||||
if (this.buf && !this.isLast(" ") && !this.isLast("\n")) {
|
||||
this.push(" ");
|
||||
}
|
||||
};
|
||||
|
||||
Buffer.prototype.removeLast = function (cha) {
|
||||
if (this.format.compact) return;
|
||||
if (!this.isLast(cha)) return;
|
||||
|
||||
this.buf = this.buf.substr(0, this.buf.length - 1);
|
||||
@@ -73,7 +73,9 @@ Buffer.prototype.removeLast = function (cha) {
|
||||
};
|
||||
|
||||
Buffer.prototype.newline = function (i, removeLast) {
|
||||
if (this.format.compact || this.format.concise) {
|
||||
if (this.format.compact) return;
|
||||
|
||||
if (this.format.concise) {
|
||||
this.space();
|
||||
return;
|
||||
}
|
||||
@@ -81,11 +83,14 @@ Buffer.prototype.newline = function (i, removeLast) {
|
||||
removeLast = removeLast || false;
|
||||
|
||||
if (isNumber(i)) {
|
||||
if (this.endsWith("{\n")) i--;
|
||||
if (this.endsWith(repeating("\n", i > 0 ? i : 0))) return;
|
||||
i = Math.min(2, i);
|
||||
|
||||
while (i--) {
|
||||
if (this.endsWith("{\n") || this.endsWith(":\n")) i--;
|
||||
if (i <= 0) return;
|
||||
|
||||
while (i > 0) {
|
||||
this._newline(removeLast);
|
||||
i--;
|
||||
}
|
||||
return;
|
||||
}
|
||||
@@ -98,11 +103,13 @@ Buffer.prototype.newline = function (i, removeLast) {
|
||||
};
|
||||
|
||||
Buffer.prototype._newline = function (removeLast) {
|
||||
// never allow more than two lines
|
||||
if (this.endsWith("\n\n")) return;
|
||||
|
||||
// remove the last newline
|
||||
if (removeLast && this.isLast("\n")) this.removeLast("\n");
|
||||
|
||||
this.removeLast(" ");
|
||||
|
||||
// remove whitespace if last character was a newline
|
||||
this._removeSpacesAfterLastNewline();
|
||||
this._push("\n");
|
||||
};
|
||||
@@ -110,6 +117,7 @@ Buffer.prototype._newline = function (removeLast) {
|
||||
/**
|
||||
* If buffer ends with a newline and some spaces after it, trim those spaces.
|
||||
*/
|
||||
|
||||
Buffer.prototype._removeSpacesAfterLastNewline = function () {
|
||||
var lastNewlineIndex = this.buf.lastIndexOf("\n");
|
||||
if (lastNewlineIndex === -1)
|
||||
@@ -130,7 +138,7 @@ Buffer.prototype._removeSpacesAfterLastNewline = function () {
|
||||
};
|
||||
|
||||
Buffer.prototype.push = function (str, noIndent) {
|
||||
if (this._indent && !noIndent && str !== "\n") {
|
||||
if (!this.format.compact && this._indent && !noIndent && str !== "\n") {
|
||||
// we have an indent level and we aren't pushing a newline
|
||||
var indent = this.getIndent();
|
||||
|
||||
@@ -138,7 +146,7 @@ Buffer.prototype.push = function (str, noIndent) {
|
||||
str = str.replace(/\n/g, "\n" + indent);
|
||||
|
||||
// we've got a newline before us so prepend on the indentation
|
||||
if (this.isLast("\n")) str = indent + str;
|
||||
if (this.isLast("\n")) this._push(indent);
|
||||
}
|
||||
|
||||
this._push(str);
|
||||
@@ -150,13 +158,13 @@ Buffer.prototype._push = function (str) {
|
||||
};
|
||||
|
||||
Buffer.prototype.endsWith = function (str) {
|
||||
var d = this.buf.length - str.length;
|
||||
return d >= 0 && this.buf.lastIndexOf(str) === d;
|
||||
return this.buf.slice(-str.length) === str;
|
||||
};
|
||||
|
||||
Buffer.prototype.isLast = function (cha, shouldTrimRight) {
|
||||
Buffer.prototype.isLast = function (cha) {
|
||||
if (this.format.compact) return false;
|
||||
|
||||
var buf = this.buf;
|
||||
if (shouldTrimRight) buf = trimRight(buf);
|
||||
var last = buf[buf.length - 1];
|
||||
|
||||
if (Array.isArray(cha)) {
|
||||
|
||||
@@ -46,11 +46,9 @@ exports.ConditionalExpression = function (node, print) {
|
||||
exports.NewExpression = function (node, print) {
|
||||
this.push("new ");
|
||||
print(node.callee);
|
||||
if (node.arguments.length || this.format.parentheses) {
|
||||
this.push("(");
|
||||
print.list(node.arguments);
|
||||
this.push(")");
|
||||
}
|
||||
this.push("(");
|
||||
print.list(node.arguments);
|
||||
this.push(")");
|
||||
};
|
||||
|
||||
exports.SequenceExpression = function (node, print) {
|
||||
|
||||
@@ -62,6 +62,10 @@ exports.ImportDeclaration = function (node, print) {
|
||||
|
||||
this.push("import ");
|
||||
|
||||
if (node.isType) {
|
||||
this.push("type ");
|
||||
}
|
||||
|
||||
var specfiers = node.specifiers;
|
||||
if (specfiers && specfiers.length) {
|
||||
var foundImportSpecifier = false;
|
||||
|
||||
@@ -22,12 +22,7 @@ exports.IfStatement = function (node, print) {
|
||||
|
||||
if (node.alternate) {
|
||||
if (this.isLast("}")) this.space();
|
||||
this.keyword("else");
|
||||
|
||||
if (this.format.format && !t.isBlockStatement(node.alternate)) {
|
||||
this.push(" ");
|
||||
}
|
||||
|
||||
this.push("else ");
|
||||
print.indentOnComments(node.alternate);
|
||||
}
|
||||
};
|
||||
@@ -153,7 +148,14 @@ exports.SwitchStatement = function (node, print) {
|
||||
this.push(")");
|
||||
this.space();
|
||||
this.push("{");
|
||||
print.sequence(node.cases, { indent: true });
|
||||
|
||||
print.sequence(node.cases, {
|
||||
indent: true,
|
||||
addNewlines: function (leading, cas) {
|
||||
if (!leading && node.cases[node.cases.length - 1] === cas) return -1;
|
||||
}
|
||||
});
|
||||
|
||||
this.push("}");
|
||||
};
|
||||
|
||||
@@ -166,8 +168,10 @@ exports.SwitchCase = function (node, print) {
|
||||
this.push("default:");
|
||||
}
|
||||
|
||||
this.newline();
|
||||
print.sequence(node.consequent, { indent: true });
|
||||
if (node.consequent.length) {
|
||||
this.newline();
|
||||
print.sequence(node.consequent, { indent: true });
|
||||
}
|
||||
};
|
||||
|
||||
exports.DebuggerStatement = function () {
|
||||
@@ -189,7 +193,7 @@ exports.VariableDeclaration = function (node, print, parent) {
|
||||
}
|
||||
|
||||
var sep = ",";
|
||||
if (hasInits) {
|
||||
if (!this.format.compact && hasInits) {
|
||||
sep += "\n" + repeating(" ", node.kind.length + 1);
|
||||
} else {
|
||||
sep += " ";
|
||||
|
||||
@@ -72,7 +72,7 @@ exports.ArrayPattern = function (node, print) {
|
||||
// both (all) of the holes.
|
||||
self.push(",");
|
||||
} else {
|
||||
if (i > 0 && !self.format.compact) self.push(" ");
|
||||
if (i > 0) self.push(" ");
|
||||
print(elem);
|
||||
if (i < len - 1) self.push(",");
|
||||
}
|
||||
|
||||
@@ -12,9 +12,9 @@ var Whitespace = require("./whitespace");
|
||||
var repeating = require("repeating");
|
||||
var SourceMap = require("./source-map");
|
||||
var Position = require("./position");
|
||||
var messages = require("../messages");
|
||||
var Buffer = require("./buffer");
|
||||
var extend = require("lodash/object/extend");
|
||||
var merge = require("lodash/object/merge");
|
||||
var each = require("lodash/collection/each");
|
||||
var n = require("./node");
|
||||
var t = require("../types");
|
||||
@@ -28,7 +28,7 @@ function CodeGenerator(ast, opts, code) {
|
||||
this.opts = opts;
|
||||
this.ast = ast;
|
||||
|
||||
this.whitespace = new Whitespace(this.tokens, this.comments);
|
||||
this.whitespace = new Whitespace(this.tokens, this.comments, this.format);
|
||||
this.position = new Position;
|
||||
this.map = new SourceMap(this.position, opts, code);
|
||||
this.buffer = new Buffer(this.position, this.format);
|
||||
@@ -47,17 +47,25 @@ CodeGenerator.normalizeOptions = function (code, opts) {
|
||||
if (indent && indent !== " ") style = indent;
|
||||
}
|
||||
|
||||
return merge({
|
||||
parentheses: true,
|
||||
var format = {
|
||||
comments: opts.comments == null || opts.comments,
|
||||
compact: false,
|
||||
concise: false,
|
||||
compact: opts.compact,
|
||||
indent: {
|
||||
adjustMultilineComment: true,
|
||||
style: style,
|
||||
base: 0
|
||||
}
|
||||
}, opts.format || {});
|
||||
};
|
||||
|
||||
if (format.compact === "auto") {
|
||||
format.compact = code.length > 100000; // 100KB
|
||||
|
||||
if (format.compact) {
|
||||
console.error(messages.get("codeGeneratorDeopt", opts.filename, "100KB"));
|
||||
}
|
||||
}
|
||||
|
||||
return format;
|
||||
};
|
||||
|
||||
CodeGenerator.generators = {
|
||||
@@ -115,11 +123,7 @@ CodeGenerator.prototype.buildPrint = function (parent) {
|
||||
|
||||
print.list = function (items, opts) {
|
||||
opts = opts || {};
|
||||
|
||||
var sep = opts.separator || ", ";
|
||||
if (self.format.compact) sep = ",";
|
||||
opts.separator = sep;
|
||||
|
||||
opts.separator = opts.separator || ", ";
|
||||
print.join(items, opts);
|
||||
};
|
||||
|
||||
@@ -167,13 +171,14 @@ CodeGenerator.prototype.print = function (node, parent, opts) {
|
||||
} else {
|
||||
// generated node
|
||||
if (!leading) lines++; // always include at least a single line after
|
||||
if (opts.addNewlines) lines += opts.addNewlines(leading, node) || 0;
|
||||
|
||||
var needs = n.needsWhitespaceAfter;
|
||||
if (leading) needs = n.needsWhitespaceBefore;
|
||||
lines += needs(node, parent);
|
||||
if (needs(node, parent)) lines++;
|
||||
|
||||
// generated nodes can't add starting file whitespace
|
||||
if (!self.buffer.get()) lines = 0;
|
||||
if (!self.buffer.buf) lines = 0;
|
||||
}
|
||||
|
||||
self.newline(lines);
|
||||
@@ -227,6 +232,7 @@ CodeGenerator.prototype.printJoin = function (print, nodes, opts) {
|
||||
each(nodes, function (node, i) {
|
||||
print(node, {
|
||||
statement: opts.statement,
|
||||
addNewlines: opts.addNewlines,
|
||||
after: function () {
|
||||
if (opts.iterator) {
|
||||
opts.iterator(node, i);
|
||||
@@ -302,6 +308,7 @@ CodeGenerator.prototype._getComments = function (key, node) {
|
||||
|
||||
CodeGenerator.prototype._printComments = function (comments) {
|
||||
if (this.format.compact) return;
|
||||
|
||||
if (!this.format.comments) return;
|
||||
if (!comments || !comments.length) return;
|
||||
|
||||
|
||||
@@ -4,9 +4,9 @@ module.exports = Node;
|
||||
|
||||
var whitespace = require("./whitespace");
|
||||
var parens = require("./parentheses");
|
||||
var t = require("../../types");
|
||||
var each = require("lodash/collection/each");
|
||||
var some = require("lodash/collection/some");
|
||||
var t = require("../../types");
|
||||
|
||||
var find = function (obj, node, parent) {
|
||||
if (!obj) return;
|
||||
@@ -42,14 +42,19 @@ Node.needsWhitespace = function (node, parent, type) {
|
||||
node = node.expression;
|
||||
}
|
||||
|
||||
var lines = find(whitespace[type].nodes, node, parent);
|
||||
if (lines) return lines;
|
||||
var linesInfo = find(whitespace.nodes, node, parent);
|
||||
|
||||
each(find(whitespace[type].list, node, parent), function (expr) {
|
||||
lines = Node.needsWhitespace(expr, node, type);
|
||||
if (lines) return false;
|
||||
});
|
||||
return lines || 0;
|
||||
if (!linesInfo) {
|
||||
var items = find(whitespace.list, node, parent);
|
||||
if (items) {
|
||||
for (var i = 0; i < items.length; i++) {
|
||||
linesInfo = Node.needsWhitespace(items[i], node, type);
|
||||
if (linesInfo) break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (linesInfo && linesInfo[type]) || 0;
|
||||
};
|
||||
|
||||
Node.needsWhitespaceBefore = function (node, parent) {
|
||||
|
||||
@@ -1,84 +1,159 @@
|
||||
"use strict";
|
||||
|
||||
var t = require("../../types");
|
||||
var each = require("lodash/collection/each");
|
||||
var map = require("lodash/collection/map");
|
||||
var isNumber = require("lodash/lang/isNumber");
|
||||
var isBoolean = require("lodash/lang/isBoolean");
|
||||
var each = require("lodash/collection/each");
|
||||
var map = require("lodash/collection/map");
|
||||
var t = require("../../types");
|
||||
|
||||
exports.before = {
|
||||
nodes: {
|
||||
Property: function (node, parent) {
|
||||
if (parent.properties[0] === node) {
|
||||
return 1;
|
||||
}
|
||||
},
|
||||
var crawl = function (node, state) {
|
||||
state = state || {};
|
||||
|
||||
SpreadProperty: function (node, parent) {
|
||||
return exports.before.nodes.Property(node, parent);
|
||||
},
|
||||
if (t.isMemberExpression(node)) {
|
||||
crawl(node.object, state);
|
||||
if (node.computed) crawl(node.property, state);
|
||||
} else if (t.isBinary(node) || t.isAssignmentExpression(node)) {
|
||||
crawl(node.left, state);
|
||||
crawl(node.right, state);
|
||||
} else if (t.isCallExpression(node)) {
|
||||
state.hasCall = true;
|
||||
crawl(node.callee, state);
|
||||
} else if (t.isFunction(node)) {
|
||||
state.hasFunction = true;
|
||||
} else if (t.isIdentifier(node)) {
|
||||
state.hasHelper = state.hasHelper || isHelper(node.callee);
|
||||
}
|
||||
|
||||
SwitchCase: function (node, parent) {
|
||||
if (parent.cases[0] === node) {
|
||||
return 1;
|
||||
}
|
||||
},
|
||||
return state;
|
||||
};
|
||||
|
||||
CallExpression: function (node) {
|
||||
if (t.isFunction(node.callee)) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
var isHelper = function (node) {
|
||||
if (t.isMemberExpression(node)) {
|
||||
return isHelper(node.object) || isHelper(node.property);
|
||||
} else if (t.isIdentifier(node)) {
|
||||
return node.name === "require" || node.name[0] === "_";
|
||||
} else if (t.isCallExpression(node)) {
|
||||
return isHelper(node.callee);
|
||||
} else if (t.isBinary(node) || t.isAssignmentExpression(node)) {
|
||||
return (t.isIdentifier(node.left) && isHelper(node.left)) || isHelper(node.right);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
exports.after = {
|
||||
nodes: {
|
||||
LogicalExpression: function (node) {
|
||||
return t.isFunction(node.left) || t.isFunction(node.right);
|
||||
},
|
||||
var isType = function (node) {
|
||||
return t.isLiteral(node) || t.isObjectExpression(node) || t.isArrayExpression(node) ||
|
||||
t.isIdentifier(node) || t.isMemberExpression(node);
|
||||
};
|
||||
|
||||
AssignmentExpression: function (node) {
|
||||
if (t.isFunction(node.right)) {
|
||||
return 1;
|
||||
exports.nodes = {
|
||||
AssignmentExpression: function (node) {
|
||||
var state = crawl(node.right);
|
||||
if ((state.hasCall && state.hasHelper) || state.hasFunction) {
|
||||
return {
|
||||
before: state.hasFunction,
|
||||
after: true
|
||||
};
|
||||
}
|
||||
},
|
||||
|
||||
SwitchCase: function (node, parent) {
|
||||
return {
|
||||
before: node.consequent.length || parent.cases[0] === node
|
||||
};
|
||||
},
|
||||
|
||||
LogicalExpression: function (node) {
|
||||
if (t.isFunction(node.left) || t.isFunction(node.right)) {
|
||||
return {
|
||||
after: true
|
||||
};
|
||||
}
|
||||
},
|
||||
|
||||
Literal: function (node) {
|
||||
if (node.value === "use strict") {
|
||||
return {
|
||||
after: true
|
||||
};
|
||||
}
|
||||
},
|
||||
|
||||
CallExpression: function (node) {
|
||||
if (t.isFunction(node.callee) || isHelper(node)) {
|
||||
return {
|
||||
before: true,
|
||||
after: true
|
||||
};
|
||||
}
|
||||
},
|
||||
|
||||
VariableDeclaration: function (node) {
|
||||
for (var i = 0; i < node.declarations.length; i++) {
|
||||
var declar = node.declarations[i];
|
||||
|
||||
var enabled = isHelper(declar.id) && !isType(declar.init);
|
||||
if (!enabled) {
|
||||
var state = crawl(declar.init);
|
||||
enabled = (isHelper(declar.init) && state.hasCall) || state.hasFunction;
|
||||
}
|
||||
|
||||
if (enabled) {
|
||||
return {
|
||||
before: true,
|
||||
after: true
|
||||
};
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
list: {
|
||||
VariableDeclaration: function (node) {
|
||||
return map(node.declarations, "init");
|
||||
},
|
||||
|
||||
ArrayExpression: function (node) {
|
||||
return node.elements;
|
||||
},
|
||||
|
||||
ObjectExpression: function (node) {
|
||||
return node.properties;
|
||||
IfStatement: function (node) {
|
||||
if (t.isBlockStatement(node.consequent)) {
|
||||
return {
|
||||
before: true,
|
||||
after: true
|
||||
};
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
exports.nodes.Property =
|
||||
exports.nodes.SpreadProperty = function (node, parent) {
|
||||
if (parent.properties[0] === node) {
|
||||
return {
|
||||
before: true
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
exports.list = {
|
||||
VariableDeclaration: function (node) {
|
||||
return map(node.declarations, "init");
|
||||
},
|
||||
|
||||
ArrayExpression: function (node) {
|
||||
return node.elements;
|
||||
},
|
||||
|
||||
ObjectExpression: function (node) {
|
||||
return node.properties;
|
||||
}
|
||||
};
|
||||
|
||||
each({
|
||||
Function: 1,
|
||||
Class: 1,
|
||||
For: 1,
|
||||
ArrayExpression: { after: 1 },
|
||||
ObjectExpression: { after: 1 },
|
||||
SwitchStatement: 1,
|
||||
IfStatement: { before: 1 },
|
||||
CallExpression: { after: 1 },
|
||||
Literal: { after: 1 }
|
||||
Function: true,
|
||||
Class: true,
|
||||
Loop: true,
|
||||
LabeledStatement: true,
|
||||
SwitchStatement: true,
|
||||
TryStatement: true
|
||||
}, function (amounts, type) {
|
||||
if (isNumber(amounts)) {
|
||||
if (isBoolean(amounts)) {
|
||||
amounts = { after: amounts, before: amounts };
|
||||
}
|
||||
|
||||
each([type].concat(t.FLIPPED_ALIAS_KEYS[type] || []), function (type) {
|
||||
each(amounts, function (amount, key) {
|
||||
exports[key].nodes[type] = function () {
|
||||
return amount;
|
||||
};
|
||||
});
|
||||
exports.nodes[type] = function () {
|
||||
return amounts;
|
||||
};
|
||||
});
|
||||
});
|
||||
|
||||
@@ -13,11 +13,13 @@ var sortBy = require("lodash/collection/sortBy");
|
||||
* @param {Number} max Array length
|
||||
* @returns {Number} shiftedIndex
|
||||
*/
|
||||
|
||||
function getLookupIndex(i, base, max) {
|
||||
i += base;
|
||||
|
||||
if (i >= max)
|
||||
if (i >= max) {
|
||||
i -= max;
|
||||
}
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
@@ -1,42 +1,59 @@
|
||||
// syntax highlighting based on https://github.com/dominictarr/ansi-highlight by the fantastic Dominic Tarr
|
||||
|
||||
var repeating = require("repeating");
|
||||
var tokenize = require("js-tokenizer");
|
||||
var chalk = require("chalk");
|
||||
var lineNumbers = require("line-numbers");
|
||||
var repeating = require("repeating");
|
||||
var jsTokens = require("js-tokens");
|
||||
var esutils = require("esutils");
|
||||
var chalk = require("chalk");
|
||||
var ary = require("lodash/function/ary");
|
||||
|
||||
var defs = {
|
||||
string1: "red",
|
||||
string2: "red",
|
||||
punct: ["white", "bold"],
|
||||
curly: "green",
|
||||
parens: ["blue", "bold"],
|
||||
square: ["yellow"],
|
||||
name: "white",
|
||||
keyword: ["cyan"],
|
||||
number: "magenta",
|
||||
regexp: "magenta",
|
||||
comment1: "grey",
|
||||
comment2: "grey"
|
||||
string: chalk.red,
|
||||
punctuation: chalk.white.bold,
|
||||
operator: chalk.white.bold,
|
||||
curly: chalk.green,
|
||||
parens: chalk.blue.bold,
|
||||
square: chalk.yellow,
|
||||
name: chalk.white,
|
||||
keyword: chalk.cyan,
|
||||
number: chalk.magenta,
|
||||
regex: chalk.magenta,
|
||||
comment: chalk.grey,
|
||||
invalid: chalk.inverse
|
||||
};
|
||||
|
||||
var highlight = function (text) {
|
||||
var colorize = function (str, col) {
|
||||
if (!col) return str;
|
||||
var newline = /\r\n|[\n\r\u2028\u2029]/;
|
||||
|
||||
if (Array.isArray(col)) {
|
||||
col.forEach(function (col) {
|
||||
str = chalk[col](str);
|
||||
});
|
||||
} else {
|
||||
str = chalk[col](str);
|
||||
var highlight = function (text) {
|
||||
var tokenType = function (match) {
|
||||
var token = jsTokens.matchToToken(match);
|
||||
if (token.type === "name" && esutils.keyword.isKeywordES6(token.value)) {
|
||||
return "keyword";
|
||||
}
|
||||
return str;
|
||||
|
||||
if (token.type === "punctuation") {
|
||||
switch (token.value) {
|
||||
case "{":
|
||||
case "}":
|
||||
return "curly";
|
||||
case "(":
|
||||
case ")":
|
||||
return "parens";
|
||||
case "[":
|
||||
case "]":
|
||||
return "square";
|
||||
}
|
||||
}
|
||||
|
||||
return token.type;
|
||||
};
|
||||
|
||||
return tokenize(text, true).map(function (str) {
|
||||
var type = tokenize.type(str);
|
||||
return colorize(str, defs[type]);
|
||||
}).join("");
|
||||
return text.replace(jsTokens, function (match) {
|
||||
var type = tokenType(arguments);
|
||||
if (type in defs) {
|
||||
var colorize = ary(defs[type], 1);
|
||||
return match.split(newline).map(colorize).join("\n");
|
||||
}
|
||||
return match;
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = function (lines, lineNumber, colNumber) {
|
||||
@@ -46,33 +63,29 @@ module.exports = function (lines, lineNumber, colNumber) {
|
||||
lines = highlight(lines);
|
||||
}
|
||||
|
||||
lines = lines.split(/\r\n|[\n\r\u2028\u2029]/);
|
||||
lines = lines.split(newline);
|
||||
|
||||
var start = Math.max(lineNumber - 3, 0);
|
||||
var end = Math.min(lines.length, lineNumber + 3);
|
||||
var width = (end + "").length;
|
||||
|
||||
if (!lineNumber && !colNumber) {
|
||||
start = 0;
|
||||
end = lines.length;
|
||||
}
|
||||
|
||||
return "\n" + lines.slice(start, end).map(function (line, i) {
|
||||
var curr = i + start + 1;
|
||||
|
||||
var gutter = curr === lineNumber ? "> " : " ";
|
||||
|
||||
var sep = curr + repeating(" ", width + 1);
|
||||
gutter += sep + "| ";
|
||||
|
||||
var str = gutter + line;
|
||||
|
||||
if (colNumber && curr === lineNumber) {
|
||||
str += "\n";
|
||||
str += repeating(" ", gutter.length - 2);
|
||||
str += "|" + repeating(" ", colNumber) + "^";
|
||||
return "\n" + lineNumbers(lines.slice(start, end), {
|
||||
start: start + 1,
|
||||
before: " ",
|
||||
after: " | ",
|
||||
transform: function (params) {
|
||||
if (params.number !== lineNumber) {
|
||||
return;
|
||||
}
|
||||
if (colNumber) {
|
||||
params.line += "\n" + params.before + repeating(" ", params.width) +
|
||||
params.after + repeating(" ", colNumber - 1) + "^";
|
||||
}
|
||||
params.before = params.before.replace(/^./, ">");
|
||||
}
|
||||
|
||||
return str;
|
||||
}).join("\n");
|
||||
};
|
||||
|
||||
@@ -17,7 +17,8 @@ exports.messages = {
|
||||
modulesIllegalExportName: "Illegal export $1",
|
||||
unknownForHead: "Unknown node type $1 in ForStatement",
|
||||
didYouMean: "Did you mean $1?",
|
||||
evalInStrictMode: "eval is not allowed in strict mode"
|
||||
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."
|
||||
};
|
||||
|
||||
exports.get = function (key) {
|
||||
|
||||
@@ -60,7 +60,7 @@ File.helpers = [
|
||||
"object-destructuring-empty",
|
||||
"temporal-undefined",
|
||||
"temporal-assert-defined",
|
||||
"tail-call"
|
||||
"self-global"
|
||||
];
|
||||
|
||||
File.validOptions = [
|
||||
@@ -82,12 +82,17 @@ File.validOptions = [
|
||||
"keepModuleIdExtensions",
|
||||
"code",
|
||||
"ast",
|
||||
"format",
|
||||
"playground",
|
||||
"experimental",
|
||||
"resolveModuleSource",
|
||||
"externalHelpers",
|
||||
"auxiliaryComment",
|
||||
"compact",
|
||||
|
||||
"resolveModuleSource",
|
||||
"moduleId",
|
||||
|
||||
// legacy
|
||||
"format",
|
||||
|
||||
// these are used by plugins
|
||||
"ignore",
|
||||
@@ -113,7 +118,6 @@ File.prototype.normalizeOptions = function (opts) {
|
||||
experimental: false,
|
||||
reactCompat: false,
|
||||
playground: false,
|
||||
whitespace: true,
|
||||
moduleIds: false,
|
||||
blacklist: [],
|
||||
whitelist: [],
|
||||
@@ -122,6 +126,7 @@ File.prototype.normalizeOptions = function (opts) {
|
||||
comments: true,
|
||||
filename: "unknown",
|
||||
modules: "common",
|
||||
compact: "auto",
|
||||
loose: [],
|
||||
code: true,
|
||||
ast: true
|
||||
@@ -133,11 +138,16 @@ File.prototype.normalizeOptions = function (opts) {
|
||||
opts.sourceRoot = slash(opts.sourceRoot);
|
||||
}
|
||||
|
||||
if (opts.moduleId) {
|
||||
opts.moduleIds = true;
|
||||
}
|
||||
|
||||
opts.basename = path.basename(opts.filename, path.extname(opts.filename));
|
||||
|
||||
opts.blacklist = util.arrayify(opts.blacklist);
|
||||
opts.whitelist = util.arrayify(opts.whitelist);
|
||||
opts.optional = util.arrayify(opts.optional);
|
||||
opts.compact = util.booleanify(opts.compact);
|
||||
opts.loose = util.arrayify(opts.loose);
|
||||
|
||||
if (includes(opts.loose, "all") || includes(opts.loose, true)) {
|
||||
@@ -166,7 +176,7 @@ File.prototype.normalizeOptions = function (opts) {
|
||||
}
|
||||
|
||||
if (opts.externalHelpers) {
|
||||
this.set("runtimeIdentifier", t.identifier("babelHelpers"));
|
||||
this.set("helpersNamespace", t.identifier("babelHelpers"));
|
||||
}
|
||||
|
||||
opts.blacklist = transform._ensureTransformerNames("blacklist", opts.blacklist);
|
||||
@@ -176,8 +186,7 @@ File.prototype.normalizeOptions = function (opts) {
|
||||
|
||||
if (opts.reactCompat) {
|
||||
opts.optional.push("reactCompat");
|
||||
console.error("The reactCompat option has been moved into the optional transformer " +
|
||||
"`reactCompat` - backwards compatibility will be removed in v4.0.0");
|
||||
console.error("The reactCompat option has been moved into the optional transformer `reactCompat`");
|
||||
}
|
||||
|
||||
var ensureEnabled = function (key) {
|
||||
@@ -324,7 +333,7 @@ File.prototype.addHelper = function (name) {
|
||||
var declar = program._declarations && program._declarations[name];
|
||||
if (declar) return declar.id;
|
||||
|
||||
var runtime = this.get("runtimeIdentifier");
|
||||
var runtime = this.get("helpersNamespace");
|
||||
if (runtime) {
|
||||
name = t.identifier(t.toIdentifier(name));
|
||||
return t.memberExpression(runtime, name);
|
||||
|
||||
@@ -44,16 +44,19 @@ module.exports = function (exports, opts) {
|
||||
};
|
||||
|
||||
exports.JSXAttribute = {
|
||||
exit: function (node) {
|
||||
var value = node.value || t.literal(true);
|
||||
|
||||
enter: function (node) {
|
||||
var value = node.value;
|
||||
if (t.isLiteral(value) && isString(value.value)) {
|
||||
value.value = value.value.replace(/\n\s+/g, " ");
|
||||
}
|
||||
},
|
||||
|
||||
exit: function (node) {
|
||||
var value = node.value || t.literal(true);
|
||||
return t.inherits(t.property("init", node.name, value), node);
|
||||
}
|
||||
};
|
||||
|
||||
exports.JSXOpeningElement = {
|
||||
exit: function (node, parent, scope, file) {
|
||||
var tagExpr = node.name;
|
||||
@@ -73,7 +76,7 @@ module.exports = function (exports, opts) {
|
||||
};
|
||||
|
||||
if (opts.pre) {
|
||||
opts.pre(state);
|
||||
opts.pre(state, file);
|
||||
}
|
||||
|
||||
var attribs = node.attributes;
|
||||
@@ -86,7 +89,7 @@ module.exports = function (exports, opts) {
|
||||
args.push(attribs);
|
||||
|
||||
if (opts.post) {
|
||||
opts.post(state);
|
||||
opts.post(state, file);
|
||||
}
|
||||
|
||||
return state.call || t.callExpression(state.callee, args);
|
||||
|
||||
@@ -5,15 +5,13 @@ var t = require("../../types");
|
||||
|
||||
var visitor = {
|
||||
enter: function (node, parent, scope, state) {
|
||||
// check if this node is an identifier that matches the same as our function id
|
||||
if (!t.isIdentifier(node, { name: state.id })) return;
|
||||
|
||||
// check if this node is the one referenced
|
||||
if (!t.isReferenced(node, parent)) return;
|
||||
// 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;
|
||||
|
||||
// check that we don't have a local variable declared as that removes the need
|
||||
// for the wrapper
|
||||
var localDeclar = scope.getBindingIdentifier(state.id);
|
||||
var localDeclar = scope.getBindingIdentifier(state.name);
|
||||
if (localDeclar !== state.outerDeclar) return;
|
||||
|
||||
state.selfReference = true;
|
||||
@@ -21,33 +19,100 @@ var visitor = {
|
||||
}
|
||||
};
|
||||
|
||||
var wrap = function (state, method, id, scope) {
|
||||
if (state.selfReference) {
|
||||
var templateName = "property-method-assignment-wrapper";
|
||||
if (method.generator) templateName += "-generator";
|
||||
return util.template(templateName, {
|
||||
FUNCTION: method,
|
||||
FUNCTION_ID: id,
|
||||
FUNCTION_KEY: scope.generateUidIdentifier(id.name),
|
||||
WRAPPER_KEY: scope.generateUidIdentifier(id.name + "Wrapper")
|
||||
});
|
||||
} else {
|
||||
method.id = id;
|
||||
return method;
|
||||
}
|
||||
};
|
||||
|
||||
var visit = function (node, name, scope) {
|
||||
var state = {
|
||||
selfAssignment: false,
|
||||
selfReference: false,
|
||||
outerDeclar: scope.getBindingIdentifier(name),
|
||||
references: [],
|
||||
name: name,
|
||||
};
|
||||
|
||||
// check to see if we have a local binding of the id we're setting inside of
|
||||
// the function, this is important as there are caveats associated
|
||||
|
||||
var bindingInfo = null; // todo: proper scope not being passed in es6/classes // scope.getOwnBindingInfo(name);
|
||||
|
||||
if (bindingInfo) {
|
||||
if (bindingInfo.kind === "param") {
|
||||
// safari will blow up in strict mode with code like:
|
||||
//
|
||||
// var t = function t(t) {};
|
||||
//
|
||||
// with the error:
|
||||
//
|
||||
// Cannot declare a parameter named 't' as it shadows the name of a
|
||||
// strict mode function.
|
||||
//
|
||||
// this isn't to the spec and they've invented this behaviour which is
|
||||
// **extremely** annoying so we avoid setting the name if it has a param
|
||||
// with the same id
|
||||
state.selfReference = true;
|
||||
} else {
|
||||
// otherwise it's defined somewhere in scope like:
|
||||
//
|
||||
// var t = function () {
|
||||
// var t = 2;
|
||||
// };
|
||||
//
|
||||
// so we can safely just set the id and move along as it shadows the
|
||||
// bound function id
|
||||
}
|
||||
} else {
|
||||
scope.traverse(node, visitor, state);
|
||||
}
|
||||
|
||||
return state;
|
||||
};
|
||||
|
||||
exports.property = function (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
|
||||
|
||||
var id = t.toIdentifier(key.value);
|
||||
key = t.identifier(id);
|
||||
|
||||
var state = {
|
||||
id: id,
|
||||
selfReference: false,
|
||||
outerDeclar: scope.getBindingIdentifier(id),
|
||||
};
|
||||
|
||||
scope.traverse(node, visitor, state);
|
||||
var name = t.toIdentifier(key.value);
|
||||
var id = t.identifier(name);
|
||||
|
||||
var method = node.value;
|
||||
|
||||
if (state.selfReference) {
|
||||
var templateName = "property-method-assignment-wrapper";
|
||||
if (method.generator) templateName += "-generator";
|
||||
node.value = util.template(templateName, {
|
||||
FUNCTION: method,
|
||||
FUNCTION_ID: key,
|
||||
FUNCTION_KEY: scope.generateUidIdentifier(id),
|
||||
WRAPPER_KEY: scope.generateUidIdentifier(id + "Wrapper")
|
||||
});
|
||||
} else {
|
||||
method.id = key;
|
||||
}
|
||||
var state = visit(method, name, scope);
|
||||
node.value = wrap(state, method, id, scope);
|
||||
};
|
||||
|
||||
exports.bare = function (node, parent, scope) {
|
||||
// has an `id` so we don't need to infer one
|
||||
if (node.id) return;
|
||||
|
||||
var id;
|
||||
if (t.isProperty(parent) && parent.kind === "init" && !parent.computed) {
|
||||
// { foo: function () {} };
|
||||
id = parent.key;
|
||||
} else if (t.isVariableDeclarator(parent)) {
|
||||
// var foo = function () {};
|
||||
id = parent.id;
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!t.isIdentifier(id)) return;
|
||||
|
||||
var name = t.toIdentifier(id.name);
|
||||
id = t.identifier(name);
|
||||
|
||||
var state = visit(node, name, scope);
|
||||
return wrap(state, node, id, scope);
|
||||
};
|
||||
|
||||
@@ -146,6 +146,8 @@ DefaultFormatter.prototype.isLocalReference = function (node, scope) {
|
||||
|
||||
DefaultFormatter.prototype.getModuleName = function () {
|
||||
var opts = this.file.opts;
|
||||
if (opts.moduleId) return opts.moduleId;
|
||||
|
||||
var filenameRelative = opts.filenameRelative;
|
||||
var moduleName = "";
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ var util = require("../../util");
|
||||
|
||||
module.exports = function (Parent) {
|
||||
var Constructor = function () {
|
||||
this.noInteropRequireImport = true;
|
||||
this.noInteropRequireExport = true;
|
||||
Parent.apply(this, arguments);
|
||||
};
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
"use strict";
|
||||
|
||||
module.exports = CommonStandardFormatter;
|
||||
|
||||
var CommonStrictFormatter = require("./common-strict");
|
||||
var util = require("../../util");
|
||||
|
||||
function CommonStandardFormatter() {
|
||||
this.noInteropRequireImport = true;
|
||||
CommonStrictFormatter.apply(this, arguments);
|
||||
}
|
||||
|
||||
util.inherits(CommonStandardFormatter, CommonStrictFormatter);
|
||||
@@ -1,11 +1,10 @@
|
||||
module.exports = {
|
||||
commonStandard: require("./common-standard"),
|
||||
commonStrict: require("./common-strict"),
|
||||
amdStrict: require("./amd-strict"),
|
||||
umdStrict: require("./umd-strict"),
|
||||
common: require("./common"),
|
||||
system: require("./system"),
|
||||
ignore: require("./ignore"),
|
||||
amd: require("./amd"),
|
||||
umd: require("./umd")
|
||||
commonStrict: require("./common-strict"),
|
||||
amdStrict: require("./amd-strict"),
|
||||
umdStrict: require("./umd-strict"),
|
||||
common: require("./common"),
|
||||
system: require("./system"),
|
||||
ignore: require("./ignore"),
|
||||
amd: require("./amd"),
|
||||
umd: require("./umd")
|
||||
};
|
||||
|
||||
@@ -4,12 +4,11 @@ module.exports = SystemFormatter;
|
||||
|
||||
var DefaultFormatter = require("./_default");
|
||||
var AMDFormatter = require("./amd");
|
||||
var useStrict = require("../helpers/use-strict");
|
||||
var util = require("../../util");
|
||||
var t = require("../../types");
|
||||
var last = require("lodash/array/last");
|
||||
var each = require("lodash/collection/each");
|
||||
var map = require("lodash/collection/map");
|
||||
var t = require("../../types");
|
||||
|
||||
function SystemFormatter(file) {
|
||||
this.exportIdentifier = file.scope.generateUidIdentifier("export");
|
||||
@@ -193,9 +192,5 @@ SystemFormatter.prototype.transform = function (program) {
|
||||
|
||||
handlerBody.push(returnStatement);
|
||||
|
||||
if (useStrict.has(block)) {
|
||||
handlerBody.unshift(block.body.shift());
|
||||
}
|
||||
|
||||
program.body = [runner];
|
||||
};
|
||||
|
||||
7
lib/babel/transformation/templates/named-function.js
Normal file
7
lib/babel/transformation/templates/named-function.js
Normal file
@@ -0,0 +1,7 @@
|
||||
(function () {
|
||||
function GET_OUTER_ID() {
|
||||
return FUNCTION_ID;
|
||||
}
|
||||
|
||||
return FUNCTION;
|
||||
})()
|
||||
@@ -1,3 +1,4 @@
|
||||
{
|
||||
FUNCTION_ID:while (true) BLOCK
|
||||
var AGAIN_ID = true;
|
||||
FUNCTION_ID: while (AGAIN_ID) BLOCK
|
||||
}
|
||||
|
||||
@@ -1,23 +0,0 @@
|
||||
(function () {
|
||||
function Tail(func, args, context) {
|
||||
this.func = func;
|
||||
this.args = args;
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
Tail.prototype._isTailDescriptor = true;
|
||||
|
||||
var isRunning = false;
|
||||
|
||||
return function (func, args, context) {
|
||||
var result = new Tail(func, args, context);
|
||||
if (!isRunning) {
|
||||
isRunning = true;
|
||||
do {
|
||||
result = result.func.apply(result.context, result.args);
|
||||
} while (result instanceof Tail || (result && result._isTailDescriptor));
|
||||
isRunning = false;
|
||||
}
|
||||
return result;
|
||||
};
|
||||
})()
|
||||
@@ -1 +1,3 @@
|
||||
{}
|
||||
{
|
||||
"selfContained": "runtime"
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ var visitor = {
|
||||
|
||||
if (t.isAssignmentExpression(parent) || t.isUpdateExpression(parent)) {
|
||||
if (parent._ignoreBlockScopingTDZ) return;
|
||||
this.parentPath.replaceNode(t.sequenceExpression([assert, parent]));
|
||||
this.parentPath.node = t.sequenceExpression([assert, parent]);
|
||||
} else {
|
||||
return t.logicalExpression("&&", assert, node);
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@ var isLet = function (node, parent) {
|
||||
};
|
||||
|
||||
var isLetInitable = function (node, parent) {
|
||||
return !t.isFor(parent) || t.isFor(parent) && parent.left !== node;
|
||||
return !t.isFor(parent) || !t.isFor(parent, { left: node });
|
||||
};
|
||||
|
||||
var isVar = function (node, parent) {
|
||||
|
||||
@@ -65,6 +65,7 @@ function ClassTransformer(node, file, scope, isStatement) {
|
||||
ClassTransformer.prototype.run = function () {
|
||||
var superName = this.superName;
|
||||
var className = this.className;
|
||||
var classBody = this.node.body.body;
|
||||
var file = this.file;
|
||||
|
||||
//
|
||||
@@ -77,13 +78,21 @@ ClassTransformer.prototype.run = function () {
|
||||
className
|
||||
]))
|
||||
]);
|
||||
var constructor;
|
||||
|
||||
var constructor;
|
||||
if (this.node.id) {
|
||||
constructor = t.functionDeclaration(className, [], constructorBody);
|
||||
body.push(constructor);
|
||||
} else {
|
||||
constructor = t.functionExpression(null, [], constructorBody);
|
||||
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)
|
||||
]));
|
||||
@@ -159,7 +168,7 @@ ClassTransformer.prototype.buildBody = function () {
|
||||
}, true);
|
||||
replaceSupers.replace();
|
||||
|
||||
if (node.key.name === "constructor") {
|
||||
if ((!node.computed && t.isIdentifier(node.key, { name: "constructor" })) || t.isLiteral(node.key, { value: "constructor" })) {
|
||||
this.pushConstructor(node);
|
||||
} else {
|
||||
this.pushMethod(node);
|
||||
|
||||
@@ -41,90 +41,114 @@ DestructuringTransformer.prototype.buildVariableDeclaration = function (id, init
|
||||
return declar;
|
||||
};
|
||||
|
||||
DestructuringTransformer.prototype.push = function (elem, parentId) {
|
||||
if (t.isObjectPattern(elem)) {
|
||||
this.pushObjectPattern(elem, parentId);
|
||||
} else if (t.isArrayPattern(elem)) {
|
||||
this.pushArrayPattern(elem, parentId);
|
||||
} else if (t.isAssignmentPattern(elem)) {
|
||||
this.pushAssignmentPattern(elem, parentId);
|
||||
DestructuringTransformer.prototype.push = function (id, init) {
|
||||
if (t.isObjectPattern(id)) {
|
||||
this.pushObjectPattern(id, init);
|
||||
} else if (t.isArrayPattern(id)) {
|
||||
this.pushArrayPattern(id, init);
|
||||
} else if (t.isAssignmentPattern(id)) {
|
||||
this.pushAssignmentPattern(id, init);
|
||||
} else {
|
||||
this.nodes.push(this.buildVariableAssignment(elem, parentId));
|
||||
this.nodes.push(this.buildVariableAssignment(id, init));
|
||||
}
|
||||
};
|
||||
|
||||
DestructuringTransformer.prototype.pushAssignmentPattern = function (pattern, parentId) {
|
||||
var tempParentId = this.scope.generateUidBasedOnNode(parentId);
|
||||
DestructuringTransformer.prototype.get = function () {
|
||||
|
||||
};
|
||||
|
||||
DestructuringTransformer.prototype.pushAssignmentPattern = function (pattern, valueRef) {
|
||||
// we need to assign the current value of the assignment to avoid evaluating
|
||||
// it more than once
|
||||
|
||||
var tempValueRef = this.scope.generateUidBasedOnNode(valueRef);
|
||||
|
||||
var declar = t.variableDeclaration("var", [
|
||||
t.variableDeclarator(tempParentId, parentId)
|
||||
t.variableDeclarator(tempValueRef, valueRef)
|
||||
]);
|
||||
declar._blockHoist = this.blockHoist;
|
||||
this.nodes.push(declar);
|
||||
|
||||
//
|
||||
|
||||
this.nodes.push(this.buildVariableAssignment(
|
||||
pattern.left,
|
||||
t.conditionalExpression(
|
||||
t.binaryExpression("===", tempParentId, t.identifier("undefined")),
|
||||
t.binaryExpression("===", tempValueRef, t.identifier("undefined")),
|
||||
pattern.right,
|
||||
tempParentId
|
||||
tempValueRef
|
||||
)
|
||||
));
|
||||
};
|
||||
|
||||
DestructuringTransformer.prototype.pushObjectSpread = function (pattern, parentId, prop, i) {
|
||||
DestructuringTransformer.prototype.pushObjectSpread = function (pattern, objRef, spreadProp, spreadPropIndex) {
|
||||
// get all the keys that appear in this object before the current spread
|
||||
|
||||
var keys = [];
|
||||
for (var i2 = 0; i2 < pattern.properties.length; i2++) {
|
||||
var prop2 = pattern.properties[i2];
|
||||
|
||||
if (i2 >= i) break;
|
||||
if (t.isSpreadProperty(prop2)) continue;
|
||||
for (var i = 0; i < pattern.properties.length; i++) {
|
||||
var prop = pattern.properties[i];
|
||||
|
||||
var key = prop2.key;
|
||||
if (t.isIdentifier(key)) {
|
||||
key = t.literal(prop2.key.name);
|
||||
}
|
||||
// we've exceeded the index of the spread property to all properties to the
|
||||
// right need to be ignored
|
||||
if (i >= spreadPropIndex) break;
|
||||
|
||||
// ignore other spread properties
|
||||
if (t.isSpreadProperty(prop)) continue;
|
||||
|
||||
var key = prop.key;
|
||||
if (t.isIdentifier(key)) key = t.literal(prop.key.name);
|
||||
keys.push(key);
|
||||
}
|
||||
|
||||
keys = t.arrayExpression(keys);
|
||||
|
||||
var value = t.callExpression(this.file.addHelper("object-without-properties"), [parentId, keys]);
|
||||
this.nodes.push(this.buildVariableAssignment(prop.argument, value));
|
||||
//
|
||||
|
||||
var value = t.callExpression(this.file.addHelper("object-without-properties"), [objRef, keys]);
|
||||
this.nodes.push(this.buildVariableAssignment(spreadProp.argument, value));
|
||||
};
|
||||
|
||||
DestructuringTransformer.prototype.pushObjectProperty = function (prop, parentId) {
|
||||
DestructuringTransformer.prototype.pushObjectProperty = function (prop, propRef) {
|
||||
if (t.isLiteral(prop.key)) prop.computed = true;
|
||||
|
||||
var pattern2 = prop.value;
|
||||
var patternId2 = t.memberExpression(parentId, prop.key, prop.computed);
|
||||
var pattern = prop.value;
|
||||
var objRef = t.memberExpression(propRef, prop.key, prop.computed);
|
||||
|
||||
if (t.isPattern(pattern2)) {
|
||||
this.push(pattern2, patternId2);
|
||||
if (t.isPattern(pattern)) {
|
||||
this.push(pattern, objRef);
|
||||
} else {
|
||||
this.nodes.push(this.buildVariableAssignment(pattern2, patternId2));
|
||||
this.nodes.push(this.buildVariableAssignment(pattern, objRef));
|
||||
}
|
||||
};
|
||||
|
||||
DestructuringTransformer.prototype.pushObjectPattern = function (pattern, parentId) {
|
||||
DestructuringTransformer.prototype.pushObjectPattern = function (pattern, objRef) {
|
||||
// https://github.com/babel/babel/issues/681
|
||||
|
||||
if (!pattern.properties.length) {
|
||||
this.nodes.push(t.expressionStatement(
|
||||
t.callExpression(this.file.addHelper("object-destructuring-empty"), [parentId])
|
||||
t.callExpression(this.file.addHelper("object-destructuring-empty"), [objRef])
|
||||
));
|
||||
}
|
||||
|
||||
if (pattern.properties.length > 1 && t.isMemberExpression(parentId)) {
|
||||
var temp = this.scope.generateUidBasedOnNode(parentId, this.file);
|
||||
this.nodes.push(this.buildVariableDeclaration(temp, parentId));
|
||||
parentId = temp;
|
||||
// if we have more than one properties in this pattern and the objectRef is a
|
||||
// member expression then we need to assign it to a temporary variable so it's
|
||||
// only evaluated once
|
||||
|
||||
if (pattern.properties.length > 1 && t.isMemberExpression(objRef)) {
|
||||
var temp = this.scope.generateUidBasedOnNode(objRef, this.file);
|
||||
this.nodes.push(this.buildVariableDeclaration(temp, objRef));
|
||||
objRef = temp;
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
for (var i = 0; i < pattern.properties.length; i++) {
|
||||
var prop = pattern.properties[i];
|
||||
if (t.isSpreadProperty(prop)) {
|
||||
this.pushObjectSpread(pattern, parentId, prop, i);
|
||||
this.pushObjectSpread(pattern, objRef, prop, i);
|
||||
} else {
|
||||
this.pushObjectProperty(prop, parentId);
|
||||
this.pushObjectProperty(prop, objRef);
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -138,19 +162,70 @@ var hasRest = function (pattern) {
|
||||
return false;
|
||||
};
|
||||
|
||||
DestructuringTransformer.prototype.pushArrayPattern = function (pattern, parentId) {
|
||||
DestructuringTransformer.prototype.canUnpackArrayPattern = function (pattern, arr) {
|
||||
// not an array so there's no way we can deal with this
|
||||
if (!t.isArrayExpression(arr)) return false;
|
||||
|
||||
// pattern has less elements than the array and doesn't have a rest so some
|
||||
// elements wont be evaluated
|
||||
if (pattern.elements.length > arr.elements.length) return;
|
||||
if (pattern.elements.length < arr.elements.length && !hasRest(pattern)) return false;
|
||||
|
||||
// deopt on holes
|
||||
for (var i = 0; i < pattern.elements.length; i++) {
|
||||
if (!pattern.elements[i]) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
DestructuringTransformer.prototype.pushUnpackedArrayPattern = function (pattern, arr) {
|
||||
for (var i = 0; i < pattern.elements.length; i++) {
|
||||
var elem = pattern.elements[i];
|
||||
if (t.isRestElement(elem)) {
|
||||
this.push(elem.argument, t.arrayExpression(arr.elements.slice(i)));
|
||||
} else {
|
||||
this.push(elem, arr.elements[i]);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
DestructuringTransformer.prototype.pushArrayPattern = function (pattern, arrayRef) {
|
||||
if (!pattern.elements) return;
|
||||
|
||||
// if we have a rest then we need all the elements
|
||||
// optimise basic array destructuring of an array expression
|
||||
//
|
||||
// we can't do this to a pattern of unequal size to it's right hand
|
||||
// array expression as then there will be values that wont be evaluated
|
||||
//
|
||||
// eg: var [a, b] = [1, 2];
|
||||
|
||||
if (this.canUnpackArrayPattern(pattern, arrayRef)) {
|
||||
return this.pushUnpackedArrayPattern(pattern, arrayRef);
|
||||
}
|
||||
|
||||
// if we have a rest then we need all the elements so don't tell
|
||||
// `scope.toArray` to only get a certain amount
|
||||
|
||||
var count = !hasRest(pattern) && pattern.elements.length;
|
||||
|
||||
var toArray = this.scope.toArray(parentId, count);
|
||||
// so we need to ensure that the `arrayRef` is an array, `scope.toArray` will
|
||||
// return a locally bound identifier if it's been inferred to be an array,
|
||||
// otherwise it'll be a call to a helper that will ensure it's one
|
||||
|
||||
var _parentId = this.scope.generateUidBasedOnNode(parentId);
|
||||
this.nodes.push(this.buildVariableDeclaration(_parentId, toArray));
|
||||
parentId = _parentId;
|
||||
var toArray = this.scope.toArray(arrayRef, count);
|
||||
|
||||
this.scope.assignTypeGeneric(parentId.name, "Array");
|
||||
if (t.isIdentifier(toArray)) {
|
||||
// we've been given an identifier so it must have been inferred to be an
|
||||
// array
|
||||
arrayRef = toArray;
|
||||
} else {
|
||||
arrayRef = this.scope.generateUidBasedOnNode(arrayRef);
|
||||
this.nodes.push(this.buildVariableDeclaration(arrayRef, toArray));
|
||||
this.scope.assignTypeGeneric(arrayRef.name, "Array");
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
for (var i = 0; i < pattern.elements.length; i++) {
|
||||
var elem = pattern.elements[i];
|
||||
@@ -158,32 +233,39 @@ DestructuringTransformer.prototype.pushArrayPattern = function (pattern, parentI
|
||||
// hole
|
||||
if (!elem) continue;
|
||||
|
||||
var newPatternId;
|
||||
var elemRef;
|
||||
|
||||
if (t.isRestElement(elem)) {
|
||||
newPatternId = this.scope.toArray(parentId);
|
||||
elemRef = this.scope.toArray(arrayRef);
|
||||
|
||||
if (i > 0) {
|
||||
newPatternId = t.callExpression(t.memberExpression(newPatternId, t.identifier("slice")), [t.literal(i)]);
|
||||
elemRef = t.callExpression(t.memberExpression(elemRef, t.identifier("slice")), [t.literal(i)]);
|
||||
}
|
||||
|
||||
// set the element to the rest element argument since we've dealt with it
|
||||
// being a rest already
|
||||
elem = elem.argument;
|
||||
} else {
|
||||
newPatternId = t.memberExpression(parentId, t.literal(i), true);
|
||||
elemRef = t.memberExpression(arrayRef, t.literal(i), true);
|
||||
}
|
||||
|
||||
this.push(elem, newPatternId);
|
||||
this.push(elem, elemRef);
|
||||
}
|
||||
};
|
||||
|
||||
DestructuringTransformer.prototype.init = function (pattern, parentId) {
|
||||
if (!t.isArrayExpression(parentId) && !t.isMemberExpression(parentId) && !t.isIdentifier(parentId)) {
|
||||
var key = this.scope.generateUidBasedOnNode(parentId);
|
||||
this.nodes.push(this.buildVariableDeclaration(key, parentId));
|
||||
parentId = key;
|
||||
DestructuringTransformer.prototype.init = function (pattern, ref) {
|
||||
// trying to destructure a value that we can't evaluate more than once so we
|
||||
// need to save it to a variable
|
||||
|
||||
if (!t.isArrayExpression(ref) && !t.isMemberExpression(ref) && !t.isIdentifier(ref)) {
|
||||
var key = this.scope.generateUidBasedOnNode(ref);
|
||||
this.nodes.push(this.buildVariableDeclaration(key, ref));
|
||||
ref = key;
|
||||
}
|
||||
|
||||
this.push(pattern, parentId);
|
||||
//
|
||||
|
||||
this.push(pattern, ref);
|
||||
};
|
||||
|
||||
exports.ForInStatement =
|
||||
@@ -244,7 +326,7 @@ exports.Function = function (node, parent, scope, file) {
|
||||
if (!t.isPattern(pattern)) return pattern;
|
||||
|
||||
hasDestructuringTransformer = true;
|
||||
var parentId = scope.generateUidIdentifier("ref");
|
||||
var ref = scope.generateUidIdentifier("ref");
|
||||
|
||||
var destructuring = new DestructuringTransformer({
|
||||
blockHoist: node.params.length - i,
|
||||
@@ -253,9 +335,9 @@ exports.Function = function (node, parent, scope, file) {
|
||||
file: file,
|
||||
kind: "var",
|
||||
});
|
||||
destructuring.init(pattern, parentId);
|
||||
destructuring.init(pattern, ref);
|
||||
|
||||
return parentId;
|
||||
return ref;
|
||||
});
|
||||
|
||||
if (!hasDestructuringTransformer) return;
|
||||
@@ -337,7 +419,7 @@ exports.AssignmentExpression = function (node, parent, scope, file) {
|
||||
return t.toSequenceExpression(nodes, scope);
|
||||
};
|
||||
|
||||
var variableDeclarationhasPattern = function (node) {
|
||||
var variableDeclarationHasPattern = function (node) {
|
||||
for (var i = 0; i < node.declarations.length; i++) {
|
||||
if (t.isPattern(node.declarations[i].id)) {
|
||||
return true;
|
||||
@@ -348,7 +430,7 @@ var variableDeclarationhasPattern = function (node) {
|
||||
|
||||
exports.VariableDeclaration = function (node, parent, scope, file) {
|
||||
if (t.isForInStatement(parent) || t.isForOfStatement(parent)) return;
|
||||
if (!variableDeclarationhasPattern(node)) return;
|
||||
if (!variableDeclarationHasPattern(node)) return;
|
||||
|
||||
var nodes = [];
|
||||
var declar;
|
||||
|
||||
@@ -37,11 +37,35 @@ exports.ForOfStatement = function (node, parent, scope, file) {
|
||||
return loop;
|
||||
};
|
||||
|
||||
var breakVisitor = {
|
||||
enter: function (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;
|
||||
|
||||
if (t.isIdentifier(left) || t.isPattern(left)) {
|
||||
if (t.isIdentifier(left) || t.isPattern(left) || t.isMemberExpression(left)) {
|
||||
// for (i of test), for ({ i } of test)
|
||||
id = left;
|
||||
} else if (t.isVariableDeclaration(left)) {
|
||||
@@ -54,9 +78,12 @@ var loose = function (node, parent, scope, file) {
|
||||
throw file.errorWithNode(left, messages.get("unknownForHead", left.type));
|
||||
}
|
||||
|
||||
var iteratorKey = scope.generateUidIdentifier("iterator");
|
||||
var isArrayKey = scope.generateUidIdentifier("isArray");
|
||||
|
||||
var loop = util.template("for-of-loose", {
|
||||
LOOP_OBJECT: scope.generateUidIdentifier("iterator"),
|
||||
IS_ARRAY: scope.generateUidIdentifier("isArray"),
|
||||
LOOP_OBJECT: iteratorKey,
|
||||
IS_ARRAY: isArrayKey,
|
||||
OBJECT: node.right,
|
||||
INDEX: scope.generateUidIdentifier("i"),
|
||||
ID: id
|
||||
@@ -68,6 +95,24 @@ var loose = function (node, parent, scope, file) {
|
||||
loop.body.body.shift();
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
scope.traverse(node, breakVisitor, {
|
||||
iteratorKey: iteratorKey,
|
||||
wrapReturn: function (node) {
|
||||
return t.ifStatement(
|
||||
t.logicalExpression(
|
||||
"&&",
|
||||
t.unaryExpression("!", isArrayKey, true),
|
||||
t.memberExpression(iteratorKey, t.identifier("return")
|
||||
)
|
||||
), node);
|
||||
},
|
||||
label: t.isLabeledStatement(parent) && parent.label.name
|
||||
});
|
||||
|
||||
//
|
||||
|
||||
return {
|
||||
declar: declar,
|
||||
loop: loop
|
||||
@@ -81,7 +126,7 @@ var spec = function (node, parent, scope, file) {
|
||||
var stepKey = scope.generateUidIdentifier("step");
|
||||
var stepValue = t.memberExpression(stepKey, t.identifier("value"));
|
||||
|
||||
if (t.isIdentifier(left) || t.isPattern(left)) {
|
||||
if (t.isIdentifier(left) || t.isPattern(left) || t.isMemberExpression(left)) {
|
||||
// for (i of test), for ({ i } of test)
|
||||
declar = t.expressionStatement(t.assignmentExpression("=", left, stepValue));
|
||||
} else if (t.isVariableDeclaration(left)) {
|
||||
@@ -93,12 +138,28 @@ var spec = function (node, parent, scope, file) {
|
||||
throw file.errorWithNode(left, messages.get("unknownForHead", left.type));
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
var iteratorKey = scope.generateUidIdentifier("iterator");
|
||||
|
||||
var loop = util.template("for-of", {
|
||||
ITERATOR_KEY: scope.generateUidIdentifier("iterator"),
|
||||
ITERATOR_KEY: iteratorKey,
|
||||
STEP_KEY: stepKey,
|
||||
OBJECT: node.right
|
||||
});
|
||||
|
||||
//
|
||||
|
||||
scope.traverse(node, breakVisitor, {
|
||||
iteratorKey: iteratorKey,
|
||||
wrapReturn: function (node) {
|
||||
return t.ifStatement(t.memberExpression(iteratorKey, t.identifier("return")), node);
|
||||
},
|
||||
label: t.isLabeledStatement(parent) && parent.label.name
|
||||
});
|
||||
|
||||
//
|
||||
|
||||
return {
|
||||
declar: declar,
|
||||
loop: loop
|
||||
|
||||
@@ -5,6 +5,9 @@ var t = require("../../../types");
|
||||
exports.check = require("../internal/modules").check;
|
||||
|
||||
exports.ImportDeclaration = function (node, parent, scope, file) {
|
||||
// flow type
|
||||
if (node.isType) return;
|
||||
|
||||
var nodes = [];
|
||||
|
||||
if (node.specifiers.length) {
|
||||
@@ -24,6 +27,9 @@ exports.ImportDeclaration = function (node, parent, scope, file) {
|
||||
};
|
||||
|
||||
exports.ExportDeclaration = function (node, parent, scope, file) {
|
||||
// flow type
|
||||
if (t.isTypeAlias(node.declaration)) return;
|
||||
|
||||
var nodes = [];
|
||||
var i;
|
||||
|
||||
|
||||
@@ -1,17 +1,15 @@
|
||||
"use strict";
|
||||
|
||||
var nameMethod = require("../../helpers/name-method");
|
||||
var t = require("../../../types");
|
||||
var clone = require("lodash/lang/clone");
|
||||
var clone = require("lodash/lang/clone");
|
||||
var t = require("../../../types");
|
||||
|
||||
exports.check = function (node) {
|
||||
return t.isProperty(node) && (node.method || node.shorthand);
|
||||
};
|
||||
|
||||
exports.Property = function (node, parent, scope, file) {
|
||||
exports.Property = function (node) {
|
||||
if (node.method) {
|
||||
node.method = false;
|
||||
nameMethod.property(node, file, scope);
|
||||
}
|
||||
|
||||
if (node.shorthand) {
|
||||
|
||||
@@ -40,6 +40,10 @@ TailCallTransformer.prototype.getFunctionId = function () {
|
||||
return this.functionId = this.functionId || this.scope.generateUidIdentifier("function");
|
||||
};
|
||||
|
||||
TailCallTransformer.prototype.getAgainId = function () {
|
||||
return this.againId = this.againId || this.scope.generateUidIdentifier("again");
|
||||
};
|
||||
|
||||
TailCallTransformer.prototype.getParams = function () {
|
||||
var params = this.params;
|
||||
|
||||
@@ -112,7 +116,12 @@ TailCallTransformer.prototype.run = function () {
|
||||
body.unshift(t.variableDeclaration("var", paramDecls));
|
||||
}
|
||||
|
||||
body.unshift(t.expressionStatement(
|
||||
t.assignmentExpression("=", this.getAgainId(), t.literal(false)))
|
||||
);
|
||||
|
||||
node.body = util.template("tail-call-body", {
|
||||
AGAIN_ID: this.getAgainId(),
|
||||
THIS_ID: this.thisId,
|
||||
ARGUMENTS_ID: this.argumentsId,
|
||||
FUNCTION_ID: this.getFunctionId(),
|
||||
@@ -286,6 +295,9 @@ TailCallTransformer.prototype.subTransformCallExpression = function (node) {
|
||||
}
|
||||
}
|
||||
|
||||
body.push(t.expressionStatement(
|
||||
t.assignmentExpression("=", this.getAgainId(), t.literal(true))
|
||||
));
|
||||
body.push(t.continueStatement(this.getFunctionId()));
|
||||
|
||||
return body;
|
||||
|
||||
@@ -1,12 +1,18 @@
|
||||
module.exports = {
|
||||
useStrict: require("./other/use-strict"),
|
||||
|
||||
// this goes at the start so we only transform the original user code
|
||||
"spec.functionName": require("./spec/function-name"),
|
||||
|
||||
"validation.undeclaredVariableCheck": require("./validation/undeclared-variable-check"),
|
||||
"validation.noForInOfAssignment": require("./validation/no-for-in-of-assignment"),
|
||||
"validation.setters": require("./validation/setters"),
|
||||
"validation.react": require("./validation/react"),
|
||||
"spec.blockScopedFunctions": require("./spec/block-scoped-functions"),
|
||||
|
||||
// needs to be before `_aliasFunction`
|
||||
"es6.arrowFunctions": require("./es6/arrow-functions"),
|
||||
|
||||
"playground.malletOperator": require("./playground/mallet-operator"),
|
||||
"playground.methodBinding": require("./playground/method-binding"),
|
||||
"playground.memoizationOperator": require("./playground/memoization-operator"),
|
||||
@@ -22,9 +28,6 @@ module.exports = {
|
||||
// needs to be before `_aliasFunction`
|
||||
"es7.comprehensions": require("./es7/comprehensions"),
|
||||
|
||||
// needs to be before `_aliasFunction`
|
||||
"es6.arrowFunctions": require("./es6/arrow-functions"),
|
||||
|
||||
"es6.classes": require("./es6/classes"),
|
||||
|
||||
asyncToGenerator: require("./other/async-to-generator"),
|
||||
@@ -76,7 +79,7 @@ module.exports = {
|
||||
// needs to be after `regenerator` due to needing `regeneratorRuntime` references
|
||||
// needs to be after `es6.forOf` due to needing `Symbol.iterator` references
|
||||
// needs to be before `es6.modules` due to dynamic imports
|
||||
selfContained: require("./other/self-contained"),
|
||||
runtime: require("./other/runtime"),
|
||||
|
||||
// needs to be before `_blockHoist` due to function hoisting etc
|
||||
"es6.modules": require("./es6/modules"),
|
||||
@@ -92,6 +95,7 @@ module.exports = {
|
||||
"spec.typeofSymbol": require("./spec/typeof-symbol"),
|
||||
"spec.undefinedToVoid": require("./spec/undefined-to-void"),
|
||||
|
||||
_useStrict: require("./internal/use-strict"),
|
||||
_moduleFormatter: require("./internal/module-formatter"),
|
||||
|
||||
"es3.propertyLiterals": require("./es3/property-literals"),
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
"use strict";
|
||||
|
||||
var useStrict = require("../../helpers/use-strict");
|
||||
var groupBy = require("lodash/collection/groupBy");
|
||||
var flatten = require("lodash/array/flatten");
|
||||
var values = require("lodash/object/values");
|
||||
var groupBy = require("lodash/collection/groupBy");
|
||||
var flatten = require("lodash/array/flatten");
|
||||
var values = require("lodash/object/values");
|
||||
|
||||
// Priority:
|
||||
//
|
||||
@@ -22,15 +21,13 @@ exports.Program = {
|
||||
}
|
||||
if (!hasChange) return;
|
||||
|
||||
useStrict.wrap(node, function () {
|
||||
var nodePriorities = groupBy(node.body, function (bodyNode) {
|
||||
var priority = bodyNode._blockHoist;
|
||||
if (priority == null) priority = 1;
|
||||
if (priority === true) priority = 2;
|
||||
return priority;
|
||||
});
|
||||
|
||||
node.body = flatten(values(nodePriorities).reverse());
|
||||
var nodePriorities = groupBy(node.body, function (bodyNode) {
|
||||
var priority = bodyNode._blockHoist;
|
||||
if (priority == null) priority = 1;
|
||||
if (priority === true) priority = 2;
|
||||
return priority;
|
||||
});
|
||||
|
||||
node.body = flatten(values(nodePriorities).reverse());
|
||||
}
|
||||
};
|
||||
|
||||
@@ -9,10 +9,10 @@ exports.BlockStatement =
|
||||
exports.Program = function (node, parent, scope, file) {
|
||||
if (!node._declarations) return;
|
||||
|
||||
var kinds = {};
|
||||
var kind;
|
||||
|
||||
useStrict.wrap(node, function () {
|
||||
var kinds = {};
|
||||
var kind;
|
||||
|
||||
for (var i in node._declarations) {
|
||||
var declar = node._declarations[i];
|
||||
|
||||
@@ -30,7 +30,7 @@ exports.Program = function (node, parent, scope, file) {
|
||||
for (kind in kinds) {
|
||||
node.body.unshift(file.attachAuxiliaryComment(t.variableDeclaration(kind, kinds[kind])));
|
||||
}
|
||||
});
|
||||
|
||||
node._declarations = null;
|
||||
node._declarations = null;
|
||||
});
|
||||
};
|
||||
|
||||
@@ -24,6 +24,9 @@ exports.ImportDeclaration = resolveModuleSource;
|
||||
exports.ExportDeclaration = function (node, parent, scope) {
|
||||
resolveModuleSource.apply(null, arguments);
|
||||
|
||||
// flow type
|
||||
if (node.isType) return;
|
||||
|
||||
var declar = node.declaration;
|
||||
|
||||
if (node.default) {
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
"use strict";
|
||||
|
||||
var t = require("../../../types");
|
||||
|
||||
exports.Program = function (program, parent, scope, file) {
|
||||
if (file.transformers.useStrict.canRun()) {
|
||||
program.body.unshift(t.expressionStatement(t.literal("use strict")));
|
||||
}
|
||||
};
|
||||
@@ -3,6 +3,29 @@
|
||||
var react = require("../../helpers/react");
|
||||
var t = require("../../../types");
|
||||
|
||||
var JSX_ANNOTATION_REGEX = /^\*\s*@jsx\s+([^\s]+)/;
|
||||
|
||||
exports.Program = function (node, parent, scope, file) {
|
||||
var id = "React.createElement";
|
||||
|
||||
for (var i = 0; i < file.ast.comments.length; i++) {
|
||||
var comment = file.ast.comments[i];
|
||||
var matches = JSX_ANNOTATION_REGEX.exec(comment.value);
|
||||
if (matches) {
|
||||
id = matches[1];
|
||||
if (id === "React.DOM") {
|
||||
throw file.errorWithNode(comment, "The @jsx React.DOM pragma has been deprecated as of React 0.12");
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
file.set("jsxIdentifier", id.split(".").map(t.identifier).reduce(function (object, property) {
|
||||
return t.memberExpression(object, property);
|
||||
}));
|
||||
};
|
||||
|
||||
require("../../helpers/build-react-transformer")(exports, {
|
||||
pre: function (state) {
|
||||
var tagName = state.tagName;
|
||||
@@ -14,7 +37,7 @@ require("../../helpers/build-react-transformer")(exports, {
|
||||
}
|
||||
},
|
||||
|
||||
post: function (state) {
|
||||
state.callee = t.memberExpression(t.identifier("React"), t.identifier("createElement"));
|
||||
post: function (state, file) {
|
||||
state.callee = file.get("jsxIdentifier");
|
||||
}
|
||||
});
|
||||
|
||||
@@ -82,7 +82,7 @@ exports.Program = function (node, parent, scope, file) {
|
||||
};
|
||||
|
||||
exports.pre = function (file) {
|
||||
file.setDynamic("runtimeIdentifier", function () {
|
||||
file.setDynamic("helpersNamespace", function () {
|
||||
return file.addImport("babel-runtime/helpers", "babelHelpers");
|
||||
});
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
"use strict";
|
||||
|
||||
var useStrict = require("../../helpers/use-strict");
|
||||
var messages = require("../../../messages");
|
||||
var t = require("../../../types");
|
||||
var messages = require("../../../messages");
|
||||
var t = require("../../../types");
|
||||
|
||||
exports.Program = function (program) {
|
||||
if (!useStrict.has(program)) {
|
||||
program.body.unshift(t.expressionStatement(t.literal("use strict")));
|
||||
var first = program.body[0];
|
||||
if (t.isExpressionStatement(first) && t.isLiteral(first.expression, { value: "use strict" })) {
|
||||
program.body.shift();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
"use strict";
|
||||
|
||||
var nameMethod = require("../../helpers/name-method");
|
||||
|
||||
exports.FunctionExpression = nameMethod.bare;
|
||||
@@ -2,14 +2,12 @@
|
||||
|
||||
module.exports = TraversalPath;
|
||||
|
||||
/* jshint maxparams:7 */
|
||||
|
||||
var traverse = require("./index");
|
||||
var includes = require("lodash/collection/includes");
|
||||
var Scope = require("./scope");
|
||||
var t = require("../types");
|
||||
|
||||
function TraversalPath(context, parent, obj, key) {
|
||||
function TraversalPath(context, parent, container, key) {
|
||||
this.shouldRemove = false;
|
||||
this.shouldSkip = false;
|
||||
this.shouldStop = false;
|
||||
@@ -19,14 +17,30 @@ function TraversalPath(context, parent, obj, key) {
|
||||
this.state = this.context.state;
|
||||
this.opts = this.context.opts;
|
||||
|
||||
this.key = key;
|
||||
this.obj = obj;
|
||||
this.container = container;
|
||||
this.key = key;
|
||||
|
||||
this.parent = parent;
|
||||
this.scope = TraversalPath.getScope(this.getNode(), parent, context.scope);
|
||||
this.state = context.state;
|
||||
|
||||
this.setScope();
|
||||
}
|
||||
|
||||
TraversalPath.getScope = function (node, parent, scope) {
|
||||
var ourScope = scope;
|
||||
|
||||
// we're entering a new scope so let's construct it!
|
||||
if (t.isScope(node, parent)) {
|
||||
ourScope = new Scope(node, parent, scope);
|
||||
}
|
||||
|
||||
return ourScope;
|
||||
};
|
||||
|
||||
TraversalPath.prototype.setScope = function () {
|
||||
this.scope = TraversalPath.getScope(this.node, this.parent, this.context.scope);
|
||||
};
|
||||
|
||||
TraversalPath.prototype.remove = function () {
|
||||
this.shouldRemove = true;
|
||||
this.shouldSkip = true;
|
||||
@@ -45,67 +59,48 @@ TraversalPath.prototype.flatten = function () {
|
||||
this.context.flatten();
|
||||
};
|
||||
|
||||
TraversalPath.getScope = function (node, parent, scope) {
|
||||
var ourScope = scope;
|
||||
Object.defineProperty(TraversalPath.prototype, "node", {
|
||||
get: function () {
|
||||
return this.container[this.key];
|
||||
},
|
||||
|
||||
// we're entering a new scope so let's construct it!
|
||||
if (t.isScope(node, parent)) {
|
||||
ourScope = new Scope(node, parent, scope);
|
||||
}
|
||||
set: function (replacement) {
|
||||
var isArray = Array.isArray(replacement);
|
||||
|
||||
return ourScope;
|
||||
};
|
||||
// inherit comments from original node to the first replacement node
|
||||
var inheritTo = replacement;
|
||||
if (isArray) inheritTo = replacement[0];
|
||||
if (inheritTo) t.inheritsComments(inheritTo, this.node);
|
||||
|
||||
TraversalPath.prototype.maybeRemove = function () {
|
||||
if (this.shouldRemove) {
|
||||
this.setNode(null);
|
||||
this.flatten();
|
||||
}
|
||||
};
|
||||
// replace the node
|
||||
this.container[this.key] = replacement;
|
||||
this.setScope();
|
||||
|
||||
TraversalPath.prototype.setNode = function (val) {
|
||||
return this.obj[this.key] = val;
|
||||
};
|
||||
|
||||
TraversalPath.prototype.getNode = function () {
|
||||
return this.obj[this.key];
|
||||
};
|
||||
|
||||
TraversalPath.prototype.replaceNode = function (replacement) {
|
||||
var isArray = Array.isArray(replacement);
|
||||
|
||||
// inherit comments from original node to the first replacement node
|
||||
var inheritTo = replacement;
|
||||
if (isArray) inheritTo = replacement[0];
|
||||
if (inheritTo) t.inheritsComments(inheritTo, this.getNode());
|
||||
|
||||
// replace the node
|
||||
this.setNode(replacement);
|
||||
|
||||
var file = this.scope && this.scope.file;
|
||||
if (file) {
|
||||
if (isArray) {
|
||||
for (var i = 0; i < replacement.length; i++) {
|
||||
file.checkNode(replacement[i], this.scope);
|
||||
var file = this.scope && this.scope.file;
|
||||
if (file) {
|
||||
if (isArray) {
|
||||
for (var i = 0; i < replacement.length; i++) {
|
||||
file.checkNode(replacement[i], this.scope);
|
||||
}
|
||||
} else {
|
||||
file.checkNode(replacement, this.scope);
|
||||
}
|
||||
} else {
|
||||
file.checkNode(replacement, this.scope);
|
||||
}
|
||||
}
|
||||
|
||||
// we're replacing a statement or block node with an array of statements so we better
|
||||
// ensure that it's a block
|
||||
if (isArray) {
|
||||
if (includes(t.STATEMENT_OR_BLOCK_KEYS, this.key) && !t.isBlockStatement(this.obj)) {
|
||||
t.ensureBlock(this.obj, this.key);
|
||||
}
|
||||
|
||||
this.flatten();
|
||||
// we're replacing a statement or block node with an array of statements so we better
|
||||
// ensure that it's a block
|
||||
if (isArray) {
|
||||
if (includes(t.STATEMENT_OR_BLOCK_KEYS, this.key) && !t.isBlockStatement(this.container)) {
|
||||
t.ensureBlock(this.container, this.key);
|
||||
}
|
||||
|
||||
this.flatten();
|
||||
}
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
TraversalPath.prototype.call = function (key) {
|
||||
var node = this.getNode();
|
||||
var node = this.node;
|
||||
if (!node) return;
|
||||
|
||||
var opts = this.opts;
|
||||
@@ -115,18 +110,18 @@ TraversalPath.prototype.call = function (key) {
|
||||
var replacement = fn.call(this, node, this.parent, this.scope, this.state);
|
||||
|
||||
if (replacement) {
|
||||
this.replaceNode(replacement);
|
||||
node = replacement;
|
||||
this.node = replacement;
|
||||
}
|
||||
|
||||
this.maybeRemove();
|
||||
|
||||
return node;
|
||||
if (this.shouldRemove) {
|
||||
this.container[this.key] = null;
|
||||
this.flatten();
|
||||
}
|
||||
};
|
||||
|
||||
TraversalPath.prototype.visit = function () {
|
||||
var opts = this.opts;
|
||||
var node = this.getNode();
|
||||
var node = this.node;
|
||||
|
||||
// type is blacklisted
|
||||
if (opts.blacklist && opts.blacklist.indexOf(node.type) > -1) {
|
||||
@@ -139,7 +134,7 @@ TraversalPath.prototype.visit = function () {
|
||||
return this.shouldStop;
|
||||
}
|
||||
|
||||
node = this.getNode();
|
||||
node = this.node;
|
||||
|
||||
if (Array.isArray(node)) {
|
||||
// traverse over these replacement nodes we purposely don't call exitNode
|
||||
@@ -154,3 +149,7 @@ TraversalPath.prototype.visit = function () {
|
||||
|
||||
return this.shouldStop;
|
||||
};
|
||||
|
||||
TraversalPath.prototype.isReferencedIdentifier = function () {
|
||||
return t.isReferencedIdentifier(this.node);
|
||||
};
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
"PrivateDeclaration": ["Statement", "Declaration"],
|
||||
|
||||
"ArrowFunctionExpression": ["Scopable", "Function", "Expression"],
|
||||
"FunctionDeclaration": ["Statement", "Declaration", "Scopable", "Function"],
|
||||
"FunctionDeclaration": ["Scopable", "Function", "Statement", "Declaration"],
|
||||
"FunctionExpression": ["Scopable", "Function", "Expression"],
|
||||
|
||||
"ImportSpecifier": ["ModuleSpecifier"],
|
||||
@@ -39,16 +39,15 @@
|
||||
"ClassDeclaration": ["Statement", "Declaration", "Class"],
|
||||
"ClassExpression": ["Class", "Expression"],
|
||||
|
||||
"ForOfStatement": ["Statement", "For", "Scopable", "Loop"],
|
||||
"ForInStatement": ["Statement", "For", "Scopable", "Loop"],
|
||||
"ForStatement": ["Statement", "For", "Scopable", "Loop"],
|
||||
"ForOfStatement": ["Scopable", "Statement", "For", "Loop"],
|
||||
"ForInStatement": ["Scopable", "Statement", "For", "Loop"],
|
||||
"ForStatement": ["Scopable", "Statement", "For", "Loop"],
|
||||
|
||||
"ObjectPattern": ["Pattern"],
|
||||
"ArrayPattern": ["Pattern"],
|
||||
"AssignmentPattern": ["Pattern"],
|
||||
|
||||
"Property": ["UserWhitespacable"],
|
||||
"JSXElement": ["UserWhitespacable", "Expression"],
|
||||
|
||||
"ArrayExpression": ["Expression"],
|
||||
"AssignmentExpression": ["Expression"],
|
||||
@@ -74,7 +73,7 @@
|
||||
|
||||
"JSXAttribute": ["JSX"],
|
||||
"JSXClosingElement": ["JSX"],
|
||||
"JSXElement": ["JSX"],
|
||||
"JSXElement": ["JSX", "Expression"],
|
||||
"JSXEmptyExpression": ["JSX"],
|
||||
"JSXExpressionContainer": ["JSX"],
|
||||
"JSXIdentifier": ["JSX"],
|
||||
|
||||
@@ -56,6 +56,12 @@ exports.arrayify = function (val) {
|
||||
throw new TypeError("illegal type for arrayify");
|
||||
};
|
||||
|
||||
exports.booleanify = function (val) {
|
||||
if (val === "true") return true;
|
||||
if (val === "false") return false;
|
||||
return val;
|
||||
};
|
||||
|
||||
var templateVisitor = {
|
||||
enter: function (node, parent, scope, nodes) {
|
||||
if (t.isExpressionStatement(node)) {
|
||||
|
||||
15
package.json
15
package.json
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "babel",
|
||||
"description": "Turn ES6 code into readable vanilla ES5 with source maps",
|
||||
"version": "4.2.1",
|
||||
"version": "4.5.0",
|
||||
"author": "Sebastian McKenzie <sebmck@gmail.com>",
|
||||
"homepage": "https://babeljs.io/",
|
||||
"repository": "babel/babel",
|
||||
@@ -12,11 +12,9 @@
|
||||
},
|
||||
"bin": {
|
||||
"6to5": "./bin/deprecated/6to5",
|
||||
"6to5-minify": "./bin/deprecated/6to5-minify",
|
||||
"6to5-node": "./bin/deprecated/6to5-node",
|
||||
"6to5-runtime": "./bin/deprecated/6to5-runtime",
|
||||
"babel": "./bin/babel/index.js",
|
||||
"babel-minify": "./bin/babel-minify",
|
||||
"babel-node": "./bin/babel-node",
|
||||
"babel-external-helpers": "./bin/babel-external-helpers"
|
||||
},
|
||||
@@ -38,12 +36,12 @@
|
||||
"test": "make test"
|
||||
},
|
||||
"dependencies": {
|
||||
"acorn-babel": "0.11.1-33",
|
||||
"acorn-babel": "0.11.1-34",
|
||||
"ast-types": "~0.6.1",
|
||||
"chalk": "^0.5.1",
|
||||
"chalk": "^1.0.0",
|
||||
"chokidar": "^0.12.6",
|
||||
"commander": "^2.6.0",
|
||||
"core-js": "^0.5.4",
|
||||
"core-js": "^0.6.1",
|
||||
"debug": "^2.1.1",
|
||||
"detect-indent": "^3.0.0",
|
||||
"estraverse": "^1.9.1",
|
||||
@@ -51,13 +49,14 @@
|
||||
"fs-readdir-recursive": "^0.1.0",
|
||||
"globals": "^6.2.0",
|
||||
"is-integer": "^1.0.4",
|
||||
"js-tokenizer": "^1.3.3",
|
||||
"js-tokens": "0.4.1",
|
||||
"leven": "^1.0.1",
|
||||
"line-numbers": "0.2.0",
|
||||
"lodash": "^3.2.0",
|
||||
"output-file-sync": "^1.1.0",
|
||||
"path-is-absolute": "^1.0.0",
|
||||
"private": "^0.1.6",
|
||||
"regenerator-babel": "0.8.10-2",
|
||||
"regenerator-babel": "0.8.13-1",
|
||||
"regexpu": "^1.1.1",
|
||||
"repeating": "^1.1.2",
|
||||
"shebang-regex": "^1.0.0",
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "babel-runtime",
|
||||
"description": "babel selfContained runtime",
|
||||
"version": "4.2.0",
|
||||
"version": "4.4.6",
|
||||
"repository": "babel/babel",
|
||||
"author": "Sebastian McKenzie <sebmck@gmail.com>"
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
var a = {
|
||||
b() {
|
||||
return b;
|
||||
}
|
||||
};
|
||||
|
||||
assert.ok(
|
||||
/return b/.test(a.b.toString()),
|
||||
'toString contains body'
|
||||
);
|
||||
@@ -1,10 +1,8 @@
|
||||
function test() {
|
||||
|
||||
|
||||
/*
|
||||
* this is comment
|
||||
*/
|
||||
|
||||
|
||||
var i = 20;
|
||||
}
|
||||
|
||||
@@ -20,11 +20,8 @@
|
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
|
||||
function test() {}
|
||||
|
||||
|
||||
|
||||
// Copyright (C) 2012 Yusuke Suzuki <utatane.tea@gmail.com>
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
function one() {
|
||||
var _arguments = arguments;
|
||||
|
||||
var inner = function () {
|
||||
return _arguments;
|
||||
};
|
||||
@@ -11,12 +12,14 @@ one(1, 2);
|
||||
|
||||
function two() {
|
||||
var _arguments = arguments;
|
||||
|
||||
var inner = function () {
|
||||
return _arguments;
|
||||
};
|
||||
|
||||
var another = function () {
|
||||
var another = function another() {
|
||||
var _arguments2 = arguments;
|
||||
|
||||
var inner2 = function () {
|
||||
return _arguments2;
|
||||
};
|
||||
@@ -28,6 +31,7 @@ two(1, 2);
|
||||
|
||||
function three() {
|
||||
var _arguments = arguments;
|
||||
|
||||
var fn = function () {
|
||||
return _arguments[0] + "bar";
|
||||
};
|
||||
@@ -37,6 +41,7 @@ three("foo");
|
||||
|
||||
function four() {
|
||||
var _arguments = arguments;
|
||||
|
||||
var fn = function () {
|
||||
return _arguments[0].foo + "bar";
|
||||
};
|
||||
@@ -54,7 +59,7 @@ five({ arguments: ["foo"] });
|
||||
|
||||
function six(obj) {
|
||||
var fn = function () {
|
||||
var fn2 = function () {
|
||||
var fn2 = function fn2() {
|
||||
return arguments[0];
|
||||
};
|
||||
return fn2("foobar");
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
var some = function () {
|
||||
var count = arguments[0] === undefined ? "30" : arguments[0];
|
||||
|
||||
console.log("count", count);
|
||||
};
|
||||
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
"use strict";
|
||||
|
||||
module.exports = {
|
||||
init: function () {
|
||||
init: function init() {
|
||||
var _this = this;
|
||||
|
||||
return new Promise(function (resolve, reject) {
|
||||
MongoClient.connect(config.mongodb, function (err, db) {
|
||||
if (err) {
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
function b() {
|
||||
var _this = this;
|
||||
|
||||
var t = function (x) {
|
||||
return _this.x + x;
|
||||
};
|
||||
|
||||
@@ -20,8 +20,10 @@
|
||||
switch (_ret) {
|
||||
case "continue":
|
||||
continue;
|
||||
|
||||
case "break":
|
||||
break;
|
||||
|
||||
default:
|
||||
if (typeof _ret === "object") return _ret.v;
|
||||
}
|
||||
|
||||
@@ -2,9 +2,11 @@
|
||||
|
||||
for (var _iterator = [1, 2, 3][Symbol.iterator](), _step; !(_step = _iterator.next()).done;) {
|
||||
var x;
|
||||
|
||||
(function () {
|
||||
var i = _step.value;
|
||||
x = 5;
|
||||
|
||||
fns.push(function () {
|
||||
return i * x;
|
||||
});
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
"use strict";
|
||||
|
||||
var _slice = Array.prototype.slice;
|
||||
|
||||
var _inherits = function (subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) subClass.__proto__ = superClass; };
|
||||
|
||||
var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } };
|
||||
@@ -8,6 +9,7 @@ var _classCallCheck = function (instance, Constructor) { if (!(instance instance
|
||||
var Test = (function (Foo) {
|
||||
function Test() {
|
||||
var _Foo$prototype$test, _Foo$prototype$test2;
|
||||
|
||||
_classCallCheck(this, Test);
|
||||
|
||||
woops["super"].test();
|
||||
@@ -25,6 +27,7 @@ var Test = (function (Foo) {
|
||||
|
||||
Test.prototype.test = function test() {
|
||||
var _Foo$prototype$test, _Foo$prototype$test2;
|
||||
|
||||
Foo.prototype.test.call(this);
|
||||
(_Foo$prototype$test = Foo.prototype.test).call.apply(_Foo$prototype$test, [this].concat(_slice.call(arguments)));
|
||||
(_Foo$prototype$test2 = Foo.prototype.test).call.apply(_Foo$prototype$test2, [this, "test"].concat(_slice.call(arguments)));
|
||||
@@ -32,10 +35,11 @@ var Test = (function (Foo) {
|
||||
|
||||
Test.foo = function foo() {
|
||||
var _Foo$foo, _Foo$foo2;
|
||||
|
||||
Foo.foo.call(this);
|
||||
(_Foo$foo = Foo.foo).call.apply(_Foo$foo, [this].concat(_slice.call(arguments)));
|
||||
(_Foo$foo2 = Foo.foo).call.apply(_Foo$foo2, [this, "test"].concat(_slice.call(arguments)));
|
||||
};
|
||||
|
||||
return Test;
|
||||
})(Foo);
|
||||
})(Foo);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
"use strict";
|
||||
|
||||
var _slice = Array.prototype.slice;
|
||||
|
||||
var _prototypeProperties = function (child, staticProps, instanceProps) { if (staticProps) Object.defineProperties(child, staticProps); if (instanceProps) Object.defineProperties(child.prototype, instanceProps); };
|
||||
|
||||
var _get = function get(object, property, receiver) { var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc && desc.writable) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } };
|
||||
@@ -12,6 +13,7 @@ var _classCallCheck = function (instance, Constructor) { if (!(instance instance
|
||||
var Test = (function (Foo) {
|
||||
function Test() {
|
||||
var _get2, _get3;
|
||||
|
||||
_classCallCheck(this, Test);
|
||||
|
||||
woops["super"].test();
|
||||
@@ -31,6 +33,7 @@ var Test = (function (Foo) {
|
||||
foo: {
|
||||
value: function foo() {
|
||||
var _get2;
|
||||
|
||||
_get(Object.getPrototypeOf(Test), "foo", this).call(this);
|
||||
_get(Object.getPrototypeOf(Test), "foo", this).apply(this, arguments);
|
||||
(_get2 = _get(Object.getPrototypeOf(Test), "foo", this)).call.apply(_get2, [this, "test"].concat(_slice.call(arguments)));
|
||||
@@ -42,6 +45,7 @@ var Test = (function (Foo) {
|
||||
test: {
|
||||
value: function test() {
|
||||
var _get2;
|
||||
|
||||
_get(Object.getPrototypeOf(Test.prototype), "test", this).call(this);
|
||||
_get(Object.getPrototypeOf(Test.prototype), "test", this).apply(this, arguments);
|
||||
(_get2 = _get(Object.getPrototypeOf(Test.prototype), "test", this)).call.apply(_get2, [this, "test"].concat(_slice.call(arguments)));
|
||||
@@ -52,4 +56,4 @@ var Test = (function (Foo) {
|
||||
});
|
||||
|
||||
return Test;
|
||||
})(Foo);
|
||||
})(Foo);
|
||||
|
||||
20
test/fixtures/transformation/es6-classes/super-class-anonymous/actual.js
vendored
Normal file
20
test/fixtures/transformation/es6-classes/super-class-anonymous/actual.js
vendored
Normal file
@@ -0,0 +1,20 @@
|
||||
class TestEmpty extends (class {}) {
|
||||
}
|
||||
|
||||
class TestConstructorOnly extends (class { constructor() {} }) {
|
||||
}
|
||||
|
||||
class TestMethodOnly extends (class { method() {} }) {
|
||||
}
|
||||
|
||||
class TestConstructorAndMethod extends (class {
|
||||
constructor() {}
|
||||
method() {}
|
||||
}) {
|
||||
}
|
||||
|
||||
class TestMultipleMethods extends (class {
|
||||
m1() {}
|
||||
m2() {}
|
||||
}) {
|
||||
}
|
||||
129
test/fixtures/transformation/es6-classes/super-class-anonymous/expected.js
vendored
Normal file
129
test/fixtures/transformation/es6-classes/super-class-anonymous/expected.js
vendored
Normal file
@@ -0,0 +1,129 @@
|
||||
"use strict";
|
||||
|
||||
var _prototypeProperties = function (child, staticProps, instanceProps) { if (staticProps) Object.defineProperties(child, staticProps); if (instanceProps) Object.defineProperties(child.prototype, instanceProps); };
|
||||
|
||||
var _inherits = function (subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) subClass.__proto__ = superClass; };
|
||||
|
||||
var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } };
|
||||
|
||||
var TestEmpty = (function (_ref) {
|
||||
function TestEmpty() {
|
||||
_classCallCheck(this, TestEmpty);
|
||||
|
||||
if (_ref != null) {
|
||||
_ref.apply(this, arguments);
|
||||
}
|
||||
}
|
||||
|
||||
_inherits(TestEmpty, _ref);
|
||||
|
||||
return TestEmpty;
|
||||
})(function _class() {
|
||||
_classCallCheck(this, _class);
|
||||
});
|
||||
|
||||
var TestConstructorOnly = (function (_ref2) {
|
||||
function TestConstructorOnly() {
|
||||
_classCallCheck(this, TestConstructorOnly);
|
||||
|
||||
if (_ref2 != null) {
|
||||
_ref2.apply(this, arguments);
|
||||
}
|
||||
}
|
||||
|
||||
_inherits(TestConstructorOnly, _ref2);
|
||||
|
||||
return TestConstructorOnly;
|
||||
})(function _class2() {
|
||||
_classCallCheck(this, _class2);
|
||||
});
|
||||
|
||||
var TestMethodOnly = (function (_ref3) {
|
||||
function TestMethodOnly() {
|
||||
_classCallCheck(this, TestMethodOnly);
|
||||
|
||||
if (_ref3 != null) {
|
||||
_ref3.apply(this, arguments);
|
||||
}
|
||||
}
|
||||
|
||||
_inherits(TestMethodOnly, _ref3);
|
||||
|
||||
return TestMethodOnly;
|
||||
})((function () {
|
||||
var _class3 = function () {
|
||||
_classCallCheck(this, _class3);
|
||||
};
|
||||
|
||||
_prototypeProperties(_class3, null, {
|
||||
method: {
|
||||
value: function method() {},
|
||||
writable: true,
|
||||
configurable: true
|
||||
}
|
||||
});
|
||||
|
||||
return _class3;
|
||||
})());
|
||||
|
||||
var TestConstructorAndMethod = (function (_ref4) {
|
||||
function TestConstructorAndMethod() {
|
||||
_classCallCheck(this, TestConstructorAndMethod);
|
||||
|
||||
if (_ref4 != null) {
|
||||
_ref4.apply(this, arguments);
|
||||
}
|
||||
}
|
||||
|
||||
_inherits(TestConstructorAndMethod, _ref4);
|
||||
|
||||
return TestConstructorAndMethod;
|
||||
})((function () {
|
||||
var _class4 = function () {
|
||||
_classCallCheck(this, _class4);
|
||||
};
|
||||
|
||||
_prototypeProperties(_class4, null, {
|
||||
method: {
|
||||
value: function method() {},
|
||||
writable: true,
|
||||
configurable: true
|
||||
}
|
||||
});
|
||||
|
||||
return _class4;
|
||||
})());
|
||||
|
||||
var TestMultipleMethods = (function (_ref5) {
|
||||
function TestMultipleMethods() {
|
||||
_classCallCheck(this, TestMultipleMethods);
|
||||
|
||||
if (_ref5 != null) {
|
||||
_ref5.apply(this, arguments);
|
||||
}
|
||||
}
|
||||
|
||||
_inherits(TestMultipleMethods, _ref5);
|
||||
|
||||
return TestMultipleMethods;
|
||||
})((function () {
|
||||
var _class5 = function () {
|
||||
_classCallCheck(this, _class5);
|
||||
};
|
||||
|
||||
_prototypeProperties(_class5, null, {
|
||||
m1: {
|
||||
value: function m1() {},
|
||||
writable: true,
|
||||
configurable: true
|
||||
},
|
||||
m2: {
|
||||
value: function m2() {},
|
||||
writable: true,
|
||||
configurable: true
|
||||
}
|
||||
});
|
||||
|
||||
return _class5;
|
||||
})());
|
||||
|
||||
@@ -1,18 +1,12 @@
|
||||
"use strict";
|
||||
|
||||
var _ref = [1, 2];
|
||||
|
||||
var a = _ref[0];
|
||||
var b = _ref[1];
|
||||
var _ref2 = [3, 4];
|
||||
|
||||
var c = _ref2[0];
|
||||
var d = _ref2[1];
|
||||
var _ref3 = { e: 5, f: 6 };
|
||||
|
||||
var e = _ref3.e;
|
||||
var f = _ref3.f;
|
||||
var _ref4 = { a: 7, b: 8 };
|
||||
|
||||
var g = _ref4.a;
|
||||
var h = _ref4.b;
|
||||
var a = 1;
|
||||
var b = 2;
|
||||
var c = 3;
|
||||
var d = 4;
|
||||
var _ref = { e: 5, f: 6 };
|
||||
var e = _ref.e;
|
||||
var f = _ref.f;
|
||||
var _ref2 = { a: 7, b: 8 };
|
||||
var g = _ref2.a;
|
||||
var h = _ref2.b;
|
||||
|
||||
9
test/fixtures/transformation/es6-destructuring/array-unpack-optimisation/actual.js
vendored
Normal file
9
test/fixtures/transformation/es6-destructuring/array-unpack-optimisation/actual.js
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
// opt
|
||||
var [a, b] = [1, 2];
|
||||
var [[a, b]] = [[1, 2]];
|
||||
var [a, b, ...c] = [1, 2, 3, 4];
|
||||
var [[a, b, ...c]] = [[1, 2, 3, 4]];
|
||||
|
||||
// deopt
|
||||
var [a, b] = [1, 2, 3];
|
||||
var [[a, b]] = [[1, 2, 3]];
|
||||
21
test/fixtures/transformation/es6-destructuring/array-unpack-optimisation/expected.js
vendored
Normal file
21
test/fixtures/transformation/es6-destructuring/array-unpack-optimisation/expected.js
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
"use strict";
|
||||
|
||||
// opt
|
||||
var a = 1;
|
||||
var b = 2;
|
||||
var a = 1;
|
||||
var b = 2;
|
||||
var a = 1;
|
||||
var b = 2;
|
||||
var c = [3, 4];
|
||||
var a = 1;
|
||||
var b = 2;
|
||||
var c = [3, 4];
|
||||
|
||||
// deopt
|
||||
var _ref = [1, 2, 3];
|
||||
var a = _ref[0];
|
||||
var b = _ref[1];
|
||||
var _ref2 = [1, 2, 3];
|
||||
var a = _ref2[0];
|
||||
var b = _ref2[1];
|
||||
@@ -1 +1 @@
|
||||
var [a, [b], [c], d] = ["hello", [", ", "junk"], ["world"]];
|
||||
var [a, [b], [c]] = ["hello", [", ", "junk"], ["world"]];
|
||||
|
||||
@@ -1,14 +1,6 @@
|
||||
"use strict";
|
||||
|
||||
var _slicedToArray = function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { var _arr = []; for (var _iterator = arr[Symbol.iterator](), _step; !(_step = _iterator.next()).done;) { _arr.push(_step.value); if (i && _arr.length === i) break; } return _arr; } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } };
|
||||
|
||||
var _ref = ["hello", [", ", "junk"], ["world"]];
|
||||
|
||||
var a = _ref[0];
|
||||
var _ref$1 = _slicedToArray(_ref[1], 1);
|
||||
|
||||
var b = _ref$1[0];
|
||||
var _ref$2 = _slicedToArray(_ref[2], 1);
|
||||
|
||||
var c = _ref$2[0];
|
||||
var d = _ref[3];
|
||||
var a = "hello";
|
||||
var _ref = [", ", "junk"];
|
||||
var b = _ref[0];
|
||||
var c = "world";
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
"use strict";
|
||||
|
||||
var _temp, _temp2;
|
||||
|
||||
var _slicedToArray = function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { var _arr = []; for (var _iterator = arr[Symbol.iterator](), _step; !(_step = _iterator.next()).done;) { _arr.push(_step.value); if (i && _arr.length === i) break; } return _arr; } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } };
|
||||
|
||||
console.log((_temp = [123], _temp2 = _slicedToArray(_temp, 1), x = _temp2[0], _temp));
|
||||
|
||||
@@ -8,4 +8,5 @@ var _ref2 = _slicedToArray(_ref, 2);
|
||||
|
||||
a = _ref2[0];
|
||||
b = _ref2[1];
|
||||
|
||||
;
|
||||
|
||||
@@ -3,11 +3,12 @@
|
||||
var _slicedToArray = function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { var _arr = []; for (var _iterator = arr[Symbol.iterator](), _step; !(_step = _iterator.next()).done;) { _arr.push(_step.value); if (i && _arr.length === i) break; } return _arr; } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } };
|
||||
|
||||
var _ref = ["foo", "hello", [", ", "junk"], ["world"]];
|
||||
|
||||
var a = _ref[1];
|
||||
|
||||
var _ref$2 = _slicedToArray(_ref[2], 1);
|
||||
|
||||
var b = _ref$2[0];
|
||||
|
||||
var _ref$3 = _slicedToArray(_ref[3], 1);
|
||||
|
||||
var c = _ref$3[0];
|
||||
|
||||
@@ -5,9 +5,11 @@ var _objectWithoutProperties = function (obj, keys) { var target = {}; for (var
|
||||
var x = _objectWithoutProperties(z, []);
|
||||
|
||||
var x = z.x;
|
||||
|
||||
var y = _objectWithoutProperties(z, ["x"]);
|
||||
|
||||
(function (_ref) {
|
||||
var x = _ref.x;
|
||||
|
||||
var y = _objectWithoutProperties(_ref, ["x"]);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -7,5 +7,6 @@ for (var _ref in obj) {
|
||||
|
||||
var name = _ref2[0];
|
||||
var value = _ref2[1];
|
||||
|
||||
print("Name: " + name + ", Value: " + value);
|
||||
}
|
||||
|
||||
@@ -5,4 +5,5 @@ var _toArray = function (arr) { return Array.isArray(arr) ? arr : Array.from(arr
|
||||
var _z = _toArray(z);
|
||||
|
||||
var x = _z[0];
|
||||
|
||||
var y = _z.slice(1);
|
||||
|
||||
@@ -8,4 +8,5 @@ var _ref2 = _slicedToArray(_ref, 2);
|
||||
|
||||
foo.foo = _ref2[0];
|
||||
foo.bar = _ref2[1];
|
||||
|
||||
;
|
||||
|
||||
@@ -6,6 +6,7 @@ var _rect$topLeft = _slicedToArray(rect.topLeft, 2);
|
||||
|
||||
var x1 = _rect$topLeft[0];
|
||||
var y1 = _rect$topLeft[1];
|
||||
|
||||
var _rect$bottomRight = _slicedToArray(rect.bottomRight, 2);
|
||||
|
||||
var x2 = _rect$bottomRight[0];
|
||||
|
||||
@@ -14,22 +14,25 @@ function somethingAdvanced(_ref) {
|
||||
function unpackObject(_ref) {
|
||||
var title = _ref.title;
|
||||
var author = _ref.author;
|
||||
|
||||
return title + " " + author;
|
||||
}
|
||||
|
||||
console.log(unpackObject({ title: "title", author: "author" }));
|
||||
|
||||
var unpackArray = function (_ref, _ref3) {
|
||||
var unpackArray = function unpackArray(_ref, _ref3) {
|
||||
var _ref2 = _slicedToArray(_ref, 3);
|
||||
|
||||
var a = _ref2[0];
|
||||
var b = _ref2[1];
|
||||
var c = _ref2[2];
|
||||
|
||||
var _ref32 = _slicedToArray(_ref3, 3);
|
||||
|
||||
var x = _ref32[0];
|
||||
var y = _ref32[1];
|
||||
var z = _ref32[2];
|
||||
|
||||
return a + b + c;
|
||||
};
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ var isSorted = function (_ref) {
|
||||
|
||||
var x = _ref2[0];
|
||||
var y = _ref2[1];
|
||||
|
||||
var wow = _ref2.slice(2);
|
||||
|
||||
if (!zs.length) return true;
|
||||
|
||||
29
test/fixtures/transformation/es6-for-of-loose/break/actual.js
vendored
Normal file
29
test/fixtures/transformation/es6-for-of-loose/break/actual.js
vendored
Normal file
@@ -0,0 +1,29 @@
|
||||
// labels
|
||||
|
||||
foo: for (let x of foo()) {
|
||||
while (true) {
|
||||
break foo;
|
||||
}
|
||||
}
|
||||
|
||||
foo: for (let x of foo()) {
|
||||
while (true) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
foo: for (let x of foo()) {
|
||||
break foo;
|
||||
}
|
||||
|
||||
// basic
|
||||
|
||||
for (let x of foo()) {
|
||||
break;
|
||||
}
|
||||
|
||||
for (let x of foo()) {
|
||||
while (true) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
100
test/fixtures/transformation/es6-for-of-loose/break/expected.js
vendored
Normal file
100
test/fixtures/transformation/es6-for-of-loose/break/expected.js
vendored
Normal file
@@ -0,0 +1,100 @@
|
||||
"use strict";
|
||||
|
||||
// labels
|
||||
|
||||
foo: for (var _iterator = foo(), _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
|
||||
var _ref;
|
||||
|
||||
if (_isArray) {
|
||||
if (_i >= _iterator.length) break;
|
||||
_ref = _iterator[_i++];
|
||||
} else {
|
||||
_i = _iterator.next();
|
||||
if (_i.done) break;
|
||||
_ref = _i.value;
|
||||
}
|
||||
|
||||
var x = _ref;
|
||||
|
||||
while (true) {
|
||||
if (!_isArray && _iterator["return"]) _iterator["return"]();
|
||||
|
||||
break foo;
|
||||
}
|
||||
}
|
||||
|
||||
foo: for (var _iterator2 = foo(), _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
|
||||
var _ref2;
|
||||
|
||||
if (_isArray2) {
|
||||
if (_i2 >= _iterator2.length) break;
|
||||
_ref2 = _iterator2[_i2++];
|
||||
} else {
|
||||
_i2 = _iterator2.next();
|
||||
if (_i2.done) break;
|
||||
_ref2 = _i2.value;
|
||||
}
|
||||
|
||||
var x = _ref2;
|
||||
|
||||
while (true) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
foo: for (var _iterator3 = foo(), _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : _iterator3[Symbol.iterator]();;) {
|
||||
var _ref3;
|
||||
|
||||
if (_isArray3) {
|
||||
if (_i3 >= _iterator3.length) break;
|
||||
_ref3 = _iterator3[_i3++];
|
||||
} else {
|
||||
_i3 = _iterator3.next();
|
||||
if (_i3.done) break;
|
||||
_ref3 = _i3.value;
|
||||
}
|
||||
|
||||
var x = _ref3;
|
||||
if (!_isArray3 && _iterator3["return"]) _iterator3["return"]();
|
||||
|
||||
break foo;
|
||||
}
|
||||
|
||||
// basic
|
||||
|
||||
for (var _iterator4 = foo(), _isArray4 = Array.isArray(_iterator4), _i4 = 0, _iterator4 = _isArray4 ? _iterator4 : _iterator4[Symbol.iterator]();;) {
|
||||
var _ref4;
|
||||
|
||||
if (_isArray4) {
|
||||
if (_i4 >= _iterator4.length) break;
|
||||
_ref4 = _iterator4[_i4++];
|
||||
} else {
|
||||
_i4 = _iterator4.next();
|
||||
if (_i4.done) break;
|
||||
_ref4 = _i4.value;
|
||||
}
|
||||
|
||||
var x = _ref4;
|
||||
if (!_isArray4 && _iterator4["return"]) _iterator4["return"]();
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
for (var _iterator5 = foo(), _isArray5 = Array.isArray(_iterator5), _i5 = 0, _iterator5 = _isArray5 ? _iterator5 : _iterator5[Symbol.iterator]();;) {
|
||||
var _ref5;
|
||||
|
||||
if (_isArray5) {
|
||||
if (_i5 >= _iterator5.length) break;
|
||||
_ref5 = _iterator5[_i5++];
|
||||
} else {
|
||||
_i5 = _iterator5.next();
|
||||
if (_i5.done) break;
|
||||
_ref5 = _i5.value;
|
||||
}
|
||||
|
||||
var x = _ref5;
|
||||
|
||||
while (true) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
for (var _iterator = arr, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
|
||||
var _ref;
|
||||
|
||||
if (_isArray) {
|
||||
if (_i >= _iterator.length) break;
|
||||
_ref = _iterator[_i++];
|
||||
@@ -10,5 +11,6 @@ for (var _iterator = arr, _isArray = Array.isArray(_iterator), _i = 0, _iterator
|
||||
if (_i.done) break;
|
||||
_ref = _i.value;
|
||||
}
|
||||
|
||||
var i = _ref;
|
||||
}
|
||||
}
|
||||
|
||||
3
test/fixtures/transformation/es6-for-of-loose/member-expression/actual.js
vendored
Normal file
3
test/fixtures/transformation/es6-for-of-loose/member-expression/actual.js
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
for (obj.prop of arr) {
|
||||
|
||||
}
|
||||
12
test/fixtures/transformation/es6-for-of-loose/member-expression/expected.js
vendored
Normal file
12
test/fixtures/transformation/es6-for-of-loose/member-expression/expected.js
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
"use strict";
|
||||
|
||||
for (var _iterator = arr, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
|
||||
if (_isArray) {
|
||||
if (_i >= _iterator.length) break;
|
||||
obj.prop = _iterator[_i++];
|
||||
} else {
|
||||
_i = _iterator.next();
|
||||
if (_i.done) break;
|
||||
obj.prop = _i.value;
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
for (var _iterator = arr, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
|
||||
var _ref;
|
||||
|
||||
if (_isArray) {
|
||||
if (_i >= _iterator.length) break;
|
||||
_ref = _iterator[_i++];
|
||||
@@ -10,11 +11,13 @@ for (var _iterator = arr, _isArray = Array.isArray(_iterator), _i = 0, _iterator
|
||||
if (_i.done) break;
|
||||
_ref = _i.value;
|
||||
}
|
||||
|
||||
var i = _ref;
|
||||
}
|
||||
|
||||
for (var _iterator2 = numbers, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
|
||||
var _ref2;
|
||||
|
||||
if (_isArray2) {
|
||||
if (_i2 >= _iterator2.length) break;
|
||||
_ref2 = _iterator2[_i2++];
|
||||
@@ -23,5 +26,6 @@ for (var _iterator2 = numbers, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _
|
||||
if (_i2.done) break;
|
||||
_ref2 = _i2.value;
|
||||
}
|
||||
|
||||
var i = _ref2;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
for (var _iterator = arr, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
|
||||
var _ref;
|
||||
|
||||
if (_isArray) {
|
||||
if (_i >= _iterator.length) break;
|
||||
_ref = _iterator[_i++];
|
||||
@@ -10,5 +11,6 @@ for (var _iterator = arr, _isArray = Array.isArray(_iterator), _i = 0, _iterator
|
||||
if (_i.done) break;
|
||||
_ref = _i.value;
|
||||
}
|
||||
|
||||
var i = _ref;
|
||||
}
|
||||
}
|
||||
|
||||
29
test/fixtures/transformation/es6-for-of/break/actual.js
vendored
Normal file
29
test/fixtures/transformation/es6-for-of/break/actual.js
vendored
Normal file
@@ -0,0 +1,29 @@
|
||||
// labels
|
||||
|
||||
foo: for (let x of foo()) {
|
||||
while (true) {
|
||||
break foo;
|
||||
}
|
||||
}
|
||||
|
||||
foo: for (let x of foo()) {
|
||||
while (true) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
foo: for (let x of foo()) {
|
||||
break foo;
|
||||
}
|
||||
|
||||
// basic
|
||||
|
||||
for (let x of foo()) {
|
||||
break;
|
||||
}
|
||||
|
||||
for (let x of foo()) {
|
||||
while (true) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
45
test/fixtures/transformation/es6-for-of/break/expected.js
vendored
Normal file
45
test/fixtures/transformation/es6-for-of/break/expected.js
vendored
Normal file
@@ -0,0 +1,45 @@
|
||||
"use strict";
|
||||
|
||||
// labels
|
||||
|
||||
foo: for (var _iterator = foo()[Symbol.iterator](), _step; !(_step = _iterator.next()).done;) {
|
||||
var x = _step.value;
|
||||
|
||||
while (true) {
|
||||
if (_iterator["return"]) _iterator["return"]();
|
||||
|
||||
break foo;
|
||||
}
|
||||
}
|
||||
|
||||
foo: for (var _iterator2 = foo()[Symbol.iterator](), _step2; !(_step2 = _iterator2.next()).done;) {
|
||||
var x = _step2.value;
|
||||
|
||||
while (true) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
foo: for (var _iterator3 = foo()[Symbol.iterator](), _step3; !(_step3 = _iterator3.next()).done;) {
|
||||
var x = _step3.value;
|
||||
if (_iterator3["return"]) _iterator3["return"]();
|
||||
|
||||
break foo;
|
||||
}
|
||||
|
||||
// basic
|
||||
|
||||
for (var _iterator4 = foo()[Symbol.iterator](), _step4; !(_step4 = _iterator4.next()).done;) {
|
||||
var x = _step4.value;
|
||||
if (_iterator4["return"]) _iterator4["return"]();
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
for (var _iterator5 = foo()[Symbol.iterator](), _step5; !(_step5 = _iterator5.next()).done;) {
|
||||
var x = _step5.value;
|
||||
|
||||
while (true) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
for (foo.bar of test) {
|
||||
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
{
|
||||
"throws": "Unknown node type \"MemberExpression\" in ForStatement"
|
||||
}
|
||||
3
test/fixtures/transformation/es6-for-of/member-expression/actual.js
vendored
Normal file
3
test/fixtures/transformation/es6-for-of/member-expression/actual.js
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
for (obj.prop of arr) {
|
||||
|
||||
}
|
||||
5
test/fixtures/transformation/es6-for-of/member-expression/expected.js
vendored
Normal file
5
test/fixtures/transformation/es6-for-of/member-expression/expected.js
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
"use strict";
|
||||
|
||||
for (var _iterator = arr[Symbol.iterator](), _step; !(_step = _iterator.next()).done;) {
|
||||
obj.prop = _step.value;
|
||||
}
|
||||
@@ -8,6 +8,7 @@ define(["exports", "module"], function (exports, module) {
|
||||
module.exports = {};
|
||||
module.exports = [];
|
||||
module.exports = foo;
|
||||
|
||||
module.exports = function () {};
|
||||
|
||||
var _default = function _default() {
|
||||
@@ -16,6 +17,7 @@ define(["exports", "module"], function (exports, module) {
|
||||
|
||||
module.exports = _default;
|
||||
function foo() {}
|
||||
|
||||
var Foo = function Foo() {
|
||||
_classCallCheck(this, Foo);
|
||||
};
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user