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

Commit

Permalink
[Flow] src/utils/function.js (#5904)
Browse files Browse the repository at this point in the history
  • Loading branch information
atwalg2 authored and jasonLaster committed Apr 24, 2018
1 parent e25f418 commit 6b2ac3a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 26 deletions.
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;
}

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

0 comments on commit 6b2ac3a

Please sign in to comment.