Skip to content

Commit aedcd8d

Browse files
committed
refactor: rewrite processor in ts
1 parent 0f786d8 commit aedcd8d

File tree

1 file changed

+28
-44
lines changed

1 file changed

+28
-44
lines changed

lib/processor.js renamed to lib/processor.ts

Lines changed: 28 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,33 @@
11
/**
22
* @author Toru Nagashima <https://github.com/mysticatea>
33
*/
4-
'use strict'
4+
import type { Linter } from 'eslint'
55

6-
/**
7-
* @typedef {import('eslint').Linter.LintMessage} LintMessage
8-
*/
9-
/**
10-
* @typedef {object} GroupState
11-
* @property {Set<string>} GroupState.disableAllKeys
12-
* @property {Map<string, string[]>} GroupState.disableRuleKeys
13-
*/
6+
type LintMessage = Linter.LintMessage
7+
8+
interface GroupState {
9+
disableAllKeys: Set<string>
10+
disableRuleKeys: Map<string, string[]>
11+
}
1412

15-
module.exports = {
16-
/** @param {string} code */
17-
preprocess(code) {
13+
export default {
14+
preprocess(code: string) {
1815
return [code]
1916
},
2017

21-
/**
22-
* @param {LintMessage[][]} messages
23-
* @returns {LintMessage[]}
24-
*/
25-
postprocess(messages) {
18+
postprocess(messages: LintMessage[][]) {
2619
const state = {
27-
/** @type {GroupState} */
2820
block: {
29-
disableAllKeys: new Set(),
30-
disableRuleKeys: new Map()
31-
},
32-
/** @type {GroupState} */
21+
disableAllKeys: new Set<string>(),
22+
disableRuleKeys: new Map<string, string[]>()
23+
} as GroupState,
3324
line: {
34-
disableAllKeys: new Set(),
35-
disableRuleKeys: new Map()
36-
}
25+
disableAllKeys: new Set<string>(),
26+
disableRuleKeys: new Map<string, string[]>()
27+
} as GroupState
3728
}
38-
/** @type {string[]} */
39-
const usedDisableDirectiveKeys = []
40-
/** @type {Map<string,LintMessage>} */
41-
const unusedDisableDirectiveReports = new Map()
29+
const usedDisableDirectiveKeys: string[] = []
30+
const unusedDisableDirectiveReports = new Map<string, LintMessage>()
4231

4332
// Filter messages which are in disabled area.
4433
const filteredMessages = messages[0].filter((message) => {
@@ -139,12 +128,11 @@ module.exports = {
139128
meta: require('./meta')
140129
}
141130

142-
/**
143-
* @param {Map<string, string[]>} disableRuleKeys
144-
* @param {string} rule
145-
* @param {string} key
146-
*/
147-
function addDisableRule(disableRuleKeys, rule, key) {
131+
function addDisableRule(
132+
disableRuleKeys: GroupState['disableRuleKeys'],
133+
rule: string,
134+
key: string
135+
) {
148136
let keys = disableRuleKeys.get(rule)
149137
if (keys) {
150138
keys.push(key)
@@ -154,11 +142,7 @@ function addDisableRule(disableRuleKeys, rule, key) {
154142
}
155143
}
156144

157-
/**
158-
* @param {LintMessage} message
159-
* @returns {string} message key
160-
*/
161-
function messageToKey(message) {
145+
function messageToKey(message: LintMessage) {
162146
return `line:${message.line},column${
163147
// -1 because +1 by ESLint's `report-translator`.
164148
message.column - 1
@@ -167,11 +151,11 @@ function messageToKey(message) {
167151

168152
/**
169153
* Compares the locations of two objects in a source file
170-
* @param {Position} itemA The first object
171-
* @param {Position} itemB The second object
172-
* @returns {number} A value less than 1 if itemA appears before itemB in the source file, greater than 1 if
154+
* @param itemA The first object
155+
* @param itemB The second object
156+
* @returns A value less than 1 if itemA appears before itemB in the source file, greater than 1 if
173157
* itemA appears after itemB in the source file, or 0 if itemA and itemB have the same location.
174158
*/
175-
function compareLocations(itemA, itemB) {
159+
function compareLocations(itemA: Position, itemB: Position) {
176160
return itemA.line - itemB.line || itemA.column - itemB.column
177161
}

0 commit comments

Comments
 (0)