1
- "use strict"
2
-
3
1
//------------------------------------------------------------------------------
4
2
// Requirements
5
3
//------------------------------------------------------------------------------
6
4
7
- const assert = require ( "assert" )
8
- const fs = require ( "fs" )
9
- const path = require ( "path" )
10
- const parser = require ( "../src" )
5
+ import type { VDocumentFragment } from "../src/ast"
6
+ import fs from "fs"
7
+ import path from "path"
8
+ import { describe , it , assert } from "vitest"
9
+ import * as parser from "../src"
11
10
12
11
//------------------------------------------------------------------------------
13
12
// Helpers
14
13
//------------------------------------------------------------------------------
15
14
15
+ // eslint-disable-next-line no-undef
16
16
const ROOT = path . join ( __dirname , "fixtures/document-fragment" )
17
17
const TARGETS = fs . readdirSync ( ROOT )
18
18
const PARSER_OPTIONS = {
@@ -26,11 +26,11 @@ const PARSER_OPTIONS = {
26
26
27
27
/**
28
28
* Remove `parent` proeprties from the given AST.
29
- * @param { string } key The key.
30
- * @param { any } value The value of the key.
31
- * @returns { any } The value of the key to output.
29
+ * @param key The key.
30
+ * @param value The value of the key.
31
+ * @returns The value of the key to output.
32
32
*/
33
- function replacer ( key , value ) {
33
+ function replacer ( key : string , value : any ) : any {
34
34
if ( key === "parent" ) {
35
35
return undefined
36
36
}
@@ -60,14 +60,15 @@ describe("services.getDocumentFragment", () => {
60
60
path . join ( ROOT , `${ name } /parser-options.js` ) ,
61
61
] . find ( ( fp ) => fs . existsSync ( fp ) )
62
62
const source = fs . readFileSync ( sourcePath , "utf8" )
63
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
63
64
const parserOptions = optionsPath ? require ( optionsPath ) : { }
64
65
const options = {
65
66
filePath : sourcePath ,
66
67
...PARSER_OPTIONS ,
67
68
...parserOptions ,
68
69
}
69
70
const result = parser . parseForESLint ( source , options )
70
- const actual = result . services . getDocumentFragment ( )
71
+ const actual = result . services ! . getDocumentFragment ( )
71
72
72
73
describe ( `'test/fixtures/document-fragment/${ name } /${ sourceFileName } '` , ( ) => {
73
74
it ( "should be parsed to valid document fragment." , ( ) => {
@@ -86,7 +87,7 @@ describe("services.getDocumentFragment", () => {
86
87
it ( "should have correct range." , ( ) => {
87
88
const resultPath = path . join ( ROOT , `${ name } /token-ranges.json` )
88
89
const expectedText = fs . readFileSync ( resultPath , "utf8" )
89
- const tokens = getAllTokens ( actual ) . map ( ( t ) =>
90
+ const tokens = getAllTokens ( actual ! ) . map ( ( t ) =>
90
91
source . slice ( t . range [ 0 ] , t . range [ 1 ] ) ,
91
92
)
92
93
const actualText = JSON . stringify ( tokens , null , 4 )
@@ -97,7 +98,7 @@ describe("services.getDocumentFragment", () => {
97
98
}
98
99
} )
99
100
100
- function getAllTokens ( fgAst ) {
101
+ function getAllTokens ( fgAst : VDocumentFragment ) {
101
102
const tokenArrays = [ fgAst . tokens , fgAst . comments ]
102
103
103
104
return Array . prototype . concat . apply ( [ ] , tokenArrays )
0 commit comments