{"record":{"id":"b70a1769906d2f0b","repo":"oxc-project/oxc","slug":"prefer-default-export-on-a-file-with-single-export","errorCode":null,"errorMessage":"Prefer default export on a file with single export.","messagePattern":"Prefer default export on a file with single export\\.","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/import/prefer_default_export.rs","lineNumber":17,"sourceCode":"use oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_span::Span;\nuse schemars::JsonSchema;\nuse serde::{Deserialize, Serialize};\nuse serde_json::Value;\n\nuse crate::{\n    context::LintContext,\n    module_record::ExportEntry,\n    rule::{DefaultRuleConfig, Rule},\n};\n\nfn prefer_default_export_diagnostic(span: Span, target: Target) -> OxcDiagnostic {\n    let msg = if target == Target::Single {\n        \"Prefer default export on a file with single export.\"\n    } else {\n        \"Prefer default export to be present on every file that has export.\"\n    };\n    OxcDiagnostic::warn(msg).with_help(\"Prefer a default export\").with_label(span)\n}\n\n#[derive(Debug, Default, PartialEq, Clone, Copy, Deserialize, Serialize, JsonSchema)]\n#[serde(rename_all = \"kebab-case\")]\nenum Target {\n    /// Prefer default export when there is only one export in the module.\n    #[default]\n    Single,\n    /// Prefer default export in any module that has exports.\n    Any,\n}\n\n#[derive(Debug, Default, Clone, Deserialize, Serialize, JsonSchema)]\n#[serde(rename_all = \"camelCase\", default, deny_unknown_fields)]\npub struct PreferDefaultExport {","sourceCodeStart":1,"sourceCodeEnd":35,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/import/prefer_default_export.rs#L1-L35","documentation":"This is oxlint's 'import/prefer-default-export' diagnostic in its default 'single' target mode. When a module contains exactly one named export, the rule asks you to make that export the default export instead. It mirrors the ESLint rule of the same name and exists because a lone named export is usually the module's main purpose, and default import syntax makes consumers shorter and refactoring simpler.","triggerScenarios":"Enable import/prefer-default-export with target \"single\" (the default) and lint a module whose module record contains exactly one export entry that is not a default export, e.g. export const theme = ... alone in a file.","commonSituations":"Style-guide-driven codebases (airbnb-style configs enable this rule) flag utility files, constants files, and single-component files that grew from snippets. The warning appears the moment a second export is removed during refactoring, leaving one survivor.","solutions":["Convert the single named export to a default export: change 'export const x = ...' to 'const x = ...; export default x;' and update importers to 'import x from ...'.","If more exports are planned, add them so the file no longer has exactly one export.","If your style guide prefers named exports everywhere, switch the rule to target \"any\" is not the fix here — instead disable the rule or override it to \"off\" in .oxlintrc.json.","Suppress one-off cases with // oxlint-disable-next-line import/prefer-default-export."],"exampleFix":"// before (theme.ts)\nexport const theme = { primary: 'blue' };\n\n// after (theme.ts)\nconst theme = { primary: 'blue' };\nexport default theme;\n\n// consumers: import theme from './theme';","handlingStrategy":"validation","validationCode":"// quick script: list files with exactly one non-default export\n// run before enabling import/prefer-default-export\nconst { execSync } = require('node:child_process');\nconsole.log(execSync(\"rg -l '^export (const|function|class) ' src/\", { encoding: 'utf8' }));","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Agree on default vs named export convention per project before enabling this rule.","During refactors that leave a single export, remember the rule will fire; convert it to a default export.","Document the convention in CONTRIBUTING so new files comply from the start."],"tags":["lint","import","export","style","esm","oxlint"],"backgroundTag":"module-export-style","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"}