-
-
Notifications
You must be signed in to change notification settings - Fork 681
/
Copy pathhtml-comments.js
259 lines (237 loc) · 8.01 KB
/
html-comments.js
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/**
* @typedef { { exceptions?: string[] } } CommentParserConfig
* @typedef { (comment: ParsedHTMLComment) => void } HTMLCommentVisitor
* @typedef { { includeDirectives?: boolean } } CommentVisitorOption
*
* @typedef { Token & { type: 'HTMLCommentOpen' } } HTMLCommentOpen
* @typedef { Token & { type: 'HTMLCommentOpenDecoration' } } HTMLCommentOpenDecoration
* @typedef { Token & { type: 'HTMLCommentValue' } } HTMLCommentValue
* @typedef { Token & { type: 'HTMLCommentClose' } } HTMLCommentClose
* @typedef { Token & { type: 'HTMLCommentCloseDecoration' } } HTMLCommentCloseDecoration
* @typedef { { open: HTMLCommentOpen, openDecoration: HTMLCommentOpenDecoration | null, value: HTMLCommentValue | null, closeDecoration: HTMLCommentCloseDecoration | null, close: HTMLCommentClose } } ParsedHTMLComment
*/
// -----------------------------------------------------------------------------
// Requirements
// -----------------------------------------------------------------------------
const utils = require('./')
// ------------------------------------------------------------------------------
// Helpers
// ------------------------------------------------------------------------------
const COMMENT_DIRECTIVE = /^\s*eslint-(?:en|dis)able/
const IE_CONDITIONAL_IF = /^\[if\s+/
const IE_CONDITIONAL_ENDIF = /\[endif\]$/
/** @type { 'HTMLCommentOpen' } */
const TYPE_HTML_COMMENT_OPEN = 'HTMLCommentOpen'
/** @type { 'HTMLCommentOpenDecoration' } */
const TYPE_HTML_COMMENT_OPEN_DECORATION = 'HTMLCommentOpenDecoration'
/** @type { 'HTMLCommentValue' } */
const TYPE_HTML_COMMENT_VALUE = 'HTMLCommentValue'
/** @type { 'HTMLCommentClose' } */
const TYPE_HTML_COMMENT_CLOSE = 'HTMLCommentClose'
/** @type { 'HTMLCommentCloseDecoration' } */
const TYPE_HTML_COMMENT_CLOSE_DECORATION = 'HTMLCommentCloseDecoration'
/**
* @param {HTMLComment} comment
* @returns {boolean}
*/
function isCommentDirective(comment) {
return COMMENT_DIRECTIVE.test(comment.value)
}
/**
* @param {HTMLComment} comment
* @returns {boolean}
*/
function isIEConditionalComment(comment) {
return (
IE_CONDITIONAL_IF.test(comment.value) ||
IE_CONDITIONAL_ENDIF.test(comment.value)
)
}
/**
* Define HTML comment parser
*
* @param {SourceCode} sourceCode The source code instance.
* @param {CommentParserConfig | null} config The config.
* @returns { (node: Token) => (ParsedHTMLComment | null) } HTML comment parser.
*/
function defineParser(sourceCode, config) {
config = config || {}
const exceptions = config.exceptions || []
/**
* Get a open decoration string from comment contents.
* @param {string} contents comment contents
* @returns {string} decoration string
*/
function getOpenDecoration(contents) {
let decoration = ''
for (const exception of exceptions) {
const length = exception.length
let index = 0
while (contents.startsWith(exception, index)) {
index += length
}
const exceptionLength = index
if (decoration.length < exceptionLength) {
decoration = contents.slice(0, exceptionLength)
}
}
return decoration
}
/**
* Get a close decoration string from comment contents.
* @param {string} contents comment contents
* @returns {string} decoration string
*/
function getCloseDecoration(contents) {
let decoration = ''
for (const exception of exceptions) {
const length = exception.length
let index = contents.length
while (contents.endsWith(exception, index)) {
index -= length
}
const exceptionLength = contents.length - index
if (decoration.length < exceptionLength) {
decoration = contents.slice(index)
}
}
return decoration
}
/**
* Parse HTMLComment.
* @param {Token} node a comment token
* @returns {ParsedHTMLComment | null} the result of HTMLComment tokens.
*/
return function parseHTMLComment(node) {
if (node.type !== 'HTMLComment') {
// Is not HTMLComment
return null
}
const htmlCommentText = sourceCode.getText(node)
if (
!htmlCommentText.startsWith('<!--') ||
!htmlCommentText.endsWith('-->')
) {
// Is not normal HTML Comment
// e.g. Error Code: "abrupt-closing-of-empty-comment", "incorrectly-closed-comment"
return null
}
let valueText = htmlCommentText.slice(4, -3)
const openDecorationText = getOpenDecoration(valueText)
valueText = valueText.slice(openDecorationText.length)
const firstCharIndex = valueText.search(/\S/)
const beforeSpace =
firstCharIndex >= 0 ? valueText.slice(0, firstCharIndex) : valueText
valueText = valueText.slice(beforeSpace.length)
const closeDecorationText = getCloseDecoration(valueText)
if (closeDecorationText) {
valueText = valueText.slice(0, -closeDecorationText.length)
}
const lastCharIndex = valueText.search(/\S\s*$/)
const afterSpace =
lastCharIndex >= 0 ? valueText.slice(lastCharIndex + 1) : valueText
if (afterSpace) {
valueText = valueText.slice(0, -afterSpace.length)
}
let tokenIndex = node.range[0]
/**
* @param {string} type
* @param {string} value
* @returns {any}
*/
const createToken = (type, value) => {
/** @type {Range} */
const range = [tokenIndex, tokenIndex + value.length]
tokenIndex = range[1]
/** @type {SourceLocation} */
let loc
return {
type,
value,
range,
get loc() {
if (loc) {
return loc
}
return (loc = {
start: sourceCode.getLocFromIndex(range[0]),
end: sourceCode.getLocFromIndex(range[1])
})
}
}
}
/** @type {HTMLCommentOpen} */
const open = createToken(TYPE_HTML_COMMENT_OPEN, '<!--')
/** @type {HTMLCommentOpenDecoration | null} */
const openDecoration = openDecorationText
? createToken(TYPE_HTML_COMMENT_OPEN_DECORATION, openDecorationText)
: null
tokenIndex += beforeSpace.length
/** @type {HTMLCommentValue | null} */
const value = valueText
? createToken(TYPE_HTML_COMMENT_VALUE, valueText)
: null
tokenIndex += afterSpace.length
/** @type {HTMLCommentCloseDecoration | null} */
const closeDecoration = closeDecorationText
? createToken(TYPE_HTML_COMMENT_CLOSE_DECORATION, closeDecorationText)
: null
/** @type {HTMLCommentClose} */
const close = createToken(TYPE_HTML_COMMENT_CLOSE, '-->')
return {
/** HTML comment open (`<!--`) */
open,
/** decoration of the start of HTML comments. (`*****` when `<!--*****`) */
openDecoration,
/** value of HTML comment. whitespaces and other tokens are not included. */
value,
/** decoration of the end of HTML comments. (`*****` when `*****-->`) */
closeDecoration,
/** HTML comment close (`-->`) */
close
}
}
}
/**
* Define HTML comment visitor
*
* @param {RuleContext} context The rule context.
* @param {CommentParserConfig | null} config The config.
* @param {HTMLCommentVisitor} visitHTMLComment The HTML comment visitor.
* @param {CommentVisitorOption} [visitorOption] The option for visitor.
* @returns {RuleListener} HTML comment visitor.
*/
function defineVisitor(context, config, visitHTMLComment, visitorOption) {
return {
Program(node) {
visitorOption = visitorOption || {}
if (utils.hasInvalidEOF(node)) {
return
}
if (!node.templateBody) {
return
}
const parse = defineParser(context.getSourceCode(), config)
for (const comment of node.templateBody.comments) {
if (comment.type !== 'HTMLComment') {
continue
}
if (!visitorOption.includeDirectives && isCommentDirective(comment)) {
// ignore directives
continue
}
if (isIEConditionalComment(comment)) {
// ignore IE conditional
continue
}
const tokens = parse(comment)
if (tokens) {
visitHTMLComment(tokens)
}
}
}
}
}
module.exports = {
defineVisitor
}