Repository: eslint/eslint
File: lib/rules/require-unicode-regexp.js (JavaScript)

Code before:
                                                if ((flagsNode.type === "Literal" && typeof flagsNode.value === "string") || flagsNode.type === "TemplateLiteral") {
                                                    const flagsNodeText = sourceCode.getText(flagsNode);
                                                    const flag = requireFlag === "u" ? "v" : "u";

                                                    if (flags.includes(flag)) {

                                                        // Avoid replacing "u" in escapes like `\uXXXX`
                                                        if (flagsNode.type === "Literal" && flagsNode.raw.includes("\\")) {
                                                            return null;
                                                        }

                                                        // Avoid replacing "u" in expressions like "`${regularFlags}g`"
                                                        if (flagsNode.type === "TemplateLiteral" && (
                                                            flagsNode.expressions.length ||
                                                            flagsNode.quasis.some(({ value: { raw } }) => raw.includes("\\"))
                                                        )) {
                                                            return null;
                                                        }

                                                        return fixer.replaceText(flagsNode, [
                                                            flagsNodeText.slice(0, flagsNodeText.length - 1).replace(
                                                                flag,
                                                                ""
                                                            ),
                                                            flagsNodeText.slice(flagsNodeText.length - 1)
                                                        ].join(replaceFlag));
                                                    }

                                                    return fixer.replaceText(flagsNode, [
                                                        flagsNodeText.slice(0, flagsNodeText.length - 1),
                                                        flagsNodeText.slice(flagsNodeText.length - 1)
                                                    ].join(replaceFlag));
                                                }

                                                // We intentionally don't suggest concatenating + "u" to non-literals
                                                return null;
                                            }

                                            const penultimateToken = sourceCode.getLastToken(refNode, { skip: 1 }); // skip closing parenthesis

                                            return fixer.insertTextAfter(
                                                penultimateToken,
                                                astUtils.isCommaToken(penultimateToken)
                                                    ? ` "${replaceFlag}",`
                                                    : `, "${replaceFlag}"`
                                            );
                                        },
                                        messageId: requireFlag === "v" ? "addVFlag" : "addUFlag"
                                    }
                                ]
                                : null

Review comment (refactor):
```suggestion
                                                        return fixer.replaceText(flagsNode, flagsNodeText.replace(flag, replaceFlag));
```

As we now have a separate fixer for this case, I think we can simplify it this way. That would also make it consistent with the similar case with regex literals where one flag is replaced with the other at the same position. 

Code after:
                                                if ((flagsNode.type === "Literal" && typeof flagsNode.value === "string") || flagsNode.type === "TemplateLiteral") {
                                                    const flagsNodeText = sourceCode.getText(flagsNode);
                                                    const flag = requireFlag === "u" ? "v" : "u";

                                                    if (flags.includes(flag)) {

                                                        // Avoid replacing "u" in escapes like `\uXXXX`
                                                        if (flagsNode.type === "Literal" && flagsNode.raw.includes("\\")) {
                                                            return null;
                                                        }

                                                        // Avoid replacing "u" in expressions like "`${regularFlags}g`"
                                                        if (flagsNode.type === "TemplateLiteral" && (
                                                            flagsNode.expressions.length ||
                                                            flagsNode.quasis.some(({ value: { raw } }) => raw.includes("\\"))
                                                        )) {
                                                            return null;
                                                        }

                                                        return fixer.replaceText(flagsNode, flagsNodeText.replace(flag, replaceFlag));
                                                    }

                                                    return fixer.replaceText(flagsNode, [
                                                        flagsNodeText.slice(0, flagsNodeText.length - 1),
                                                        flagsNodeText.slice(flagsNodeText.length - 1)
                                                    ].join(replaceFlag));
                                                }

                                                // We intentionally don't suggest concatenating + "u" to non-literals
                                                return null;
                                            }

                                            const penultimateToken = sourceCode.getLastToken(refNode, { skip: 1 }); // skip closing parenthesis

                                            return fixer.insertTextAfter(
                                                penultimateToken,
                                                astUtils.isCommaToken(penultimateToken)
                                                    ? ` "${replaceFlag}",`
                                                    : `, "${replaceFlag}"`
                                            );
                                        },
                                        messageId: requireFlag === "v" ? "addVFlag" : "addUFlag"
                                    }
                                ]
                                : null
                        });
                    }
                }
            }
        };
    }