Skip to content

Commit

Permalink
Use canTokensBeAdjacent
Browse files Browse the repository at this point in the history
  • Loading branch information
Yash-Singh1 committed Sep 17, 2021
1 parent dc34aa3 commit 52e6e00
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/rules/prefer-regex-literals.js
Expand Up @@ -12,6 +12,7 @@
const astUtils = require("./utils/ast-utils");
const { CALL, CONSTRUCT, ReferenceTracker, findVariable, getStaticValue } = require("eslint-utils");
const { validateRegExpLiteral } = require("regexpp");
const { canTokensBeAdjacent } = require("./utils/ast-utils");

//------------------------------------------------------------------------------
// Helpers
Expand Down Expand Up @@ -312,9 +313,13 @@ module.exports = {
messageId: "unexpectedRegExp",
...(noFix ? {} : {
fix(fixer) {
const tokenAfter = sourceCode.getTokenAfter(node);

return fixer.replaceTextRange(
node.range,
(text[node.range[0] - 1] === "/" ? " " : "") + newRegExpValue + (["in", "instanceof"].includes((sourceCode.getTokenAfter(node) || {}).value) && text[node.range[1]] === "i" ? " " : "")
(tokenBefore && !canTokensBeAdjacent(tokenBefore, newRegExpValue) && /\S/u.test(text[node.range[0] - 1]) ? " " : "") +
newRegExpValue +
(tokenAfter && !canTokensBeAdjacent(newRegExpValue, tokenAfter) && /\S/u.test(text[node.range[1]]) ? " " : "")
);
}
})
Expand Down

0 comments on commit 52e6e00

Please sign in to comment.