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

Commit

Permalink
[Parser] Stop using babel-traverse (#5592)
Browse files Browse the repository at this point in the history
* Remove functions using Babel's Scope APIs.
* Use a substitute NodePath for simpler AST traversal.
  • Loading branch information
loganfsmyth authored and jasonLaster committed Mar 2, 2018
1 parent 00dad2c commit e463161
Show file tree
Hide file tree
Showing 21 changed files with 292 additions and 519 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
"install": "prettier --write package.json"
},
"dependencies": {
"@babel/traverse": "^7.0.0-beta.39",
"@babel/types": "^7.0.0-beta.39",
"babylon": "^7.0.0-beta.39",
"codemirror": "^5.28.0",
Expand Down
2 changes: 1 addition & 1 deletion src/utils/quick-open.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import { endTruncateStr } from "./utils";
import { isPretty, getSourcePath } from "./source";

import type { Location as BabelLocation } from "@babel/traverse";
import type { Location as BabelLocation } from "@babel/types";
import type { SourcesMap } from "../reducers/sources";
import type { QuickOpenType } from "../reducers/quick-open";
import type {
Expand Down
11 changes: 7 additions & 4 deletions src/workers/parser/getScopes/visitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@

import isEmpty from "lodash/isEmpty";
import type { SourceId, Location } from "../../../types";
import type { Node, Location as BabelLocation } from "@babel/traverse";
import * as t from "@babel/types";
import type { BabelNode, TraversalAncestors } from "@babel/types";
import type {
Node,
TraversalAncestors,
Location as BabelLocation
} from "@babel/types";
import { isGeneratedId } from "devtools-source-map";
import getFunctionName from "../utils/getFunctionName";
import { getAst } from "../utils/ast";
Expand Down Expand Up @@ -357,7 +360,7 @@ function createGlobalScope(
const scopeCollectionVisitor = {
// eslint-disable-next-line complexity
enter(
node: BabelNode,
node: Node,
ancestors: TraversalAncestors,
state: ScopeCollectionVisitorState
) {
Expand Down Expand Up @@ -523,7 +526,7 @@ const scopeCollectionVisitor = {
}
},
exit(
node: BabelNode,
node: Node,
ancestors: TraversalAncestors,
state: ScopeCollectionVisitorState
) {
Expand Down
56 changes: 38 additions & 18 deletions src/workers/parser/getSymbols.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@
import flatten from "lodash/flatten";
import * as t from "@babel/types";

import createSimplePath, { type SimplePath } from "./utils/simple-path";
import { traverseAst } from "./utils/ast";
import { isVariable, isFunction, getVariables } from "./utils/helpers";
import { inferClassName } from "./utils/inferClassName";
import getFunctionName from "./utils/getFunctionName";

import type {
NodePath,
Node,
TraversalAncestors,
Location as BabelLocation
} from "@babel/traverse";
} from "@babel/types";

let symbolDeclarations = new Map();

Expand Down Expand Up @@ -53,7 +54,7 @@ export type SymbolDeclarations = {
comments: Array<SymbolDeclaration>
};

function getFunctionParameterNames(path: NodePath): string[] {
function getFunctionParameterNames(path: SimplePath): string[] {
if (path.node.params != null) {
return path.node.params.map(param => {
if (param.type !== "AssignmentPattern") {
Expand Down Expand Up @@ -92,8 +93,8 @@ function getFunctionParameterNames(path: NodePath): string[] {
return [];
}

function getVariableNames(path: NodePath): SymbolDeclaration[] {
if (t.isObjectProperty(path) && !isFunction(path.node.value)) {
function getVariableNames(path: SimplePath): SymbolDeclaration[] {
if (t.isObjectProperty(path.node) && !isFunction(path.node.value)) {
if (path.node.key.type === "StringLiteral") {
return [
{
Expand Down Expand Up @@ -147,14 +148,14 @@ function getSpecifiers(specifiers) {
return specifiers.map(specifier => specifier.local && specifier.local.name);
}

function extractSymbol(path, symbols) {
function extractSymbol(path: SimplePath, symbols) {
if (isVariable(path)) {
symbols.variables.push(...getVariableNames(path));
}

if (isFunction(path)) {
symbols.functions.push({
name: getFunctionName(path.node, path.parentPath.node),
name: getFunctionName(path.node, path.parent),
klass: inferClassName(path),
location: path.node.loc,
parameterNames: getFunctionParameterNames(path),
Expand Down Expand Up @@ -270,9 +271,12 @@ function extractSymbols(sourceId) {
};

const ast = traverseAst(sourceId, {
enter(path: NodePath) {
enter(node: Node, ancestors: TraversalAncestors) {
try {
extractSymbol(path, symbols);
const path = createSimplePath(ancestors);
if (path) {
extractSymbol(path, symbols);
}
} catch (e) {
console.error(e);
}
Expand All @@ -288,9 +292,9 @@ function extractSymbols(sourceId) {
function extendSnippet(
name: string,
expression: string,
path: NodePath,
prevPath: NodePath
) {
path: SimplePath | null = null,
prevPath: SimplePath | null = null
): string | void {
const computed = path && path.node.computed;
const prevComputed = prevPath && prevPath.node.computed;
const prevArray = t.isArrayExpression(prevPath);
Expand Down Expand Up @@ -340,8 +344,8 @@ function getMemberSnippet(node: Node, expression: string = "") {
}

function getObjectSnippet(
path: NodePath,
prevPath: NodePath,
path: SimplePath | null,
prevPath: SimplePath | null,
expression: string = ""
) {
if (!path) {
Expand All @@ -359,11 +363,15 @@ function getObjectSnippet(
}

function getArraySnippet(
path: NodePath,
prevPath: NodePath,
path: SimplePath,
prevPath: SimplePath,
expression: string
) {
const index = prevPath.parentPath.key;
if (!prevPath.parentPath) {
throw new Error("Assertion failure - path should exist");
}

const index = `${prevPath.parentPath.containerIndex}`;
const extendedExpression = extendSnippet(index, expression, path, prevPath);

const nextPrevPath = path;
Expand All @@ -372,7 +380,15 @@ function getArraySnippet(
return getSnippet(nextPath, nextPrevPath, extendedExpression);
}

function getSnippet(path, prevPath, expression = "") {
function getSnippet(
path: SimplePath | null,
prevPath: SimplePath | null = null,
expression = ""
) {
if (!path) {
return expression;
}

if (t.isVariableDeclaration(path)) {
const node = path.node.declarations[0];
const name = node.id.name;
Expand Down Expand Up @@ -423,6 +439,10 @@ function getSnippet(path, prevPath, expression = "") {
}

if (t.isArrayExpression(path)) {
if (!prevPath) {
throw new Error("Assertion failure - path should exist");
}

return getArraySnippet(path, prevPath, expression);
}
}
Expand Down
1 change: 0 additions & 1 deletion src/workers/parser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export const stopParserWorker = dispatcher.stop.bind(dispatcher);
export const getClosestExpression = dispatcher.task("getClosestExpression");
export const getSymbols = dispatcher.task("getSymbols");
export const getScopes = dispatcher.task("getScopes");
export const getVariablesInScope = dispatcher.task("getVariablesInScope");
export const findOutOfScopeLocations = dispatcher.task(
"findOutOfScopeLocations"
);
Expand Down
8 changes: 2 additions & 6 deletions src/workers/parser/pauseLocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import * as t from "@babel/types";

import type { Location } from "../../types";

import { fastTraverseAst } from "./utils/ast";
import { traverseAst } from "./utils/ast";

const STOP = {};

Expand All @@ -19,11 +19,7 @@ export function isInvalidPauseLocation(location: Location) {
};

try {
fastTraverseAst(
location.sourceId,
{ enter: invalidLocationVisitor },
state
);
traverseAst(location.sourceId, { enter: invalidLocationVisitor }, state);
} catch (e) {
if (e !== STOP) {
throw e;
Expand Down
52 changes: 0 additions & 52 deletions src/workers/parser/scopes.js

This file was deleted.

36 changes: 27 additions & 9 deletions src/workers/parser/steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,38 @@
* 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/>. */

import type { Source } from "../../types";
// @flow

import * as t from "@babel/types";
import type { SimplePath } from "./utils/simple-path";
import type { SourceId } from "../../types";
import { AstPosition } from "./types";
import { getClosestPath } from "./utils/closest";
import { isAwaitExpression, isYieldExpression } from "./utils/helpers";
import type { NodePath } from "@babel/traverse";

export function getNextStep(sourceId: Source, pausedPosition: AstPosition) {
export function getNextStep(sourceId: SourceId, pausedPosition: AstPosition) {
const currentExpression = getSteppableExpression(sourceId, pausedPosition);
if (!currentExpression) {
return null;
}
const currentStatement = currentExpression.getStatementParent();

const currentStatement = currentExpression.find(p => {
return p.inList && t.isStatement(p.node);
});

if (!currentStatement) {
throw new Error(
"Assertion failure - this should always find at least Program"
);
}

return _getNextStep(currentStatement, pausedPosition);
}

function getSteppableExpression(sourceId: string, pausedPosition: AstPosition) {
function getSteppableExpression(
sourceId: SourceId,
pausedPosition: AstPosition
) {
const closestPath = getClosestPath(sourceId, pausedPosition);

if (!closestPath) {
Expand All @@ -28,12 +44,14 @@ function getSteppableExpression(sourceId: string, pausedPosition: AstPosition) {
return closestPath;
}

return closestPath.find(p => p.isAwaitExpression() || p.isYieldExpression());
return closestPath.find(
p => t.isAwaitExpression(p.node) || t.isYieldExpression(p.node)
);
}

function _getNextStep(statement: NodePath, position: AstPosition) {
const nextStatement = statement.getSibling(statement.key + 1);
if (nextStatement.node) {
function _getNextStep(statement: SimplePath, position: AstPosition) {
const nextStatement = statement.getSibling(1);
if (nextStatement) {
return {
...nextStatement.node.loc.start,
sourceId: position.sourceId
Expand Down
57 changes: 0 additions & 57 deletions src/workers/parser/tests/__snapshots__/closest.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -64,60 +64,3 @@ Object {
},
}
`;

exports[`parser getClosestScope Can find the function declaration for square 1`] = `
FunctionDeclaration (1:0,3:1)
async: false
body: BlockStatement (1:19,3:1)
body:
- ReturnStatement (2:2,2:15)
argument: BinaryExpression (2:9,2:14)
left: Identifier (2:9,2:10)
name: 'n'
operator: '*'
right: Identifier (2:13,2:14)
name: 'n'
directives: []
generator: false
id: Identifier (1:9,1:15)
name: 'square'
params:
- Identifier (1:16,1:17)
name: 'n'
`;

exports[`parser getClosestScope finds a scope given at the end 1`] = `
FunctionDeclaration (9:0,11:1)
async: true
body: BlockStatement (9:25,11:1)
body:
- ReturnStatement (10:2,10:15)
argument: StringLiteral (10:9,10:14)
extra:
raw: '"meh"'
rawValue: 'meh'
value: 'meh'
directives: []
generator: false
id: Identifier (9:15,9:22)
name: 'slowFoo'
params: []
`;

exports[`parser getClosestScope finds the scope at the beginning 1`] = `
FunctionDeclaration (5:7,7:1)
async: false
body: BlockStatement (5:24,7:1)
body:
- ReturnStatement (6:2,6:15)
argument: StringLiteral (6:9,6:14)
extra:
raw: '"yay"'
rawValue: 'yay'
value: 'yay'
directives: []
generator: false
id: Identifier (5:16,5:21)
name: 'exFoo'
params: []
`;
Loading

0 comments on commit e463161

Please sign in to comment.