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 parse = require ( "../src" ) . parseForESLint
11
+ import type {
12
+ ESLintProgram ,
13
+ Token ,
14
+ VElement ,
15
+ VExpressionContainer ,
16
+ } from "../src/ast"
17
+ import type TokenStore from "../src/external/token-store"
18
+ import { assert , beforeAll , describe , it } from "vitest"
19
+ import { parseForESLint as parse } from "../src"
14
20
15
21
//------------------------------------------------------------------------------
16
22
// Helpers
@@ -26,10 +32,10 @@ const PARSER_OPTIONS = {
26
32
27
33
/**
28
34
* Get the value of the given node.
29
- * @param { ASTNode } token The node to get value.
30
- * @returns { string } The value of the node.
35
+ * @param token The node to get value.
36
+ * @returns The value of the node.
31
37
*/
32
- function toValue ( token ) {
38
+ function toValue ( token : Token ) : string {
33
39
if ( token . type === "HTMLAssociation" ) {
34
40
return "="
35
41
}
@@ -48,18 +54,18 @@ describe("services.getTemplateBodyTokenStore", () => {
48
54
<!--comment1-->
49
55
<div a="b" v-show="c < 3 && ok == "ok""><!--comment2-->{{ message /*comment3*/ }}<!--comment4--></div>
50
56
</template>`
51
- let ast = null
52
- let tokens = null
57
+ let ast : ESLintProgram | null = null
58
+ let tokens : TokenStore | null = null
53
59
54
- before ( ( ) => {
60
+ beforeAll ( ( ) => {
55
61
const result = parse ( code , { filePath : "test.vue" , ...PARSER_OPTIONS } )
56
62
ast = result . ast
57
- tokens = result . services . getTemplateBodyTokenStore ( )
63
+ tokens = result . services ! . getTemplateBodyTokenStore ( )
58
64
} )
59
65
60
66
describe ( "ast.templateBody" , ( ) => {
61
67
it ( "should return all tokens (except comments) in the template." , ( ) => {
62
- const actual = tokens . getTokens ( ast . templateBody ) . map ( toValue )
68
+ const actual = tokens ! . getTokens ( ast ! . templateBody ! ) . map ( toValue )
63
69
64
70
assert . deepStrictEqual ( actual , [
65
71
"template" ,
@@ -94,8 +100,8 @@ describe("services.getTemplateBodyTokenStore", () => {
94
100
} )
95
101
96
102
it ( "should return all tokens (include comments) in the template if you give {includeComments: true} option." , ( ) => {
97
- const actual = tokens
98
- . getTokens ( ast . templateBody , { includeComments : true } )
103
+ const actual = tokens !
104
+ . getTokens ( ast ! . templateBody ! , { includeComments : true } )
99
105
. map ( toValue )
100
106
101
107
assert . deepStrictEqual ( actual , [
@@ -137,17 +143,17 @@ describe("services.getTemplateBodyTokenStore", () => {
137
143
138
144
describe ( "ast.templateBody.children[0] (VText)" , ( ) => {
139
145
it ( "should return a text token." , ( ) => {
140
- const node = ast . templateBody . children [ 0 ]
141
- const actual = tokens . getTokens ( node ) . map ( toValue )
146
+ const node = ast ! . templateBody ! . children [ 0 ]
147
+ const actual = tokens ! . getTokens ( node ) . map ( toValue )
142
148
143
149
assert . deepStrictEqual ( actual , [ "\n " ] )
144
150
} )
145
151
} )
146
152
147
153
describe ( "ast.templateBody.children[2] (VElement)" , ( ) => {
148
154
it ( "should return all tokens in the element." , ( ) => {
149
- const node = ast . templateBody . children [ 2 ]
150
- const actual = tokens . getTokens ( node ) . map ( toValue )
155
+ const node = ast ! . templateBody ! . children [ 2 ]
156
+ const actual = tokens ! . getTokens ( node ) . map ( toValue )
151
157
152
158
assert . deepStrictEqual ( actual , [
153
159
"div" ,
@@ -177,8 +183,8 @@ describe("services.getTemplateBodyTokenStore", () => {
177
183
178
184
describe ( "ast.templateBody.children[2].startTag (VStartTag)" , ( ) => {
179
185
it ( "should return all tokens in the tag." , ( ) => {
180
- const node = ast . templateBody . children [ 2 ] . startTag
181
- const actual = tokens . getTokens ( node ) . map ( toValue )
186
+ const node = ( ast ! . templateBody ! . children [ 2 ] as VElement ) . startTag
187
+ const actual = tokens ! . getTokens ( node ) . map ( toValue )
182
188
183
189
assert . deepStrictEqual ( actual , [
184
190
"div" ,
@@ -203,46 +209,49 @@ describe("services.getTemplateBodyTokenStore", () => {
203
209
204
210
describe ( "ast.templateBody.children[2].startTag.attributes[0] (VAttribute)" , ( ) => {
205
211
it ( "should return all tokens in the attribute." , ( ) => {
206
- const node = ast . templateBody . children [ 2 ] . startTag . attributes [ 0 ]
207
- const actual = tokens . getTokens ( node ) . map ( toValue )
212
+ const node = ( ast ! . templateBody ! . children [ 2 ] as VElement ) . startTag
213
+ . attributes [ 0 ]
214
+ const actual = tokens ! . getTokens ( node ) . map ( toValue )
208
215
209
216
assert . deepStrictEqual ( actual , [ "a" , "=" , "b" ] )
210
217
} )
211
218
} )
212
219
213
220
describe ( "ast.templateBody.children[2].startTag.attributes[0].key (VIdentifier)" , ( ) => {
214
221
it ( "should return the identifier token." , ( ) => {
215
- const node = ast . templateBody . children [ 2 ] . startTag . attributes [ 0 ] . key
216
- const actual = tokens . getTokens ( node ) . map ( toValue )
222
+ const node = ( ast ! . templateBody ! . children [ 2 ] as VElement ) . startTag
223
+ . attributes [ 0 ] . key
224
+ const actual = tokens ! . getTokens ( node ) . map ( toValue )
217
225
218
226
assert . deepStrictEqual ( actual , [ "a" ] )
219
227
} )
220
228
} )
221
229
222
230
describe ( "ast.templateBody.children[2].startTag.attributes[0].value (VAttributeValue)" , ( ) => {
223
231
it ( "should return the value token." , ( ) => {
224
- const node =
225
- ast . templateBody . children [ 2 ] . startTag . attributes [ 0 ] . value
226
- const actual = tokens . getTokens ( node ) . map ( toValue )
232
+ const node = ( ast ! . templateBody ! . children [ 2 ] as VElement ) . startTag
233
+ . attributes [ 0 ] . value !
234
+ const actual = tokens ! . getTokens ( node ) . map ( toValue )
227
235
228
236
assert . deepStrictEqual ( actual , [ "b" ] )
229
237
} )
230
238
} )
231
239
232
240
describe ( "ast.templateBody.children[2].startTag.attributes[1].key (VDirectiveKey)" , ( ) => {
233
241
it ( "should return the identifier token." , ( ) => {
234
- const node = ast . templateBody . children [ 2 ] . startTag . attributes [ 1 ] . key
235
- const actual = tokens . getTokens ( node ) . map ( toValue )
242
+ const node = ( ast ! . templateBody ! . children [ 2 ] as VElement ) . startTag
243
+ . attributes [ 1 ] . key
244
+ const actual = tokens ! . getTokens ( node ) . map ( toValue )
236
245
237
246
assert . deepStrictEqual ( actual , [ "v-show" ] )
238
247
} )
239
248
} )
240
249
241
250
describe ( "ast.templateBody.children[2].startTag.attributes[1].value (VExpressionContainer)" , ( ) => {
242
251
it ( "should return all tokens in the value." , ( ) => {
243
- const node =
244
- ast . templateBody . children [ 2 ] . startTag . attributes [ 1 ] . value
245
- const actual = tokens . getTokens ( node ) . map ( toValue )
252
+ const node = ( ast ! . templateBody ! . children [ 2 ] as VElement ) . startTag
253
+ . attributes [ 1 ] . value !
254
+ const actual = tokens ! . getTokens ( node ) . map ( toValue )
246
255
247
256
assert . deepStrictEqual ( actual , [
248
257
'"' ,
@@ -260,10 +269,11 @@ describe("services.getTemplateBodyTokenStore", () => {
260
269
261
270
describe ( "ast.templateBody.children[2].startTag.attributes[1].value.expression (BinaryExpression)" , ( ) => {
262
271
it ( "should return all tokens in the expression." , ( ) => {
263
- const node =
264
- ast . templateBody . children [ 2 ] . startTag . attributes [ 1 ] . value
265
- . expression
266
- const actual = tokens . getTokens ( node ) . map ( toValue )
272
+ const node = (
273
+ ( ast ! . templateBody ! . children [ 2 ] as VElement ) . startTag
274
+ . attributes [ 1 ] . value as VExpressionContainer
275
+ ) . expression !
276
+ const actual = tokens ! . getTokens ( node ) . map ( toValue )
267
277
268
278
assert . deepStrictEqual ( actual , [
269
279
"c" ,
@@ -279,8 +289,8 @@ describe("services.getTemplateBodyTokenStore", () => {
279
289
280
290
describe ( "ast.templateBody.children[2].endTag (VEndTag)" , ( ) => {
281
291
it ( "should return all tokens in the tag." , ( ) => {
282
- const node = ast . templateBody . children [ 2 ] . endTag
283
- const actual = tokens . getTokens ( node ) . map ( toValue )
292
+ const node = ( ast ! . templateBody ! . children [ 2 ] as VElement ) . endTag !
293
+ const actual = tokens ! . getTokens ( node ) . map ( toValue )
284
294
285
295
assert . deepStrictEqual ( actual , [ "div" , ">" ] )
286
296
} )
@@ -289,15 +299,13 @@ describe("services.getTemplateBodyTokenStore", () => {
289
299
describe ( "TokenStore#get{Range,Loc}()" , ( ) => {
290
300
it ( "should return loc and range." , ( ) => {
291
301
const {
292
- templateBody : {
293
- children : [ node ] ,
294
- tokens : [ token ] ,
295
- } ,
296
- } = ast
297
- assert . equal ( typeof tokens . getRange ( node ) [ 0 ] , "number" )
298
- assert . equal ( typeof tokens . getRange ( token ) [ 1 ] , "number" )
299
- assert . equal ( typeof tokens . getLoc ( node ) . start . line , "number" )
300
- assert . equal ( typeof tokens . getLoc ( node ) . end . column , "number" )
302
+ children : [ node ] ,
303
+ tokens : [ token ] ,
304
+ } = ast ! . templateBody !
305
+ assert . equal ( typeof tokens ! . getRange ( node ) [ 0 ] , "number" )
306
+ assert . equal ( typeof tokens ! . getRange ( token ) [ 1 ] , "number" )
307
+ assert . equal ( typeof tokens ! . getLoc ( node ) . start . line , "number" )
308
+ assert . equal ( typeof tokens ! . getLoc ( node ) . end . column , "number" )
301
309
} )
302
310
} )
303
311
} )
0 commit comments