Skip to content
This repository has been archived by the owner on Jan 11, 2023. It is now read-only.

[Flow] src/utils/function.js #5904

Merged
merged 1 commit into from
Apr 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 20 additions & 21 deletions src/utils/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,20 @@ import { xor, range } from "lodash";
import { convertToList } from "./pause/pausePoints";

import type { Location, Source, ColumnPosition } from "../types";
import type { Symbols } from "../reducers/ast";

import type {
AstPosition,
AstLocation,
PausePoints,
SymbolDeclarations
} from "../workers/parser";
import type { AstPosition, AstLocation, PausePoints } from "../workers/parser";

export function findBestMatchExpression(
symbols: SymbolDeclarations,
symbols: Symbols,
tokenPos: ColumnPosition
) {
const { memberExpressions, identifiers, literals } = symbols;
const { line, column } = tokenPos;
if (symbols.loading) {
return null;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jasonLaster hey Jason so I think having if(symbols.loading) return null make sense in this case. What do you think?

Copy link
Contributor

@jasonLaster jasonLaster Apr 20, 2018

Choose a reason for hiding this comment

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

yes. that definitely works

const { line, column } = tokenPos;
const { memberExpressions, identifiers, literals } = symbols;
const members = memberExpressions.filter(({ computed }) => !computed);

return []
Expand Down Expand Up @@ -107,18 +106,18 @@ function findClosestofSymbol(declarations: any[], location: Location) {
}, null);
}

export function findClosestFunction(
symbols: SymbolDeclarations,
location: Location
) {
const { functions } = symbols;
return findClosestofSymbol(functions, location);
export function findClosestFunction(symbols: ?Symbols, location: Location) {
if (!symbols || symbols.loading) {
return null;
}

return findClosestofSymbol(symbols.functions, location);
}

export function findClosestClass(
symbols: SymbolDeclarations,
location: Location
) {
const { classes } = symbols;
return findClosestofSymbol(classes, location);
export function findClosestClass(symbols: Symbols, location: Location) {
if (!symbols || symbols.loading) {
return null;
}

return findClosestofSymbol(symbols.functions, location);
}
17 changes: 12 additions & 5 deletions src/utils/function.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,28 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */

// @flow
import { findClosestFunction } from "./ast";
import { correctIndentation } from "./indentation";
import type { Source } from "../types";
import type { Symbols } from "../reducers/ast";

export function findFunctionText(line, source, symbols) {
export function findFunctionText(
line: number,
source: Source,
symbols: ?Symbols
): ?string {
const func = findClosestFunction(symbols, {
sourceId: source.id,
line,
column: Infinity
});
if (!func) {

if (!func || !source.text) {
return null;
}

const {
location: { start, end }
} = func;
const { location: { start, end } } = func;
const lines = source.text.split("\n");
const firstLine = lines[start.line - 1].slice(start.column);
const lastLine = lines[end.line - 1].slice(0, end.column);
Expand Down