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

[Parser] Stop using babel-traverse #5592

Merged
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
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
15 changes: 9 additions & 6 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 @@ -316,7 +319,7 @@ function findIdentifierInScopes(
}

function createGlobalScope(
ast: BabelNode,
ast: Node,
sourceId: SourceId
): { global: TempScope, lexical: TempScope } {
const global = createTempScope("object", "Global", null, {
Expand Down Expand Up @@ -362,7 +365,7 @@ function createGlobalScope(
const scopeCollectionVisitor = {
// eslint-disable-next-line complexity
enter(
node: BabelNode,
node: Node,
ancestors: TraversalAncestors,
state: ScopeCollectionVisitorState
) {
Expand Down Expand Up @@ -618,7 +621,7 @@ const scopeCollectionVisitor = {
}
},
exit(
node: BabelNode,
node: Node,
ancestors: TraversalAncestors,
state: ScopeCollectionVisitorState
) {
Expand All @@ -633,7 +636,7 @@ const scopeCollectionVisitor = {

function buildMetaBindings(
sourceId: SourceId,
node: BabelNode,
node: Node,
ancestors: TraversalAncestors,
parentIndex: number = ancestors.length - 1
): BindingMetaValue | null {
Expand Down
59 changes: 41 additions & 18 deletions src/workers/parser/getSymbols.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@
import flatten from "lodash/flatten";
import * as t from "@babel/types";

import type {
Node,
TraversalAncestors,
Location as BabelLocation
} 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, Location as BabelLocation } from "babel-traverse";

let symbolDeclarations = new Map();

export type ClassDeclaration = {|
Expand Down Expand Up @@ -49,7 +53,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 @@ -88,8 +92,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 @@ -143,14 +147,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 @@ -274,9 +278,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 @@ -292,9 +299,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 @@ -344,8 +351,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 @@ -363,11 +370,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 @@ -376,7 +387,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 @@ -427,6 +446,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
Loading