33 * @copyright 2017 Toru Nagashima. All rights reserved.
44 * See LICENSE file in root directory for full license.
55 */
6- "use strict"
76
87//------------------------------------------------------------------------------
98// Requirements
109//------------------------------------------------------------------------------
1110
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"
1420
1521//------------------------------------------------------------------------------
1622// Helpers
@@ -26,10 +32,10 @@ const PARSER_OPTIONS = {
2632
2733/**
2834 * 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.
3137 */
32- function toValue ( token ) {
38+ function toValue ( token : Token ) : string {
3339 if ( token . type === "HTMLAssociation" ) {
3440 return "="
3541 }
@@ -48,18 +54,18 @@ describe("services.getTemplateBodyTokenStore", () => {
4854 <!--comment1-->
4955 <div a="b" v-show="c < 3 && ok == "ok""><!--comment2-->{{ message /*comment3*/ }}<!--comment4--></div>
5056</template>`
51- let ast = null
52- let tokens = null
57+ let ast : ESLintProgram | null = null
58+ let tokens : TokenStore | null = null
5359
54- before ( ( ) => {
60+ beforeAll ( ( ) => {
5561 const result = parse ( code , { filePath : "test.vue" , ...PARSER_OPTIONS } )
5662 ast = result . ast
57- tokens = result . services . getTemplateBodyTokenStore ( )
63+ tokens = result . services ! . getTemplateBodyTokenStore ( )
5864 } )
5965
6066 describe ( "ast.templateBody" , ( ) => {
6167 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 )
6369
6470 assert . deepStrictEqual ( actual , [
6571 "template" ,
@@ -94,8 +100,8 @@ describe("services.getTemplateBodyTokenStore", () => {
94100 } )
95101
96102 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 } )
99105 . map ( toValue )
100106
101107 assert . deepStrictEqual ( actual , [
@@ -137,17 +143,17 @@ describe("services.getTemplateBodyTokenStore", () => {
137143
138144 describe ( "ast.templateBody.children[0] (VText)" , ( ) => {
139145 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 )
142148
143149 assert . deepStrictEqual ( actual , [ "\n " ] )
144150 } )
145151 } )
146152
147153 describe ( "ast.templateBody.children[2] (VElement)" , ( ) => {
148154 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 )
151157
152158 assert . deepStrictEqual ( actual , [
153159 "div" ,
@@ -177,8 +183,8 @@ describe("services.getTemplateBodyTokenStore", () => {
177183
178184 describe ( "ast.templateBody.children[2].startTag (VStartTag)" , ( ) => {
179185 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 )
182188
183189 assert . deepStrictEqual ( actual , [
184190 "div" ,
@@ -203,46 +209,49 @@ describe("services.getTemplateBodyTokenStore", () => {
203209
204210 describe ( "ast.templateBody.children[2].startTag.attributes[0] (VAttribute)" , ( ) => {
205211 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 )
208215
209216 assert . deepStrictEqual ( actual , [ "a" , "=" , "b" ] )
210217 } )
211218 } )
212219
213220 describe ( "ast.templateBody.children[2].startTag.attributes[0].key (VIdentifier)" , ( ) => {
214221 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 )
217225
218226 assert . deepStrictEqual ( actual , [ "a" ] )
219227 } )
220228 } )
221229
222230 describe ( "ast.templateBody.children[2].startTag.attributes[0].value (VAttributeValue)" , ( ) => {
223231 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 )
227235
228236 assert . deepStrictEqual ( actual , [ "b" ] )
229237 } )
230238 } )
231239
232240 describe ( "ast.templateBody.children[2].startTag.attributes[1].key (VDirectiveKey)" , ( ) => {
233241 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 )
236245
237246 assert . deepStrictEqual ( actual , [ "v-show" ] )
238247 } )
239248 } )
240249
241250 describe ( "ast.templateBody.children[2].startTag.attributes[1].value (VExpressionContainer)" , ( ) => {
242251 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 )
246255
247256 assert . deepStrictEqual ( actual , [
248257 '"' ,
@@ -260,10 +269,11 @@ describe("services.getTemplateBodyTokenStore", () => {
260269
261270 describe ( "ast.templateBody.children[2].startTag.attributes[1].value.expression (BinaryExpression)" , ( ) => {
262271 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 )
267277
268278 assert . deepStrictEqual ( actual , [
269279 "c" ,
@@ -279,8 +289,8 @@ describe("services.getTemplateBodyTokenStore", () => {
279289
280290 describe ( "ast.templateBody.children[2].endTag (VEndTag)" , ( ) => {
281291 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 )
284294
285295 assert . deepStrictEqual ( actual , [ "div" , ">" ] )
286296 } )
@@ -289,15 +299,13 @@ describe("services.getTemplateBodyTokenStore", () => {
289299 describe ( "TokenStore#get{Range,Loc}()" , ( ) => {
290300 it ( "should return loc and range." , ( ) => {
291301 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" )
301309 } )
302310 } )
303311} )
0 commit comments