Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tools: auto fix custom eslint rule for no-unescaped-regexp-dot #16673

Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions test/parallel/test-eslint-no-unescaped-regexp-dot.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ new RuleTester().run('no-unescaped-regexp-dot', rule, {
],
invalid: [
{
code: '/./',
errors: [{ message: 'Unescaped dot character in regular expression' }]
code: String.raw`/./`,
errors: [{ message: 'Unescaped dot character in regular expression' }],
output: String.raw`/\./`
},
{
code: String.raw`/\\./`,
errors: [{ message: 'Unescaped dot character in regular expression' }]
errors: [{ message: 'Unescaped dot character in regular expression' }],
output: String.raw`/\./`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems wrong... ? I believe it should be more like:

String.raw`/\\\./`

The \\ is part of the RegExp.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the logic and test file.

}
]
});
21 changes: 20 additions & 1 deletion tools/eslint-rules/no-unescaped-regexp-dot.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,29 @@ module.exports = function(context) {

function report(node, startOffset) {
const indexOfDot = sourceCode.getIndexFromLoc(node.loc.start) + startOffset;
const expression = sourceCode.getText(node);
const dotOffset = expression.indexOf('.');
var correctedExpression = '';

if (expression.charAt(dotOffset - 1) === '\\') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this code should run unless a fixer is requested.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@apapirovski Fixed.

correctedExpression = expression.slice(0, dotOffset - 1) +
expression.slice(dotOffset);
} else {
correctedExpression = expression.slice(0, dotOffset) +
'\\' +
expression.slice(dotOffset);
}

context.report({
node,
loc: sourceCode.getLocFromIndex(indexOfDot),
message: 'Unescaped dot character in regular expression'
message: 'Unescaped dot character in regular expression',
fix: (fixer) => {
return fixer.replaceText(
node,
correctedExpression
);
}
});
}

Expand Down