Skip to content

Commit e088d9e

Browse files
committed
feat: suggestions
1 parent 9373b88 commit e088d9e

File tree

1 file changed

+68
-12
lines changed

1 file changed

+68
-12
lines changed

lib/rules/invalid-regex-rule.js

Lines changed: 68 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ function foundStartLocation(source, foundAt) {
2828
* @param {{ regex: RegExp, id: string | undefined, message: string | undefined }} pattern Information of the pattern to be use to inspect.
2929
* @param {*} replace Replacement function
3030
* @param {Function} report Report function
31+
* @param {*[]} [suggestions] Array of suggestion functions.
3132
*/
32-
function checkRegex(source, pattern, replace, report) {
33+
function checkRegex(source, pattern, replace, report, suggestions = []) {
3334
/**
3435
* @param {string} source Text to be checked.
3536
* @param {RegExp} regex Regular Expression use to check.
@@ -53,7 +54,11 @@ function checkRegex(source, pattern, replace, report) {
5354
report({
5455
loc: { start: foundStartLocation(source, matchDetail.matchStart) },
5556
message,
56-
fix: replace(0, matchDetail)
57+
fix: replace(0, matchDetail),
58+
suggest: suggestions.map(s => ({
59+
desc: s.message,
60+
fix: s.replacement(0, matchDetail),
61+
})),
5762
})
5863
}
5964
}
@@ -106,14 +111,27 @@ function createReplacement(replacement) {
106111
}
107112

108113
function checkPatterns(fileName, source, patterns, report) {
109-
patterns.forEach(pattern => shouldCheck(pattern.files, fileName) &&
110-
checkRegex(source, pattern, createReplacement(pattern.details.replacement), report))
114+
patterns.forEach(pattern => {
115+
if (!shouldCheck(pattern.files, fileName)) {
116+
return
117+
}
118+
119+
const replacement = createReplacement(pattern.details.replacement)
120+
let suggestions = pattern.details.suggestions || []
121+
suggestions = suggestions.map((d) => ({
122+
message: d.message,
123+
replacement: createReplacement(d.replacement)
124+
}))
125+
126+
checkRegex(source, pattern, replacement, report, suggestions)
127+
})
111128
}
112129

113130
module.exports = {
114131
meta: {
115132
type: 'suggestion',
116133
fixable: 'code',
134+
hasSuggestions: true,
117135
docs: {
118136
description: 'Invalid regular expressions to be reported',
119137
category: 'Stylistic Issues',
@@ -168,15 +186,53 @@ module.exports = {
168186
maxProperties: 1
169187
}]
170188
},
171-
message: {
172-
title: 'Invalid message',
173-
description: 'Message to be shown when Invalid pattern is found',
174-
type: 'string',
175-
minLength: 3
189+
suggestions: {
190+
type: 'array',
191+
description: 'Array of suggestions to be used when the pattern is found',
192+
title: 'Suggestions',
193+
items: {
194+
type: 'object',
195+
description: 'Suggestion to be used when the pattern is found',
196+
properties: {
197+
message: {
198+
title: 'Suggestion message',
199+
description: 'Message to be shown when the suggestion is applied',
200+
type: 'string',
201+
minLength: 3
202+
},
203+
replacement: {
204+
oneOf: [{
205+
title: 'Replacement',
206+
description: 'Replacement for invalid pattern',
207+
type: 'string'
208+
}, {
209+
title: 'Detailed replacement',
210+
description: 'Detailed replacements for invalid patterns',
211+
type: 'object',
212+
properties: {
213+
function: {
214+
title: 'Replacement function',
215+
description: 'Function used to replace the found pattern. It receives the found text and must return the replacement text',
216+
type: 'string',
217+
minLength: 1
218+
}
219+
},
220+
minProperties: 1,
221+
maxProperties: 1
222+
}]
223+
}
224+
},
225+
},
226+
message: {
227+
title: 'Invalid message',
228+
description: 'Message to be shown when Invalid pattern is found',
229+
type: 'string',
230+
minLength: 3
231+
},
232+
files: FILES_FIELD_DEFINITION
176233
},
177-
files: FILES_FIELD_DEFINITION
178-
},
179-
required: ['regex']
234+
required: ['regex']
235+
}
180236
}]
181237
},
182238
minItems: 1

0 commit comments

Comments
 (0)