{"record":{"id":"d32992cda8598d2c","repo":"oxc-project/oxc","slug":"assign-literal-to-a-variable-before-exporting-as-m","errorCode":null,"errorMessage":"Assign literal to a variable before exporting as module default","messagePattern":"Assign literal to a variable before exporting as module default","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/import/no_anonymous_default_export.rs","lineNumber":191,"sourceCode":"                ctx.diagnostic(no_anonymous_default_export_diagnostic(\n                    export_decl.span,\n                    \"Assign instance to a variable before exporting as module default\",\n                ));\n            }\n            ExportDefaultDeclarationKind::ArrayExpression(_) if !self.allow_array => {\n                ctx.diagnostic(no_anonymous_default_export_diagnostic(\n                    export_decl.span,\n                    \"Assign array to a variable before exporting as module default\",\n                ));\n            }\n            _ => {\n                if let Some(expr) = export_decl.declaration.as_expression()\n                    && !self.allow_literal\n                    && (expr.is_literal() || matches!(expr, Expression::TemplateLiteral(_)))\n                {\n                    ctx.diagnostic(no_anonymous_default_export_diagnostic(\n                        export_decl.span,\n                        \"Assign literal to a variable before exporting as module default\",\n                    ));\n                }\n            }\n        }\n    }\n}\n\n#[test]\nfn test() {\n    use crate::tester::Tester;\n    use serde_json::json;\n\n    let pass = vec![\n        (\"export default function foo() {}\", None),\n        (\"export default class MyClass {}\", None),\n        (\"const foo = 123; export default foo\", None),\n        (\"export default foo(bar)\", None),\n        (\"export default []\", Some(json!([{ \"allowArray\": true }]))),","sourceCodeStart":173,"sourceCodeEnd":209,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/import/no_anonymous_default_export.rs#L173-L209","documentation":"Diagnostic from the oxlint rule import/no-anonymous-default-export (style category). It fires for the catch-all branch: the default export is a literal. Because the code checks `expr.is_literal() || matches!(expr, Expression::TemplateLiteral(_))`, this covers numbers, strings, booleans, null, BigInt and regex literals, and ALL template literals — even ones with substitutions like ``export default `${name}```. `allowLiteral` defaults to false.","triggerScenarios":"`export default 42`, `export default \"foo\"`, `export default true`, `export default null`, `export default /^123/`, and any `export default \\`...\\`` (template literals with or without placeholders) when the rule is on and `allowLiteral` is false. Emitted from the `_ =>` fallback arm in crates/oxc_linter/src/rules/import/no_anonymous_default_export.rs:191.","commonSituations":"Marker/config modules (\"export default 42\" version constants, regex patterns); code migrated from projects that allowed literal defaults; note that developers are often surprised the template-literal case is flagged even when static.","solutions":["Assign the literal to a named constant, then export it: `const VERSION = 42; export default VERSION;`","Enable `[\"warn\", { \"allowLiteral\": true }]` if literal defaults are acceptable in your codebase","Add a targeted disable comment for intentional one-offs (e.g. generated files)"],"exampleFix":"// before\nexport default `v1.2.3`;\n\n// after\nconst VERSION = `v1.2.3`;\nexport default VERSION;","handlingStrategy":"validation","validationCode":"// .oxlintrc.json\n{ \"rules\": { \"import/no-anonymous-default-export\": [\"warn\", { \"allowLiteral\": true }] } }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Remember the fallback branch flags ALL template literals as default exports, even substituted ones","Name exported constants (VERSION, PATTERN) — it also improves stack traces and debugging","Check new shared configs/presets for which allow* flags they set before adopting"],"tags":["lint","oxlint","imports","default-export","literals","template-literal","style"],"backgroundTag":"anonymous-default-export","analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}