Skip to content

Commit 52e752b

Browse files
committed
feat: move ast to vitest
1 parent 3386bab commit 52e752b

File tree

3 files changed

+58
-44
lines changed

3 files changed

+58
-44
lines changed

test/ast.js renamed to test/ast.test.ts

Lines changed: 56 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,29 @@
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 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"
1921

2022
//------------------------------------------------------------------------------
2123
// Helpers
2224
//------------------------------------------------------------------------------
23-
const Linter = eslint.Linter
25+
// eslint-disable-next-line no-undef
2426
const ROOT = path.join(__dirname, "fixtures/ast")
2527
const TARGETS = fs.readdirSync(ROOT)
26-
const PARSER_OPTIONS = {
28+
const PARSER_OPTIONS: ParserOptions = {
2729
comment: true,
2830
ecmaVersion: "latest",
2931
sourceType: "module",
@@ -33,22 +35,28 @@ const PARSER_OPTIONS = {
3335
eslintScopeManager: true,
3436
}
3537

38+
type TreeNode = {
39+
type?: string
40+
text?: string
41+
children: TreeNode[]
42+
}
43+
3644
/**
3745
* 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.
4149
*/
42-
function getTree(source, parserOptions) {
50+
function getTree(source: string, parserOptions: any) {
4351
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
4755

48-
const maketree = {
56+
const maketree: Rule.RuleModule = {
4957
create: (ruleContext) =>
5058
ruleContext.sourceCode.parserServices.defineTemplateBodyVisitor({
51-
"*"(node) {
59+
"*"(node: Node) {
5260
stack.push(current)
5361
current.children.push(
5462
(current = {
@@ -59,7 +67,7 @@ function getTree(source, parserOptions) {
5967
)
6068
},
6169
"*:exit"() {
62-
current = stack.pop()
70+
current = stack.pop()!
6371
},
6472
}),
6573
}
@@ -75,10 +83,10 @@ function getTree(source, parserOptions) {
7583
},
7684
},
7785
languageOptions: {
78-
parser: parser,
86+
parser,
7987
ecmaVersion: parserOptions.ecmaVersion ?? "latest",
8088
sourceType: parserOptions.sourceType ?? "module",
81-
parserOptions: parserOptions,
89+
parserOptions,
8290
},
8391
rules: { "test/maketree": "error" },
8492
},
@@ -92,28 +100,27 @@ function getTree(source, parserOptions) {
92100

93101
/**
94102
* 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.
98106
*/
99-
function nodeToString(node, source) {
107+
function nodeToString(node: Node, source: string): string {
100108
return node ? `${node.type}[${source.slice(...node.range)}]` : "undefined"
101109
}
102110

103111
/**
104112
* 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.
108115
*/
109-
function validateParent(source, parserOptions) {
116+
function validateParent(source: string, parserOptions: any) {
110117
const linter = new Linter({ configType: "flat" })
111-
const stack = []
118+
const stack: Node[] = []
112119

113-
const validateparent = {
120+
const validateparent: Rule.RuleModule = {
114121
create: (ruleContext) =>
115122
ruleContext.sourceCode.parserServices.defineTemplateBodyVisitor({
116-
"*"(node) {
123+
"*"(node: Node) {
117124
if (stack.length >= 1) {
118125
const parent = stack.at(-1)
119126
assert(
@@ -124,7 +131,7 @@ function validateParent(source, parserOptions) {
124131
)} should be ${nodeToString(
125132
parent,
126133
source,
127-
)}, but got ${nodeToString(node.parent, source)}`,
134+
)}, but got ${nodeToString(node.parent!, source)}`,
128135
)
129136
}
130137
stack.push(node)
@@ -149,7 +156,7 @@ function validateParent(source, parserOptions) {
149156
parser,
150157
ecmaVersion: parserOptions.ecmaVersion ?? "latest",
151158
sourceType: parserOptions.sourceType ?? "module",
152-
parserOptions: parserOptions,
159+
parserOptions,
153160
},
154161
rules: { "test/validateparent": "error" },
155162
},
@@ -173,8 +180,13 @@ describe("Template AST", () => {
173180
const requirementsPath = path.join(ROOT, `${name}/requirements.json`)
174181
const servicesPath = path.join(ROOT, `${name}/services.json`)
175182
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+
)
178190
? JSON.parse(fs.readFileSync(requirementsPath, "utf8"))
179191
: {}
180192
const services = fs.existsSync(servicesPath)
@@ -185,12 +197,13 @@ describe("Template AST", () => {
185197
Object.entries(parserOptions.templateTokenizer).map(
186198
([key, value]) => [
187199
key,
188-
path.resolve(__dirname, "../", value),
200+
// eslint-disable-next-line no-undef
201+
path.resolve(__dirname, "../", value as string),
189202
],
190203
),
191204
)
192205
}
193-
const options = {
206+
const options: ParserOptions = {
194207
filePath: sourcePath,
195208
...PARSER_OPTIONS,
196209
...parserOptions,
@@ -201,7 +214,7 @@ describe("Template AST", () => {
201214
const version =
202215
pkgName === "node"
203216
? process.version
204-
: require(`${pkgName}/package.json`).version
217+
: require(`${pkgName}/package.json`).version // eslint-disable-line @typescript-eslint/no-require-imports
205218
return !semver.satisfies(version, pkgVersion)
206219
})
207220
) {
@@ -252,7 +265,8 @@ describe("Template AST", () => {
252265
})
253266

254267
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) ?? []
256270
lines.push(String.fromCodePoint(0))
257271
for (const token of getAllTokens(actual.ast)) {
258272
const line0 = token.loc.start.line - 1
@@ -318,7 +332,7 @@ describe("Template AST", () => {
318332
if (services) {
319333
it("should have correct services.", () => {
320334
assert.deepStrictEqual(
321-
Object.keys(actual.services).sort(),
335+
Object.keys(actual.services!).sort(),
322336
services,
323337
)
324338
})

test/test-utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type {
77
} from "eslint-scope"
88
import type { ESLintProgram, Token } from "../src/ast"
99
import type { ParserOptions } from "../src/common/parser-options"
10-
import escope from "eslint-scope"
10+
import * as escope from "eslint-scope"
1111

1212
/**
1313
* Remove `parent` properties from the given AST.

vitest.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { defineConfig } from "vitest/config"
33
export default defineConfig({
44
test: {
55
include: [
6-
"test/{parser-options,crlf,define-document-visitor,define-custom-blocks-visitor,parser-options-project,document-fragment,tokens,variables-references}.test.ts",
6+
"test/{parser-options,crlf,define-document-visitor,define-custom-blocks-visitor,parser-options-project,document-fragment,tokens,variables-references,ast}.test.ts",
77
],
88
},
99
})

0 commit comments

Comments
 (0)