diff --git a/lib/rules/no-misleading-character-class.js b/lib/rules/no-misleading-character-class.js index c0e87aeefcc..4016c1b3573 100644 --- a/lib/rules/no-misleading-character-class.js +++ b/lib/rules/no-misleading-character-class.js @@ -205,11 +205,14 @@ const kinds = Object.keys(findCharacterSequences); * or the node's `regex` property. * The purpose of this method is to provide a replacement for `getStaticValue` on Node.js 18, where `getStaticValue` returns `null` if a regular expression literal contains the `v` flag. * A limitation of this method is that it can only detect a regular expression if the specified node is itself a regular expression literal node. - * @param {ASTNode} node The node to be inspected. + * @param {ASTNode|null|undefined} node The node to be inspected. * @param {Scope} [initialScope] Optional scope to start finding variables. If this scope was given, this tries to resolve identifier references which are in the given node as much as possible. * @returns {{ value: any } | { value: undefined, optional?: true } | { regex: { pattern: string, flags: string } } | null} The static value of the node, or `null`. */ function getStaticValueOrRegex(node, initialScope) { + if (!node) { + return null; + } if (node.type === "Literal" && node.regex) { return { regex: node.regex }; } diff --git a/tests/lib/rules/no-misleading-character-class.js b/tests/lib/rules/no-misleading-character-class.js index 724dc5fd194..6a276ae12c2 100644 --- a/tests/lib/rules/no-misleading-character-class.js +++ b/tests/lib/rules/no-misleading-character-class.js @@ -40,6 +40,7 @@ ruleTester.run("no-misleading-character-class", rule, { "var r = /🇯🇵/", "var r = /[JP]/", "var r = /👨‍👩‍👦/", + "new RegExp()", "var r = RegExp(/[👍]/u)", "const regex = /[👍]/u; new RegExp(regex);", {