Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions crates/oxc_linter/src/rules/react/only_export_components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,44 +12,36 @@ use crate::{
rule::{DefaultRuleConfig, Rule},
};

const SCOPE: &str = "eslint-plugin-react-refresh";

fn export_all_components_diagnostic(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("This rule can't verify that `export *` only exports components.")
.with_label(span)
.with_error_code_scope(SCOPE)
}

fn named_export_components_diagnostic(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Fast refresh only works when a file only exports components. Use a new file to share constants or functions between components.")
.with_label(span)
.with_error_code_scope(SCOPE)
}

fn anonymous_components_diagnostic(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn(
"Fast refresh can't handle anonymous components. Add a name to your export.",
)
.with_label(span)
.with_error_code_scope(SCOPE)
}

fn local_components_diagnostic(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Fast refresh only works when a file only exports components. Move your component(s) to a separate file.")
.with_label(span)
.with_error_code_scope(SCOPE)
}

fn no_export_diagnostic(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Fast refresh only works when a file has exports. Move your component(s) to a separate file.")
.with_label(span)
.with_error_code_scope(SCOPE)
}

fn react_context_diagnostic(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Fast refresh only works when a file only exports components. Move your React context(s) to a separate file.")
.with_label(span)
.with_error_code_scope(SCOPE)
}

#[derive(Debug, Default, Clone, Deserialize)]
Expand Down Expand Up @@ -100,6 +92,8 @@ declare_oxc_lint!(
/// (e.g. `createRoot(...).render(<App />)`) to match what integrations like
/// `react-refresh` expect.
///
/// This rule is based on the rule from `eslint-plugin-react-refresh`.
///
/// ### Why is this bad?
///
/// Fast Refresh can only reliably retain state if a module exports components and
Expand Down
42 changes: 21 additions & 21 deletions crates/oxc_linter/src/snapshots/react_only_export_components.snap
Original file line number Diff line number Diff line change
@@ -1,129 +1,129 @@
---
source: crates/oxc_linter/src/tester.rs
---
⚠ eslint-plugin-react-refresh(only-export-components): Fast refresh only works when a file only exports components. Use a new file to share constants or functions between components.
⚠ eslint-plugin-react(only-export-components): Fast refresh only works when a file only exports components. Use a new file to share constants or functions between components.
╭─[only_export_components.tsx:1:14]
1 │ export const foo = () => {}; export const Bar = () => {};
· ───
╰────

⚠ eslint-plugin-react-refresh(only-export-components): Fast refresh only works when a file only exports components. Use a new file to share constants or functions between components.
⚠ eslint-plugin-react(only-export-components): Fast refresh only works when a file only exports components. Use a new file to share constants or functions between components.
╭─[only_export_components.tsx:1:14]
1 │ export const foo = () => {}; export const Bar = () => {};
· ───
╰────

⚠ eslint-plugin-react-refresh(only-export-components): Fast refresh only works when a file only exports components. Use a new file to share constants or functions between components.
⚠ eslint-plugin-react(only-export-components): Fast refresh only works when a file only exports components. Use a new file to share constants or functions between components.
╭─[only_export_components.tsx:1:14]
1 │ export const foo = 4; export const Bar = () => {};
· ───
╰────

⚠ eslint-plugin-react-refresh(only-export-components): Fast refresh only works when a file only exports components. Use a new file to share constants or functions between components.
⚠ eslint-plugin-react(only-export-components): Fast refresh only works when a file only exports components. Use a new file to share constants or functions between components.
╭─[only_export_components.tsx:1:46]
1 │ export function Component() {}; export const Aa = 'a'
· ──
╰────

⚠ eslint-plugin-react-refresh(only-export-components): Fast refresh only works when a file only exports components. Use a new file to share constants or functions between components.
⚠ eslint-plugin-react(only-export-components): Fast refresh only works when a file only exports components. Use a new file to share constants or functions between components.
╭─[only_export_components.tsx:1:47]
1 │ const foo = 4; const Bar = () => {}; export { foo, Bar };
· ───
╰────

⚠ eslint-plugin-react-refresh(only-export-components): This rule can't verify that `export *` only exports components.
⚠ eslint-plugin-react(only-export-components): This rule can't verify that `export *` only exports components.
╭─[only_export_components.tsx:1:1]
1 │ export * from './foo';
· ──────────────────────
╰────

⚠ eslint-plugin-react-refresh(only-export-components): Fast refresh can't handle anonymous components. Add a name to your export.
⚠ eslint-plugin-react(only-export-components): Fast refresh can't handle anonymous components. Add a name to your export.
╭─[only_export_components.tsx:1:1]
1 │ export default () => {};
· ────────────────────────
╰────

⚠ eslint-plugin-react-refresh(only-export-components): Fast refresh can't handle anonymous components. Add a name to your export.
⚠ eslint-plugin-react(only-export-components): Fast refresh can't handle anonymous components. Add a name to your export.
╭─[only_export_components.tsx:1:1]
1 │ export default memo(() => {});
· ──────────────────────────────
╰────

⚠ eslint-plugin-react-refresh(only-export-components): Fast refresh can't handle anonymous components. Add a name to your export.
⚠ eslint-plugin-react(only-export-components): Fast refresh can't handle anonymous components. Add a name to your export.
╭─[only_export_components.tsx:1:16]
1 │ export default function () {};
· ──────────────
╰────

⚠ eslint-plugin-react-refresh(only-export-components): Fast refresh only works when a file only exports components. Use a new file to share constants or functions between components.
⚠ eslint-plugin-react(only-export-components): Fast refresh only works when a file only exports components. Use a new file to share constants or functions between components.
╭─[only_export_components.tsx:1:14]
1 │ export const CONSTANT = 3; export const Foo = () => {};
· ────────
╰────

⚠ eslint-plugin-react-refresh(only-export-components): Fast refresh only works when a file only exports components. Use a new file to share constants or functions between components.
⚠ eslint-plugin-react(only-export-components): Fast refresh only works when a file only exports components. Use a new file to share constants or functions between components.
╭─[only_export_components.tsx:1:13]
1 │ export enum Tab { Home, Settings }; export const Bar = () => {};
· ───
╰────

⚠ eslint-plugin-react-refresh(only-export-components): Fast refresh only works when a file only exports components. Move your component(s) to a separate file.
⚠ eslint-plugin-react(only-export-components): Fast refresh only works when a file only exports components. Move your component(s) to a separate file.
╭─[only_export_components.tsx:1:7]
1 │ const Tab = () => {}; export const tabs = [<Tab />, <Tab />];
· ───
╰────

⚠ eslint-plugin-react-refresh(only-export-components): Fast refresh only works when a file has exports. Move your component(s) to a separate file.
⚠ eslint-plugin-react(only-export-components): Fast refresh only works when a file has exports. Move your component(s) to a separate file.
╭─[only_export_components.tsx:1:7]
1 │ const App = () => {}; createRoot(document.getElementById('root')).render(<App />);
· ───
╰────

⚠ eslint-plugin-react-refresh(only-export-components): Fast refresh only works when a file only exports components. Use a new file to share constants or functions between components.
⚠ eslint-plugin-react(only-export-components): Fast refresh only works when a file only exports components. Use a new file to share constants or functions between components.
╭─[only_export_components.tsx:3:22]
2 │ import React from 'react';
3 │ export const CONSTANT = 3; export const Foo = () => {};
· ────────
4 │
╰────

⚠ eslint-plugin-react-refresh(only-export-components): Fast refresh can't handle anonymous components. Add a name to your export.
⚠ eslint-plugin-react(only-export-components): Fast refresh can't handle anonymous components. Add a name to your export.
╭─[only_export_components.tsx:1:1]
1 │ export default compose()(MainView);
· ───────────────────────────────────
╰────

⚠ eslint-plugin-react-refresh(only-export-components): Fast refresh only works when a file only exports components. Use a new file to share constants or functions between components.
⚠ eslint-plugin-react(only-export-components): Fast refresh only works when a file only exports components. Use a new file to share constants or functions between components.
╭─[only_export_components.tsx:1:75]
1 │ export const loader = () => {}; export const Bar = () => {}; export const foo = () => {};
· ───
╰────

⚠ eslint-plugin-react-refresh(only-export-components): Fast refresh only works when a file only exports components. Move your component(s) to a separate file.
⚠ eslint-plugin-react(only-export-components): Fast refresh only works when a file only exports components. Move your component(s) to a separate file.
╭─[only_export_components.tsx:1:7]
1 │ const Foo = () => {}; export { Foo as "🍌"}
· ───
╰────

⚠ eslint-plugin-react-refresh(only-export-components): Fast refresh only works when a file only exports components. Move your React context(s) to a separate file.
⚠ eslint-plugin-react(only-export-components): Fast refresh only works when a file only exports components. Move your React context(s) to a separate file.
╭─[only_export_components.tsx:1:51]
1 │ export const MyComponent = () => {}; export const MyContext = createContext('test');
· ─────────
╰────

⚠ eslint-plugin-react-refresh(only-export-components): Fast refresh only works when a file only exports components. Move your React context(s) to a separate file.
⚠ eslint-plugin-react(only-export-components): Fast refresh only works when a file only exports components. Move your React context(s) to a separate file.
╭─[only_export_components.tsx:1:51]
1 │ export const MyComponent = () => {}; export const MyContext = React.createContext('test');
· ─────────
╰────

⚠ eslint-plugin-react-refresh(only-export-components): Fast refresh can't handle anonymous components. Add a name to your export.
⚠ eslint-plugin-react(only-export-components): Fast refresh can't handle anonymous components. Add a name to your export.
╭─[only_export_components.tsx:1:31]
1 │ const MyComponent = () => {}; export default observer(MyComponent);
· ─────────────────────────────────────
╰────

⚠ eslint-plugin-react-refresh(only-export-components): Fast refresh only works when a file only exports components. Move your component(s) to a separate file.
⚠ eslint-plugin-react(only-export-components): Fast refresh only works when a file only exports components. Move your component(s) to a separate file.
╭─[only_export_components.tsx:1:7]
1 │ const MyComponent = () => {}; export default observer(MyComponent);
· ───────────
Expand Down
Loading