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

Code before:
                                            const replaceFlag = requireFlag ?? "u";

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

                                                    return fixer.replaceText(flagsNode, [
                                                        flagsNodeText.slice(0, flagsNodeText.length - 1).replace(
                                                            flag,
                                                            ""
                                                        ),
                                                        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 (suggestion):
```suggestion
                                                    ? ` "${replaceFlag}",`
```

These would be extra quotes inside the produced string literal. Can you also add a test for this case? (apparently, this code path wasn't tested at all).

Code after:
                                            const replaceFlag = requireFlag ?? "u";

                                            if (flagsNode) {
                                                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
                        });
                    }
                }