Skip to content

fix(parser): add simpleTraverse, replaces private ESLint util #628

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

Merged
merged 2 commits into from
Jun 20, 2019
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
8 changes: 4 additions & 4 deletions packages/parser/src/parser.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { TSESLint } from '@typescript-eslint/experimental-utils';
import {
AST_NODE_TYPES,
parseAndGenerateServices,
TSESTreeOptions,
ParserServices,
TSESTreeOptions,
} from '@typescript-eslint/typescript-estree';
import { TSESLint } from '@typescript-eslint/experimental-utils';
import traverser from 'eslint/lib/util/traverser';
import { analyzeScope } from './analyze-scope';
import { simpleTraverse } from './simple-traverse';
import { visitorKeys } from './visitor-keys';

type ParserOptions = TSESLint.ParserOptions;
Expand Down Expand Up @@ -87,7 +87,7 @@ export function parseForESLint(
const { ast, services } = parseAndGenerateServices(code, parserOptions);
ast.sourceType = options.sourceType;

traverser.traverse(ast, {
simpleTraverse(ast, {
enter(node) {
switch (node.type) {
// Function#body cannot be null in ESTree spec.
Expand Down
58 changes: 58 additions & 0 deletions packages/parser/src/simple-traverse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { TSESTree } from '@typescript-eslint/typescript-estree';
import { visitorKeys } from './visitor-keys';

function isValidNode(x: any): x is TSESTree.Node {
return x !== null && typeof x === 'object' && typeof x.type === 'string';
}

function getVisitorKeysForNode(
allVisitorKeys: typeof visitorKeys,
node: TSESTree.Node,
): readonly string[] {
const keys = allVisitorKeys[node.type];
return keys || [];
}

interface SimpleTraverseOptions {
enter: (node: TSESTree.Node, parent: TSESTree.Node | undefined) => void;
}

class SimpleTraverser {
private allVisitorKeys = visitorKeys;
private enter: SimpleTraverseOptions['enter'];

constructor({ enter }: SimpleTraverseOptions) {
this.enter = enter;
}

traverse(node: unknown, parent: TSESTree.Node | undefined) {
if (!isValidNode(node)) {
return;
}
this.enter(node, parent);

const keys = getVisitorKeysForNode(this.allVisitorKeys, node);
if (keys.length < 1) {
return;
}

for (const key of keys) {
const childOrChildren = node[key as keyof typeof node];

if (Array.isArray(childOrChildren)) {
for (const child of childOrChildren) {
this.traverse(child, node);
}
} else {
this.traverse(childOrChildren, node);
}
}
}
}

export function simpleTraverse(
startingNode: TSESTree.Node,
options: SimpleTraverseOptions,
) {
new SimpleTraverser(options).traverse(startingNode, undefined);
}
2 changes: 1 addition & 1 deletion packages/parser/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
"declaration": true,
"outDir": "./dist"
},
"include": ["src", "typings"]
"include": ["src"]
}
2 changes: 1 addition & 1 deletion packages/parser/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "./tsconfig.build.json",
"include": ["src", "typings", "tests", "tools"],
"include": ["src", "tests", "tools"],
"exclude": ["tests/fixtures"]
}
14 changes: 0 additions & 14 deletions packages/parser/typings/eslint.d.ts

This file was deleted.