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

Code before:

        messages: {
            addUFlag: "Add the 'u' flag.",
            requireUFlag: "Use the 'u' flag."
        },

        schema: []
    },

    create(context) {
        return {
            "Literal[regex]"(node) {
                const flags = node.regex.flags || "";

                if (!flags.includes("u")) {
                    context.report({
                        messageId: "requireUFlag",
                        node,
                        suggest: [
                            {
                                fix(fixer) {
                                    return fixer.insertTextAfter(node, "u");
                                },
                                messageId: "addUFlag"
                            }
                        ]
                    });
                }
            },

            Program() {
                const scope = context.getScope();
                const sourceCode = context.getSourceCode();
                const tracker = new ReferenceTracker(scope);
                const trackMap = {
                    RegExp: { [CALL]: true, [CONSTRUCT]: true }
                };

                for (const { node } of tracker.iterateGlobalReferences(trackMap)) {
                    const flagsNode = node.arguments[1];
                    const flags = getStringIfConstant(flagsNode, scope);

                    if (!flagsNode || (typeof flags === "string" && !flags.includes("u"))) {
                        context.report({
                            messageId: "requireUFlag",
                            node,
                            suggest: [
                                {
                                    fix(fixer) {
                                        if (flagsNode) {
                                            if (flagsNode.type === "Literal") {

Review comment (security):
We should check if the regex literal would be syntactically valid with the `u` flag, and not provide the suggestion if it wouldn't be valid.

For example:

```js
/\a/
```

If the rule provides a suggestion that adds `u`, after the suggestion is applied the code will have a parsing error:

```js
/\a/u // Parsing error: Invalid regular expression: /\a/: Invalid escape
```

You can check how this is implemented in the [`no-misleading-character-class`](https://github.com/eslint/eslint/blob/1fbf1184fed57df02640aad4659afb54dc26a2e9/lib/rules/no-misleading-character-class.js#L195) rule.

(the same for the RegExp constructor branch)

Code after:

        hasSuggestions: true,

        messages: {
            addUFlag: "Add the 'u' flag.",
            requireUFlag: "Use the 'u' flag."
        },

        schema: []
    },

    create(context) {

        const sourceCode = context.getSourceCode();

        return {
            "Literal[regex]"(node) {
                const flags = node.regex.flags || "";

                if (!flags.includes("u")) {
                    context.report({
                        messageId: "requireUFlag",
                        node,
                        suggest: isValidWithUnicodeFlag(context.languageOptions.ecmaVersion, node.regex.pattern)
                            ? [
                                {
                                    fix(fixer) {
                                        return fixer.insertTextAfter(node, "u");
                                    },
                                    messageId: "addUFlag"
                                }
                            ]
                            : null
                    });
                }
            },

            Program(node) {
                const scope = sourceCode.getScope(node);
                const tracker = new ReferenceTracker(scope);
                const trackMap = {
                    RegExp: { [CALL]: true, [CONSTRUCT]: true }
                };

                for (const { node: refNode } of tracker.iterateGlobalReferences(trackMap)) {
                    const [patternNode, flagsNode] = refNode.arguments;
                    const pattern = getStringIfConstant(patternNode, scope);
                    const flags = getStringIfConstant(flagsNode, scope);

                    if (!flagsNode || (typeof flags === "string" && !flags.includes("u"))) {
                        context.report({