Skip to content

Commit ff01725

Browse files
tools: auto fix custom eslint rule for no-unescaped-regexp-dot
1. Added fixer method for resolving lint error. 2. Added additional valid tests + Output for current and new invalid tests. Refs: nodejs#16636
1 parent 46ca177 commit ff01725

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

test/parallel/test-eslint-no-unescaped-regexp-dot.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,26 @@ new RuleTester().run('no-unescaped-regexp-dot', rule, {
1313
'/.*/',
1414
'/.?/',
1515
'/.{5}/',
16-
String.raw`/\\\./`
16+
String.raw`/\\\./`,
17+
RegExp(/^\.$/),
18+
RegExp(/^.+$/),
19+
RegExp(/^\\\.$/),
1720
],
1821
invalid: [
1922
{
2023
code: '/./',
21-
errors: [{ message: 'Unescaped dot character in regular expression' }]
24+
errors: [{ message: 'Unescaped dot character in regular expression' }],
25+
output: '/\\./'
2226
},
2327
{
2428
code: String.raw`/\\./`,
25-
errors: [{ message: 'Unescaped dot character in regular expression' }]
29+
errors: [{ message: 'Unescaped dot character in regular expression' }],
30+
output: String.raw`/\./`,
31+
},
32+
{
33+
code: 'RegExp(/^\\.$/)',
34+
errors: [{ message: 'Unescaped dot character in regular expression' }],
35+
output: RegExp(/^\.$/),
2636
}
2737
]
2838
});

tools/eslint-rules/no-unescaped-regexp-dot.js

+20-1
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,29 @@ module.exports = function(context) {
1616

1717
function report(node, startOffset) {
1818
const indexOfDot = sourceCode.getIndexFromLoc(node.loc.start) + startOffset;
19+
const expression = sourceCode.getText(node);
20+
const dotOffset = expression.indexOf('.');
21+
var correctedExpression = '';
22+
23+
if (expression.charAt(dotOffset - 1) === '\\') {
24+
correctedExpression = expression.slice(0, dotOffset - 1) +
25+
expression.slice(dotOffset);
26+
} else {
27+
correctedExpression = expression.slice(0, dotOffset) +
28+
'\\' +
29+
expression.slice(dotOffset);
30+
}
31+
1932
context.report({
2033
node,
2134
loc: sourceCode.getLocFromIndex(indexOfDot),
22-
message: 'Unescaped dot character in regular expression'
35+
message: 'Unescaped dot character in regular expression',
36+
fix: (fixer) => {
37+
return fixer.replaceText(
38+
node,
39+
correctedExpression
40+
);
41+
}
2342
});
2443
}
2544

0 commit comments

Comments
 (0)