Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from Apr 23, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't using const id: { [string]: NodePath } = Object.create(null); above work?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No flow says that the result of Object.create() is not compatible to object type. facebook/flow#4967

return ids;
}

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