Skip to content

Commit

Permalink
Merge pull request #3186 from hzoo/add-some-flow
Browse files Browse the repository at this point in the history
add some more flow types
  • Loading branch information
hzoo committed Feb 4, 2016
2 parents 7d2ec4a + f59faee commit 807e190
Show file tree
Hide file tree
Showing 22 changed files with 150 additions and 70 deletions.
4 changes: 3 additions & 1 deletion .flowconfig
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[ignore]
.*/packages/.*/lib

[include]

Expand All @@ -10,6 +11,7 @@ lib/types.js
[options]
strip_root=true
suppress_comment= \\(.\\|\n\\)*\\$FlowFixMe
suppress_comment= \\(.\\|\n\\)*\\$FlowIssue

[version]
0.20.1
0.21.0
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ test-cov: clean
test-ci:
make lint
NODE_ENV=test make bootstrap
./node_modules/.bin/flow check
node scripts/run-flow-check.js
./scripts/test-cov.sh
cat ./coverage/coverage.json | ./node_modules/codecov.io/bin/codecov.io.js

Expand Down
78 changes: 56 additions & 22 deletions lib/types.js

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@
"devDependencies": {
"async": "^1.5.0",
"babel": "5.8.21",
"babel-eslint": "^4.0.6",
"babel-eslint": "^4.1.8",
"babel-plugin-flow-comments": "^1.0.9",
"bin-version-check": "^2.1.0",
"browserify": "^11.2.0",
"bundle-collapser": "^1.2.1",
"chai": "^2.2.0",
"chalk": "1.1.1",
"codecov.io": "^0.1.6",
"derequire": "^2.0.2",
"es5-shim": "^4.1.7",
"eslint": "1.8.0",
"flow-bin": "^0.20.0",
"eslint": "~1.10.3",
"flow-bin": "^0.21.0",
"fs-readdir-recursive": "^0.1.2",
"gulp": "^3.9.0",
"gulp-babel": "^5.3.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-core/src/helpers/normalize-ast.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* @noflow */
/* @flow */

import * as t from "babel-types";

Expand Down
2 changes: 1 addition & 1 deletion packages/babel-core/src/transformation/file/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ export default class File extends Store {
}
}
makeResult({ code, map, ast, ignored } /*: BabelFileResult */): BabelFileResult {
makeResult({ code, map, ast, ignored }: BabelFileResult): BabelFileResult {
let result = {
metadata: null,
options: this.opts,
Expand Down
14 changes: 8 additions & 6 deletions packages/babel-core/src/transformation/pipeline.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
/* @noflow */
/* @flow */
/* global BabelFileResult */
/* global BabelFileMetadata */

import normalizeAst from "../helpers/normalize-ast";
import Plugin from "./plugin";
import File from "./file";

export default class Pipeline {
lint(code: string, opts?: Object = {}) {
lint(code: string, opts?: Object = {}): BabelFileResult {
opts.code = false;
opts.mode = "lint";
return this.transform(code, opts);
}

pretransform(code: string, opts?: Object) {
pretransform(code: string, opts?: Object): BabelFileResult {
let file = new File(opts, this);
return file.wrap(code, function () {
file.addCode(code);
Expand All @@ -20,7 +22,7 @@ export default class Pipeline {
});
}

transform(code: string, opts?: Object) {
transform(code: string, opts?: Object): BabelFileResult {
let file = new File(opts, this);
return file.wrap(code, function () {
file.addCode(code);
Expand All @@ -29,7 +31,7 @@ export default class Pipeline {
});
}

analyse(code: string, opts: Object = {}, visitor?) {
analyse(code: string, opts: Object = {}, visitor?: Object): ?BabelFileMetadata {
opts.code = false;
if (visitor) {
opts.plugins = opts.plugins || [];
Expand All @@ -38,7 +40,7 @@ export default class Pipeline {
return this.transform(code, opts).metadata;
}

transformFromAst(ast, code: string, opts: Object) {
transformFromAst(ast: Object, code: string, opts: Object): BabelFileResult {
ast = normalizeAst(ast);

let file = new File(opts, this);
Expand Down
4 changes: 2 additions & 2 deletions packages/babel-core/src/util.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* @noflow */
/* @flow */

import escapeRegExp from "lodash/string/escapeRegExp";
import startsWith from "lodash/string/startsWith";
Expand All @@ -16,7 +16,7 @@ export { inherits, inspect } from "util";
* Test if a filename ends with a compilable extension.
*/

export function canCompile(filename: string, altExts?: Array<string>) {
export function canCompile(filename: string, altExts?: Array<string>): boolean {
let exts = altExts || canCompile.EXTENSIONS;
let ext = path.extname(filename);
return contains(exts, ext);
Expand Down
22 changes: 13 additions & 9 deletions packages/babel-generator/src/buffer.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* @flow */

import type Position from "./position";
import repeating from "repeating";
import trimRight from "trim-right";
Expand All @@ -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) {
Expand All @@ -44,15 +48,15 @@ export default class Buffer {
* Get the current trimmed buffer.
*/

get() {
get(): string {
return trimRight(this.buf);
}

/**
* Get the current indent.
*/

getIndent() {
getIndent(): string {
if (this.format.compact || this.format.concise) {
return "";
} else {
Expand All @@ -64,7 +68,7 @@ export default class Buffer {
* Get the current indent size.
*/

indentSize() {
indentSize(): number {
return this.getIndent().length;
}

Expand Down Expand Up @@ -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";
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)) {
Expand Down
2 changes: 2 additions & 0 deletions packages/babel-generator/src/generators/jsx.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* @flow */

export function JSXAttribute(node: Object) {
this.print(node.name, node);
if (node.value) {
Expand Down
2 changes: 2 additions & 0 deletions packages/babel-generator/src/generators/statements.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* @flow */

import repeating from "repeating";
import * as t from "babel-types";

Expand Down
29 changes: 18 additions & 11 deletions packages/babel-generator/src/node/whitespace.js
Original file line number Diff line number Diff line change
@@ -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.
*
Expand Down Expand Up @@ -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 {
Expand All @@ -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
};
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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,
Expand All @@ -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];

Expand All @@ -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,
Expand All @@ -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
Expand All @@ -181,23 +188,23 @@ exports.list = {
* Return VariableDeclaration declarations init properties.
*/

VariableDeclaration(node) {
VariableDeclaration(node: Object): Array<Object> {
return map(node.declarations, "init");
},

/**
* Return VariableDeclaration elements.
*/

ArrayExpression(node) {
ArrayExpression(node: Object): Array<Object> {
return node.elements;
},

/**
* Return VariableDeclaration properties.
*/

ObjectExpression(node) {
ObjectExpression(node: Object): Array<Object> {
return node.properties;
}
};
Expand Down
6 changes: 3 additions & 3 deletions packages/babel-traverse/src/path/lib/virtual-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ export let ReferencedMemberExpression = {

export let BindingIdentifier = {
types: ["Identifier"],
checkPath({ node, parent } /*: NodePath */): boolean {
checkPath({ node, parent }: NodePath): boolean {
return t.isIdentifier(node) && t.isBinding(node, parent);
}
};

export let Statement = {
types: ["Statement"],
checkPath({ node, parent } /*: NodePath */): boolean {
checkPath({ node, parent }: NodePath): boolean {
if (t.isStatement(node)) {
if (t.isVariableDeclaration(node)) {
if (t.isForXStatement(parent, { left: node })) return false;
Expand Down Expand Up @@ -106,7 +106,7 @@ export let Pure = {

export let Flow = {
types: ["Flow", "ImportDeclaration", "ExportDeclaration"],
checkPath({ node } /*: NodePath */): boolean {
checkPath({ node }: NodePath): boolean {
if (t.isFlow(node)) {
return true;
} else if (t.isImportDeclaration(node)) {
Expand Down
4 changes: 3 additions & 1 deletion packages/babel-types/src/converters.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* @noflow */

import isPlainObject from "lodash/lang/isPlainObject";
import isNumber from "lodash/lang/isNumber";
import isRegExp from "lodash/lang/isRegExp";
Expand Down Expand Up @@ -218,7 +220,7 @@ export function toExpression(node: Object): Object {
}
}

export function toBlock(node, parent: Object): Object {
export function toBlock(node: Object, parent: Object): Object {
if (t.isBlockStatement(node)) {
return node;
}
Expand Down
4 changes: 3 additions & 1 deletion packages/babel-types/src/flow.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* @flow */

import * as t from "./index";

/**
Expand All @@ -19,7 +21,7 @@ export function createUnionTypeAnnotation(types: Array<Object>) {
* Dedupe type annotations.
*/

export function removeTypeDuplicates(nodes: Array<Object>) {
export function removeTypeDuplicates(nodes: Array<Object>): Array<Object> {
let generics = {};
let bases = {};

Expand Down

0 comments on commit 807e190

Please sign in to comment.