-
-
Notifications
You must be signed in to change notification settings - Fork 400
/
Copy pathcore.ts
233 lines (198 loc) · 6 KB
/
core.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import { isConfiguration } from './configuration'
import HTMLParser from './htmlparser'
import Reporter, { ReportMessageCallback } from './reporter'
import * as HTMLRules from './rules'
import {
Configuration,
Hint,
isRuleSeverity,
Rule,
Ruleset,
RuleSeverity,
} from './types'
export interface FormatOptions {
colors?: boolean
indent?: number
}
const HTMLHINT_RECOMMENDED = 'htmlhint:recommended'
const HTMLHINT_LEGACY = 'htmlhint:legacy'
const DEFAULT_RULESETS: Record<string, Ruleset> = {
[HTMLHINT_RECOMMENDED]: {
'alt-require': 'warn',
'attr-lowercase': 'warn',
'attr-no-duplication': 'error',
'attr-no-unnecessary-whitespace': 'warn',
'attr-unsafe-chars': 'warn',
'attr-value-double-quotes': 'warn',
'id-class-ad-disabled': 'warn',
'id-unique': 'error',
'space-tab-mixed-disabled': 'warn',
'spec-char-escape': 'warn',
'src-not-empty': 'error',
'tag-pair': 'warn',
'tagname-lowercase': 'warn',
'tagname-specialchars': 'error',
'title-require': 'warn',
},
[HTMLHINT_LEGACY]: {
'attr-lowercase': 'error',
'attr-no-duplication': 'error',
'attr-value-double-quotes': 'error',
'doctype-first': 'error',
'id-unique': 'error',
'spec-char-escape': 'error',
'src-not-empty': 'error',
'tag-pair': 'error',
'tagname-lowercase': 'error',
'title-require': 'error',
},
}
class HTMLHintCore {
public rules: { [id: string]: Rule } = {}
public addRule(rule: Rule) {
this.rules[rule.id] = rule
}
public verify(
html: string,
config: Configuration = { extends: [HTMLHINT_RECOMMENDED] }
) {
if (!isConfiguration(config)) {
throw new Error('The HTMLHint configuration is invalid')
}
let ruleset: Ruleset = {}
// If an empty configuration is passed, use the recommended ruleset
if (config.extends === undefined && config.rules === undefined) {
config.extends = [HTMLHINT_RECOMMENDED]
}
// Iterate through extensions and merge rulesets into ruleset
for (const extend of config.extends ?? []) {
if (typeof extend === 'string') {
const extendRuleset = DEFAULT_RULESETS[extend] ?? {}
ruleset = { ...ruleset, ...extendRuleset }
}
}
// Apply self-configured rules
ruleset = { ...ruleset, ...(config.rules ?? {}) }
// If no rules have been configured, return immediately
if (Object.keys(ruleset).length === 0) {
// console.log('Please configure some HTMLHint rules')
return []
}
// parse inline ruleset
html = html.replace(
/^\s*<!--\s*htmlhint\s+([^\r\n]+?)\s*-->/i,
(all, strRuleset: string) => {
// For example:
// all is '<!-- htmlhint alt-require:warn-->'
// strRuleset is 'alt-require:warn'
strRuleset.replace(
/(?:^|,)\s*([^:,]+)\s*(?:\:\s*([^,\s]+))?/g,
(all, ruleId: string, value: string | undefined) => {
// For example:
// all is 'alt-require:warn'
// ruleId is 'alt-require'
// value is 'warn'
ruleset[ruleId] = isRuleSeverity(value) ? value : 'error'
return ''
}
)
return ''
}
)
const parser = new HTMLParser()
const reporter = new Reporter(html, ruleset)
const rules = this.rules
let rule: Rule
for (const id in ruleset) {
rule = rules[id]
const ruleConfig = ruleset[id]
const ruleSeverity: RuleSeverity = Array.isArray(ruleConfig)
? ruleConfig[0]
: ruleConfig
if (rule !== undefined && ruleSeverity !== 'off') {
const reportMessageCallback: ReportMessageCallback = reporter[
ruleSeverity
].bind(reporter)
rule.init(
parser,
reportMessageCallback,
Array.isArray(ruleConfig) ? ruleConfig[1] : undefined
)
}
}
parser.parse(html)
return reporter.messages
}
public format(arrMessages: Hint[], options: FormatOptions = {}) {
const arrLogs: string[] = []
const colors = {
white: '',
grey: '',
red: '',
reset: '',
}
if (options.colors) {
colors.white = '\x1b[37m'
colors.grey = '\x1b[90m'
colors.red = '\x1b[31m'
colors.reset = '\x1b[39m'
}
const indent = options.indent || 0
arrMessages.forEach((hint) => {
const leftWindow = 40
const rightWindow = leftWindow + 20
let evidence = hint.evidence
const line = hint.line
const col = hint.col
const evidenceCount = evidence.length
let leftCol = col > leftWindow + 1 ? col - leftWindow : 1
let rightCol =
evidence.length > col + rightWindow ? col + rightWindow : evidenceCount
if (col < leftWindow + 1) {
rightCol += leftWindow - col + 1
}
evidence = evidence.replace(/\t/g, ' ').substring(leftCol - 1, rightCol)
// add ...
if (leftCol > 1) {
evidence = `...${evidence}`
leftCol -= 3
}
if (rightCol < evidenceCount) {
evidence += '...'
}
// show evidence
arrLogs.push(
`${colors.white + repeatStr(indent)}L${line} |${
colors.grey
}${evidence}${colors.reset}`
)
// show pointer & message
let pointCol = col - leftCol
// add double byte character
// eslint-disable-next-line no-control-regex
const match = evidence.substring(0, pointCol).match(/[^\u0000-\u00ff]/g)
if (match !== null) {
pointCol += match.length
}
arrLogs.push(
`${
colors.white +
repeatStr(indent) +
repeatStr(String(line).length + 3 + pointCol)
}^ ${colors.red}${hint.message} (${hint.rule.id})${colors.reset}`
)
})
return arrLogs
}
}
// repeat string
function repeatStr(n: number, str?: string) {
return new Array(n + 1).join(str || ' ')
}
export const HTMLHint = new HTMLHintCore()
Object.keys(HTMLRules).forEach((key) => {
// TODO: need a fix
// @ts-expect-error
HTMLHint.addRule(HTMLRules[key])
})
export { HTMLRules, Reporter, HTMLParser }