diff --git a/packages/babel-traverse/src/path/family.js b/packages/babel-traverse/src/path/family.js index 6ef3f4535cb6..0af3d2b110cb 100644 --- a/packages/babel-traverse/src/path/family.js +++ b/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"; @@ -17,7 +18,7 @@ function addCompletionRecords(path, paths) { return paths; } -export function getCompletionRecords(): Array { +export function getCompletionRecords(): NodePath[] { let paths = []; if (this.isIfStatement()) { @@ -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, @@ -60,10 +61,10 @@ export function getNextSibling(): NodePath { return this.getSibling(this.key + 1); } -export function getAllNextSiblings(): Array { +export function getAllNextSiblings(): NodePath[] { let _key = this.key; - let sibling: NodePath = this.getSibling(++_key); - const siblings: Array = []; + let sibling = this.getSibling(++_key); + const siblings = []; while (sibling.node) { siblings.push(sibling); sibling = this.getSibling(++_key); @@ -71,10 +72,10 @@ export function getAllNextSiblings(): Array { return siblings; } -export function getAllPrevSiblings(): Array { +export function getAllPrevSiblings(): NodePath[] { let _key = this.key; - let sibling: NodePath = this.getSibling(--_key); - const siblings: Array = []; + let sibling = this.getSibling(--_key); + const siblings = []; while (sibling.node) { siblings.push(sibling); sibling = this.getSibling(--_key); @@ -85,7 +86,7 @@ export function getAllPrevSiblings(): Array { 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) { @@ -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]; @@ -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 { @@ -138,11 +145,11 @@ 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); } @@ -150,9 +157,9 @@ export function getOuterBindingIdentifiers(duplicates?): Object { // 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); @@ -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); }