Compare commits

...

32 Commits

Author SHA1 Message Date
Sebastian McKenzie
b1252b865b v4.7.4 2015-03-10 03:29:47 +11:00
Sebastian McKenzie
068b1341d9 fix erroneous whitespace 2015-03-10 03:28:36 +11:00
Sebastian McKenzie
20eb143915 remove Number from the list of valid runtime constructors - fixes #981 2015-03-10 03:27:13 +11:00
Sebastian McKenzie
1302a86bf2 scope -> scopable class alias key 2015-03-10 03:22:55 +11:00
Sebastian McKenzie
b7831f1d7b prepend mocha command with node 2015-03-10 03:15:31 +11:00
Sebastian McKenzie
e46cabb21f remove code coverage from travis make task 2015-03-10 03:12:50 +11:00
Sebastian McKenzie
fff06a047e Revert "delegate node path removal to after call"
This reverts commit 5e0236d9a7.
2015-03-10 02:50:43 +11:00
Sebastian McKenzie
6b0320fc83 disable test262 tests by default 2015-03-10 02:34:35 +11:00
Sebastian McKenzie
5dde63fa0a more elaborate tests for #980 2015-03-10 02:13:00 +11:00
Sebastian McKenzie
672118149a hoist all directives - fixes #980 2015-03-10 01:53:51 +11:00
Sebastian McKenzie
5e0236d9a7 delegate node path removal to after call 2015-03-10 01:44:58 +11:00
Sebastian McKenzie
5101664e7d update 4.7.4 changelog 2015-03-10 01:44:44 +11:00
Sebastian McKenzie
2d684a06d3 update changelog version to 4.7.4 2015-03-10 01:23:24 +11:00
Sebastian McKenzie
e31bad8f42 add 4.7.3 changelog 2015-03-10 01:17:14 +11:00
Sebastian McKenzie
644b4373fc rewrite named function expressions in async function transformers - fixes #979 2015-03-10 01:16:38 +11:00
Sebastian McKenzie
62f37c1e62 fix util.booleanify 2015-03-10 01:10:58 +11:00
Sebastian McKenzie
fc0e89463b use flow types for annotations - goodbye JSDoc! 2015-03-10 00:35:52 +11:00
Sebastian McKenzie
56b6a795a4 remove path.refreshScope until i'm more confident on it's functionality so it doesn't break peopls stuff 2015-03-09 23:09:22 +11:00
Sebastian McKenzie
17b34a2959 dynamic scope tracking, toot toot - fixes #957 2015-03-09 22:07:05 +11:00
Sebastian McKenzie
c4da0253c5 move flattenable keys to types 2015-03-09 16:55:00 +11:00
Sebastian McKenzie
5f2df40f6d consistent whitespace in patch 2015-03-09 16:54:50 +11:00
Sebastian McKenzie
7d20a9b882 add traversal path todos 2015-03-09 16:54:41 +11:00
Sebastian McKenzie
ee0ac9f149 use spread in messages 2015-03-09 16:54:30 +11:00
Sebastian McKenzie
c5d3f42d8a alias core-js/library in babel-runtime - closes #965 2015-03-09 16:54:19 +11:00
Sebastian McKenzie
d162919a53 update symols transformer name 2015-03-09 03:39:30 +11:00
Sebastian McKenzie
d1f712344d add istanbul auxiliary comment task to makefile 2015-03-09 03:38:39 +11:00
Sebastian McKenzie
e985912b54 add Binary node cleanup 2015-03-09 03:38:27 +11:00
Sebastian McKenzie
97a21e4a34 update spec.typeofSymbol references to es6.symbols 2015-03-09 03:38:13 +11:00
Sebastian McKenzie
d8fb7812f7 more utility.removeConsole tests 2015-03-09 03:37:42 +11:00
Sebastian McKenzie
bfe63f5885 rename spec.typeofSymbol transformer to es6.symbols 2015-03-09 03:37:26 +11:00
Sebastian McKenzie
44ca6873a3 add super to contextVariables 2015-03-09 03:37:05 +11:00
Sebastian McKenzie
2f66ea7338 4.7.3 2015-03-08 04:33:50 +11:00
52 changed files with 516 additions and 414 deletions

1
.gitignore vendored
View File

@@ -11,7 +11,6 @@ test/tmp
coverage
dist
.package.json
packages/babel-runtime/*.js
packages/babel-runtime/helpers/*.js
packages/babel-runtime/regenerator/*.js
lib

View File

@@ -13,6 +13,15 @@ _Note: Gaps between patch versions are faulty/broken releases._
See [CHANGELOG - 6to5](CHANGELOG-6to5.md) for the pre-4.0.0 version changelog.
## 4.7.4
* **Bug Fix**
* Rewrite named function expressions in optional async function transformers.
* Hoist directives.
* Remove `Number` from the list of valid `runtime` constructors.
* **Internal**
* `spec.typeofSymbol` transformer has been renamed to `es6.symbols`.
## 4.7.2
* **New Feature**

View File

@@ -8,11 +8,14 @@ BABEL_CMD = node_modules/babel/bin/babel
export NODE_ENV = test
.PHONY: clean test test-cov test-clean test-travis test-simple test-all test-browser publish build bootstrap publish-core publish-runtime build-core watch-core
.PHONY: clean test test-cov test-clean test-travis test-simple test-all test-browser publish build bootstrap publish-core publish-runtime build-core watch-core build-core-test
build-core:
node $(BABEL_CMD) src --out-dir lib --copy-files
build-core-test:
node $(BABEL_CMD) src --out-dir lib --copy-files --auxiliary-comment "istanbul ignore next"
watch-core:
node $(BABEL_CMD) src --out-dir lib --watch --copy-files
@@ -40,26 +43,19 @@ test-clean:
rm -rf test/tmp
test:
$(MOCHA_CMD)
node $(MOCHA_CMD)
make test-clean
test-simple:
# excludes test262
export SIMPLE_BABEL_TESTS=1; \
make test
test-all:
export ALL_BABEL_TESTS=1; \
make test
test-cov:
rm -rf coverage
export SIMPLE_BABEL_TESTS=1; \
make build-core-test
node $(ISTANBUL_CMD) $(MOCHA_CMD) --
test-travis: bootstrap build
node $(ISTANBUL_CMD) $(MOCHA_CMD) --
if test -n "$$CODECLIMATE_REPO_TOKEN"; then codeclimate < coverage/lcov.info; fi
test-travis: bootstrap build test
test-browser:
mkdir -p dist

View File

@@ -1,7 +1,7 @@
{
"name": "babel",
"description": "Turn ES6 code into readable vanilla ES5 with source maps",
"version": "4.7.3",
"version": "4.7.4",
"author": "Sebastian McKenzie <sebmck@gmail.com>",
"homepage": "https://babeljs.io/",
"repository": "babel/babel",

View File

@@ -0,0 +1 @@
module.exports = require("core-js/library");

View File

@@ -1,7 +1,10 @@
{
"name": "babel-runtime",
"description": "babel selfContained runtime",
"version": "4.7.2",
"version": "4.7.3",
"repository": "babel/babel",
"author": "Sebastian McKenzie <sebmck@gmail.com>"
}
"author": "Sebastian McKenzie <sebmck@gmail.com>",
"dependencies": {
"core-js": "^0.6.1"
}
}

View File

@@ -24,14 +24,10 @@ export var messages = {
unsupportedOutputType: "Unsupported output type $1"
};
export function get(key) {
export function get(key: String, ...args) {
var msg = messages[key];
if (!msg) throw new ReferenceError(`Unknown message ${JSON.stringify(key)}`);
var args = [];
for (var i = 1; i < arguments.length; i++) {
args.push(arguments[i]);
}
args = parseArgs(args);
return msg.replace(/\$(\d+)/g, function (str, i) {
@@ -39,7 +35,7 @@ export function get(key) {
});
}
export function parseArgs(args) {
export function parseArgs(args: Array<any>) {
return args.map(function (val) {
if (val != null && val.inspect) {
return val.inspect();

View File

@@ -9,8 +9,8 @@ extend(estraverse.VisitorKeys, t.VISITOR_KEYS);
// regenerator-babel/ast-types
var def = types.Type.def;
var or = types.Type.or;
var def = types.Type.def;
var or = types.Type.or;
def("File")
.bases("Node")

View File

@@ -121,7 +121,7 @@ export default class File {
"accept"
];
normalizeOptions(opts) {
normalizeOptions(opts: Object) {
opts = assign({}, opts);
for (var key in opts) {
@@ -227,7 +227,7 @@ export default class File {
return opts;
};
isLoose(key) {
isLoose(key: string) {
return includes(this.opts.loose, key);
}
@@ -259,13 +259,13 @@ export default class File {
this.transformers = transformers;
}
debug(msg) {
debug(msg?: string) {
var parts = this.opts.filename;
if (msg) parts += `: ${msg}`;
util.debug(parts);
}
getModuleFormatter(type) {
getModuleFormatter(type: string) {
var ModuleFormatter = isFunction(type) ? type : transform.moduleFormatters[type];
if (!ModuleFormatter) {
@@ -280,7 +280,7 @@ export default class File {
return new ModuleFormatter(this);
}
parseInputSourceMap(code) {
parseInputSourceMap(code: string) {
var opts = this.opts;
var inputMap = convertSourceMap.fromSource(code);
@@ -292,7 +292,7 @@ export default class File {
return code;
}
parseShebang(code) {
parseShebang(code: string) {
var shebangMatch = shebangRegex.exec(code);
if (shebangMatch) {
@@ -305,15 +305,15 @@ export default class File {
return code;
}
set(key, val) {
set(key: string, val): any {
return this.data[key] = val;
};
setDynamic(key, fn) {
setDynamic(key: string, fn: Function) {
this.dynamicData[key] = fn;
}
get(key) {
get(key: string): any {
var data = this.data[key];
if (data) {
return data;
@@ -325,7 +325,7 @@ export default class File {
}
}
addImport(source, name, noDefault) {
addImport(source: string, name?: string, noDefault?: boolean): Object {
name ||= source;
var id = this.dynamicImportIds[name];
@@ -349,11 +349,11 @@ export default class File {
return id;
}
isConsequenceExpressionStatement(node) {
isConsequenceExpressionStatement(node: Object): boolean {
return t.isExpressionStatement(node) && this.lastStatements.indexOf(node) >= 0;
}
attachAuxiliaryComment(node) {
attachAuxiliaryComment(node: Object): Object {
var comment = this.opts.auxiliaryComment;
if (comment) {
node.leadingComments ||= [];
@@ -365,7 +365,7 @@ export default class File {
return node;
}
addHelper(name) {
addHelper(name: string): Object {
if (!includes(File.helpers, name)) {
throw new ReferenceError(`Unknown helper ${name}`);
}
@@ -408,14 +408,14 @@ export default class File {
return err;
}
addCode(code) {
addCode(code: string) {
code = (code || "") + "";
code = this.parseInputSourceMap(code);
this.code = code;
return this.parseShebang(code);
}
parse(code) {
parse(code: string) {
code = this.addCode(code);
var opts = this.opts;
@@ -452,7 +452,7 @@ export default class File {
this.call("post");
}
call(key) {
call(key: string) {
var stack = this.transformerStack;
for (var i = 0; i < stack.length; i++) {
var transformer = stack[i].transformer;
@@ -480,7 +480,7 @@ export default class File {
});
}
mergeSourceMap(map) {
mergeSourceMap(map: Object) {
var opts = this.opts;
var inputMap = opts.inputSourceMap;
@@ -502,7 +502,11 @@ export default class File {
return map;
}
generate() {
generate(): {
code: string;
map?: Object;
ast?: Object;
} {
var opts = this.opts;
var ast = this.ast;

View File

@@ -1,7 +1,7 @@
import t from "../../types";
var visitor = {
enter(node) {
var awaitVisitor = {
enter(node, parent, scope, state) {
if (t.isFunction(node)) this.skip();
if (t.isAwaitExpression(node)) {
@@ -16,13 +16,23 @@ var visitor = {
}
};
var referenceVisitor = {
enter(node, parent, scope, state) {
var name = state.id.name;
if (t.isReferencedIdentifier(node, parent, { name: name }) && scope.bindingIdentifierEquals(name, state.id)) {
return state.ref ||= scope.generateUidIdentifier(name);
}
}
};
export default function (node, callId, scope) {
node.async = false;
node.generator = true;
scope.traverse(node, visitor);
scope.traverse(node, awaitVisitor, state);
var call = t.callExpression(callId, [node]);
var id = node.id;
node.id = null;
@@ -33,6 +43,16 @@ export default function (node, callId, scope) {
declar._blockHoist = true;
return declar;
} else {
if (id) {
var state = { id: id };
scope.traverse(node, referenceVisitor, state);
if (state.ref) {
scope.parent.push({ id: state.ref });
return t.assignmentExpression("=", state.ref, call);
}
}
return call;
}
};

View File

@@ -47,12 +47,9 @@ export default class ReplaceSupers {
/**
* Description
*
* @param {Object} opts
* @param {Boolean} [inClass]
*/
constructor(opts, inClass) {
constructor(opts: Object, inClass?: boolean = false) {
this.topLevelThisReference = opts.topLevelThisReference;
this.methodNode = opts.methodNode;
this.superRef = opts.superRef;
@@ -76,15 +73,9 @@ export default class ReplaceSupers {
*
* _set(Object.getPrototypeOf(CLASS.prototype), "METHOD", "VALUE", this)
*
* @param {Node} property
* @param {Node} value
* @param {Boolean} isComputed
* @param {Node} thisExpression
*
* @returns {Node}
*/
setSuperProperty(property, value, isComputed, thisExpression) {
setSuperProperty(property: Object, value: Object, isComputed: boolean, thisExpression: Object): Object {
return t.callExpression(
this.file.addHelper("set"),
[
@@ -108,14 +99,9 @@ export default class ReplaceSupers {
*
* _get(Object.getPrototypeOf(CLASS.prototype), "METHOD", this)
*
* @param {Node} property
* @param {Boolean} isComputed
* @param {Node} thisExpression
*
* @returns {Node}
*/
getSuperProperty(property, isComputed, thisExpression) {
getSuperProperty(property: Object, isComputed: boolean, thisExpression: Object): Object {
return t.callExpression(
this.file.addHelper("get"),
[
@@ -141,12 +127,9 @@ export default class ReplaceSupers {
/**
* Description
*
* @param {Object} node
* @param {Boolean} topLevel
*/
traverseLevel(node, topLevel) {
traverseLevel(node: Object, topLevel: boolean) {
var state = { self: this, topLevel: topLevel };
this.scope.traverse(node, visitor, state);
}
@@ -169,14 +152,9 @@ export default class ReplaceSupers {
/**
* Description
*
* @param {Object} node
* @param {Object} id
* @param {Object} parent
* @returns {Object}
*/
getLooseSuperProperty(id, parent) {
getLooseSuperProperty(id: Object, parent: Object) {
var methodNode = this.methodNode;
var methodName = methodNode.key;
var superRef = this.superRef || t.identifier("Function");
@@ -211,13 +189,9 @@ export default class ReplaceSupers {
/**
* Description
*
* @param {Function} getThisReference
* @param {Object} node
* @param {Object} parent
*/
looseHandle(getThisReference, node, parent) {
looseHandle(getThisReference: Function, node: Object, parent: Object) {
if (t.isIdentifier(node, { name: "super" })) {
this.hasSuper = true;
return this.getLooseSuperProperty(node, parent);
@@ -235,13 +209,9 @@ export default class ReplaceSupers {
/**
* Description
*
* @param {Function} getThisReference
* @param {Object} node
* @param {Object} parent
*/
specHandle(getThisReference, node, parent) {
specHandle(getThisReference: Function, node: Object, parent: Object) {
var methodNode = this.methodNode;
var property;
var computed;

View File

@@ -4,7 +4,7 @@ import object from "../helpers/object";
import File from "./file";
import each from "lodash/collection/each";
export default function transform(code, opts) {
export default function transform(code: code, opts?: Object) {
var file = new File(opts);
return file.parse(code);
}
@@ -18,7 +18,7 @@ transform.fromAst = function (ast, code, opts) {
return file.generate();
};
transform._ensureTransformerNames = function (type, rawKeys) {
transform._ensureTransformerNames = function (type: string, rawKeys: Array<string>) {
var keys = [];
for (var i = 0; i < rawKeys.length; i++) {

View File

@@ -6,14 +6,14 @@ import includes from "lodash/collection/includes";
*/
export default class TransformerPass {
constructor(file, transformer) {
constructor(file: File, transformer: Transformer) {
this.transformer = transformer;
this.shouldRun = !transformer.check;
this.handlers = transformer.handlers;
this.file = file;
}
canRun() {
canRun(): boolean {
var transformer = this.transformer;
var opts = this.file.opts;
@@ -42,7 +42,7 @@ export default class TransformerPass {
return true;
}
checkNode(node) {
checkNode(node: Object): boolean {
var check = this.transformer.check;
if (check) {
return this.shouldRun = check(node);

View File

@@ -12,7 +12,7 @@ import each from "lodash/collection/each";
*/
export default class Transformer {
constructor(transformerKey, transformer, opts) {
constructor(transformerKey: key, transformer: Object, opts: Object) {
transformer = assign({}, transformer);
var take = function (key) {
@@ -36,7 +36,7 @@ export default class Transformer {
this.key = transformerKey;
}
normalize(transformer) {
normalize(transformer: Object): Object {
if (isFunction(transformer)) {
transformer = { ast: transformer };
}
@@ -65,7 +65,7 @@ export default class Transformer {
return transformer;
}
buildPass(file) {
buildPass(file: File): TransformerPass {
return new TransformerPass(file, this);
}
}

View File

@@ -1,6 +1,7 @@
{
"selfContained": "runtime",
"unicode-regex": "regex.unicode",
"spec.typeofSymbol": "es6.symbols",
"minification.deadCodeElimination": "utility.deadCodeElimination",
"minification.removeConsoleCalls": "utility.removeConsole",

View File

@@ -75,7 +75,7 @@ export function Loop(node, parent, scope, file) {
export function BlockStatement(block, parent, scope, file) {
if (!t.isLoop(parent)) {
var blockScoping = new BlockScoping(false, block, parent, scope, file);
var blockScoping = new BlockScoping(null, block, parent, scope, file);
blockScoping.run();
}
}
@@ -222,15 +222,9 @@ class BlockScoping {
/**
* Description
*
* @param {Boolean|Node} loopParent
* @param {Node} block
* @param {Node} parent
* @param {Scope} scope
* @param {File} file
*/
constructor(loopParent, block, parent, scope, file) {
constructor(loopParent?: Object, block: Object, parent: Object, scope: Scope, file: File) {
this.loopParent = loopParent;
this.parent = parent;
this.scope = scope;
@@ -466,12 +460,9 @@ class BlockScoping {
/**
* Turn a `VariableDeclaration` into an array of `AssignmentExpressions` with
* their declarations hoisted to before the closure wrapper.
*
* @param {Node} node VariableDeclaration
* @returns {Array}
*/
pushDeclar(node) {
pushDeclar(node: { type: "VariableDeclaration" }): Array<Object> {
this.body.push(t.variableDeclaration(node.kind, node.declarations.map(function (declar) {
return t.variableDeclarator(declar.id);
})));
@@ -491,12 +482,9 @@ class BlockScoping {
/**
* Push the closure to the body.
*
* @param {Node} ret Identifier
* @param {Node} call CallExpression
*/
build(ret, call) {
build(ret: { type: "Identifier" }, call: { type: "CallExpression" }) {
var has = this.has;
if (has.hasReturn || has.hasBreakContinue) {
this.buildHas(ret, call);
@@ -507,12 +495,9 @@ class BlockScoping {
/**
* Description
*
* @param {Node} ret Identifier
* @param {Node} call CallExpression
*/
buildHas(ret, call) {
buildHas(ret: { type: "Identifier" }, call: { type: "CallExpression" }) {
var body = this.body;
body.push(t.variableDeclaration("var", [

View File

@@ -56,14 +56,9 @@ class ClassTransformer {
/**
* Description
*
* @param {Node} node
* @param {Node} parent
* @param {Scope} scope
* @param {File} file
*/
constructor(node, parent, scope, file) {
constructor(node: Object, parent: Object, scope: Scope, file: File) {
this.parent = parent;
this.scope = scope;
this.node = node;
@@ -245,11 +240,9 @@ class ClassTransformer {
/**
* Description
*
* @param {Node} node
*/
verifyConstructor(node) {
verifyConstructor(node: Object) {
return; // enable this for the next major
var state = {
@@ -267,11 +260,9 @@ class ClassTransformer {
/**
* Push a method to its respective mutatorMap.
*
* @param {Node} node MethodDefinition
*/
pushMethod(node) {
pushMethod(node: { type: "MethodDefinition" }) {
var methodName = node.key;
var kind = node.kind;
@@ -308,11 +299,9 @@ class ClassTransformer {
/**
* Description
*
* @param {Node} node
*/
pushProperty(node) {
pushProperty(node: Object) {
if (!node.value) return;
var key;
@@ -332,11 +321,9 @@ class ClassTransformer {
/**
* Replace the constructor body of our class.
*
* @param {Node} method MethodDefinition
*/
pushConstructor(method) {
pushConstructor(method: { type: "MethodDefinition" }) {
if (method.kind) {
throw this.file.errorWithNode(method, messages.get("classesIllegalConstructorKind"));
}

View File

@@ -94,7 +94,7 @@ export default {
_aliasFunctions: require("./internal/alias-functions"),
"spec.typeofSymbol": require("./spec/typeof-symbol"),
"es6.symbols": require("./es6/symbols"),
"spec.undefinedToVoid": require("./spec/undefined-to-void"),
_strict: require("./internal/strict"),

View File

@@ -13,3 +13,18 @@ export var ExpressionStatement = {
if (!node.expression) this.remove();
}
};
export var Binary = {
exit(node) {
var right = node.right;
var left = node.left;
if (!left && !right) {
this.remove();
} else if (!left) {
return right;
} else if (!right) {
return left;
}
}
};

View File

@@ -32,9 +32,11 @@ export function ExportDeclaration(node, parent, scope) {
if (node.default) {
if (t.isClassDeclaration(declar)) {
// export default class Foo {};
this.node = [getDeclar(), node];
node.declaration = declar.id;
return [getDeclar(), node];
} else if (t.isClassExpression(declar)) {
// export default class {};
var temp = scope.generateUidIdentifier("default");
declar = t.variableDeclaration("var", [
t.variableDeclarator(temp, declar)
@@ -42,25 +44,26 @@ export function ExportDeclaration(node, parent, scope) {
node.declaration = temp;
return [getDeclar(), node];
} else if (t.isFunctionDeclaration(declar)) {
// export default function Foo() {}
node._blockHoist = 2;
node.declaration = declar.id;
return [getDeclar(), node];
}
} else {
if (t.isFunctionDeclaration(declar)) {
// export function Foo() {}
node.specifiers = [t.importSpecifier(declar.id, declar.id)];
node.declaration = null;
node._blockHoist = 2;
return [getDeclar(), node];
} else if (t.isVariableDeclaration(declar)) {
// export var foo = "bar";
var specifiers = [];
var bindings = t.getBindingIdentifiers(declar);
for (var key in bindings) {
var id = bindings[key];
specifiers.push(t.exportSpecifier(id, id));
}
return [declar, t.exportDeclaration(null, specifiers)];
}
}

View File

@@ -25,3 +25,16 @@ export function Property(node, parent, scope, file) {
}
export { Property as MethodDefinition };
export function BlockStatement(node) {
for (var i = 0; i < node.body.length; i++) {
var bodyNode = node.body[i];
if (t.isExpressionStatement(bodyNode) && t.isLiteral(bodyNode.expression)) {
bodyNode._blockHoist = Infinity;
} else {
return;
}
}
}
export { BlockStatement as Program };

View File

@@ -16,8 +16,7 @@ var ALIASABLE_CONSTRUCTORS = [
"Map",
"WeakMap",
"Set",
"WeakSet",
"Number"
"WeakSet"
];
var astVisitor = {

View File

@@ -1,6 +1,7 @@
import TraversalPath from "./path";
import flatten from "lodash/array/flatten";
import compact from "lodash/array/compact";
import t from "../types";
export default class TraversalContext {
constructor(scope, opts, state, parentPath) {
@@ -42,7 +43,7 @@ export default class TraversalContext {
if (this.shouldFlatten) {
node[key] = flatten(node[key]);
if (key === "body" || key === "expressions") {
if (t.FLATTENABLE_KEYS.indexOf(key) >= 0) {
// we can safely compact this
node[key] = compact(node[key]);
}

View File

@@ -57,9 +57,8 @@ export default class TraversalPath {
}
setContext(parentPath, context, key) {
this.shouldRemove = false;
this.shouldSkip = false;
this.shouldStop = false;
this.shouldSkip = false;
this.shouldStop = false;
this.parentPath = parentPath || this.parentPath;
this.context = context;
@@ -71,8 +70,9 @@ export default class TraversalPath {
}
remove() {
this.shouldRemove = true;
this.shouldSkip = true;
this._refresh(this.node, []);
this.container[this.key] = null;
this.flatten();
}
skip() {
@@ -88,30 +88,43 @@ export default class TraversalPath {
this.context.flatten();
}
_refresh(oldNode, newNodes) {
// todo
}
refresh() {
var node = this.node;
this._refresh(node, [node]);
}
get node() {
return this.container[this.key];
}
set node(replacement) {
var isArray = Array.isArray(replacement);
if (!replacement) return this.remove();
var oldNode = this.node;
var isArray = Array.isArray(replacement);
var replacements = isArray ? replacement : [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.node);
var inheritTo = replacements[0];
if (inheritTo) t.inheritsComments(inheritTo, oldNode);
// replace the node
this.container[this.key] = replacement;
// potentially create new scope
this.setScope();
// refresh scope with new/removed bindings
this._refresh(oldNode, replacements);
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);
for (var i = 0; i < replacements.length; i++) {
file.checkNode(replacements[i], this.scope);
}
}
@@ -123,6 +136,7 @@ export default class TraversalPath {
}
this.flatten();
// TODO: duplicate internal path metadata across the new node paths
}
}
@@ -139,11 +153,6 @@ export default class TraversalPath {
if (replacement) {
this.node = replacement;
}
if (this.shouldRemove) {
this.container[this.key] = null;
this.flatten();
}
}
isBlacklisted() {
@@ -163,16 +172,17 @@ export default class TraversalPath {
var node = this.node;
var opts = this.opts;
if (Array.isArray(node)) {
// traverse over these replacement nodes we purposely don't call exitNode
// as the original node has been destroyed
for (var i = 0; i < node.length; i++) {
traverse.node(node[i], opts, this.scope, this.state, this);
if (node) {
if (Array.isArray(node)) {
// traverse over these replacement nodes we purposely don't call exitNode
// as the original node has been destroyed
for (var i = 0; i < node.length; i++) {
traverse.node(node[i], opts, this.scope, this.state, this);
}
} else {
traverse.node(node, opts, this.scope, this.state, this);
this.call("exit");
}
} else {
traverse.node(node, opts, this.scope, this.state, this);
this.call("exit");
}
return this.shouldStop;

View File

@@ -9,7 +9,6 @@ import object from "../helpers/object";
import each from "lodash/collection/each";
import t from "../types";
var functionVariableVisitor = {
enter(node, parent, scope, state) {
if (t.isFor(node)) {
@@ -64,14 +63,9 @@ export default class Scope {
/**
* This searches the current "scope" and collects all references/bindings
* within.
*
* @param {Node} block
* @param {Node} parentBlock
* @param {Scope} [parent]
* @param {File} [file]
*/
constructor(block, parentBlock, parent, file) {
constructor(block: Object, parentBlock: Object, parent?: Scope, file?: File) {
this.parent = parent;
this.file = parent ? parent.file : file;
@@ -82,28 +76,22 @@ export default class Scope {
}
static globals = flatten([globals.builtin, globals.browser, globals.node].map(Object.keys));
static contextVariables = ["this", "arguments"];
static contextVariables = ["this", "arguments", "super"];
/**
* Description
*
* @param {Object} node
* @param {Object} opts
* @param [state]
*/
traverse(node, opts, state) {
traverse(node: Object, opts: Object, state?) {
traverse(node, opts, this, state);
}
/**
* Description
*
* @param {String} [name="temp"]
*/
generateTemp(name) {
var id = this.generateUidIdentifier(name || "temp");
generateTemp(name: string = "temp") {
var id = this.generateUidIdentifier(name);
this.push({
key: id.name,
id: id
@@ -113,11 +101,9 @@ export default class Scope {
/**
* Description
*
* @param {String} name
*/
generateUidIdentifier(name) {
generateUidIdentifier(name: string) {
var id = t.identifier(this.generateUid(name));
this.getFunctionParent().registerBinding("uid", id);
return id;
@@ -125,11 +111,9 @@ export default class Scope {
/**
* Description
*
* @param {String} name
*/
generateUid(name) {
generateUid(name: string) {
name = t.toIdentifier(name).replace(/^_+/, "");
var uid;
@@ -149,12 +133,9 @@ export default class Scope {
/*
* Description
*
* @param {Object} parent
* @returns {Object}
*/
generateUidBasedOnNode(parent) {
generateUidBasedOnNode(parent: Object): Object {
var node = parent;
if (t.isAssignmentExpression(parent)) {
@@ -190,12 +171,9 @@ export default class Scope {
/**
* Description
*
* @param {Object} node
* @returns {Object}
*/
generateTempBasedOnNode(node) {
generateTempBasedOnNode(node: Object): ?Object {
if (t.isIdentifier(node) && this.hasBinding(node.name)) {
return null;
}
@@ -208,7 +186,11 @@ export default class Scope {
return id;
}
checkBlockScopedCollisions(kind, name, id) {
/**
* Description
*/
checkBlockScopedCollisions(kind: string, name: string, id: Object) {
var local = this.getOwnBindingInfo(name);
if (!local) return;
@@ -220,7 +202,11 @@ export default class Scope {
}
}
rename(oldName, newName) {
/**
* Description
*/
rename(oldName: string, newName: string) {
newName ||= this.generateUidIdentifier(oldName).name;
var info = this.getBindingInfo(oldName);
@@ -246,13 +232,17 @@ export default class Scope {
}
});
this.clearOwnBinding(oldName);
scope.removeOwnBinding(oldName);
scope.bindings[newName] = info;
binding.name = newName;
}
inferType(node) {
/**
* Description
*/
inferType(node: Object) {
var target;
if (t.isVariableDeclarator(node)) {
@@ -284,7 +274,11 @@ export default class Scope {
}
}
isTypeGeneric(name, genericName) {
/**
* Description
*/
isTypeGeneric(name: string, genericName: string) {
var info = this.getBindingInfo(name);
if (!info) return false;
@@ -292,18 +286,30 @@ export default class Scope {
return t.isGenericTypeAnnotation(type) && t.isIdentifier(type.id, { name: genericName });
}
assignTypeGeneric(name, type) {
/**
* Description
*/
assignTypeGeneric(name: string, type: Object) {
this.assignType(name, t.genericTypeAnnotation(t.identifier(type)));
}
assignType(name, type) {
/**
* Description
*/
assignType(name: string, type: Object) {
var info = this.getBindingInfo(name);
if (!info) return;
info.typeAnnotation = type;
}
getTypeAnnotation(name, id, node) {
/**
* Description
*/
getTypeAnnotation(id: Object, node: Object): Object {
var info = {
annotation: null,
inferred: false
@@ -328,7 +334,11 @@ export default class Scope {
return info;
}
toArray(node, i) {
/**
* Description
*/
toArray(node: Object, i?: number) {
var file = this.file;
if (t.isIdentifier(node) && this.isTypeGeneric(node.name, "Array")) {
@@ -354,11 +364,25 @@ export default class Scope {
return t.callExpression(file.addHelper(helperName), args);
}
clearOwnBinding(name) {
delete this.bindings[name];
/**
* Description
*/
refreshDeclaration(node: Object) {
if (t.isBlockScoped(node)) {
this.getBlockParent().registerDeclaration(node);
} else if (t.isVariableDeclaration(node, { kind: "var" })) {
this.getFunctionParent().registerDeclaration(node);
} else if (node === this.block) {
this.recrawl();
}
}
registerDeclaration(node) {
/**
* Description
*/
registerDeclaration(node: Object) {
if (t.isFunctionDeclaration(node)) {
this.registerBinding("hoisted", node);
} else if (t.isVariableDeclaration(node)) {
@@ -374,7 +398,11 @@ export default class Scope {
}
}
registerBindingReassignment(node) {
/**
* Description
*/
registerBindingReassignment(node: Object) {
var ids = t.getBindingIdentifiers(node);
for (var name in ids) {
var info = this.getBindingInfo(name);
@@ -389,7 +417,11 @@ export default class Scope {
}
}
registerBinding(kind, node) {
/**
* Description
*/
registerBinding(kind: string, node: Object) {
if (!kind) throw new ReferenceError("no `kind`");
var ids = t.getBindingIdentifiers(node);
@@ -399,7 +431,7 @@ export default class Scope {
this.checkBlockScopedCollisions(kind, name, id);
var typeInfo = this.getTypeAnnotation(name, id, node);
var typeInfo = this.getTypeAnnotation(id, node);
this.bindings[name] = {
typeAnnotationInferred: typeInfo.inferred,
@@ -413,18 +445,19 @@ export default class Scope {
}
}
registerVariableDeclaration(declar) {
var declars = declar.declarations;
for (var i = 0; i < declars.length; i++) {
this.registerBinding(declar.kind, declars[i]);
}
/**
* Description
*/
addGlobal(node: Object) {
this.globals[node.name] = node;
}
addGlobal(node) {
this.globals[node.name] = node;
};
/**
* Description
*/
hasGlobal(name) {
hasGlobal(name: string): boolean {
var scope = this;
do {
@@ -434,6 +467,19 @@ export default class Scope {
return false;
}
/**
* Description
*/
recrawl() {
this.block._scopeInfo = null;
this.crawl();
}
/**
* Description
*/
crawl() {
var block = this.block;
var i;
@@ -474,6 +520,12 @@ export default class Scope {
}
}
// Class
if (t.isClass(block) && block.id) {
this.registerBinding("var", block.id);
}
// Function - params, rest
if (t.isFunction(block)) {
@@ -519,11 +571,9 @@ export default class Scope {
/**
* Description
*
* @param {Object} opts
*/
push(opts) {
push(opts: Object) {
var block = this.block;
if (t.isLoop(block) || t.isCatchClause(block) || t.isFunction(block)) {
@@ -533,7 +583,7 @@ export default class Scope {
if (t.isBlockStatement(block) || t.isProgram(block)) {
block._declarations ||= {};
block._declarations[opts.key] = {
block._declarations[opts.key || opts.id.name] = {
kind: opts.kind || "var",
id: opts.id,
init: opts.init
@@ -557,12 +607,23 @@ export default class Scope {
}
/**
* Walks the scope tree and gathers **all** bindings.
*
* @returns {Object}
* Walk up the scope tree until we hit either a BlockStatement/Loop or reach the
* very top and hit Program.
*/
getAllBindings() {
getBlockParent() {
var scope = this;
while (scope.parent && !t.isFunction(scope.block) && !t.isLoop(scope.block) && !t.isFunction(scope.block)) {
scope = scope.parent;
}
return scope;
}
/**
* Walks the scope tree and gathers **all** bindings.
*/
getAllBindings(): Object {
var ids = object();
var scope = this;
@@ -576,12 +637,9 @@ export default class Scope {
/**
* Walks the scope tree and gathers all declarations of `kind`.
*
* @param {String} kind
* @returns {Object}
*/
getAllBindingsOfKind(kind) {
getAllBindingsOfKind(kind: string): Object {
var ids = object();
var scope = this;
@@ -596,15 +654,19 @@ export default class Scope {
return ids;
}
// misc
/**
* Description
*/
bindingIdentifierEquals(name, node) {
bindingIdentifierEquals(name: string, node: Object): boolean {
return this.getBindingIdentifier(name) === node;
}
// get
/**
* Description
*/
getBindingInfo(name) {
getBindingInfo(name: string) {
var scope = this;
do {
@@ -613,26 +675,45 @@ export default class Scope {
} while (scope = scope.parent);
}
getOwnBindingInfo(name) {
/**
* Description
*/
getOwnBindingInfo(name: string) {
return this.bindings[name];
}
getBindingIdentifier(name) {
/**
* Description
*/
getBindingIdentifier(name: string) {
var info = this.getBindingInfo(name);
return info && info.identifier;
}
getOwnBindingIdentifier(name) {
/**
* Description
*/
getOwnBindingIdentifier(name: string) {
var binding = this.bindings[name];
return binding && binding.identifier;
}
/**
* Description
*/
getOwnImmutableBindingValue(name) {
getOwnImmutableBindingValue(name: string) {
return this._immutableBindingInfoToValue(this.getOwnBindingInfo(name));
}
getImmutableBindingValue(name) {
/**
* Description
*/
getImmutableBindingValue(name: string) {
return this._immutableBindingInfoToValue(this.getBindingInfo(name));
}
@@ -658,13 +739,19 @@ export default class Scope {
}
}
// has
/**
* Description
*/
hasOwnBinding(name) {
hasOwnBinding(name: string) {
return !!this.getOwnBindingInfo(name);
}
hasBinding(name) {
/**
* Description
*/
hasBinding(name: string) {
if (!name) return false;
if (this.hasOwnBinding(name)) return true;
if (this.parentHasBinding(name)) return true;
@@ -673,7 +760,28 @@ export default class Scope {
return false;
}
parentHasBinding(name) {
/**
* Description
*/
parentHasBinding(name: string) {
return this.parent && this.parent.hasBinding(name);
}
/**
* Description
*/
removeOwnBinding(name: string) {
this.bindings[name] = null;
}
/**
* Description
*/
removeBinding(name: string) {
var info = this.getBindingInfo(name);
if (info) info.scope.removeOwnBinding(name);
}
}

View File

@@ -25,7 +25,7 @@
"ImportSpecifier": ["ModuleSpecifier"],
"ExportSpecifier": ["ModuleSpecifier"],
"BlockStatement": ["Statement", "Scopable"],
"BlockStatement": ["Scopable", "Statement"],
"Program": ["Scopable"],
"CatchClause": ["Scopable"],
@@ -36,8 +36,8 @@
"SpreadProperty": ["UnaryLike"],
"SpreadElement": ["UnaryLike"],
"ClassDeclaration": ["Statement", "Declaration", "Class"],
"ClassExpression": ["Class", "Expression"],
"ClassDeclaration": ["Scopable", "Class", "Statement", "Declaration"],
"ClassExpression": ["Scopable", "Class", "Expression"],
"ForOfStatement": ["Scopable", "Statement", "For", "Loop"],
"ForInStatement": ["Scopable", "Statement", "For", "Loop"],

View File

@@ -16,12 +16,9 @@ export default t;
/**
* Registers `is[Type]` and `assert[Type]` generated functions for a given `type`.
* Pass `skipAliasCheck` to force it to directly compare `node.type` with `type`.
*
* @param {String} type
* @param {Boolean?} skipAliasCheck
*/
function registerType(type, skipAliasCheck) {
function registerType(type: string, skipAliasCheck?: boolean) {
var is = t[`is${type}`] = function (node, opts) {
return t.is(type, node, opts, skipAliasCheck);
};
@@ -36,6 +33,7 @@ function registerType(type, skipAliasCheck) {
t.STATEMENT_OR_BLOCK_KEYS = ["consequent", "body", "alternate"];
t.NATIVE_TYPE_NAMES = ["Array", "Object", "Number", "Boolean", "Date", "Array", "String"];
t.FLATTENABLE_KEYS = ["body", "expressions"];
t.FOR_INIT_KEYS = ["left", "init"];
t.VISITOR_KEYS = require("./visitor-keys");
@@ -66,15 +64,9 @@ t.TYPES = Object.keys(t.VISITOR_KEYS).concat(Object.keys(t.FLIPPED_ALIAS_KEYS));
*
* For better performance, use this instead of `is[Type]` when `type` is unknown.
* Optionally, pass `skipAliasCheck` to directly compare `node.type` with `type`.
*
* @param {String} type
* @param {Node} node
* @param {Object?} opts
* @param {Boolean?} skipAliasCheck
* @returns {Boolean} isOfType
*/
t.is = function (type, node, opts, skipAliasCheck) {
t.is = function (type: string, node: Object, opts?: Object, skipAliasCheck?: boolean): boolean {
if (!node) return false;
var typeMatches = type === node.type;
@@ -132,12 +124,9 @@ each(t.BUILDER_KEYS, function (keys, type) {
/**
* Description
*
* @param {Object} node
* @returns {Object}
*/
t.toComputedKey = function (node, key) {
t.toComputedKey = function (node: Object, key: Object): Object {
if (!node.computed) {
if (t.isIdentifier(key)) key = t.literal(key.name);
}
@@ -151,12 +140,9 @@ t.toComputedKey = function (node, key) {
* declarations hoisted to the top of the current scope.
*
* Expression statements are just resolved to their standard expression.
*
* @param {Array} nodes
* @param {Scope} scope
*/
t.toSequenceExpression = function (nodes, scope) {
t.toSequenceExpression = function (nodes: Array<Object>, scope: Scope): Object {
var exprs = [];
each(nodes, function (node) {
@@ -185,13 +171,9 @@ t.toSequenceExpression = function (nodes, scope) {
/*
* Description
*
* @param {Object} actual
* @param {Object} expected
* @returns {Boolean}
*/
t.shallowEqual = function (actual, expected) {
t.shallowEqual = function (actual: Object, expected: Object): boolean {
var keys = Object.keys(expected);
for (var i = 0; i < keys.length; i++) {
@@ -207,14 +189,9 @@ t.shallowEqual = function (actual, expected) {
/**
* Description
*
* @param {Object} member
* @param {Object} append
* @param {Boolean} [computed]
* @returns {Object} member
*/
t.appendToMemberExpression = function (member, append, computed) {
t.appendToMemberExpression = function (member: Object, append: Object, computed?: boolean): Object {
member.object = t.memberExpression(member.object, member.property, member.computed);
member.property = append;
member.computed = !!computed;
@@ -223,26 +200,18 @@ t.appendToMemberExpression = function (member, append, computed) {
/**
* Description
*
* @param {Object} member
* @param {Object} append
* @returns {Object} member
*/
t.prependToMemberExpression = function (member, append) {
t.prependToMemberExpression = function (member: Object, append: Object): Object {
member.object = t.memberExpression(append, member.object);
return member;
};
/**
* Check if the input `node` is a reference to a bound variable.
*
* @param {Object} node
* @param {Object} parent
* @returns {Boolean}
*/
t.isReferenced = function (node, parent) {
t.isReferenced = function (node: Object, parent: Object): boolean {
// yes: PARENT[NODE]
// yes: NODE.child
// no: parent.CHILD
@@ -346,36 +315,26 @@ t.isReferenced = function (node, parent) {
/**
* Check if the input `node` is an `Identifier` and `isReferenced`.
*
* @param {Node} node
* @parma {Node} parent
* @returns {Boolean}
*/
t.isReferencedIdentifier = function (node, parent, opts) {
t.isReferencedIdentifier = function (node: Object, parent: Object, opts?: Object): boolean {
return t.isIdentifier(node, opts) && t.isReferenced(node, parent);
};
/**
* Check if the input `name` is a valid identifier name
* and isn't a reserved word.
*
* @param {String} name
* @returns {Boolean}
*/
t.isValidIdentifier = function (name) {
t.isValidIdentifier = function (name: string): boolean {
return isString(name) && esutils.keyword.isIdentifierName(name) && !esutils.keyword.isReservedWordES6(name, true);
};
/*
* Description
*
* @param {String} name
* @returns {String}
*/
t.toIdentifier = function (name) {
t.toIdentifier = function (name: string): string {
if (t.isIdentifier(name)) return name.name;
name = name + "";
@@ -400,24 +359,17 @@ t.toIdentifier = function (name) {
/**
* Description
*
* @param {Object} node
* @param {String=} key
*/
t.ensureBlock = function (node, key) {
key ||= "body";
t.ensureBlock = function (node: Object, key: string = "body") {
return node[key] = t.toBlock(node[key], node);
};
/**
* Description
*
* @param {Object} node
* @returns {Object}
*/
t.clone = function (node) {
t.clone = function (node: Object): Object {
var newNode = {};
for (var key in node) {
if (key[0] === "_") continue;
@@ -428,12 +380,9 @@ t.clone = function (node) {
/**
* Description
*
* @param {Object} node
* @returns {Object}
*/
t.cloneDeep = function (node) {
t.cloneDeep = function (node: Object): Object {
var newNode = {};
for (var key in node) {
@@ -461,13 +410,9 @@ t.cloneDeep = function (node) {
*
* For example, given the match `React.createClass` it would match the
* parsed nodes of `React.createClass` and `React["createClass"]`.
*
* @param {String} match Dot-delimited string
* @param {Boolean} [allowPartial] Allow a partial match
* @returns {Function}
*/
t.buildMatchMemberExpression = function (match, allowPartial) {
t.buildMatchMemberExpression = function (match:string, allowPartial?: boolean): Function {
var parts = match.split(".");
return function (member) {
@@ -517,12 +462,10 @@ t.buildMatchMemberExpression = function (match, allowPartial) {
/**
* Description
*
* @param {Object} node
* @param {Boolean} [ignore]
* @returns {Object|Boolean}
*/
t.toStatement = function (node, ignore) {
t.toStatement = function (node: Object, ignore?: boolean) {
if (t.isStatement(node)) {
return node;
}
@@ -559,12 +502,9 @@ t.toStatement = function (node, ignore) {
/**
* Description
*
* @param {Object} node
* @returns {Object}
*/
t.toExpression = function (node) {
t.toExpression = function (node: Object): Object {
if (t.isExpressionStatement(node)) {
node = node.expression;
}
@@ -584,13 +524,9 @@ t.toExpression = function (node) {
/**
* Description
*
* @param {Object} node
* @param {Object} parent
* @returns {Object}
*/
t.toBlock = function (node, parent) {
t.toBlock = function (node: Object, parent: Object): Object {
if (t.isBlockStatement(node)) {
return node;
}
@@ -617,12 +553,9 @@ t.toBlock = function (node, parent) {
/**
* Return a list of binding identifiers associated with
* the input `node`.
*
* @param {Object} node
* @returns {Array|Object}
*/
t.getBindingIdentifiers = function (node) {
t.getBindingIdentifiers = function (node: Object): Object {
var search = [].concat(node);
var ids = object();
@@ -657,7 +590,9 @@ t.getBindingIdentifiers.keys = {
ImportBatchSpecifier: ["name"],
VariableDeclarator: ["id"],
FunctionDeclaration: ["id"],
FunctionExpression: ["id"],
ClassDeclaration: ["id"],
ClassExpression: ["id"],
SpreadElement: ["argument"],
RestElement: ["argument"],
UpdateExpression: ["argument"],
@@ -675,34 +610,25 @@ t.getBindingIdentifiers.keys = {
/**
* Description
*
* @param {Object} node
* @returns {Boolean}
*/
t.isLet = function (node) {
t.isLet = function (node: Object): boolean {
return t.isVariableDeclaration(node) && (node.kind !== "var" || node._let);
};
/**
* Description
*
* @param {Object} node
* @returns {Boolean}
*/
t.isBlockScoped = function (node) {
t.isBlockScoped = function (node: Object): boolean {
return t.isFunctionDeclaration(node) || t.isClassDeclaration(node) || t.isLet(node);
};
/**
* Description
*
* @param {Object} node
* @returns {Boolean}
*/
t.isVar = function (node) {
t.isVar = function (node: Object): boolean {
return t.isVariableDeclaration(node, { kind: "var" }) && !node._let;
};
@@ -712,12 +638,9 @@ t.COMMENT_KEYS = ["leadingComments", "trailingComments"];
/**
* Description
*
* @param {Object} child
* @returns {Object} child
*/
t.removeComments = function (child) {
t.removeComments = function (child: Object): Object {
each(t.COMMENT_KEYS, function (key) {
delete child[key];
});
@@ -726,13 +649,9 @@ t.removeComments = function (child) {
/**
* Description
*
* @param {Object} child
* @param {Object} parent
* @returns {Object} child
*/
t.inheritsComments = function (child, parent) {
t.inheritsComments = function (child: Object, parent: Object): Object {
each(t.COMMENT_KEYS, function (key) {
child[key] = uniq(compact([].concat(child[key], parent[key])));
});
@@ -741,13 +660,9 @@ t.inheritsComments = function (child, parent) {
/**
* Description
*
* @param {Object} child
* @param {Object} parent
* @returns {Object} child
*/
t.inherits = function (child, parent) {
t.inherits = function (child: Object, parent: Object): Object {
child._declarations = parent._declarations;
child._scopeInfo = parent._scopeInfo;
child.range = parent.range;
@@ -764,12 +679,9 @@ t.inherits = function (child, parent) {
/**
* Description
*
* @param {Object} node
* @returns {Array}
*/
t.getLastStatements = function (node) {
t.getLastStatements = function (node: Object): Array<Object> {
var nodes = [];
var add = function (node) {
@@ -792,23 +704,17 @@ t.getLastStatements = function (node) {
/**
* Description
*
* @param {Object} specifier
* @returns {String}
*/
t.getSpecifierName = function (specifier) {
t.getSpecifierName = function (specifier: Object): Object {
return specifier.name || specifier.id;
};
/**
* Description
*
* @param {Object} specifier
* @returns {String}
*/
t.getSpecifierId = function (specifier) {
t.getSpecifierId = function (specifier: Object): Object {
if (specifier.default) {
return t.identifier("default");
} else {
@@ -818,24 +724,17 @@ t.getSpecifierId = function (specifier) {
/**
* Description
*
* @param {Object} specifier
* @returns {Boolean}
*/
t.isSpecifierDefault = function (specifier) {
t.isSpecifierDefault = function (specifier: Object): boolean {
return specifier.default || t.isIdentifier(specifier.id) && specifier.id.name === "default";
};
/**
* Description
*
* @param {Node} node
* @param {Node} parent
* @returns {Boolean}
*/
t.isScope = function (node, parent) {
t.isScope = function (node: Object, parent: Object): boolean {
if (t.isBlockStatement(node)) {
if (t.isLoop(parent.block, { body: node })) {
return false;
@@ -851,12 +750,9 @@ t.isScope = function (node, parent) {
/**
* Description
*
* @param {Node} node
* @returns {Boolean}
*/
t.isImmutable = function (node) {
t.isImmutable = function (node: Object): boolean {
if (t.isLiteral(node)) {
if (node.regex) {
// regexes are mutable
@@ -894,12 +790,9 @@ t.isImmutable = function (node) {
*
* if (!t.evaluateTruthy(node)) falsyLogic();
*
* @param {Node} node
* @param {Scope} scope
* @returns {Boolean}
*/
t.evaluateTruthy = function (node, scope) {
t.evaluateTruthy = function (node: Object, scope: Scope): boolean {
var res = t.evaluate(node, scope);
if (res.confident) return !!res.value;
};
@@ -917,12 +810,9 @@ t.evaluateTruthy = function (node, scope) {
* 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) {
t.evaluate = function (node: Object, scope: Scope): { confident: boolean; value: any } {
var confident = true;
var value = evaluate(node);
@@ -1014,12 +904,9 @@ t.evaluate = function (node, scope) {
/**
* Description
*
* @param value
* @returns {Node}
*/
t.valueToNode = function (value) {
t.valueToNode = function (value: any): Object {
if (value === undefined) {
return t.identifier("undefined");
}

View File

@@ -20,7 +20,7 @@ export { inherits, inspect } from "util";
export var debug = buildDebug("babel");
export function canCompile(filename, altExts) {
export function canCompile(filename: string, altExts?: Array<string>) {
var exts = altExts || canCompile.EXTENSIONS;
var ext = path.extname(filename);
return contains(exts, ext);
@@ -28,7 +28,7 @@ export function canCompile(filename, altExts) {
canCompile.EXTENSIONS = [".js", ".jsx", ".es6", ".es"];
export function resolve(loc) {
export function resolve(loc: string) {
try {
return require.resolve(loc);
} catch (err) {
@@ -36,11 +36,11 @@ export function resolve(loc) {
}
}
export function list(val) {
export function list(val: string): Array<string> {
return val ? val.split(",") : [];
}
export function regexify(val) {
export function regexify(val: any): RegExp {
if (!val) return new RegExp(/.^/);
if (Array.isArray(val)) val = val.join("|");
if (isString(val)) return new RegExp(val);
@@ -48,7 +48,7 @@ export function regexify(val) {
throw new TypeError("illegal type for regexify");
}
export function arrayify(val) {
export function arrayify(val: any): Array {
if (!val) return [];
if (isBoolean(val)) return [val];
if (isString(val)) return list(val);
@@ -56,7 +56,7 @@ export function arrayify(val) {
throw new TypeError("illegal type for arrayify");
}
export function booleanify(val) {
export function booleanify(val: any): boolean {
if (val === "true") return true;
if (val === "false") return false;
return val;
@@ -76,7 +76,7 @@ var templateVisitor = {
//
export function template(name, nodes, keepExpression) {
export function template(name: string, nodes?: Array<Object>, keepExpression?: boolean): Object {
var ast = exports.templates[name];
if (!ast) throw new ReferenceError(`unknown template ${name}`);
@@ -102,7 +102,7 @@ export function template(name, nodes, keepExpression) {
}
}
export function parseTemplate(loc, code) {
export function parseTemplate(loc: string, code: string): Object {
var ast = parse({ filename: loc }, code).program;
ast = traverse.removeProperties(ast);
return ast;

View File

@@ -0,0 +1,5 @@
var foo = async function bar() {
console.log(bar);
};
foo();

View File

@@ -0,0 +1,9 @@
"use strict";
var _bar;
var foo = _bar = babelHelpers.asyncToGenerator(function* () {
console.log(_bar);
});
foo();

View File

@@ -0,0 +1,5 @@
var foo = async function bar() {
console.log(bar);
};
foo();

View File

@@ -0,0 +1,11 @@
"use strict";
var _bluebird = require("bluebird");
var _bar;
var foo = _bar = _bluebird.coroutine(function* () {
console.log(_bar);
});
foo();

View File

@@ -0,0 +1,22 @@
class MyCtrl {
constructor(a) {
"any directive prologue";
foo;
}
}
class MyCtrl2 {
constructor(a) {
"a";
"b";
foo;
}
}
class MyCtrl3 {
constructor(a) {
"a";
foo;
"b";
}
}

View File

@@ -0,0 +1,21 @@
"use strict";
var MyCtrl = function MyCtrl(a) {
"any directive prologue";
babelHelpers.classCallCheck(this, MyCtrl);
foo;
};
var MyCtrl2 = function MyCtrl2(a) {
"a";
"b";
babelHelpers.classCallCheck(this, MyCtrl2);
foo;
};
var MyCtrl3 = function MyCtrl3(a) {
"a";
babelHelpers.classCallCheck(this, MyCtrl3);
foo;
"b";
};

View File

@@ -0,0 +1,3 @@
{
"optional": ["es6.symbols"]
}

View File

@@ -1,3 +0,0 @@
{
"optional": ["spec.typeofSymbol"]
}

View File

@@ -0,0 +1,4 @@
function foo() {
true && console.log("foo");
blah();
}

View File

@@ -0,0 +1,6 @@
"use strict";
function foo() {
true;
blah();
}

View File

@@ -0,0 +1,2 @@
true && console.log("foo");
blah();

View File

@@ -0,0 +1,4 @@
"use strict";
true;
blah();

View File

@@ -1,3 +0,0 @@
"use strict";
function foo() {}

View File

@@ -0,0 +1,5 @@
"use strict";
function foo() {
blah();
}

View File

@@ -48,7 +48,7 @@ suite("kangax/compat-table", function () {
code = transform(code, {
filename: key,
blacklist: ["strict"],
optional: ["spec.typeofSymbol", "es6.blockScopingTDZ"]
optional: ["es6.symbols", "es6.blockScopingTDZ"]
}).code;
code = '"use strict";\n' + code;

View File

@@ -1,4 +1,4 @@
if (process.env.SIMPLE_BABEL_TESTS) return;
if (!process.env.ALL_BABEL_TESTS) return;
require("./_helper").assertVendor("test262");

View File

@@ -90,7 +90,7 @@ require("./_transformation-helper")({
"Syntax/StrictKeywords"
]
}, {
optional: ["spec.typeofSymbol"],
optional: ["es6.symbols"],
experimental: true
}, function (opts, task) {
if (!_.contains(task.exec.loc, "module.js")) {