add some more flow types

This commit is contained in:
Henry Zhu
2015-12-18 01:19:06 -05:00
parent effaf820c3
commit c2d7e95e1a
19 changed files with 75 additions and 45 deletions

View File

@@ -1,3 +1,5 @@
/* @flow */
import type Position from "./position";
import repeating from "repeating";
import trimRight from "trim-right";
@@ -21,17 +23,19 @@ export default class Buffer {
this.last = "";
}
printedCommentStarts: Object;
parenPushNewlineState: ?Object;
buf: string;
position: Position;
_indent: number;
format: Object;
buf: string;
last: string;
/**
* Description
*/
catchUp(node) {
catchUp(node: Object) {
// catch up to this nodes newline if we're behind
if (node.loc && this.format.retainLines && this.buf) {
while (this.position.line < node.loc.start.line) {
@@ -44,7 +48,7 @@ export default class Buffer {
* Get the current trimmed buffer.
*/
get() {
get(): string {
return trimRight(this.buf);
}
@@ -52,7 +56,7 @@ export default class Buffer {
* Get the current indent.
*/
getIndent() {
getIndent(): string {
if (this.format.compact || this.format.concise) {
return "";
} else {
@@ -64,7 +68,7 @@ export default class Buffer {
* Get the current indent size.
*/
indentSize() {
indentSize(): number {
return this.getIndent().length;
}
@@ -222,7 +226,7 @@ export default class Buffer {
_removeSpacesAfterLastNewline() {
let lastNewlineIndex = this.buf.lastIndexOf("\n");
if (lastNewlineIndex >= 0 && this.buf.trimRight().length <= lastNewlineIndex) {
if (lastNewlineIndex >= 0 && this.get().length <= lastNewlineIndex) {
this.buf = this.buf.substring(0, lastNewlineIndex + 1);
this.last = "\n";
}
@@ -251,7 +255,7 @@ export default class Buffer {
* Push a string to the buffer.
*/
_push(str) {
_push(str: string): void {
// see startTerminatorless() instance method
let parenPushNewlineState = this.parenPushNewlineState;
if (parenPushNewlineState) {
@@ -296,12 +300,12 @@ export default class Buffer {
* Test if a character is last in the buffer.
*/
isLast(cha: string) {
isLast(cha: string): boolean {
if (this.format.compact) return false;
return this._isLast(cha);
}
_isLast(cha: string) {
_isLast(cha: string): boolean {
let last = this.last;
if (Array.isArray(cha)) {

View File

@@ -1,3 +1,5 @@
/* @flow */
export function JSXAttribute(node: Object) {
this.print(node.name, node);
if (node.value) {

View File

@@ -1,3 +1,5 @@
/* @flow */
import repeating from "repeating";
import * as t from "babel-types";

View File

@@ -1,8 +1,15 @@
/* @noflow */
import isBoolean from "lodash/lang/isBoolean";
import each from "lodash/collection/each";
import map from "lodash/collection/map";
import * as t from "babel-types";
type WhitespaceObject = {
before?: boolean,
after?: boolean
};
/**
* Crawl a node to test if it contains a CallExpression, a Function, or a Helper.
*
@@ -63,7 +70,7 @@ exports.nodes = {
* Test if AssignmentExpression needs whitespace.
*/
AssignmentExpression(node) {
AssignmentExpression(node: Object): ?WhitespaceObject {
let state = crawl(node.right);
if ((state.hasCall && state.hasHelper) || state.hasFunction) {
return {
@@ -77,7 +84,7 @@ exports.nodes = {
* Test if SwitchCase needs whitespace.
*/
SwitchCase(node, parent) {
SwitchCase(node: Object, parent: Object): ?WhitespaceObject {
return {
before: node.consequent.length || parent.cases[0] === node
};
@@ -87,7 +94,7 @@ exports.nodes = {
* Test if LogicalExpression needs whitespace.
*/
LogicalExpression(node) {
LogicalExpression(node: Object): ?WhitespaceObject {
if (t.isFunction(node.left) || t.isFunction(node.right)) {
return {
after: true
@@ -99,7 +106,7 @@ exports.nodes = {
* Test if Literal needs whitespace.
*/
Literal(node) {
Literal(node: Object): ?WhitespaceObject {
if (node.value === "use strict") {
return {
after: true
@@ -111,7 +118,7 @@ exports.nodes = {
* Test if CallExpression needs whitespace.
*/
CallExpression(node) {
CallExpression(node: Object): ?WhitespaceObject {
if (t.isFunction(node.callee) || isHelper(node)) {
return {
before: true,
@@ -124,7 +131,7 @@ exports.nodes = {
* Test if VariableDeclaration needs whitespace.
*/
VariableDeclaration(node) {
VariableDeclaration(node: Object): ?WhitespaceObject {
for (let i = 0; i < node.declarations.length; i++) {
let declar = node.declarations[i];
@@ -147,7 +154,7 @@ exports.nodes = {
* Test if IfStatement needs whitespace.
*/
IfStatement(node) {
IfStatement(node: Object): ?WhitespaceObject {
if (t.isBlockStatement(node.consequent)) {
return {
before: true,
@@ -163,7 +170,7 @@ exports.nodes = {
exports.nodes.ObjectProperty =
exports.nodes.ObjectMethod =
exports.nodes.SpreadProperty = function (node, parent) {
exports.nodes.SpreadProperty = function (node: Object, parent): ?WhitespaceObject {
if (parent.properties[0] === node) {
return {
before: true
@@ -181,7 +188,7 @@ exports.list = {
* Return VariableDeclaration declarations init properties.
*/
VariableDeclaration(node) {
VariableDeclaration(node: Object): Array<Object> {
return map(node.declarations, "init");
},
@@ -189,7 +196,7 @@ exports.list = {
* Return VariableDeclaration elements.
*/
ArrayExpression(node) {
ArrayExpression(node: Object): Array<Object> {
return node.elements;
},
@@ -197,7 +204,7 @@ exports.list = {
* Return VariableDeclaration properties.
*/
ObjectExpression(node) {
ObjectExpression(node: Object): Array<Object> {
return node.properties;
}
};