Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(parser): add simpleTraverse, replaces private ESLint util (#628)
- Loading branch information
1 parent
34cfa53
commit aa206c4
Showing
5 changed files
with
64 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,5 @@ | |
"declaration": true, | ||
"outDir": "./dist" | ||
}, | ||
"include": ["src", "typings"] | ||
"include": ["src"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |
This file was deleted.
Oops, something went wrong.