Skip to content

Commit

Permalink
Fix flow types in traverse/path/family and enable flow (#9870)
Browse files Browse the repository at this point in the history
  • Loading branch information
danez authored and hzoo committed Apr 23, 2019
1 parent 4198d91 commit bf17871
Showing 1 changed file with 28 additions and 18 deletions.
46 changes: 28 additions & 18 deletions packages/babel-traverse/src/path/family.js
@@ -1,3 +1,4 @@
// @flow
// This file contains methods responsible for dealing with/retrieving children or siblings.

import type TraversalContext from "../index";
Expand All @@ -17,7 +18,7 @@ function addCompletionRecords(path, paths) {
return paths;
}

export function getCompletionRecords(): Array {
export function getCompletionRecords(): NodePath[] {
let paths = [];

if (this.isIfStatement()) {
Expand All @@ -42,7 +43,7 @@ export function getCompletionRecords(): Array {
return paths;
}

export function getSibling(key): NodePath {
export function getSibling(key: string): NodePath {
return NodePath.get({
parentPath: this.parentPath,
parent: this.parent,
Expand All @@ -60,21 +61,21 @@ export function getNextSibling(): NodePath {
return this.getSibling(this.key + 1);
}

export function getAllNextSiblings(): Array<NodePath> {
export function getAllNextSiblings(): NodePath[] {
let _key = this.key;
let sibling: NodePath = this.getSibling(++_key);
const siblings: Array<NodePath> = [];
let sibling = this.getSibling(++_key);
const siblings = [];
while (sibling.node) {
siblings.push(sibling);
sibling = this.getSibling(++_key);
}
return siblings;
}

export function getAllPrevSiblings(): Array<NodePath> {
export function getAllPrevSiblings(): NodePath[] {
let _key = this.key;
let sibling: NodePath = this.getSibling(--_key);
const siblings: Array<NodePath> = [];
let sibling = this.getSibling(--_key);
const siblings = [];
while (sibling.node) {
siblings.push(sibling);
sibling = this.getSibling(--_key);
Expand All @@ -85,7 +86,7 @@ export function getAllPrevSiblings(): Array<NodePath> {
export function get(
key: string,
context?: boolean | TraversalContext,
): NodePath {
): NodePath | NodePath[] {
if (context === true) context = this.context;
const parts = key.split(".");
if (parts.length === 1) {
Expand All @@ -97,7 +98,10 @@ export function get(
}
}

export function _getKey(key, context?) {
export function _getKey(
key: string,
context?: TraversalContext,
): NodePath | NodePath[] {
const node = this.node;
const container = node[key];

Expand All @@ -122,9 +126,12 @@ export function _getKey(key, context?) {
}
}

export function _getPattern(parts, context) {
export function _getPattern(
parts: string[],
context?: TraversalContext,
): NodePath | NodePath[] {
let path = this;
for (const part of (parts: Array)) {
for (const part of parts) {
if (part === ".") {
path = path.parentPath;
} else {
Expand All @@ -138,21 +145,21 @@ export function _getPattern(parts, context) {
return path;
}

export function getBindingIdentifiers(duplicates?): Object {
export function getBindingIdentifiers(duplicates?: boolean): Object {
return t.getBindingIdentifiers(this.node, duplicates);
}

export function getOuterBindingIdentifiers(duplicates?): Object {
export function getOuterBindingIdentifiers(duplicates?: boolean): Object {
return t.getOuterBindingIdentifiers(this.node, duplicates);
}

// original source - https://github.com/babel/babel/blob/master/packages/babel-types/src/retrievers.js
// path.getBindingIdentifiers returns nodes where the following re-implementation
// returns paths
export function getBindingIdentifierPaths(
duplicates = false,
outerOnly = false,
) {
duplicates?: boolean = false,
outerOnly?: boolean = false,
): { [string]: NodePath } {
const path = this;
let search = [].concat(path);
const ids = Object.create(null);
Expand Down Expand Up @@ -203,9 +210,12 @@ export function getBindingIdentifierPaths(
}
}

// $FlowIssue Object.create() is object type
return ids;
}

export function getOuterBindingIdentifierPaths(duplicates?) {
export function getOuterBindingIdentifierPaths(
duplicates?: boolean,
): { [string]: NodePath } {
return this.getBindingIdentifierPaths(duplicates, true);
}

0 comments on commit bf17871

Please sign in to comment.