{"record":{"id":"2354492557fd9af8","repo":"oxc-project/oxc","slug":"regular-expression-literal-is-unnecessarily-wrappe","errorCode":null,"errorMessage":"Regular expression literal is unnecessarily wrapped within a `RegExp` constructor.","messagePattern":"Regular expression literal is unnecessarily wrapped within a `RegExp` constructor\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/eslint/prefer_regex_literals.rs","lineNumber":25,"sourceCode":"};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_span::Span;\n\nuse crate::{\n    AstNode,\n    context::LintContext,\n    rule::{DefaultRuleConfig, Rule},\n    utils::{is_regexp_callee, is_string_raw_member_expression},\n};\n\nfn unexpected_regexp_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Use a regular expression literal instead of the `RegExp` constructor.\")\n        .with_label(span)\n}\n\nfn unexpected_redundant_regexp_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\n        \"Regular expression literal is unnecessarily wrapped within a `RegExp` constructor.\",\n    )\n    .with_label(span)\n}\n\nfn unexpected_redundant_regexp_with_flags_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\n        \"Use regular expression literal with flags instead of the `RegExp` constructor.\",\n    )\n    .with_label(span)\n}\n\n#[derive(Debug, Default, Clone, Serialize, Deserialize, JsonSchema)]\n#[serde(rename_all = \"camelCase\", default, deny_unknown_fields)]\nstruct PreferRegexLiteralsConfig {\n    /// By default, this rule doesn’t check when a regex literal is unnecessarily wrapped in a `RegExp` constructor call.\n    /// When the option `disallowRedundantWrapping` is set to `true`, the rule will also disallow such unnecessary patterns.\n    ///","sourceCodeStart":7,"sourceCodeEnd":43,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/eslint/prefer_regex_literals.rs#L7-L43","documentation":"oxlint `eslint/prefer-regex-literals` with the option `disallowRedundantWrapping: true`: a regex literal wrapped directly in a `RegExp` constructor (`new RegExp(/abc/)`) is redundant because the literal is already a RegExp object. The message comes from `unexpected_redundant_regexp_diagnostic` (prefer_regex_literals.rs:25). Per the config doc comment in the file, this variant is only checked when `disallowRedundantWrapping` is enabled — by default the rule ignores such wrapping.","triggerScenarios":"Enabling `{ \"disallowRedundantWrapping\": true }` in the rule config and writing `new RegExp(/abc/)` or `RegExp(/abc/)` — a single regex-literal argument with no flags argument.","commonSituations":"Config copied from an ESLint setup that sets disallowRedundantWrapping; refactored code where flags were removed from the inner literal but the wrapper stayed; generated or migrated code.","solutions":["Unwrap the literal: `new RegExp(/abc/)` becomes `/abc/`.","Run `oxlint --fix` — the rule ships an auto-fixer for this variant.","If the wrapping is intentional (e.g. defensive cloning of a shared regex), disable for the line or set `disallowRedundantWrapping` back to `false` (the default)."],"exampleFix":"// before\nconst re = new RegExp(/abc/);\n\n// after\nconst re = /abc/;","handlingStrategy":"validation","validationCode":"# find regex literals wrapped in a RegExp constructor\nrg -n 'new\\s+RegExp\\(\\s*/' src/","typeGuard":null,"tryCatchPattern":null,"preventionTips":["When refactoring a `new RegExp(\"...\", flags)` into a literal, remove the constructor wrapper in the same commit.","Keep `disallowRedundantWrapping` enabled in shared configs so redundant wrapping never lands.","Run `oxlint --fix` in CI or pre-commit; the redundant-wrapping variants are auto-fixable."],"tags":["eslint","oxlint","regex","redundant-code","lint"],"backgroundTag":"prefer-regex-literals","analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}