use flow types for annotations - goodbye JSDoc!

This commit is contained in:
Sebastian McKenzie 2015-03-10 00:35:52 +11:00
parent 56b6a795a4
commit fc0e89463b
12 changed files with 134 additions and 397 deletions

View File

@ -24,7 +24,7 @@ export var messages = {
unsupportedOutputType: "Unsupported output type $1"
};
export function get(key, ...args) {
export function get(key: String, ...args) {
var msg = messages[key];
if (!msg) throw new ReferenceError(`Unknown message ${JSON.stringify(key)}`);
@ -35,7 +35,7 @@ export function get(key, ...args) {
});
}
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

@ -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

@ -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

@ -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

@ -70,7 +70,7 @@ export default class TraversalPath {
}
remove() {
this.refreshScope(this.node, []);
this._refresh(this.node, []);
this.container[this.key] = null;
this.flatten();
}
@ -88,13 +88,13 @@ export default class TraversalPath {
this.context.flatten();
}
refreshScope(oldNode, newNodes) {
_refresh(oldNode, newNodes) {
// todo
}
refresh() {
var node = this.node;
this.refreshScope(node, [node]);
this._refresh(node, [node]);
}
get node() {
@ -119,7 +119,7 @@ export default class TraversalPath {
this.setScope();
// refresh scope with new/removed bindings
this.refreshScope(oldNode, replacements);
this._refresh(oldNode, replacements);
var file = this.scope && this.scope.file;
if (file) {

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;
@ -86,24 +80,18 @@ export default class Scope {
/**
* 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;
}
@ -210,13 +188,9 @@ export default class Scope {
/**
* Description
*
* @param {String} kind
* @param {String} name
* @param {Node} id
*/
checkBlockScopedCollisions(kind, name, id) {
checkBlockScopedCollisions(kind: string, name: string, id: Object) {
var local = this.getOwnBindingInfo(name);
if (!local) return;
@ -230,12 +204,9 @@ export default class Scope {
/**
* Description
*
* @param {String} oldName
* @param {String} newName
*/
rename(oldName, newName) {
rename(oldName: string, newName: string) {
newName ||= this.generateUidIdentifier(oldName).name;
var info = this.getBindingInfo(oldName);
@ -269,11 +240,9 @@ export default class Scope {
/**
* Description
*
* @param {Node} node
*/
inferType(node) {
inferType(node: Object) {
var target;
if (t.isVariableDeclarator(node)) {
@ -307,12 +276,9 @@ export default class Scope {
/**
* Description
*
* @param {String} name
* @param {String} genericName
*/
isTypeGeneric(name, genericName) {
isTypeGeneric(name: string, genericName: string) {
var info = this.getBindingInfo(name);
if (!info) return false;
@ -322,23 +288,17 @@ export default class Scope {
/**
* Description
*
* @param {String} name
* @param {Node} type
*/
assignTypeGeneric(name, type) {
assignTypeGeneric(name: string, type: Object) {
this.assignType(name, t.genericTypeAnnotation(t.identifier(type)));
}
/**
* Description
*
* @param {String} name
* @param {Node} type
*/
assignType(name, type) {
assignType(name: string, type: Object) {
var info = this.getBindingInfo(name);
if (!info) return;
@ -347,13 +307,9 @@ export default class Scope {
/**
* Description
*
* @param name
* @param id
* @param {Node} node
*/
getTypeAnnotation(name, id, node) {
getTypeAnnotation(id: Object, node: Object): Object {
var info = {
annotation: null,
inferred: false
@ -380,12 +336,9 @@ export default class Scope {
/**
* Description
*
* @param {Node} node
* @param {Number} [i]
*/
toArray(node, i) {
toArray(node: Object, i?: number) {
var file = this.file;
if (t.isIdentifier(node) && this.isTypeGeneric(node.name, "Array")) {
@ -413,11 +366,9 @@ export default class Scope {
/**
* Description
*
* @param {Node} node
*/
refreshDeclaration(node) {
refreshDeclaration(node: Object) {
if (t.isBlockScoped(node)) {
this.getBlockParent().registerDeclaration(node);
} else if (t.isVariableDeclaration(node, { kind: "var" })) {
@ -429,11 +380,9 @@ export default class Scope {
/**
* Description
*
* @param {Node} node
*/
registerDeclaration(node) {
registerDeclaration(node: Object) {
if (t.isFunctionDeclaration(node)) {
this.registerBinding("hoisted", node);
} else if (t.isVariableDeclaration(node)) {
@ -451,11 +400,9 @@ export default class Scope {
/**
* Description
*
* @param {Node} node
*/
registerBindingReassignment(node) {
registerBindingReassignment(node: Object) {
var ids = t.getBindingIdentifiers(node);
for (var name in ids) {
var info = this.getBindingInfo(name);
@ -472,12 +419,9 @@ export default class Scope {
/**
* Description
*
* @param {String} kind
* @param {Node} node
*/
registerBinding(kind, node) {
registerBinding(kind: string, node: Object) {
if (!kind) throw new ReferenceError("no `kind`");
var ids = t.getBindingIdentifiers(node);
@ -487,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,
@ -503,21 +447,17 @@ export default class Scope {
/**
* Description
*
* @param {Node} node
*/
addGlobal(node) {
addGlobal(node: Object) {
this.globals[node.name] = node;
}
/**
* Description
*
* @param {String} name
*/
hasGlobal(name) {
hasGlobal(name: string): boolean {
var scope = this;
do {
@ -631,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)) {
@ -683,11 +621,9 @@ export default class Scope {
/**
* Walks the scope tree and gathers **all** bindings.
*
* @returns {Object}
*/
getAllBindings() {
getAllBindings(): Object {
var ids = object();
var scope = this;
@ -701,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;
@ -723,23 +656,17 @@ export default class Scope {
/**
* Description
*
* @param {String} name
* @param {Object} node
* @returns {Boolean}
*/
bindingIdentifierEquals(name, node) {
bindingIdentifierEquals(name: string, node: Object): boolean {
return this.getBindingIdentifier(name) === node;
}
/**
* Description
*
* @param {String} name
*/
getBindingInfo(name) {
getBindingInfo(name: string) {
var scope = this;
do {
@ -750,53 +677,43 @@ export default class Scope {
/**
* Description
*
* @param {String} name
*/
getOwnBindingInfo(name) {
getOwnBindingInfo(name: string) {
return this.bindings[name];
}
/**
* Description
*
* @param {String} name
*/
getBindingIdentifier(name) {
getBindingIdentifier(name: string) {
var info = this.getBindingInfo(name);
return info && info.identifier;
}
/**
* Description
*
* @param {String} name
*/
getOwnBindingIdentifier(name) {
getOwnBindingIdentifier(name: string) {
var binding = this.bindings[name];
return binding && binding.identifier;
}
/**
* Description
*
* @param {String} name
*/
getOwnImmutableBindingValue(name) {
getOwnImmutableBindingValue(name: string) {
return this._immutableBindingInfoToValue(this.getOwnBindingInfo(name));
}
/**
* Description
*
* @param {String} name
*/
getImmutableBindingValue(name) {
getImmutableBindingValue(name: string) {
return this._immutableBindingInfoToValue(this.getBindingInfo(name));
}
@ -824,21 +741,17 @@ export default class Scope {
/**
* Description
*
* @param {String} name
*/
hasOwnBinding(name) {
hasOwnBinding(name: string) {
return !!this.getOwnBindingInfo(name);
}
/**
* Description
*
* @param {String} name
*/
hasBinding(name) {
hasBinding(name: string) {
if (!name) return false;
if (this.hasOwnBinding(name)) return true;
if (this.parentHasBinding(name)) return true;
@ -849,31 +762,25 @@ export default class Scope {
/**
* Description
*
* @param {String} name
*/
parentHasBinding(name) {
parentHasBinding(name: string) {
return this.parent && this.parent.hasBinding(name);
}
/**
* Description
*
* @param {String} name
*/
removeOwnBinding(name) {
removeOwnBinding(name: string) {
this.bindings[name] = null;
}
/**
* Description
*
* @param {String} name
*/
removeBinding(name) {
removeBinding(name: string) {
var info = this.getBindingInfo(name);
if (info) info.scope.removeOwnBinding(name);
}

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);
};
@ -67,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;
@ -133,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);
}
@ -152,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) {
@ -186,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++) {
@ -208,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;
@ -224,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
@ -347,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 + "";
@ -401,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;
@ -429,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) {
@ -462,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) {
@ -518,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;
}
@ -560,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;
}
@ -585,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;
}
@ -618,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();
@ -678,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;
};
@ -715,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];
});
@ -729,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])));
});
@ -744,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;
@ -767,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) {
@ -795,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 {
@ -821,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;
@ -854,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
@ -897,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;
};
@ -920,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);
@ -1017,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,10 +56,10 @@ 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;
return !!val;
}
var templateVisitor = {
@ -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;