Skip to content

Commit

Permalink
fix(parser): add simpleTraverse, replaces private ESLint util (#628)
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesHenry committed Jun 20, 2019
1 parent 34cfa53 commit aa206c4
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 20 deletions.
8 changes: 4 additions & 4 deletions packages/parser/src/parser.ts
@@ -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
@@ -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
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
@@ -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.

0 comments on commit aa206c4

Please sign in to comment.