1
- const escope = require ( "eslint-scope" )
2
-
3
- module . exports = { replacer, getAllTokens, scopeToJSON, analyze }
1
+ import type { Identifier } from "estree"
2
+ import type { Scope , ScopeManager } from "eslint-scope"
3
+ import type { ESLintProgram , Token } from "../src/ast"
4
+ import type { ParserOptions } from "../src/common/parser-options"
5
+ import escope from "eslint-scope"
4
6
5
7
/**
6
8
* Remove `parent` properties from the given AST.
7
- * @param { string } key The key.
8
- * @param { any } value The value of the key.
9
- * @returns { any } The value of the key to output.
9
+ * @param key The key.
10
+ * @param value The value of the key.
11
+ * @returns The value of the key to output.
10
12
*/
11
- function replacer ( key , value ) {
13
+ export function replacer ( key : string , value : any ) : any {
12
14
if ( key === "parent" ) {
13
15
return undefined
14
16
}
@@ -25,21 +27,21 @@ function replacer(key, value) {
25
27
26
28
/**
27
29
* Get all tokens of the given AST.
28
- * @param { ASTNode } ast The root node of AST.
29
- * @returns { Token[] } Tokens.
30
+ * @param ast The root node of AST.
31
+ * @returns Tokens.
30
32
*/
31
- function getAllTokens ( ast ) {
33
+ export function getAllTokens ( ast : ESLintProgram ) : Token [ ] {
32
34
const tokenArrays = [ ast . tokens , ast . comments ]
33
35
if ( ast . templateBody != null ) {
34
36
tokenArrays . push ( ast . templateBody . tokens , ast . templateBody . comments )
35
37
}
36
38
return Array . prototype . concat . apply ( [ ] , tokenArrays )
37
39
}
38
40
39
- function scopeToJSON ( scopeManager ) {
41
+ export function scopeToJSON ( scopeManager : ScopeManager ) {
40
42
return JSON . stringify ( normalizeScope ( scopeManager . globalScope ) , replacer , 4 )
41
43
42
- function normalizeScope ( scope ) {
44
+ function normalizeScope ( scope : Scope ) {
43
45
return {
44
46
type : scope . type ,
45
47
variables : scope . variables . map ( normalizeVar ) ,
@@ -83,7 +85,7 @@ function scopeToJSON(scopeManager) {
83
85
}
84
86
}
85
87
86
- function normalizeId ( identifier ) {
88
+ function normalizeId ( identifier : Identifier ) {
87
89
return (
88
90
identifier && {
89
91
type : identifier . type ,
@@ -104,7 +106,7 @@ function scopeToJSON(scopeManager) {
104
106
/**
105
107
* Analyze scope
106
108
*/
107
- function analyze ( ast , parserOptions ) {
109
+ export function analyze ( ast : ESLintProgram , parserOptions : ParserOptions ) {
108
110
const ecmaVersion = parserOptions . ecmaVersion ?? 2022
109
111
const ecmaFeatures = parserOptions . ecmaFeatures ?? { }
110
112
const sourceType = parserOptions . sourceType ?? "script"
0 commit comments