Fix flow types in traverse/path/family and enable flow (#9870)

This commit is contained in:
Daniel Tschinder 2019-04-23 07:46:55 -07:00 committed by Henry Zhu
parent 4198d91b89
commit bf17871b82

View File

@ -1,3 +1,4 @@
// @flow
// This file contains methods responsible for dealing with/retrieving children or siblings. // This file contains methods responsible for dealing with/retrieving children or siblings.
import type TraversalContext from "../index"; import type TraversalContext from "../index";
@ -17,7 +18,7 @@ function addCompletionRecords(path, paths) {
return paths; return paths;
} }
export function getCompletionRecords(): Array { export function getCompletionRecords(): NodePath[] {
let paths = []; let paths = [];
if (this.isIfStatement()) { if (this.isIfStatement()) {
@ -42,7 +43,7 @@ export function getCompletionRecords(): Array {
return paths; return paths;
} }
export function getSibling(key): NodePath { export function getSibling(key: string): NodePath {
return NodePath.get({ return NodePath.get({
parentPath: this.parentPath, parentPath: this.parentPath,
parent: this.parent, parent: this.parent,
@ -60,10 +61,10 @@ export function getNextSibling(): NodePath {
return this.getSibling(this.key + 1); return this.getSibling(this.key + 1);
} }
export function getAllNextSiblings(): Array<NodePath> { export function getAllNextSiblings(): NodePath[] {
let _key = this.key; let _key = this.key;
let sibling: NodePath = this.getSibling(++_key); let sibling = this.getSibling(++_key);
const siblings: Array<NodePath> = []; const siblings = [];
while (sibling.node) { while (sibling.node) {
siblings.push(sibling); siblings.push(sibling);
sibling = this.getSibling(++_key); sibling = this.getSibling(++_key);
@ -71,10 +72,10 @@ export function getAllNextSiblings(): Array<NodePath> {
return siblings; return siblings;
} }
export function getAllPrevSiblings(): Array<NodePath> { export function getAllPrevSiblings(): NodePath[] {
let _key = this.key; let _key = this.key;
let sibling: NodePath = this.getSibling(--_key); let sibling = this.getSibling(--_key);
const siblings: Array<NodePath> = []; const siblings = [];
while (sibling.node) { while (sibling.node) {
siblings.push(sibling); siblings.push(sibling);
sibling = this.getSibling(--_key); sibling = this.getSibling(--_key);
@ -85,7 +86,7 @@ export function getAllPrevSiblings(): Array<NodePath> {
export function get( export function get(
key: string, key: string,
context?: boolean | TraversalContext, context?: boolean | TraversalContext,
): NodePath { ): NodePath | NodePath[] {
if (context === true) context = this.context; if (context === true) context = this.context;
const parts = key.split("."); const parts = key.split(".");
if (parts.length === 1) { if (parts.length === 1) {
@ -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 node = this.node;
const container = node[key]; const container = node[key];
@ -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; let path = this;
for (const part of (parts: Array)) { for (const part of parts) {
if (part === ".") { if (part === ".") {
path = path.parentPath; path = path.parentPath;
} else { } else {
@ -138,11 +145,11 @@ export function _getPattern(parts, context) {
return path; return path;
} }
export function getBindingIdentifiers(duplicates?): Object { export function getBindingIdentifiers(duplicates?: boolean): Object {
return t.getBindingIdentifiers(this.node, duplicates); return t.getBindingIdentifiers(this.node, duplicates);
} }
export function getOuterBindingIdentifiers(duplicates?): Object { export function getOuterBindingIdentifiers(duplicates?: boolean): Object {
return t.getOuterBindingIdentifiers(this.node, duplicates); return t.getOuterBindingIdentifiers(this.node, duplicates);
} }
@ -150,9 +157,9 @@ export function getOuterBindingIdentifiers(duplicates?): Object {
// path.getBindingIdentifiers returns nodes where the following re-implementation // path.getBindingIdentifiers returns nodes where the following re-implementation
// returns paths // returns paths
export function getBindingIdentifierPaths( export function getBindingIdentifierPaths(
duplicates = false, duplicates?: boolean = false,
outerOnly = false, outerOnly?: boolean = false,
) { ): { [string]: NodePath } {
const path = this; const path = this;
let search = [].concat(path); let search = [].concat(path);
const ids = Object.create(null); const ids = Object.create(null);
@ -203,9 +210,12 @@ export function getBindingIdentifierPaths(
} }
} }
// $FlowIssue Object.create() is object type
return ids; return ids;
} }
export function getOuterBindingIdentifierPaths(duplicates?) { export function getOuterBindingIdentifierPaths(
duplicates?: boolean,
): { [string]: NodePath } {
return this.getBindingIdentifierPaths(duplicates, true); return this.getBindingIdentifierPaths(duplicates, true);
} }