Skip to content

Commit d07b415

Browse files
committed
refactor: use replacer in test-utils
1 parent 6dea54a commit d07b415

File tree

2 files changed

+17
-35
lines changed

2 files changed

+17
-35
lines changed

test/document-fragment.test.ts

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import fs from "fs"
77
import path from "path"
88
import { describe, it, assert } from "vitest"
99
import * as parser from "../src"
10+
import { replacer } from "./test-utils"
1011

1112
//------------------------------------------------------------------------------
1213
// Helpers
@@ -24,27 +25,6 @@ const PARSER_OPTIONS = {
2425
sourceType: "module",
2526
}
2627

27-
/**
28-
* Remove `parent` proeprties from the given AST.
29-
* @param key The key.
30-
* @param value The value of the key.
31-
* @returns The value of the key to output.
32-
*/
33-
function replacer(key: string, value: any): any {
34-
if (key === "parent") {
35-
return undefined
36-
}
37-
if (key === "errors" && Array.isArray(value)) {
38-
return value.map((e) => ({
39-
message: e.message,
40-
index: e.index,
41-
lineNumber: e.lineNumber,
42-
column: e.column,
43-
}))
44-
}
45-
return value
46-
}
47-
4828
//------------------------------------------------------------------------------
4929
// Main
5030
//------------------------------------------------------------------------------

test/test-utils.js renamed to test/test-utils.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
const escope = require("eslint-scope")
2-
3-
module.exports = { replacer, getAllTokens, scopeToJSON, analyze }
1+
import type { Identifier } from "estree"
2+
import type { Scope, ScopeManager } from "eslint-scope"
3+
import type { ESLintProgram, Token } from "../src/ast"
4+
import type { ParserOptions } from "../src/common/parser-options"
5+
import escope from "eslint-scope"
46

57
/**
68
* Remove `parent` properties from the given AST.
7-
* @param {string} key The key.
8-
* @param {any} value The value of the key.
9-
* @returns {any} The value of the key to output.
9+
* @param key The key.
10+
* @param value The value of the key.
11+
* @returns The value of the key to output.
1012
*/
11-
function replacer(key, value) {
13+
export function replacer(key: string, value: any): any {
1214
if (key === "parent") {
1315
return undefined
1416
}
@@ -25,21 +27,21 @@ function replacer(key, value) {
2527

2628
/**
2729
* Get all tokens of the given AST.
28-
* @param {ASTNode} ast The root node of AST.
29-
* @returns {Token[]} Tokens.
30+
* @param ast The root node of AST.
31+
* @returns Tokens.
3032
*/
31-
function getAllTokens(ast) {
33+
export function getAllTokens(ast: ESLintProgram): Token[] {
3234
const tokenArrays = [ast.tokens, ast.comments]
3335
if (ast.templateBody != null) {
3436
tokenArrays.push(ast.templateBody.tokens, ast.templateBody.comments)
3537
}
3638
return Array.prototype.concat.apply([], tokenArrays)
3739
}
3840

39-
function scopeToJSON(scopeManager) {
41+
export function scopeToJSON(scopeManager: ScopeManager) {
4042
return JSON.stringify(normalizeScope(scopeManager.globalScope), replacer, 4)
4143

42-
function normalizeScope(scope) {
44+
function normalizeScope(scope: Scope) {
4345
return {
4446
type: scope.type,
4547
variables: scope.variables.map(normalizeVar),
@@ -83,7 +85,7 @@ function scopeToJSON(scopeManager) {
8385
}
8486
}
8587

86-
function normalizeId(identifier) {
88+
function normalizeId(identifier: Identifier) {
8789
return (
8890
identifier && {
8991
type: identifier.type,
@@ -104,7 +106,7 @@ function scopeToJSON(scopeManager) {
104106
/**
105107
* Analyze scope
106108
*/
107-
function analyze(ast, parserOptions) {
109+
export function analyze(ast: ESLintProgram, parserOptions: ParserOptions) {
108110
const ecmaVersion = parserOptions.ecmaVersion ?? 2022
109111
const ecmaFeatures = parserOptions.ecmaFeatures ?? {}
110112
const sourceType = parserOptions.sourceType ?? "script"

0 commit comments

Comments
 (0)