-
-
Notifications
You must be signed in to change notification settings - Fork 681
/
Copy pathno-irregular-whitespace.js
231 lines (209 loc) · 7.4 KB
/
no-irregular-whitespace.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
/**
* @author Yosuke Ota
* @fileoverview Rule to disalow whitespace that is not a tab or space, whitespace inside strings and comments are allowed
*/
'use strict'
const utils = require('../utils')
const ALL_IRREGULARS =
/[\f\v\u0085\uFEFF\u00A0\u1680\u180E\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u200B\u202F\u205F\u3000\u2028\u2029]/u
const IRREGULAR_WHITESPACE =
/[\f\v\u0085\uFEFF\u00A0\u1680\u180E\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u200B\u202F\u205F\u3000]+/gmu
const IRREGULAR_LINE_TERMINATORS = /[\u2028\u2029]/gmu
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'disallow irregular whitespace in `.vue` files',
categories: undefined,
url: 'https://eslint.vuejs.org/rules/no-irregular-whitespace.html',
extensionSource: {
url: 'https://eslint.org/docs/rules/no-irregular-whitespace',
name: 'ESLint core'
}
},
schema: [
{
type: 'object',
properties: {
skipComments: { type: 'boolean' },
skipStrings: { type: 'boolean' },
skipTemplates: { type: 'boolean' },
skipRegExps: { type: 'boolean' },
skipHTMLAttributeValues: { type: 'boolean' },
skipHTMLTextContents: { type: 'boolean' }
},
additionalProperties: false
}
],
messages: {
disallow: 'Irregular whitespace not allowed.'
}
},
/**
* @param {RuleContext} context - The rule context.
* @returns {RuleListener} AST event handlers.
*/
create(context) {
// Module store of error indexes that we have found
/** @type {number[]} */
let errorIndexes = []
// Lookup the `skipComments` option, which defaults to `false`.
const options = context.options[0] || {}
const skipComments = !!options.skipComments
const skipStrings = options.skipStrings !== false
const skipRegExps = !!options.skipRegExps
const skipTemplates = !!options.skipTemplates
const skipHTMLAttributeValues = !!options.skipHTMLAttributeValues
const skipHTMLTextContents = !!options.skipHTMLTextContents
const sourceCode = context.getSourceCode()
/**
* Removes errors that occur inside a string node
* @param {ASTNode | Token} node to check for matching errors.
* @returns {void}
* @private
*/
function removeWhitespaceError(node) {
const [startIndex, endIndex] = node.range
errorIndexes = errorIndexes.filter(
(errorIndex) => errorIndex < startIndex || endIndex <= errorIndex
)
}
/**
* Checks literal nodes for errors that we are choosing to ignore and calls the relevant methods to remove the errors
* @param {Literal} node to check for matching errors.
* @returns {void}
* @private
*/
function removeInvalidNodeErrorsInLiteral(node) {
const shouldCheckStrings = skipStrings && typeof node.value === 'string'
const shouldCheckRegExps = skipRegExps && Boolean(node.regex)
// If we have irregular characters, remove them from the errors list
if (
(shouldCheckStrings || shouldCheckRegExps) &&
ALL_IRREGULARS.test(sourceCode.getText(node))
) {
removeWhitespaceError(node)
}
}
/**
* Checks template string literal nodes for errors that we are choosing to ignore and calls the relevant methods to remove the errors
* @param {TemplateElement} node to check for matching errors.
* @returns {void}
* @private
*/
function removeInvalidNodeErrorsInTemplateLiteral(node) {
if (ALL_IRREGULARS.test(node.value.raw)) {
removeWhitespaceError(node)
}
}
/**
* Checks HTML attribute value nodes for errors that we are choosing to ignore and calls the relevant methods to remove the errors
* @param {VLiteral} node to check for matching errors.
* @returns {void}
* @private
*/
function removeInvalidNodeErrorsInHTMLAttributeValue(node) {
if (ALL_IRREGULARS.test(sourceCode.getText(node))) {
removeWhitespaceError(node)
}
}
/**
* Checks HTML text content nodes for errors that we are choosing to ignore and calls the relevant methods to remove the errors
* @param {VText} node to check for matching errors.
* @returns {void}
* @private
*/
function removeInvalidNodeErrorsInHTMLTextContent(node) {
if (ALL_IRREGULARS.test(sourceCode.getText(node))) {
removeWhitespaceError(node)
}
}
/**
* Checks comment nodes for errors that we are choosing to ignore and calls the relevant methods to remove the errors
* @param {Comment | HTMLComment | HTMLBogusComment} node to check for matching errors.
* @returns {void}
* @private
*/
function removeInvalidNodeErrorsInComment(node) {
if (ALL_IRREGULARS.test(node.value)) {
removeWhitespaceError(node)
}
}
/**
* Checks the program source for irregular whitespaces and irregular line terminators
* @returns {void}
* @private
*/
function checkForIrregularWhitespace() {
const source = sourceCode.getText()
let match
while ((match = IRREGULAR_WHITESPACE.exec(source)) !== null) {
errorIndexes.push(match.index)
}
while ((match = IRREGULAR_LINE_TERMINATORS.exec(source)) !== null) {
errorIndexes.push(match.index)
}
}
checkForIrregularWhitespace()
if (errorIndexes.length === 0) {
return {}
}
const bodyVisitor = utils.defineTemplateBodyVisitor(context, {
...(skipHTMLAttributeValues
? {
'VAttribute[directive=false] > VLiteral':
removeInvalidNodeErrorsInHTMLAttributeValue
}
: {}),
...(skipHTMLTextContents
? { VText: removeInvalidNodeErrorsInHTMLTextContent }
: {}),
// inline scripts
Literal: removeInvalidNodeErrorsInLiteral,
...(skipTemplates
? { TemplateElement: removeInvalidNodeErrorsInTemplateLiteral }
: {})
})
return {
...bodyVisitor,
Literal: removeInvalidNodeErrorsInLiteral,
...(skipTemplates
? { TemplateElement: removeInvalidNodeErrorsInTemplateLiteral }
: {}),
'Program:exit'(node) {
if (bodyVisitor['Program:exit']) {
bodyVisitor['Program:exit'](node)
}
const templateBody = node.templateBody
if (skipComments) {
// First strip errors occurring in comment nodes.
for (const node of sourceCode.getAllComments()) {
removeInvalidNodeErrorsInComment(node)
}
if (templateBody) {
for (const node of templateBody.comments) {
removeInvalidNodeErrorsInComment(node)
}
}
}
// Removes errors that occur outside script and template
const [scriptStart, scriptEnd] = node.range
const [templateStart, templateEnd] = templateBody
? templateBody.range
: [0, 0]
errorIndexes = errorIndexes.filter(
(errorIndex) =>
(scriptStart <= errorIndex && errorIndex < scriptEnd) ||
(templateStart <= errorIndex && errorIndex < templateEnd)
)
// If we have any errors remaining, report on them
for (const errorIndex of errorIndexes) {
context.report({
loc: sourceCode.getLocFromIndex(errorIndex),
messageId: 'disallow'
})
}
}
}
}
}