3
3
* @copyright 2017 Toru Nagashima. All rights reserved.
4
4
* See LICENSE file in root directory for full license.
5
5
*/
6
- "use strict"
7
6
8
7
//------------------------------------------------------------------------------
9
8
// Requirements
10
9
//------------------------------------------------------------------------------
11
10
12
- const assert = require ( "assert" )
13
- const fs = require ( "fs" )
14
- const path = require ( "path" )
15
- const parser = require ( "../src" )
16
- const eslint = require ( "eslint" )
17
- const semver = require ( "semver" )
18
- const { scopeToJSON, analyze, replacer, getAllTokens } = require ( "./test-utils" )
11
+ import type { Rule } from "eslint"
12
+ import type { Node } from "../src/ast"
13
+ import type { ParserOptions } from "../src/common/parser-options"
14
+ import fs from "node:fs"
15
+ import path from "node:path"
16
+ import { describe , it , assert } from "vitest"
17
+ import { Linter } from "eslint"
18
+ import semver from "semver"
19
+ import * as parser from "../src"
20
+ import { scopeToJSON , analyze , replacer , getAllTokens } from "./test-utils"
19
21
20
22
//------------------------------------------------------------------------------
21
23
// Helpers
22
24
//------------------------------------------------------------------------------
23
- const Linter = eslint . Linter
25
+ // eslint-disable-next-line no-undef
24
26
const ROOT = path . join ( __dirname , "fixtures/ast" )
25
27
const TARGETS = fs . readdirSync ( ROOT )
26
- const PARSER_OPTIONS = {
28
+ const PARSER_OPTIONS : ParserOptions = {
27
29
comment : true ,
28
30
ecmaVersion : "latest" ,
29
31
sourceType : "module" ,
@@ -33,22 +35,28 @@ const PARSER_OPTIONS = {
33
35
eslintScopeManager : true ,
34
36
}
35
37
38
+ type TreeNode = {
39
+ type ?: string
40
+ text ?: string
41
+ children : TreeNode [ ]
42
+ }
43
+
36
44
/**
37
45
* Create simple tree.
38
- * @param { string } source The source code.
39
- * @param { object } parserOptions The parser options.
40
- * @returns { object } Simple tree.
46
+ * @param source The source code.
47
+ * @param parserOptions The parser options.
48
+ * @returns Simple tree.
41
49
*/
42
- function getTree ( source , parserOptions ) {
50
+ function getTree ( source : string , parserOptions : any ) {
43
51
const linter = new Linter ( { configType : "flat" } )
44
- const stack = [ ]
45
- const root = { children : [ ] }
46
- let current = root
52
+ const stack : TreeNode [ ] = [ ]
53
+ const root : TreeNode = { children : [ ] }
54
+ let current : TreeNode = root
47
55
48
- const maketree = {
56
+ const maketree : Rule . RuleModule = {
49
57
create : ( ruleContext ) =>
50
58
ruleContext . sourceCode . parserServices . defineTemplateBodyVisitor ( {
51
- "*" ( node ) {
59
+ "*" ( node : Node ) {
52
60
stack . push ( current )
53
61
current . children . push (
54
62
( current = {
@@ -59,7 +67,7 @@ function getTree(source, parserOptions) {
59
67
)
60
68
} ,
61
69
"*:exit" ( ) {
62
- current = stack . pop ( )
70
+ current = stack . pop ( ) !
63
71
} ,
64
72
} ) ,
65
73
}
@@ -75,10 +83,10 @@ function getTree(source, parserOptions) {
75
83
} ,
76
84
} ,
77
85
languageOptions : {
78
- parser : parser ,
86
+ parser,
79
87
ecmaVersion : parserOptions . ecmaVersion ?? "latest" ,
80
88
sourceType : parserOptions . sourceType ?? "module" ,
81
- parserOptions : parserOptions ,
89
+ parserOptions,
82
90
} ,
83
91
rules : { "test/maketree" : "error" } ,
84
92
} ,
@@ -92,28 +100,27 @@ function getTree(source, parserOptions) {
92
100
93
101
/**
94
102
* Convert a given node to string.
95
- * @param { Node } node The node to make string expression.
96
- * @param { string } source The source code.
97
- * @returns { string } The string expression of the node.
103
+ * @param node The node to make string expression.
104
+ * @param source The source code.
105
+ * @returns The string expression of the node.
98
106
*/
99
- function nodeToString ( node , source ) {
107
+ function nodeToString ( node : Node , source : string ) : string {
100
108
return node ? `${ node . type } [${ source . slice ( ...node . range ) } ]` : "undefined"
101
109
}
102
110
103
111
/**
104
112
* Validate the parent property of every node.
105
- * @param {string } source The source code.
106
- * @param {object } parserOptions The parser options.
107
- * @returns {void }
113
+ * @param source The source code.
114
+ * @param parserOptions The parser options.
108
115
*/
109
- function validateParent ( source , parserOptions ) {
116
+ function validateParent ( source : string , parserOptions : any ) {
110
117
const linter = new Linter ( { configType : "flat" } )
111
- const stack = [ ]
118
+ const stack : Node [ ] = [ ]
112
119
113
- const validateparent = {
120
+ const validateparent : Rule . RuleModule = {
114
121
create : ( ruleContext ) =>
115
122
ruleContext . sourceCode . parserServices . defineTemplateBodyVisitor ( {
116
- "*" ( node ) {
123
+ "*" ( node : Node ) {
117
124
if ( stack . length >= 1 ) {
118
125
const parent = stack . at ( - 1 )
119
126
assert (
@@ -124,7 +131,7 @@ function validateParent(source, parserOptions) {
124
131
) } should be ${ nodeToString (
125
132
parent ,
126
133
source ,
127
- ) } , but got ${ nodeToString ( node . parent , source ) } `,
134
+ ) } , but got ${ nodeToString ( node . parent ! , source ) } `,
128
135
)
129
136
}
130
137
stack . push ( node )
@@ -149,7 +156,7 @@ function validateParent(source, parserOptions) {
149
156
parser,
150
157
ecmaVersion : parserOptions . ecmaVersion ?? "latest" ,
151
158
sourceType : parserOptions . sourceType ?? "module" ,
152
- parserOptions : parserOptions ,
159
+ parserOptions,
153
160
} ,
154
161
rules : { "test/validateparent" : "error" } ,
155
162
} ,
@@ -173,8 +180,13 @@ describe("Template AST", () => {
173
180
const requirementsPath = path . join ( ROOT , `${ name } /requirements.json` )
174
181
const servicesPath = path . join ( ROOT , `${ name } /services.json` )
175
182
const source = fs . readFileSync ( sourcePath , "utf8" )
176
- const parserOptions = optionsPath ? require ( optionsPath ) : { }
177
- const requirements = fs . existsSync ( requirementsPath )
183
+
184
+ const parserOptions : ParserOptions = optionsPath
185
+ ? require ( optionsPath ) // eslint-disable-line @typescript-eslint/no-require-imports
186
+ : { }
187
+ const requirements : Record < string , string > = fs . existsSync (
188
+ requirementsPath ,
189
+ )
178
190
? JSON . parse ( fs . readFileSync ( requirementsPath , "utf8" ) )
179
191
: { }
180
192
const services = fs . existsSync ( servicesPath )
@@ -185,12 +197,13 @@ describe("Template AST", () => {
185
197
Object . entries ( parserOptions . templateTokenizer ) . map (
186
198
( [ key , value ] ) => [
187
199
key ,
188
- path . resolve ( __dirname , "../" , value ) ,
200
+ // eslint-disable-next-line no-undef
201
+ path . resolve ( __dirname , "../" , value as string ) ,
189
202
] ,
190
203
) ,
191
204
)
192
205
}
193
- const options = {
206
+ const options : ParserOptions = {
194
207
filePath : sourcePath ,
195
208
...PARSER_OPTIONS ,
196
209
...parserOptions ,
@@ -201,7 +214,7 @@ describe("Template AST", () => {
201
214
const version =
202
215
pkgName === "node"
203
216
? process . version
204
- : require ( `${ pkgName } /package.json` ) . version
217
+ : require ( `${ pkgName } /package.json` ) . version // eslint-disable-line @typescript-eslint/no-require-imports
205
218
return ! semver . satisfies ( version , pkgVersion )
206
219
} )
207
220
) {
@@ -252,7 +265,8 @@ describe("Template AST", () => {
252
265
} )
253
266
254
267
it ( "should have correct location." , ( ) => {
255
- const lines = source . match ( / [ ^ \r \n ] * (?: \r ? \n | $ ) / gu) ?? [ ]
268
+ const lines : string [ ] =
269
+ source . match ( / [ ^ \r \n ] * (?: \r ? \n | $ ) / gu) ?? [ ]
256
270
lines . push ( String . fromCodePoint ( 0 ) )
257
271
for ( const token of getAllTokens ( actual . ast ) ) {
258
272
const line0 = token . loc . start . line - 1
@@ -318,7 +332,7 @@ describe("Template AST", () => {
318
332
if ( services ) {
319
333
it ( "should have correct services." , ( ) => {
320
334
assert . deepStrictEqual (
321
- Object . keys ( actual . services ) . sort ( ) ,
335
+ Object . keys ( actual . services ! ) . sort ( ) ,
322
336
services ,
323
337
)
324
338
} )
0 commit comments