Skip to content

Commit 8919f73

Browse files
fix: suggestion helper function
1 parent 3d9b43b commit 8919f73

File tree

1 file changed

+38
-59
lines changed

1 file changed

+38
-59
lines changed

lib/rules/no-invalid-html-attribute.js

Lines changed: 38 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,21 @@ const messages = {
234234
spaceDelimited: '”{{attributeName}}“ attribute values should be space delimited.',
235235
};
236236

237+
const fixModes = {
238+
remove: 'remove',
239+
replaceTextRange: 'removeTextRange',
240+
removeRange: 'removeRange'
241+
}
242+
243+
const fixMessages = {
244+
removeDefault : '"remove {{attributeName}}"',
245+
removeEmpty:'"remove empty attribute {{attributeName}}"',
246+
removeInvalid: '“remove invalid attribute {{reportingValue}}”',
247+
removeWhitespaces: 'remove whitespaces in “{{reportingValue}}”',
248+
249+
250+
}
251+
237252
function splitIntoRangedParts(node, regex) {
238253
const valueRangeStart = node.range[0] + 1; // the plus one is for the initial quote
239254

@@ -254,14 +269,7 @@ function checkLiteralValueNode(context, attributeName, node, parentNode, parentN
254269
report(context, messages.onlyStrings, 'onlyStrings', {
255270
node,
256271
data: { attributeName },
257-
suggest: [
258-
{
259-
desc: "To Be Replaced By Appropriate Message",
260-
fix: function(fixer) {
261-
return fixer.remove(parentNode);
262-
}
263-
}
264-
]
272+
suggestion(fixMessages.removeNonString, fixer, fixModes.remove, parentNode)
265273
});
266274
return;
267275
}
@@ -270,14 +278,7 @@ function checkLiteralValueNode(context, attributeName, node, parentNode, parentN
270278
report(context, messages.noEmpty, 'noEmpty', {
271279
node,
272280
data: { attributeName },
273-
suggest: [
274-
{
275-
desc: "To Be Replaced By Appropriate Message",
276-
fix: function(fixer) {
277-
return fixer.remove(parentNode);
278-
}
279-
}
280-
]
281+
suggestion(fixMessages.removeEmpty, fixer, fixModes.remove, attributeName)
281282
});
282283
return;
283284
}
@@ -293,14 +294,7 @@ function checkLiteralValueNode(context, attributeName, node, parentNode, parentN
293294
attributeName,
294295
reportingValue,
295296
},
296-
suggest: [
297-
{
298-
desc: "To Be Replaced By Appropriate Message",
299-
fix: function(fixer) {
300-
return fixer.removeRange(singlePart.range);
301-
}
302-
}
303-
]
297+
suggestion(fixMessages.removeInvalid, fixer, fixModes.removeRange, singlePart.range)
304298
});
305299
} else if (!allowedTags.has(parentNodeName)) {
306300
report(context, messages.notValidFor, 'notValidFor', {
@@ -310,14 +304,7 @@ function checkLiteralValueNode(context, attributeName, node, parentNode, parentN
310304
reportingValue,
311305
elementName: parentNodeName,
312306
},
313-
suggest: [
314-
{
315-
desc: "To Be Replaced By Appropriate Message",
316-
fix: function(fixer) {
317-
return fixer.removeRange(singlePart.range);
318-
}
319-
}
320-
]
307+
suggestion(fixMessages.removeInvalid, fixer, fixModes.removeRange, singlePart.range)
321308
});
322309
}
323310
}
@@ -357,27 +344,13 @@ function checkLiteralValueNode(context, attributeName, node, parentNode, parentN
357344
report(context, messages.spaceDelimited, 'spaceDelimited', {
358345
node,
359346
data: { attributeName },
360-
suggest: [
361-
{
362-
desc: "To Be Replaced By Appropriate Message",
363-
fix: function(fixer) {
364-
return fixer.removeRange(whitespacePart.range);
365-
}
366-
}
367-
]
347+
suggestion(fixMessages.removeWhitespaces, fixer, fixModes.removeRange, whitespacePart.range)
368348
});
369349
} else if (whitespacePart.value !== '\u0020') {
370350
report(context, messages.spaceDelimited, 'spaceDelimited', {
371351
node,
372352
data: { attributeName },
373-
suggest: [
374-
{
375-
desc: "To Be Replaced By Appropriate Message",
376-
fix: function(fixer) {
377-
return fixer.removeRange(whitespacePart.range, '\u0020');
378-
}
379-
}
380-
]
353+
suggestion(fixMessages.removeWhitespaces, fixer, mofixModes.replaceTextRange, whitespacePart.range, '\u0020' )
381354
});
382355
}
383356
}
@@ -388,17 +361,23 @@ const DEFAULT_ATTRIBUTES = ['rel'];
388361
function checkAttribute(context, node) {
389362
const attribute = node.name.name;
390363

391-
function fix(fixer) {
392-
return fixer.remove(node);
364+
function fix(fixer, mode, ...args) {
365+
if (mode === 'removeTextRange') {
366+
return fixer.replaceTextRange(args)
367+
}
368+
else if (mode === 'removeRange') {
369+
return fixer.removeRange(args)
370+
}
371+
else {
372+
return fixer.remove(args)
373+
}
393374
}
394375

395-
function suggestion(desc, fixer){
376+
function suggestion(desc, fixer, mode, ...args) {
396377
return [
397378
{
398-
desc: desc,
399-
fix: function(fixer) {
400-
return fixer.remove(node);
401-
}
379+
desc: desc,
380+
fix(fixer, mode, args)
402381
}
403382
]
404383
}
@@ -415,7 +394,7 @@ function checkAttribute(context, node) {
415394
attributeName: attribute,
416395
tagNames,
417396
},
418-
suggest:suggestion("hehe"),
397+
suggestion(fixMessages.removeDefault, fixer, fixModes.remove, attributeName)
419398
});
420399
return;
421400
}
@@ -424,7 +403,7 @@ function checkAttribute(context, node) {
424403
report(context, messages.emptyIsMeaningless, 'emptyIsMeaningless', {
425404
node,
426405
data: { attributeName: attribute },
427-
suggest:suggestion("hehe"),
406+
suggestion(fixMessages.removeEmpty, fixer, fixModes.removemode, attributeName)
428407
});
429408
return;
430409
}
@@ -445,7 +424,7 @@ function checkAttribute(context, node) {
445424
report(context, messages.onlyStrings, 'onlyStrings', {
446425
node,
447426
data: { attributeName: attribute },
448-
suggest:suggestion("hehe"),
427+
suggestion(fixMessages.removeDefault, fixer, fixModes.remove, attributeName)
449428
});
450429
return;
451430
}
@@ -454,7 +433,7 @@ function checkAttribute(context, node) {
454433
report(context, messages.onlyStrings, 'onlyStrings', {
455434
node,
456435
data: { attributeName: attribute },
457-
suggest:suggestion("hehe"),
436+
suggestion(fixMessages.removeDefault fixer, fixModes.remove, attributeName)
458437
});
459438
}
460439
}

0 commit comments

Comments
 (0)