add flow type annotations
This commit is contained in:
@@ -1,20 +1,12 @@
|
||||
/* @flow */
|
||||
|
||||
import whitespace from "./whitespace";
|
||||
import * as parens from "./parentheses";
|
||||
import each from "lodash/collection/each";
|
||||
import some from "lodash/collection/some";
|
||||
import * as t from "babel-types";
|
||||
|
||||
/**
|
||||
* Test if node matches a set of type-matcher pairs.
|
||||
* @example
|
||||
* find({
|
||||
* VariableDeclaration(node, parent) {
|
||||
* return true;
|
||||
* }
|
||||
* }, node, parent);
|
||||
*/
|
||||
|
||||
let find = function (obj, node, parent) {
|
||||
function find(obj, node, parent) {
|
||||
if (!obj) return;
|
||||
let result;
|
||||
|
||||
@@ -30,30 +22,21 @@ let find = function (obj, node, parent) {
|
||||
}
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
/**
|
||||
* Whitespace and Parenthesis related methods for nodes.
|
||||
*/
|
||||
}
|
||||
|
||||
export default class Node {
|
||||
constructor(node, parent) {
|
||||
constructor(node: Object, parent: Object) {
|
||||
this.parent = parent;
|
||||
this.node = node;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if `node` can have whitespace set by the user.
|
||||
*/
|
||||
parent: Object;
|
||||
node: Object;
|
||||
|
||||
static isUserWhitespacable(node) {
|
||||
return t.isUserWhitespacable(node);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if a `node` requires whitespace.
|
||||
*/
|
||||
|
||||
static needsWhitespace(node, parent, type) {
|
||||
if (!node) return 0;
|
||||
|
||||
@@ -76,26 +59,14 @@ export default class Node {
|
||||
return (linesInfo && linesInfo[type]) || 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if a `node` requires whitespace before it.
|
||||
*/
|
||||
|
||||
static needsWhitespaceBefore(node, parent) {
|
||||
return Node.needsWhitespace(node, parent, "before");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if a `note` requires whitespace after it.
|
||||
*/
|
||||
|
||||
static needsWhitespaceAfter(node, parent) {
|
||||
return Node.needsWhitespace(node, parent, "after");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if a `node` needs parentheses around it.
|
||||
*/
|
||||
|
||||
static needsParens(node, parent) {
|
||||
if (!parent) return false;
|
||||
|
||||
@@ -112,10 +83,6 @@ export default class Node {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add all static methods from `Node` to `Node.prototype`.
|
||||
*/
|
||||
|
||||
each(Node, function (fn, key) {
|
||||
Node.prototype[key] = function () {
|
||||
// Avoid leaking arguments to prevent deoptimization
|
||||
|
||||
@@ -1,40 +1,50 @@
|
||||
import each from "lodash/collection/each";
|
||||
/* @flow */
|
||||
|
||||
import * as t from "babel-types";
|
||||
|
||||
const PRECEDENCE = {};
|
||||
const PRECEDENCE = {
|
||||
"||": 0,
|
||||
"&&": 1,
|
||||
"|": 2,
|
||||
"^": 3,
|
||||
"&": 4,
|
||||
"==": 5,
|
||||
"===": 5,
|
||||
"!=": 5,
|
||||
"!==": 5,
|
||||
"<": 6,
|
||||
">": 6,
|
||||
"<=": 6,
|
||||
">=": 6,
|
||||
in: 6,
|
||||
instanceof: 6,
|
||||
">>": 7,
|
||||
"<<": 7,
|
||||
">>>": 7,
|
||||
"+": 8,
|
||||
"-": 8,
|
||||
"*": 9,
|
||||
"/": 9,
|
||||
"%": 9,
|
||||
"**": 10
|
||||
};
|
||||
|
||||
each([
|
||||
["||"],
|
||||
["&&"],
|
||||
["|"],
|
||||
["^"],
|
||||
["&"],
|
||||
["==", "===", "!=", "!=="],
|
||||
["<", ">", "<=", ">=", "in", "instanceof"],
|
||||
[">>", "<<", ">>>"],
|
||||
["+", "-"],
|
||||
["*", "/", "%"],
|
||||
["**"]
|
||||
], function (tier, i) {
|
||||
each(tier, function (op) {
|
||||
PRECEDENCE[op] = i;
|
||||
});
|
||||
});
|
||||
|
||||
export function NullableTypeAnnotation(node, parent) {
|
||||
export function NullableTypeAnnotation(node: Object, parent: Object): boolean {
|
||||
return t.isArrayTypeAnnotation(parent);
|
||||
}
|
||||
|
||||
export { NullableTypeAnnotation as FunctionTypeAnnotation };
|
||||
|
||||
export function UpdateExpression(node, parent) {
|
||||
export function UpdateExpression(node: Object, parent: Object): boolean {
|
||||
if (t.isMemberExpression(parent) && parent.object === node) {
|
||||
// (foo++).test()
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
export function ObjectExpression(node, parent) {
|
||||
export function ObjectExpression(node: Object, parent: Object): boolean {
|
||||
if (t.isExpressionStatement(parent)) {
|
||||
// ({ foo: "bar" });
|
||||
return true;
|
||||
@@ -48,7 +58,7 @@ export function ObjectExpression(node, parent) {
|
||||
return false;
|
||||
}
|
||||
|
||||
export function Binary(node, parent) {
|
||||
export function Binary(node: Object, parent: Object): boolean {
|
||||
if ((t.isCallExpression(parent) || t.isNewExpression(parent)) && parent.callee === node) {
|
||||
return true;
|
||||
}
|
||||
@@ -76,9 +86,11 @@ export function Binary(node, parent) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
export function BinaryExpression(node, parent) {
|
||||
export function BinaryExpression(node: Object, parent: Object): boolean {
|
||||
if (node.operator === "in") {
|
||||
// let i = (1 in []);
|
||||
if (t.isVariableDeclarator(parent)) {
|
||||
@@ -90,11 +102,13 @@ export function BinaryExpression(node, parent) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
export function SequenceExpression(node, parent) {
|
||||
export function SequenceExpression(node: Object, parent: Object): boolean {
|
||||
if (t.isForStatement(parent)) {
|
||||
// Although parentheses wouldn't hurt around sequence
|
||||
// Although parentheses wouldn"t hurt around sequence
|
||||
// expressions in the head of for loops, traditional style
|
||||
// dictates that e.g. i++, j++ should not be wrapped with
|
||||
// parentheses.
|
||||
@@ -110,7 +124,7 @@ export function SequenceExpression(node, parent) {
|
||||
return true;
|
||||
}
|
||||
|
||||
export function YieldExpression(node, parent) {
|
||||
export function YieldExpression(node: Object, parent: Object): boolean {
|
||||
return t.isBinary(parent) ||
|
||||
t.isUnaryLike(parent) ||
|
||||
t.isCallExpression(parent) ||
|
||||
@@ -120,7 +134,7 @@ export function YieldExpression(node, parent) {
|
||||
t.isYieldExpression(parent);
|
||||
}
|
||||
|
||||
export function ClassExpression(node, parent) {
|
||||
export function ClassExpression(node: Object, parent: Object): boolean {
|
||||
// (class {});
|
||||
if (t.isExpressionStatement(parent)) {
|
||||
return true;
|
||||
@@ -130,13 +144,15 @@ export function ClassExpression(node, parent) {
|
||||
if (t.isExportDeclaration(parent)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
export function UnaryLike(node, parent) {
|
||||
export function UnaryLike(node: Object, parent: Object): boolean {
|
||||
return t.isMemberExpression(parent) && parent.object === node;
|
||||
}
|
||||
|
||||
export function FunctionExpression(node, parent) {
|
||||
export function FunctionExpression(node: Object, parent: Object): boolean {
|
||||
// (function () {});
|
||||
if (t.isExpressionStatement(parent)) {
|
||||
return true;
|
||||
@@ -156,9 +172,11 @@ export function FunctionExpression(node, parent) {
|
||||
if (t.isCallExpression(parent) && parent.callee === node) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
export function ConditionalExpression(node, parent) {
|
||||
export function ConditionalExpression(node: Object, parent: Object): boolean {
|
||||
if (t.isUnaryLike(parent)) {
|
||||
return true;
|
||||
}
|
||||
@@ -184,7 +202,7 @@ export function ConditionalExpression(node, parent) {
|
||||
return false;
|
||||
}
|
||||
|
||||
export function AssignmentExpression(node) {
|
||||
export function AssignmentExpression(node: Object): boolean {
|
||||
if (t.isObjectPattern(node.left)) {
|
||||
return true;
|
||||
} else {
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
/* @flow */
|
||||
|
||||
/**
|
||||
* Printer for nodes, needs a `generator` and a `parent`.
|
||||
*/
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
/* @flow */
|
||||
|
||||
import isBoolean from "lodash/lang/isBoolean";
|
||||
import each from "lodash/collection/each";
|
||||
import map from "lodash/collection/map";
|
||||
|
||||
Reference in New Issue
Block a user