Compare commits
36 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
864169c1eb | ||
|
|
930d7f9aa6 | ||
|
|
ed40ec03d1 | ||
|
|
73062ae240 | ||
|
|
bafa3e0d4c | ||
|
|
f3d2b2bb81 | ||
|
|
a74b67d4eb | ||
|
|
625416862f | ||
|
|
8dda8838ba | ||
|
|
96d3a25213 | ||
|
|
727208f376 | ||
|
|
1b046a6ecb | ||
|
|
3b8ed0d401 | ||
|
|
d4c98d7738 | ||
|
|
4e44af819f | ||
|
|
a65acd73ca | ||
|
|
b7cb2bcb7b | ||
|
|
5ac4dc0541 | ||
|
|
99ddd02b0a | ||
|
|
19cfee14fd | ||
|
|
fea487bf91 | ||
|
|
ff2cbd5a2e | ||
|
|
12e01f9e71 | ||
|
|
f62436909d | ||
|
|
e42c1adeb6 | ||
|
|
e30dbbab94 | ||
|
|
e3daa28e60 | ||
|
|
9e5d94126c | ||
|
|
95798bee0b | ||
|
|
63f25ab038 | ||
|
|
27a8f2d2ea | ||
|
|
78434bb404 | ||
|
|
82833a8901 | ||
|
|
93d5288d71 | ||
|
|
5d3074b460 | ||
|
|
a57475abc9 |
27
CHANGELOG.md
27
CHANGELOG.md
@@ -13,6 +13,33 @@ _Note: Gaps between patch versions are faulty/broken releases._
|
||||
|
||||
See [CHANGELOG - 6to5](CHANGELOG-6to5.md) for the pre-4.0.0 version changelog.
|
||||
|
||||
## 4.6.6
|
||||
|
||||
* **Bug Fix**
|
||||
* Fix incorrect method call in `utility.deadCodeElimination` transformer.
|
||||
* Fix `es6.blockScopingTDZ` transformer duplicating binding nodes.
|
||||
|
||||
## 4.6.5
|
||||
|
||||
* **Internal**
|
||||
* `useStrict` transformer has been renamed to `strict`.
|
||||
|
||||
## 4.6.4
|
||||
|
||||
* **Bug Fix**
|
||||
* Fix `ForOfStatement` not proplery inheriting labels.
|
||||
* When in closure mode in block scoping transformer, properly check for variable shadowing.
|
||||
* **New Feature**
|
||||
* New `utility.inlineEnvironmentVariables` and `utility.inlineExpression` transformers.
|
||||
|
||||
## 4.6.3
|
||||
|
||||
* **Bug Fix**
|
||||
* Fix `arguments` being incorrectly aliased in arrow function rest parameter optimisation.
|
||||
* Make deoptimisation trigger safer.
|
||||
* **New Feature**
|
||||
* Flow types are now retained when blacklisting the `flow` transformer.
|
||||
|
||||
## 4.6.1
|
||||
|
||||
* **Bug Fix**
|
||||
|
||||
@@ -53,7 +53,6 @@ $ mocha test/transformation.js
|
||||
#### Code Standards
|
||||
|
||||
* **General**
|
||||
* No ES6 syntax features or methods, exclusively ES5.
|
||||
* Max of five arguments for functions
|
||||
* Max depth of four nested blocks
|
||||
* 2-spaced soft tabs
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "babel",
|
||||
"description": "Turn ES6 code into readable vanilla ES5 with source maps",
|
||||
"version": "4.6.2",
|
||||
"version": "4.6.6",
|
||||
"author": "Sebastian McKenzie <sebmck@gmail.com>",
|
||||
"homepage": "https://babeljs.io/",
|
||||
"repository": "babel/babel",
|
||||
@@ -57,7 +57,7 @@
|
||||
"path-is-absolute": "^1.0.0",
|
||||
"private": "^0.1.6",
|
||||
"regenerator-babel": "0.8.13-1",
|
||||
"regexpu": "^1.1.1",
|
||||
"regexpu": "^1.1.2",
|
||||
"repeating": "^1.1.2",
|
||||
"shebang-regex": "^1.0.0",
|
||||
"slash": "^1.0.0",
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "babel-runtime",
|
||||
"description": "babel selfContained runtime",
|
||||
"version": "4.6.1",
|
||||
"version": "4.6.5",
|
||||
"repository": "babel/babel",
|
||||
"author": "Sebastian McKenzie <sebmck@gmail.com>"
|
||||
}
|
||||
@@ -25,7 +25,7 @@ transform.load = function (url, callback, opts = {}, hold) {
|
||||
if (!hold) transform.run.apply(transform, param);
|
||||
if (callback) callback(param);
|
||||
} else {
|
||||
throw new Error("Could not load " + url);
|
||||
throw new Error(`Could not load ${url}`);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -46,7 +46,7 @@ var compile = function (filename) {
|
||||
var opts = extend({}, transformOpts);
|
||||
resolveRc(filename, opts);
|
||||
|
||||
var cacheKey = filename + ":" + JSON.stringify(opts);
|
||||
var cacheKey = `${filename}:${JSON.stringify(opts)}`;
|
||||
|
||||
if (cache) {
|
||||
var cached = cache[cacheKey];
|
||||
|
||||
@@ -23,7 +23,7 @@ export default function (loc, opts = {}) {
|
||||
try {
|
||||
json = JSON.parse(content);
|
||||
} catch (err) {
|
||||
err.message = file + ": " + err.message;
|
||||
err.message = `${file}: ${err.message}`;
|
||||
throw err;
|
||||
}
|
||||
|
||||
|
||||
@@ -140,7 +140,7 @@ export default class Buffer {
|
||||
var indent = this.getIndent();
|
||||
|
||||
// replace all newlines with newlines with the indentation
|
||||
str = str.replace(/\n/g, "\n" + indent);
|
||||
str = str.replace(/\n/g, `\n${indent}`);
|
||||
|
||||
// we've got a newline before us so prepend on the indentation
|
||||
if (this.isLast("\n")) this._push(indent);
|
||||
|
||||
@@ -109,16 +109,19 @@ export function ExpressionStatement(node, print) {
|
||||
this.semicolon();
|
||||
}
|
||||
|
||||
exports.BinaryExpression =
|
||||
exports.LogicalExpression =
|
||||
exports.AssignmentPattern =
|
||||
exports.AssignmentExpression = function (node, print) {
|
||||
export function AssignmentExpression(node, print) {
|
||||
// todo: add cases where the spaces can be dropped when in compact mode
|
||||
print(node.left);
|
||||
this.push(" ");
|
||||
this.push(node.operator);
|
||||
this.push(" ");
|
||||
print(node.right);
|
||||
}
|
||||
|
||||
export {
|
||||
AssignmentExpression as BinaryExpression,
|
||||
AssignmentExpression as LogicalExpression,
|
||||
AssignmentExpression as AssignmentPattern
|
||||
};
|
||||
|
||||
var SCIENTIFIC_NOTATION = /e/i;
|
||||
|
||||
@@ -44,8 +44,7 @@ export function _method(node, print) {
|
||||
print(value.body);
|
||||
}
|
||||
|
||||
exports.FunctionDeclaration =
|
||||
exports.FunctionExpression = function (node, print) {
|
||||
export function FunctionExpression(node, print) {
|
||||
if (node.async) this.push("async ");
|
||||
this.push("function");
|
||||
if (node.generator) this.push("*");
|
||||
@@ -60,7 +59,9 @@ exports.FunctionExpression = function (node, print) {
|
||||
this._params(node, print);
|
||||
this.space();
|
||||
print(node.body);
|
||||
};
|
||||
}
|
||||
|
||||
export { FunctionExpression as FunctionDeclaration };
|
||||
|
||||
export function ArrowFunctionExpression(node, print) {
|
||||
if (node.async) this.push("async ");
|
||||
|
||||
@@ -5,7 +5,7 @@ export function ImportSpecifier(node, print) {
|
||||
if (t.isSpecifierDefault(node)) {
|
||||
print(t.getSpecifierName(node));
|
||||
} else {
|
||||
return exports.ExportSpecifier.apply(this, arguments);
|
||||
return ExportSpecifier.apply(this, arguments);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,6 @@ import each from "lodash/collection/each";
|
||||
|
||||
each(["BindMemberExpression", "BindFunctionExpression"], function (type) {
|
||||
exports[type] = function () {
|
||||
throw new ReferenceError("Trying to render non-standard playground node " + JSON.stringify(type));
|
||||
throw new ReferenceError(`Trying to render non-standard playground node ${JSON.stringify(type)}`);
|
||||
};
|
||||
});
|
||||
|
||||
@@ -60,7 +60,7 @@ var buildForXStatement = function (op) {
|
||||
this.keyword("for");
|
||||
this.push("(");
|
||||
print(node.left);
|
||||
this.push(" " + op + " ");
|
||||
this.push(` ${op} `);
|
||||
print(node.right);
|
||||
this.push(")");
|
||||
print.block(node.body);
|
||||
@@ -192,7 +192,7 @@ export function VariableDeclaration(node, print, parent) {
|
||||
|
||||
var sep = ",";
|
||||
if (!this.format.compact && hasInits) {
|
||||
sep += "\n" + repeating(" ", node.kind.length + 1);
|
||||
sep += `\n${repeating(" ", node.kind.length + 1)}`;
|
||||
} else {
|
||||
sep += " ";
|
||||
}
|
||||
|
||||
@@ -4,12 +4,12 @@ export function Identifier(node) {
|
||||
this.push(node.name);
|
||||
}
|
||||
|
||||
exports.RestElement =
|
||||
exports.SpreadElement =
|
||||
exports.SpreadProperty = function (node, print) {
|
||||
export function RestElement(node, print) {
|
||||
this.push("...");
|
||||
print(node.argument);
|
||||
};
|
||||
}
|
||||
|
||||
export { RestElement as SpreadElement, RestElement as SpreadProperty };
|
||||
|
||||
export function VirtualPropertyExpression(node, print) {
|
||||
print(node.object);
|
||||
@@ -17,8 +17,7 @@ export function VirtualPropertyExpression(node, print) {
|
||||
print(node.property);
|
||||
}
|
||||
|
||||
exports.ObjectExpression =
|
||||
exports.ObjectPattern = function (node, print) {
|
||||
export function ObjectExpression(node, print) {
|
||||
var props = node.properties;
|
||||
|
||||
if (props.length) {
|
||||
@@ -32,7 +31,9 @@ exports.ObjectPattern = function (node, print) {
|
||||
} else {
|
||||
this.push("{}");
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export { ObjectExpression as ObjectPattern };
|
||||
|
||||
export function Property(node, print) {
|
||||
if (node.method || node.kind === "get" || node.kind === "set") {
|
||||
@@ -53,8 +54,7 @@ export function Property(node, print) {
|
||||
}
|
||||
}
|
||||
|
||||
exports.ArrayExpression =
|
||||
exports.ArrayPattern = function (node, print) {
|
||||
export function ArrayExpression(node, print) {
|
||||
var elems = node.elements;
|
||||
var len = elems.length;
|
||||
|
||||
@@ -76,7 +76,9 @@ exports.ArrayPattern = function (node, print) {
|
||||
});
|
||||
|
||||
this.push("]");
|
||||
};
|
||||
}
|
||||
|
||||
export { ArrayExpression as ArrayPattern };
|
||||
|
||||
export function Literal(node) {
|
||||
var val = node.value;
|
||||
@@ -89,7 +91,7 @@ export function Literal(node) {
|
||||
} else if (type === "boolean") {
|
||||
this.push(val ? "true" : "false");
|
||||
} else if (node.regex) {
|
||||
this.push("/" + node.regex.pattern + "/" + node.regex.flags);
|
||||
this.push(`/${node.regex.pattern}/${node.regex.flags}`);
|
||||
} else if (val === null) {
|
||||
this.push("null");
|
||||
}
|
||||
|
||||
@@ -187,7 +187,7 @@ class CodeGenerator {
|
||||
|
||||
this.printTrailingComments(node, parent);
|
||||
} else {
|
||||
throw new ReferenceError("unknown node of type " + JSON.stringify(node.type) + " with constructor " + JSON.stringify(node && node.constructor.name));
|
||||
throw new ReferenceError(`unknown node of type ${JSON.stringify(node.type)} with constructor ${JSON.stringify(node && node.constructor.name)}`);
|
||||
}
|
||||
|
||||
this.format.concise = oldConcise;
|
||||
@@ -238,9 +238,9 @@ class CodeGenerator {
|
||||
generateComment(comment) {
|
||||
var val = comment.value;
|
||||
if (comment.type === "Line") {
|
||||
val = "//" + val;
|
||||
val = `//${val}`;
|
||||
} else {
|
||||
val = "/*" + val + "*/";
|
||||
val = `/*${val}*/`;
|
||||
}
|
||||
return val;
|
||||
}
|
||||
@@ -319,7 +319,7 @@ class CodeGenerator {
|
||||
}
|
||||
|
||||
var indent = Math.max(this.indentSize(), column);
|
||||
val = val.replace(/\n/g, "\n" + repeating(" ", indent));
|
||||
val = val.replace(/\n/g, `\n${repeating(" ", indent)}`);
|
||||
}
|
||||
|
||||
if (column === 0) {
|
||||
|
||||
@@ -81,8 +81,7 @@ module.exports = function (lines, lineNumber, colNumber) {
|
||||
return;
|
||||
}
|
||||
if (colNumber) {
|
||||
params.line += "\n" + params.before + repeating(" ", params.width) +
|
||||
params.after + repeating(" ", colNumber - 1) + "^";
|
||||
params.line += `\n${params.before}${repeating(" ", params.width)}${params.after}${repeating(" ", colNumber - 1)}^`;
|
||||
}
|
||||
params.before = params.before.replace(/^./, ">");
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ module.exports = function (opts, code, callback) {
|
||||
} catch (err) {
|
||||
if (!err._babel) {
|
||||
err._babel = true;
|
||||
var message = opts.filename + ": " + err.message;
|
||||
var message = `${opts.filename}: ${err.message}`;
|
||||
|
||||
var loc = err.loc;
|
||||
if (loc) {
|
||||
|
||||
@@ -22,14 +22,14 @@ export var messages = {
|
||||
};
|
||||
|
||||
export function get(key) {
|
||||
var msg = exports.messages[key];
|
||||
if (!msg) throw new ReferenceError("Unknown message `" + key + "`");
|
||||
var msg = messages[key];
|
||||
if (!msg) throw new ReferenceError(`Unknown message ${JSON.stringify(key)}`);
|
||||
|
||||
var args = [];
|
||||
for (var i = 1; i < arguments.length; i++) {
|
||||
args.push(arguments[i]);
|
||||
}
|
||||
args = exports.parseArgs(args);
|
||||
args = parseArgs(args);
|
||||
|
||||
return msg.replace(/\$(\d+)/g, function (str, i) {
|
||||
return args[--i];
|
||||
|
||||
@@ -118,7 +118,7 @@ export default class File {
|
||||
|
||||
for (var key in opts) {
|
||||
if (key[0] !== "_" && File.validOptions.indexOf(key) < 0) {
|
||||
throw new ReferenceError("Unknown option: " + key);
|
||||
throw new ReferenceError(`Unknown option: ${key}`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -248,7 +248,7 @@ export default class File {
|
||||
|
||||
debug(msg) {
|
||||
var parts = this.opts.filename;
|
||||
if (msg) parts += ": " + msg;
|
||||
if (msg) parts += `: ${msg}`;
|
||||
util.debug(parts);
|
||||
}
|
||||
|
||||
@@ -261,7 +261,7 @@ export default class File {
|
||||
}
|
||||
|
||||
if (!ModuleFormatter) {
|
||||
throw new ReferenceError("Unknown module formatter type " + JSON.stringify(type));
|
||||
throw new ReferenceError(`Unknown module formatter type ${JSON.stringify(type)}`);
|
||||
}
|
||||
|
||||
return new ModuleFormatter(this);
|
||||
@@ -338,7 +338,7 @@ export default class File {
|
||||
|
||||
addHelper(name) {
|
||||
if (!includes(File.helpers, name)) {
|
||||
throw new ReferenceError("Unknown helper " + name);
|
||||
throw new ReferenceError(`Unknown helper ${name}`);
|
||||
}
|
||||
|
||||
var program = this.ast.program;
|
||||
@@ -371,7 +371,7 @@ export default class File {
|
||||
|
||||
errorWithNode(node, msg, Error = SyntaxError) {
|
||||
var loc = node.loc.start;
|
||||
var err = new Error("Line " + loc.line + ": " + msg);
|
||||
var err = new Error(`Line ${loc.line}: ${msg}`);
|
||||
err.loc = loc;
|
||||
return err;
|
||||
}
|
||||
@@ -388,7 +388,7 @@ export default class File {
|
||||
var opts = this.opts;
|
||||
|
||||
opts.allowImportExportEverywhere = this.isLoose("es6.modules");
|
||||
opts.strictMode = this.transformers.useStrict.canRun();
|
||||
opts.strictMode = this.transformers.strict.canRun();
|
||||
|
||||
return parse(opts, code, (tree) => {
|
||||
this.transform(tree);
|
||||
@@ -430,6 +430,13 @@ export default class File {
|
||||
}
|
||||
|
||||
checkNode(node, scope) {
|
||||
if (Array.isArray(node)) {
|
||||
for (var i = 0; i < node.length; i++) {
|
||||
this.checkNode(node[i], scope);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
var stack = this.transformerStack;
|
||||
scope ||= this.scope;
|
||||
|
||||
@@ -463,7 +470,7 @@ export default class File {
|
||||
|
||||
if (this.shebang) {
|
||||
// add back shebang
|
||||
result.code = this.shebang + "\n" + result.code;
|
||||
result.code = `${this.shebang}\n${result.code}`;
|
||||
}
|
||||
|
||||
if (opts.sourceMap === "inline") {
|
||||
|
||||
@@ -10,7 +10,7 @@ export function push(mutatorMap, key, kind, computed, value) {
|
||||
|
||||
if (t.isIdentifier(key)) {
|
||||
alias = key.name;
|
||||
if (computed) alias = "computed:" + alias;
|
||||
if (computed) alias = `computed:${alias}`;
|
||||
} else if (t.isLiteral(key)) {
|
||||
alias = String(key.value);
|
||||
} else {
|
||||
|
||||
@@ -23,7 +23,7 @@ var getObjRef = function (node, nodes, file, scope) {
|
||||
return ref;
|
||||
}
|
||||
} else {
|
||||
throw new Error("We can't explode this node type " + node.type);
|
||||
throw new Error(`We can't explode this node type ${node.type}`);
|
||||
}
|
||||
|
||||
var temp = scope.generateUidBasedOnNode(ref);
|
||||
|
||||
@@ -7,7 +7,7 @@ export function has(node) {
|
||||
|
||||
export function wrap(node, callback) {
|
||||
var useStrictNode;
|
||||
if (exports.has(node)) {
|
||||
if (has(node)) {
|
||||
useStrictNode = node.body.shift();
|
||||
}
|
||||
|
||||
@@ -25,9 +25,12 @@ transform._ensureTransformerNames = function (type, rawKeys) {
|
||||
var key = rawKeys[i];
|
||||
|
||||
var deprecatedKey = transform.deprecatedTransformerMap[key];
|
||||
if (deprecatedKey) {
|
||||
var aliasKey = transform.aliasTransformerMap[key];
|
||||
if (aliasKey) {
|
||||
keys.push(aliasKey);
|
||||
} else if (deprecatedKey) {
|
||||
// deprecated key, remap it to the new one
|
||||
console.error("The transformer " + key + " has been renamed to " + deprecatedKey);
|
||||
console.error(`The transformer ${key} has been renamed to ${deprecatedKey}`);
|
||||
rawKeys.push(deprecatedKey);
|
||||
} else if (transform.transformers[key]) {
|
||||
// valid key
|
||||
@@ -37,7 +40,7 @@ transform._ensureTransformerNames = function (type, rawKeys) {
|
||||
keys = keys.concat(transform.namespaces[key]);
|
||||
} else {
|
||||
// invalid key
|
||||
throw new ReferenceError("Unknown transformer " + key + " specified in " + type);
|
||||
throw new ReferenceError(`Unknown transformer ${key} specified in ${type}`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,6 +52,7 @@ transform.transformers = object();
|
||||
transform.namespaces = object();
|
||||
|
||||
transform.deprecatedTransformerMap = require("./transformers/deprecated");
|
||||
transform.aliasTransformerMap = require("./transformers/aliases");
|
||||
transform.moduleFormatters = require("./modules");
|
||||
|
||||
import rawTransformers from "./transformers";
|
||||
|
||||
@@ -56,7 +56,7 @@ export default class TransformerPass {
|
||||
|
||||
var file = this.file;
|
||||
|
||||
file.debug("Running transformer " + this.transformer.key);
|
||||
file.debug(`Running transformer ${this.transformer.key}`);
|
||||
|
||||
file.scope.traverse(file.ast, this.handlers, file);
|
||||
}
|
||||
|
||||
3
src/babel/transformation/transformers/aliases.json
Normal file
3
src/babel/transformation/transformers/aliases.json
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"useStrict": "strict"
|
||||
}
|
||||
@@ -1,4 +1,8 @@
|
||||
{
|
||||
"selfContained": "runtime",
|
||||
"unicode-regex": "regex.unicode"
|
||||
"unicode-regex": "regex.unicode",
|
||||
|
||||
"minification.deadCodeElimination": "utility.deadCodeElimination",
|
||||
"minification.removeConsoleCalls": "utility.removeConsole",
|
||||
"minification.removeDebugger": "utility.removeDebugger"
|
||||
}
|
||||
|
||||
@@ -32,12 +32,10 @@ export function BlockStatement(node, parent, scope, file) {
|
||||
var letRefs = node._letReferences;
|
||||
if (!letRefs) return;
|
||||
|
||||
var state = {
|
||||
scope.traverse(node, visitor, {
|
||||
letRefs: letRefs,
|
||||
file: file
|
||||
};
|
||||
|
||||
scope.traverse(node, visitor, state);
|
||||
});
|
||||
}
|
||||
|
||||
export { BlockStatement as Program, BlockStatement as Loop };
|
||||
|
||||
@@ -189,7 +189,7 @@ var loopVisitor = {
|
||||
return;
|
||||
}
|
||||
|
||||
loopText = loopText + "|" + node.label.name;
|
||||
loopText = `${loopText}|${node.label.name}`;
|
||||
} else {
|
||||
// we shouldn't be transforming these statements because
|
||||
// they don't refer to the actual loop we're scopifying
|
||||
@@ -327,7 +327,7 @@ class BlockScoping {
|
||||
for (var name in outsideRefs) {
|
||||
var id = outsideRefs[name];
|
||||
|
||||
if (this.scope.hasGlobal(id.name)) {
|
||||
if (this.scope.hasGlobal(id.name) || this.scope.parentHasBinding(id.name)) {
|
||||
delete outsideRefs[id.name];
|
||||
delete this.letReferences[id.name];
|
||||
|
||||
|
||||
@@ -182,7 +182,7 @@ class ClassTransformer {
|
||||
}
|
||||
|
||||
// we have no constructor, we have a super, and the super doesn't appear to be falsy
|
||||
if (!this.hasConstructor && this.hasSuper && !t.isFalsyExpression(superName)) {
|
||||
if (!this.hasConstructor && this.hasSuper && t.evaluateTruthy(superName, this.scope) !== false) {
|
||||
var helperName = "class-super-constructor-call";
|
||||
if (this.isLoose) helperName += "-loose";
|
||||
constructor.body.body.push(util.template(helperName, {
|
||||
|
||||
@@ -56,12 +56,12 @@ export { ForOfStatement as ForInStatement };
|
||||
exports.Function = function (node, parent, scope, file) {
|
||||
var nodes = [];
|
||||
|
||||
var hasDestructuringTransformer = false;
|
||||
var hasDestructuring = false;
|
||||
|
||||
node.params = node.params.map(function (pattern, i) {
|
||||
if (!t.isPattern(pattern)) return pattern;
|
||||
|
||||
hasDestructuringTransformer = true;
|
||||
hasDestructuring = true;
|
||||
var ref = scope.generateUidIdentifier("ref");
|
||||
|
||||
var destructuring = new DestructuringTransformer({
|
||||
@@ -69,15 +69,16 @@ exports.Function = function (node, parent, scope, file) {
|
||||
nodes: nodes,
|
||||
scope: scope,
|
||||
file: file,
|
||||
kind: "var",
|
||||
kind: "let"
|
||||
});
|
||||
destructuring.init(pattern, ref);
|
||||
|
||||
return ref;
|
||||
});
|
||||
|
||||
if (!hasDestructuringTransformer) return;
|
||||
if (!hasDestructuring) return;
|
||||
|
||||
file.checkNode(nodes);
|
||||
t.ensureBlock(node);
|
||||
|
||||
var block = node.body;
|
||||
|
||||
@@ -32,7 +32,11 @@ export function ForOfStatement(node, parent, scope, file) {
|
||||
// todo: find out why this is necessary? #538
|
||||
loop._scopeInfo = node._scopeInfo;
|
||||
|
||||
return build.node;
|
||||
if (build.replaceParent) {
|
||||
this.parentPath.node = build.node;
|
||||
} else {
|
||||
return build.node;
|
||||
}
|
||||
}
|
||||
|
||||
var breakVisitor = {
|
||||
@@ -97,7 +101,9 @@ var loose = function (node, parent, scope, file) {
|
||||
|
||||
scope.traverse(node, breakVisitor, {
|
||||
iteratorKey: iteratorKey,
|
||||
wrapReturn: function (node) {
|
||||
label: t.isLabeledStatement(parent) && parent.label.name,
|
||||
|
||||
wrapReturn(node) {
|
||||
return t.ifStatement(
|
||||
t.logicalExpression(
|
||||
"&&",
|
||||
@@ -105,8 +111,7 @@ var loose = function (node, parent, scope, file) {
|
||||
t.memberExpression(iteratorKey, t.identifier("return")
|
||||
)
|
||||
), node);
|
||||
},
|
||||
label: t.isLabeledStatement(parent) && parent.label.name
|
||||
}
|
||||
});
|
||||
|
||||
//
|
||||
@@ -151,14 +156,22 @@ var spec = function (node, parent, scope, file) {
|
||||
BODY: null
|
||||
});
|
||||
|
||||
var loop = template[3].block.body[0];
|
||||
var isLabeledParent = t.isLabeledStatement(parent);
|
||||
|
||||
var tryBody = template[3].block.body;
|
||||
var loop = tryBody[0];
|
||||
|
||||
if (isLabeledParent) {
|
||||
tryBody[0] = t.labeledStatement(parent.label, loop);
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
scope.traverse(node, breakVisitor, {
|
||||
iteratorKey: iteratorKey,
|
||||
label: t.isLabeledStatement(parent) && parent.label.name,
|
||||
wrapReturn: function (node) {
|
||||
label: isLabeledParent && parent.label.name,
|
||||
|
||||
wrapReturn(node) {
|
||||
return t.ifStatement(t.memberExpression(iteratorKey, t.identifier("return")), node);
|
||||
}
|
||||
});
|
||||
@@ -166,8 +179,9 @@ var spec = function (node, parent, scope, file) {
|
||||
//
|
||||
|
||||
return {
|
||||
declar: declar,
|
||||
loop: loop,
|
||||
node: template
|
||||
replaceParent: isLabeledParent,
|
||||
declar: declar,
|
||||
loop: loop,
|
||||
node: template
|
||||
};
|
||||
};
|
||||
|
||||
@@ -61,7 +61,7 @@ exports.Function = function (node, parent, scope, file) {
|
||||
scope.traverse(param, iifeVisitor, state);
|
||||
}
|
||||
|
||||
if (file.transformers["es6.blockScopingTDZ"].canRun()) {
|
||||
if (file.transformers["es6.blockScopingTDZ"].canRun() && t.isIdentifier(param)) {
|
||||
pushDefNode(param, t.identifier("undefined"), i);
|
||||
}
|
||||
|
||||
|
||||
@@ -29,7 +29,6 @@ var memberExpressionOptimisationVisitor = {
|
||||
var prop = parent.property;
|
||||
if (isNumber(prop.value) || t.isUnaryExpression(prop) || t.isBinaryExpression(prop)) {
|
||||
state.candidates.push(this);
|
||||
state.canOptimise = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -56,7 +55,7 @@ var hasRest = function (node) {
|
||||
return t.isRestElement(node.params[node.params.length - 1]);
|
||||
};
|
||||
|
||||
exports.Function = function (node, parent, scope) {
|
||||
exports.Function = function (node, parent, scope, file) {
|
||||
if (!hasRest(node)) return;
|
||||
|
||||
var rest = node.params.pop().argument;
|
||||
@@ -70,10 +69,12 @@ exports.Function = function (node, parent, scope) {
|
||||
if (t.isPattern(rest)) {
|
||||
var pattern = rest;
|
||||
rest = scope.generateUidIdentifier("ref");
|
||||
var declar = t.variableDeclaration("var", pattern.elements.map(function (elem, index) {
|
||||
|
||||
var declar = t.variableDeclaration("let", pattern.elements.map(function (elem, index) {
|
||||
var accessExpr = t.memberExpression(rest, t.literal(index), true);
|
||||
return t.variableDeclarator(elem, accessExpr);
|
||||
}));
|
||||
file.checkNode(declar);
|
||||
node.body.body.unshift(declar);
|
||||
}
|
||||
|
||||
@@ -81,7 +82,7 @@ exports.Function = function (node, parent, scope) {
|
||||
|
||||
var state = {
|
||||
outerBinding: scope.getBindingIdentifier(rest.name),
|
||||
canOptimise: false,
|
||||
canOptimise: true,
|
||||
candidates: [],
|
||||
method: node,
|
||||
name: rest.name
|
||||
@@ -90,7 +91,7 @@ exports.Function = function (node, parent, scope) {
|
||||
scope.traverse(node, memberExpressionOptimisationVisitor, state);
|
||||
|
||||
// we only have shorthands and there's no other references
|
||||
if (state.canOptimise) {
|
||||
if (state.canOptimise && state.candidates.length) {
|
||||
for (var i = 0; i < state.candidates.length; i++) {
|
||||
var candidate = state.candidates[i];
|
||||
candidate.node = argsId;
|
||||
|
||||
@@ -229,7 +229,7 @@ class TailCallTransformer {
|
||||
subTransform(node) {
|
||||
if (!node) return;
|
||||
|
||||
var handler = this["subTransform" + node.type];
|
||||
var handler = this[`subTransform${node.type}`];
|
||||
if (handler) return handler.call(this, node);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
export default {
|
||||
useStrict: require("./other/use-strict"),
|
||||
strict: require("./other/strict"),
|
||||
|
||||
"validation.undeclaredVariableCheck": require("./validation/undeclared-variable-check"),
|
||||
"validation.noForInOfAssignment": require("./validation/no-for-in-of-assignment"),
|
||||
@@ -97,16 +97,18 @@ export default {
|
||||
"spec.typeofSymbol": require("./spec/typeof-symbol"),
|
||||
"spec.undefinedToVoid": require("./spec/undefined-to-void"),
|
||||
|
||||
_useStrict: require("./internal/use-strict"),
|
||||
_strict: require("./internal/strict"),
|
||||
_moduleFormatter: require("./internal/module-formatter"),
|
||||
|
||||
"es3.propertyLiterals": require("./es3/property-literals"),
|
||||
"es3.memberExpressionLiterals": require("./es3/member-expression-literals"),
|
||||
|
||||
"minification.removeDebugger": require("./minification/remove-debugger"),
|
||||
"minification.removeConsoleCalls": require("./minification/remove-console-calls"),
|
||||
"minification.deadCodeElimination": require("./minification/dead-code-elimination"),
|
||||
"minification.renameLocalVariables": require("./minification/rename-local-variables"),
|
||||
"utility.removeDebugger": require("./utility/remove-debugger"),
|
||||
"utility.removeConsole": require("./utility/remove-console"),
|
||||
|
||||
"utility.inlineEnvironmentVariables": require("./utility/inline-environment-variables"),
|
||||
"utility.inlineExpressions": require("./utility/inline-expressions"),
|
||||
"utility.deadCodeElimination": require("./utility/dead-code-elimination"),
|
||||
|
||||
_cleanUp: require("./internal/cleanup")
|
||||
};
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import * as useStrict from "../../helpers/use-strict";
|
||||
import * as strict from "../../helpers/strict";
|
||||
import t from "../../../types";
|
||||
|
||||
export var secondPass = true;
|
||||
@@ -6,7 +6,7 @@ export var secondPass = true;
|
||||
export function BlockStatement(node, parent, scope, file) {
|
||||
if (!node._declarations) return;
|
||||
|
||||
useStrict.wrap(node, function () {
|
||||
strict.wrap(node, function () {
|
||||
var kinds = {};
|
||||
var kind;
|
||||
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import * as useStrict from "../../helpers/use-strict";
|
||||
import * as strict from "../../helpers/strict";
|
||||
|
||||
export function Program(program, parent, scope, file) {
|
||||
if (!file.transformers["es6.modules"].canRun()) return;
|
||||
|
||||
useStrict.wrap(program, function () {
|
||||
strict.wrap(program, function () {
|
||||
program.body = file.dynamicImports.concat(program.body);
|
||||
});
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import t from "../../../types";
|
||||
|
||||
export function Program(program, parent, scope, file) {
|
||||
if (file.transformers.useStrict.canRun()) {
|
||||
if (file.transformers.strict.canRun()) {
|
||||
program.body.unshift(t.expressionStatement(t.literal("use strict")));
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
//import t from "../../../types";
|
||||
|
||||
export var optional = true;
|
||||
|
||||
export function Scopable() {
|
||||
//for (var name in scope.bindings) {
|
||||
// scope.rename(name, scope.generateUidIdentifier("a").name);
|
||||
//}
|
||||
}
|
||||
@@ -13,6 +13,13 @@ export function Class(node) {
|
||||
node.implements = null;
|
||||
}
|
||||
|
||||
exports.Function = function (node) {
|
||||
for (var i = 0; i < node.params.length; i++) {
|
||||
var param = node.params[i];
|
||||
param.optional = false;
|
||||
}
|
||||
};
|
||||
|
||||
export function TypeCastExpression(node) {
|
||||
return node.expression;
|
||||
}
|
||||
|
||||
@@ -1,31 +1,41 @@
|
||||
import t from "../../../types";
|
||||
|
||||
function toStatements(node) {
|
||||
if (t.isBlockStatement(node)) {
|
||||
var hasBlockScoped = false;
|
||||
|
||||
for (var i = 0; i < node.body.length; i++) {
|
||||
var bodyNode = node.body[i];
|
||||
if (t.isBlockScoped(bodyNode)) hasBlockScoped = true;
|
||||
}
|
||||
|
||||
if (!hasBlockScoped) {
|
||||
return node.body;
|
||||
}
|
||||
}
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
export var optional = true;
|
||||
|
||||
export function ExpressionStatement(node) {
|
||||
// remove consequence-less expressions such as local variables and literals
|
||||
// note: will remove directives
|
||||
//
|
||||
// var foo = true; foo; -> var foo = true;
|
||||
// "foo"; ->
|
||||
//
|
||||
|
||||
var expr = node.expression;
|
||||
if (t.isLiteral(expr) || (t.isIdentifier(node) && t.hasBinding(node.name))) {
|
||||
this.remove();
|
||||
export function ConditionalExpression(node, parent, scope) {
|
||||
var evaluateTest = t.evaluateTruthy(node.test, scope);
|
||||
if (evaluateTest === true) {
|
||||
return node.consequent;
|
||||
} else if (evaluateTest === false) {
|
||||
return node.alternate;
|
||||
}
|
||||
}
|
||||
|
||||
export var IfStatement = {
|
||||
exit(node) {
|
||||
// todo: in scenarios where we can just return the consequent or
|
||||
// alternate we should drop the block statement if it contains no
|
||||
// block scoped variables
|
||||
|
||||
exit(node, parent, scope) {
|
||||
var consequent = node.consequent;
|
||||
var alternate = node.alternate;
|
||||
var test = node.test;
|
||||
|
||||
var evaluateTest = t.evaluateTruthy(test, scope);
|
||||
|
||||
// we can check if a test will be truthy 100% and if so then we can inline
|
||||
// the consequent and completely ignore the alternate
|
||||
//
|
||||
@@ -33,8 +43,8 @@ export var IfStatement = {
|
||||
// if ("foo") { foo; } -> { foo; }
|
||||
//
|
||||
|
||||
if (t.isLiteral(test) && test.value) {
|
||||
return consequent;
|
||||
if (evaluateTest === true) {
|
||||
return toStatements(consequent);
|
||||
}
|
||||
|
||||
// we can check if a test will be falsy 100% and if so we can inline the
|
||||
@@ -44,9 +54,9 @@ export var IfStatement = {
|
||||
// if ("") { bar; } ->
|
||||
//
|
||||
|
||||
if (t.isFalsyExpression(test)) {
|
||||
if (evaluateTest === false) {
|
||||
if (alternate) {
|
||||
return alternate;
|
||||
return toStatements(alternate);
|
||||
} else {
|
||||
return this.remove();
|
||||
}
|
||||
@@ -67,7 +77,7 @@ export var IfStatement = {
|
||||
// if (foo) {} else { bar; } -> if (!foo) { bar; }
|
||||
//
|
||||
|
||||
if (t.blockStatement(consequent) && !consequent.body.length && t.isBlockStatement(alternate) && alternate.body.length) {
|
||||
if (t.isBlockStatement(consequent) && !consequent.body.length && t.isBlockStatement(alternate) && alternate.body.length) {
|
||||
node.consequent = node.alternate;
|
||||
node.alternate = null;
|
||||
node.test = t.unaryExpression("!", test, true);
|
||||
@@ -0,0 +1,14 @@
|
||||
import t from "../../../types";
|
||||
|
||||
export var optional = true;
|
||||
|
||||
var match = t.buildMatchMemberExpression("process.env");
|
||||
|
||||
export function MemberExpression(node) {
|
||||
if (match(node.object)) {
|
||||
var key = t.toComputedKey(node, node.property);
|
||||
if (t.isLiteral(key)) {
|
||||
return t.valueToNode(process.env[key.value]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import t from "../../../types";
|
||||
|
||||
export var optional = true;
|
||||
|
||||
export function Expression(node, parent, scope) {
|
||||
var res = t.evaluate(node, scope);
|
||||
if (res.confident) return t.valueToNode(res.value);
|
||||
}
|
||||
|
||||
export function Identifier() {
|
||||
// override Expression
|
||||
}
|
||||
@@ -9,7 +9,7 @@ function traverse(parent, opts, scope, state) {
|
||||
|
||||
if (!opts.noScope && !scope) {
|
||||
if (parent.type !== "Program" && parent.type !== "File") {
|
||||
throw new Error("Must pass a scope unless traversing a Program/File got a " + parent.type + " node");
|
||||
throw new Error(`Must pass a scope unless traversing a Program/File got a ${parent.type} node`);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -143,7 +143,7 @@ export default class Scope {
|
||||
_generateUid(name, i) {
|
||||
var id = name;
|
||||
if (i > 1) id += i;
|
||||
return "_" + id;
|
||||
return `_${id}`;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -415,7 +415,7 @@ export default class Scope {
|
||||
registerVariableDeclaration(declar) {
|
||||
var declars = declar.declarations;
|
||||
for (var i = 0; i < declars.length; i++) {
|
||||
this.registerBinding(declars[i], declar.kind);
|
||||
this.registerBinding(declar.kind, declars[i]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -538,7 +538,7 @@ export default class Scope {
|
||||
init: opts.init
|
||||
};
|
||||
} else {
|
||||
throw new TypeError("cannot add a declaration here in node type " + block.type);
|
||||
throw new TypeError(`cannot add a declaration here in node type ${block.type}`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -626,6 +626,37 @@ export default class Scope {
|
||||
return binding && binding.identifier;
|
||||
}
|
||||
|
||||
|
||||
getOwnImmutableBindingValue(name) {
|
||||
return this._immutableBindingInfoToValue(this.getOwnBindingInfo(name));
|
||||
}
|
||||
|
||||
getImmutableBindingValue(name) {
|
||||
return this._immutableBindingInfoToValue(this.getBindingInfo(name));
|
||||
}
|
||||
|
||||
_immutableBindingInfoToValue(info) {
|
||||
if (!info) return;
|
||||
|
||||
// can't guarantee this value is the same
|
||||
if (info.reassigned) return;
|
||||
|
||||
var node = info.node;
|
||||
if (t.isVariableDeclarator(node)) {
|
||||
if (t.isIdentifier(node.id)) {
|
||||
node = node.init;
|
||||
} else {
|
||||
// otherwise it's probably a destructuring like:
|
||||
// var { foo } = "foo";
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (t.isImmutable(node)) {
|
||||
return node;
|
||||
}
|
||||
}
|
||||
|
||||
// has
|
||||
|
||||
hasOwnBinding(name) {
|
||||
|
||||
@@ -84,6 +84,11 @@
|
||||
"name": null
|
||||
},
|
||||
|
||||
"LabeledStatement": {
|
||||
"label": null,
|
||||
"body": null
|
||||
},
|
||||
|
||||
"Literal": {
|
||||
"value": null
|
||||
},
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
import toFastProperties from "../helpers/to-fast-properties";
|
||||
import isPlainObject from "lodash/lang/isPlainObject";
|
||||
import isNumber from "lodash/lang/isNumber";
|
||||
import isRegExp from "lodash/lang/isRegExp";
|
||||
import isString from "lodash/lang/isString";
|
||||
import compact from "lodash/array/compact";
|
||||
import esutils from "esutils";
|
||||
@@ -18,14 +21,14 @@ export default t;
|
||||
*/
|
||||
|
||||
function registerType(type, skipAliasCheck) {
|
||||
var is = t["is" + type] = function (node, opts) {
|
||||
var is = t[`is${type}`] = function (node, opts) {
|
||||
return t.is(type, node, opts, skipAliasCheck);
|
||||
};
|
||||
|
||||
t["assert" + type] = function (node, opts) {
|
||||
t[`assert${type}`] = function (node, opts) {
|
||||
opts ||= {};
|
||||
if (!is(node, opts)) {
|
||||
throw new Error("Expected type " + JSON.stringify(type) + " with option " + JSON.stringify(opts));
|
||||
throw new Error(`Expected type ${JSON.stringify(type)} with option ${JSON.stringify(opts)}`);
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -392,7 +395,7 @@ t.toIdentifier = function (name) {
|
||||
});
|
||||
|
||||
if (!t.isValidIdentifier(name)) {
|
||||
name = "_" + name;
|
||||
name = `_${name}`;
|
||||
}
|
||||
|
||||
return name || "_";
|
||||
@@ -503,7 +506,7 @@ t.toStatement = function (node, ignore) {
|
||||
if (ignore) {
|
||||
return false;
|
||||
} else {
|
||||
throw new Error("cannot turn " + node.type + " to a statement");
|
||||
throw new Error(`cannot turn ${node.type} to a statement`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -533,7 +536,7 @@ t.toExpression = function (node) {
|
||||
if (t.isExpression(node)) {
|
||||
return node;
|
||||
} else {
|
||||
throw new Error("cannot turn " + node.type + " to an expression");
|
||||
throw new Error(`cannot turn ${node.type} to an expression`);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -804,5 +807,205 @@ t.isScope = function (node, parent) {
|
||||
return t.isScopable(node);
|
||||
};
|
||||
|
||||
/**
|
||||
* Description
|
||||
*
|
||||
* @param {Node} node
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
|
||||
t.isImmutable = function (node) {
|
||||
if (t.isLiteral(node)) {
|
||||
if (node.regex) {
|
||||
// regexes are mutable
|
||||
return false;
|
||||
} else {
|
||||
// immutable!
|
||||
return true;
|
||||
}
|
||||
} else if (t.isIdentifier(node)) {
|
||||
if (node.name === "undefined") {
|
||||
// immutable!
|
||||
return true;
|
||||
} else {
|
||||
// no idea...
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
/**
|
||||
* Walk the input `node` and statically evaluate if it's truthy.
|
||||
*
|
||||
* Returning `true` when we're sure that the expression will evaluate to a
|
||||
* truthy value, `false` if we're sure that it will evaluate to a falsy
|
||||
* value and `undefined` if we aren't sure. Because of this please do not
|
||||
* rely on coercion when using this method and check with === if it's false.
|
||||
*
|
||||
* For example do:
|
||||
*
|
||||
* if (t.evaluateTruthy(node) === false) falsyLogic();
|
||||
*
|
||||
* **AND NOT**
|
||||
*
|
||||
* if (!t.evaluateTruthy(node)) falsyLogic();
|
||||
*
|
||||
* @param {Node} node
|
||||
* @param {Scope} scope
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
|
||||
t.evaluateTruthy = function (node, scope) {
|
||||
var res = t.evaluate(node, scope);
|
||||
if (res.confident) return !!res.value;
|
||||
};
|
||||
|
||||
/**
|
||||
* Walk the input `node` and statically evaluate it.
|
||||
*
|
||||
* Returns an pbject in the form `{ confident, value }`. `confident` indicates
|
||||
* whether or not we had to drop out of evaluating the expression because of
|
||||
* hitting an unknown node that we couldn't confidently find the value of.
|
||||
*
|
||||
* Example:
|
||||
*
|
||||
* t.evaluate(parse("5 + 5")) // { confident: true, value: 10 }
|
||||
* t.evaluate(parse("!true")) // { confident: true, value: false }
|
||||
* t.evaluate(parse("foo + foo")) // { confident: false, value: undefined }
|
||||
*
|
||||
* @param {Node} node
|
||||
* @param {Scope} scope
|
||||
* @returns {Object}
|
||||
*/
|
||||
|
||||
t.evaluate = function (node, scope) {
|
||||
var confident = true;
|
||||
|
||||
var value = evaluate(node);
|
||||
if (!confident) value = undefined;
|
||||
return {
|
||||
confident: confident,
|
||||
value: value
|
||||
};
|
||||
|
||||
function evaluate(node) {
|
||||
if (!confident) return;
|
||||
|
||||
if (t.isSequenceExpression(node)) {
|
||||
return evaluate(node.expressions[node.expressions.length - 1]);
|
||||
}
|
||||
|
||||
if (t.isLiteral(node)) {
|
||||
if (node.regex && node.value === null) {
|
||||
// we have a regex and we can't represent it natively
|
||||
} else {
|
||||
return node.value;
|
||||
}
|
||||
}
|
||||
|
||||
if (t.isConditionalExpression(node)) {
|
||||
if (evaluate(node.test)) {
|
||||
return evaluate(node.consequent);
|
||||
} else {
|
||||
return evaluate(node.alternate);
|
||||
}
|
||||
}
|
||||
|
||||
if (t.isIdentifier(node)) {
|
||||
if (node.name === "undefined") {
|
||||
return undefined;
|
||||
} else {
|
||||
return evaluate(scope.getImmutableBindingValue(node.name));
|
||||
}
|
||||
}
|
||||
|
||||
if (t.isUnaryExpression(node, { prefix: true })) {
|
||||
var arg = evaluate(node.argument);
|
||||
switch (node.operator) {
|
||||
case "void": return undefined;
|
||||
case "!": return !arg;
|
||||
case "+": return +arg;
|
||||
case "-": return -arg;
|
||||
}
|
||||
}
|
||||
|
||||
if (t.isArrayExpression(node) || t.isObjectExpression(node)) {
|
||||
// we could evaluate these but it's probably impractical and not very useful
|
||||
}
|
||||
|
||||
if (t.isLogicalExpression(node)) {
|
||||
var left = evaluate(node.left);
|
||||
var right = evaluate(node.right);
|
||||
|
||||
switch (node.operator) {
|
||||
case "||": return left || right;
|
||||
case "&&": return left && right;
|
||||
}
|
||||
}
|
||||
|
||||
if (t.isBinaryExpression(node)) {
|
||||
var left = evaluate(node.left);
|
||||
var right = evaluate(node.right);
|
||||
|
||||
switch (node.operator) {
|
||||
case "-": return left - right;
|
||||
case "+": return left + right;
|
||||
case "/": return left / right;
|
||||
case "*": return left * right;
|
||||
case "%": return left % right;
|
||||
case "<": return left < right;
|
||||
case ">": return left > right;
|
||||
case "<=": return left <= right;
|
||||
case ">=": return left >= right;
|
||||
case "==": return left == right;
|
||||
case "!=": return left != right;
|
||||
case "===": return left === right;
|
||||
case "!==": return left !== right;
|
||||
}
|
||||
}
|
||||
|
||||
confident = false;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Description
|
||||
*
|
||||
* @param value
|
||||
* @returns {Node}
|
||||
*/
|
||||
|
||||
t.valueToNode = function (value) {
|
||||
if (value === undefined) {
|
||||
return t.identifier("undefined");
|
||||
}
|
||||
|
||||
if (value === true || value === false || value === null || isString(value) || isNumber(value) || isRegExp(value)) {
|
||||
return t.literal(value);
|
||||
}
|
||||
|
||||
if (Array.isArray(value)) {
|
||||
return t.arrayExpression(value.map(t.valueToNode));
|
||||
}
|
||||
|
||||
if (isPlainObject(value)) {
|
||||
var props = [];
|
||||
for (var key in value) {
|
||||
var nodeKey;
|
||||
if (t.isValidIdentifier(key)) {
|
||||
nodeKey = t.identifier(key);
|
||||
} else {
|
||||
nodeKey = t.literal(key);
|
||||
}
|
||||
props.push(t.property("init", nodeKey, t.valueToNode(value[key])));
|
||||
}
|
||||
return t.objectExpression(props);
|
||||
}
|
||||
|
||||
throw new Error("don't know how to turn this value into a node");
|
||||
};
|
||||
|
||||
toFastProperties(t);
|
||||
toFastProperties(t.VISITOR_KEYS);
|
||||
|
||||
@@ -20,7 +20,7 @@ export { inherits, inspect } from "util";
|
||||
export var debug = buildDebug("babel");
|
||||
|
||||
export function canCompile(filename, altExts) {
|
||||
var exts = altExts || exports.canCompile.EXTENSIONS;
|
||||
var exts = altExts || canCompile.EXTENSIONS;
|
||||
var ext = path.extname(filename);
|
||||
return contains(exts, ext);
|
||||
}
|
||||
@@ -50,7 +50,7 @@ export function regexify(val) {
|
||||
export function arrayify(val) {
|
||||
if (!val) return [];
|
||||
if (isBoolean(val)) return [val];
|
||||
if (isString(val)) return exports.list(val);
|
||||
if (isString(val)) return list(val);
|
||||
if (Array.isArray(val)) return val;
|
||||
throw new TypeError("illegal type for arrayify");
|
||||
}
|
||||
@@ -75,7 +75,7 @@ var templateVisitor = {
|
||||
|
||||
export function template(name, nodes, keepExpression) {
|
||||
var ast = exports.templates[name];
|
||||
if (!ast) throw new ReferenceError("unknown template " + name);
|
||||
if (!ast) throw new ReferenceError(`unknown template ${name}`);
|
||||
|
||||
if (nodes === true) {
|
||||
keepExpression = true;
|
||||
|
||||
9
test/fixtures/transformation/es6-block-scoping-tdz-fail/destructuring.js
vendored
Normal file
9
test/fixtures/transformation/es6-block-scoping-tdz-fail/destructuring.js
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
function foo(
|
||||
{ x: { y: { z: a = b } = {}, w: b = 20 }, a: c = 30 }
|
||||
) {
|
||||
assert.equal(a, 10);
|
||||
assert.equal(b, 20);
|
||||
assert.equal(c, 30);
|
||||
}
|
||||
|
||||
foo({ x: {} });
|
||||
9
test/fixtures/transformation/es6-block-scoping-tdz-pass/destructuring.js
vendored
Normal file
9
test/fixtures/transformation/es6-block-scoping-tdz-pass/destructuring.js
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
function foo(
|
||||
{ x: { y: { z: a = 10 } = {}, w: b = 20 }, a: c = 30 }
|
||||
) {
|
||||
assert.equal(a, 10);
|
||||
assert.equal(b, 20);
|
||||
assert.equal(c, 30);
|
||||
}
|
||||
|
||||
foo({ x: {} });
|
||||
5
test/fixtures/transformation/es6-block-scoping/wrap-closure-shadow-variables/actual.js
vendored
Normal file
5
test/fixtures/transformation/es6-block-scoping/wrap-closure-shadow-variables/actual.js
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
let a = 1;
|
||||
for (let a = 1; a < 100; a++) {
|
||||
items.forEach(item => a);
|
||||
}
|
||||
console.log(a);
|
||||
11
test/fixtures/transformation/es6-block-scoping/wrap-closure-shadow-variables/expected.js
vendored
Normal file
11
test/fixtures/transformation/es6-block-scoping/wrap-closure-shadow-variables/expected.js
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
"use strict";
|
||||
|
||||
var a = 1;
|
||||
for (var _a = 1; _a < 100; _a++) {
|
||||
(function (_a) {
|
||||
items.forEach(function (item) {
|
||||
return _a;
|
||||
});
|
||||
})(_a);
|
||||
}
|
||||
console.log(a);
|
||||
@@ -2,89 +2,89 @@
|
||||
|
||||
// labels
|
||||
|
||||
foo: {
|
||||
var _iteratorNormalCompletion = true;
|
||||
var _didIteratorError = false;
|
||||
var _iteratorError = undefined;
|
||||
var _iteratorNormalCompletion = true;
|
||||
var _didIteratorError = false;
|
||||
var _iteratorError = undefined;
|
||||
|
||||
try {
|
||||
for (var _iterator = foo()[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
|
||||
var x = _step.value;
|
||||
try {
|
||||
foo: for (var _iterator = foo()[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
|
||||
var x = _step.value;
|
||||
|
||||
while (true) {
|
||||
if (_iterator["return"]) _iterator["return"]();
|
||||
|
||||
break foo;
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
_didIteratorError = true;
|
||||
_iteratorError = err;
|
||||
} finally {
|
||||
try {
|
||||
if (!_iteratorNormalCompletion && _iterator["return"]) {
|
||||
_iterator["return"]();
|
||||
}
|
||||
} finally {
|
||||
if (_didIteratorError) {
|
||||
throw _iteratorError;
|
||||
}
|
||||
}
|
||||
}
|
||||
}foo: {
|
||||
var _iteratorNormalCompletion2 = true;
|
||||
var _didIteratorError2 = false;
|
||||
var _iteratorError2 = undefined;
|
||||
|
||||
try {
|
||||
for (var _iterator2 = foo()[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
|
||||
var x = _step2.value;
|
||||
|
||||
while (true) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
_didIteratorError2 = true;
|
||||
_iteratorError2 = err;
|
||||
} finally {
|
||||
try {
|
||||
if (!_iteratorNormalCompletion2 && _iterator2["return"]) {
|
||||
_iterator2["return"]();
|
||||
}
|
||||
} finally {
|
||||
if (_didIteratorError2) {
|
||||
throw _iteratorError2;
|
||||
}
|
||||
}
|
||||
}
|
||||
}foo: {
|
||||
var _iteratorNormalCompletion3 = true;
|
||||
var _didIteratorError3 = false;
|
||||
var _iteratorError3 = undefined;
|
||||
|
||||
try {
|
||||
for (var _iterator3 = foo()[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
|
||||
var x = _step3.value;
|
||||
if (_iterator3["return"]) _iterator3["return"]();
|
||||
while (true) {
|
||||
if (_iterator["return"]) _iterator["return"]();
|
||||
|
||||
break foo;
|
||||
}
|
||||
} catch (err) {
|
||||
_didIteratorError3 = true;
|
||||
_iteratorError3 = err;
|
||||
}
|
||||
} catch (err) {
|
||||
_didIteratorError = true;
|
||||
_iteratorError = err;
|
||||
} finally {
|
||||
try {
|
||||
if (!_iteratorNormalCompletion && _iterator["return"]) {
|
||||
_iterator["return"]();
|
||||
}
|
||||
} finally {
|
||||
try {
|
||||
if (!_iteratorNormalCompletion3 && _iterator3["return"]) {
|
||||
_iterator3["return"]();
|
||||
}
|
||||
} finally {
|
||||
if (_didIteratorError3) {
|
||||
throw _iteratorError3;
|
||||
}
|
||||
if (_didIteratorError) {
|
||||
throw _iteratorError;
|
||||
}
|
||||
}
|
||||
} // basic
|
||||
}
|
||||
|
||||
var _iteratorNormalCompletion2 = true;
|
||||
var _didIteratorError2 = false;
|
||||
var _iteratorError2 = undefined;
|
||||
|
||||
try {
|
||||
foo: for (var _iterator2 = foo()[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
|
||||
var x = _step2.value;
|
||||
|
||||
while (true) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
_didIteratorError2 = true;
|
||||
_iteratorError2 = err;
|
||||
} finally {
|
||||
try {
|
||||
if (!_iteratorNormalCompletion2 && _iterator2["return"]) {
|
||||
_iterator2["return"]();
|
||||
}
|
||||
} finally {
|
||||
if (_didIteratorError2) {
|
||||
throw _iteratorError2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var _iteratorNormalCompletion3 = true;
|
||||
var _didIteratorError3 = false;
|
||||
var _iteratorError3 = undefined;
|
||||
|
||||
try {
|
||||
foo: for (var _iterator3 = foo()[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
|
||||
var x = _step3.value;
|
||||
if (_iterator3["return"]) _iterator3["return"]();
|
||||
|
||||
break foo;
|
||||
}
|
||||
} catch (err) {
|
||||
_didIteratorError3 = true;
|
||||
_iteratorError3 = err;
|
||||
} finally {
|
||||
try {
|
||||
if (!_iteratorNormalCompletion3 && _iterator3["return"]) {
|
||||
_iterator3["return"]();
|
||||
}
|
||||
} finally {
|
||||
if (_didIteratorError3) {
|
||||
throw _iteratorError3;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// basic
|
||||
|
||||
var _iteratorNormalCompletion4 = true;
|
||||
var _didIteratorError4 = false;
|
||||
@@ -137,4 +137,4 @@ try {
|
||||
throw _iteratorError5;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,17 +8,23 @@ var y = function (foo, ...bar) {
|
||||
};
|
||||
};
|
||||
|
||||
var b = function (x, y, ...args) {
|
||||
console.log(args[0]);
|
||||
args.pop();
|
||||
console.log(args[1]);
|
||||
};
|
||||
|
||||
var z = function (foo, ...bar) {
|
||||
var x = function () {
|
||||
bar[1] = 5;
|
||||
};
|
||||
var x = function () {
|
||||
bar[1] = 5;
|
||||
};
|
||||
};
|
||||
|
||||
var a = function (foo, ...bar) {
|
||||
return bar.join(',');
|
||||
return bar.join(',');
|
||||
};
|
||||
|
||||
var b = function (foo, ...bar) {
|
||||
var join = "join";
|
||||
return bar[join];
|
||||
var join = "join";
|
||||
return bar[join];
|
||||
};
|
||||
|
||||
@@ -1,47 +1,56 @@
|
||||
"use strict";
|
||||
|
||||
var x = function x(foo) {
|
||||
for (var _len = arguments.length, bar = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
||||
bar[_key - 1] = arguments[_key];
|
||||
}
|
||||
for (var _len = arguments.length, bar = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
||||
bar[_key - 1] = arguments[_key];
|
||||
}
|
||||
|
||||
console.log(bar);
|
||||
console.log(bar);
|
||||
};
|
||||
|
||||
var y = function y(foo) {
|
||||
for (var _len = arguments.length, bar = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
||||
bar[_key - 1] = arguments[_key];
|
||||
}
|
||||
for (var _len = arguments.length, bar = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
||||
bar[_key - 1] = arguments[_key];
|
||||
}
|
||||
|
||||
var x = function z(bar) {
|
||||
bar[1] = 5;
|
||||
};
|
||||
var x = function z(bar) {
|
||||
bar[1] = 5;
|
||||
};
|
||||
};
|
||||
|
||||
var b = function b(x, y) {
|
||||
for (var _len = arguments.length, args = Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
|
||||
args[_key - 2] = arguments[_key];
|
||||
}
|
||||
|
||||
console.log(args[0]);
|
||||
args.pop();
|
||||
console.log(args[1]);
|
||||
};
|
||||
|
||||
var z = function z(foo) {
|
||||
for (var _len = arguments.length, bar = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
||||
bar[_key - 1] = arguments[_key];
|
||||
}
|
||||
for (var _len = arguments.length, bar = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
||||
bar[_key - 1] = arguments[_key];
|
||||
}
|
||||
|
||||
var x = function x() {
|
||||
bar[1] = 5;
|
||||
};
|
||||
var x = function x() {
|
||||
bar[1] = 5;
|
||||
};
|
||||
};
|
||||
|
||||
var a = function a(foo) {
|
||||
for (var _len = arguments.length, bar = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
||||
bar[_key - 1] = arguments[_key];
|
||||
}
|
||||
for (var _len = arguments.length, bar = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
||||
bar[_key - 1] = arguments[_key];
|
||||
}
|
||||
|
||||
return bar.join(",");
|
||||
return bar.join(",");
|
||||
};
|
||||
|
||||
var b = function b(foo) {
|
||||
for (var _len = arguments.length, bar = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
||||
bar[_key - 1] = arguments[_key];
|
||||
}
|
||||
for (var _len = arguments.length, bar = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
||||
bar[_key - 1] = arguments[_key];
|
||||
}
|
||||
|
||||
var join = "join";
|
||||
return bar[join];
|
||||
var join = "join";
|
||||
return bar[join];
|
||||
};
|
||||
|
||||
|
||||
@@ -61,7 +61,7 @@ class Foo7 {
|
||||
class Foo8 {
|
||||
"bar"() {}
|
||||
}
|
||||
function foo(requiredParam, optParam?) {}
|
||||
function foo(requiredParam, optParam) {}
|
||||
class Foo9 {}
|
||||
class Foo10 {}
|
||||
var x = 4;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
{
|
||||
"blacklist": "useStrict",
|
||||
"blacklist": "strict",
|
||||
"optional": "reactCompat"
|
||||
}
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
{
|
||||
"blacklist": ["useStrict", "es6.tailCall"]
|
||||
"blacklist": ["strict", "es6.tailCall"]
|
||||
}
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
{
|
||||
"blacklist": ["useStrict"]
|
||||
"blacklist": ["strict"]
|
||||
}
|
||||
|
||||
@@ -47,7 +47,7 @@ suite("kangax/compat-table", function () {
|
||||
test(key, function () {
|
||||
code = transform(code, {
|
||||
filename: key,
|
||||
blacklist: ["useStrict"],
|
||||
blacklist: ["strict"],
|
||||
optional: ["spec.typeofSymbol", "es6.blockScopingTDZ"]
|
||||
}).code;
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ var regeneratorLoc = __dirname + "/../vendor/regenerator";
|
||||
suite("regenerator", function () {
|
||||
setup(function () {
|
||||
require("../register")({
|
||||
blacklist: ["useStrict"],
|
||||
blacklist: ["strict"],
|
||||
experimental: true
|
||||
});
|
||||
});
|
||||
|
||||
@@ -29,7 +29,7 @@ var check = function (loc) {
|
||||
|
||||
transform(file, {
|
||||
filename: loc,
|
||||
blacklist: ["useStrict"],
|
||||
blacklist: ["strict"],
|
||||
_anal: true
|
||||
});
|
||||
} catch (err) {
|
||||
|
||||
@@ -86,6 +86,6 @@ require("./_transformation-helper")({
|
||||
experimental: true
|
||||
}, function (opts, task) {
|
||||
if (!_.contains(task.exec.loc, "module.js")) {
|
||||
opts.blacklist = ["useStrict"];
|
||||
opts.blacklist = ["strict"];
|
||||
}
|
||||
});
|
||||
|
||||
@@ -1,15 +1,88 @@
|
||||
var assert = require("assert");
|
||||
var parse = require("../lib/babel/helpers/parse");
|
||||
var t = require("../lib/babel/types");
|
||||
|
||||
suite("types", function () {
|
||||
test("isFalsyExpression", function () {
|
||||
assert.ok(t.isFalsyExpression(t.literal("")));
|
||||
assert.ok(t.isFalsyExpression(t.literal(null)));
|
||||
assert.ok(t.isFalsyExpression(t.literal(0)));
|
||||
assert.ok(t.isFalsyExpression(t.identifier("undefined")));
|
||||
test("evaluate", function () {
|
||||
var evaluateAssert = function (code, expect) {
|
||||
var res = t.evaluate(parse({}, code).program.body[0].expression);
|
||||
assert.ok(res.confident, "Not confident");
|
||||
assert.equal(res.value, expect);
|
||||
assert.equal(res.value, eval(code));
|
||||
};
|
||||
|
||||
// Literal
|
||||
evaluateAssert("5", 5);
|
||||
evaluateAssert("true", true);
|
||||
evaluateAssert("false", false);
|
||||
evaluateAssert("null", null);
|
||||
evaluateAssert("'foo'", "foo");
|
||||
|
||||
// SequenceExpression
|
||||
evaluateAssert("(false, true)", true);
|
||||
evaluateAssert("(false, false)", false);
|
||||
|
||||
// Identifier
|
||||
evaluateAssert("undefined", undefined);
|
||||
|
||||
// ConditionalExpression
|
||||
evaluateAssert("true ? 'foo' : 'bar'", "foo");
|
||||
evaluateAssert("false ? 'foo' : 'bar'", "bar");
|
||||
evaluateAssert("'' ? 'foo' : 'bar'", "bar");
|
||||
evaluateAssert("'foobar' ? 'foo' : 'bar'", "foo");
|
||||
evaluateAssert("5 < 22 ? 'foo' : 'bar'", "foo");
|
||||
evaluateAssert("22 < 5 ? 'foo' : 'bar'", "bar");
|
||||
|
||||
// BinaryExpression -
|
||||
evaluateAssert("5 - 5", 0);
|
||||
evaluateAssert("5 - 6", -1);
|
||||
evaluateAssert("5.5 - 6", -0.5);
|
||||
|
||||
// BinaryExpression +
|
||||
evaluateAssert("5 + 5", 10);
|
||||
evaluateAssert("5 + 6", 11);
|
||||
evaluateAssert("5.5 + 6", 11.5);
|
||||
evaluateAssert("-5 + 5", 0);
|
||||
|
||||
// BinaryExpression /
|
||||
|
||||
// BinaryExpression *
|
||||
|
||||
// BinaryExpression %
|
||||
|
||||
// BinaryExpression <
|
||||
|
||||
// BinaryExpression >
|
||||
|
||||
// BinaryExpression <=
|
||||
|
||||
// BinaryExpression >=
|
||||
|
||||
// BinaryExpression ==
|
||||
|
||||
// BinaryExpression !=
|
||||
|
||||
// BinaryExpression ===
|
||||
|
||||
// BinaryExpression !==
|
||||
|
||||
// UnaryExpression void
|
||||
|
||||
// UnaryExpression !
|
||||
|
||||
// UnaryExpression +
|
||||
|
||||
// UnaryExpression -
|
||||
|
||||
// LogicalExpression ||
|
||||
|
||||
// LogicalExpression &&
|
||||
evaluateAssert("true && true", true);
|
||||
evaluateAssert("true && false", false);
|
||||
evaluateAssert("false && false", false);
|
||||
});
|
||||
|
||||
test("evaluateTruthy", function () {
|
||||
|
||||
assert.ok(!t.isFalsyExpression(t.literal("foobar")));
|
||||
assert.ok(!t.isFalsyExpression(t.literal(5)));
|
||||
assert.ok(!t.isFalsyExpression(t.identifier("foobar")));
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user