Skip to content

Commit

Permalink
feat: nextjs plugin (oxc-project#1948)
Browse files Browse the repository at this point in the history
  • Loading branch information
camc314 authored and IWANABETHATGUY committed May 29, 2024
1 parent 37cae98 commit 4aeb0a8
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 9 deletions.
4 changes: 4 additions & 0 deletions crates/oxc_cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ pub struct EnablePlugins {
/// Enable the JSX-a11y plugin and detect accessibility problems
#[bpaf(switch, hide_usage)]
pub jsx_a11y_plugin: bool,

/// Enable the Next.js plugin and detect Next.js problems
#[bpaf(switch, hide_usage)]
pub nextjs_plugin: bool,
}

#[derive(Debug, Clone, Bpaf)]
Expand Down
3 changes: 2 additions & 1 deletion crates/oxc_cli/src/lint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ impl Runner for LintRunner {
.with_timing(misc_options.timing)
.with_import_plugin(enable_plugins.import_plugin)
.with_jest_plugin(enable_plugins.jest_plugin)
.with_jsx_a11y_plugin(enable_plugins.jsx_a11y_plugin);
.with_jsx_a11y_plugin(enable_plugins.jsx_a11y_plugin)
.with_nextjs_plugin(enable_plugins.nextjs_plugin);

let linter = match Linter::from_options(lint_options) {
Ok(lint_service) => lint_service,
Expand Down
10 changes: 10 additions & 0 deletions crates/oxc_linter/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub struct LintOptions {
pub import_plugin: bool,
pub jest_plugin: bool,
pub jsx_a11y_plugin: bool,
pub nextjs_plugin: bool,
}

impl Default for LintOptions {
Expand All @@ -38,6 +39,7 @@ impl Default for LintOptions {
import_plugin: false,
jest_plugin: false,
jsx_a11y_plugin: false,
nextjs_plugin: false,
}
}
}
Expand Down Expand Up @@ -86,6 +88,12 @@ impl LintOptions {
self.jsx_a11y_plugin = yes;
self
}

#[must_use]
pub fn with_nextjs_plugin(mut self, yes: bool) -> Self {
self.nextjs_plugin = yes;
self
}
}

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
Expand Down Expand Up @@ -141,6 +149,7 @@ impl TryFrom<&Number> for AllowWarnDeny {

const JEST_PLUGIN_NAME: &str = "jest";
const JSX_A11Y_PLUGIN_NAME: &str = "jsx_a11y";
const NEXTJS_PLUGIN_NAME: &str = "nextjs";

impl LintOptions {
/// # Errors
Expand Down Expand Up @@ -210,6 +219,7 @@ impl LintOptions {

may_exclude_plugin_rules(self.jest_plugin, JEST_PLUGIN_NAME);
may_exclude_plugin_rules(self.jsx_a11y_plugin, JSX_A11Y_PLUGIN_NAME);
may_exclude_plugin_rules(self.nextjs_plugin, NEXTJS_PLUGIN_NAME);

rules
}
Expand Down
4 changes: 3 additions & 1 deletion crates/oxc_linter/src/rules/nextjs/google_font_display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,5 +211,7 @@ fn test() {
"#,
];

Tester::new_without_config(GoogleFontDisplay::NAME, pass, fail).test_and_snapshot();
Tester::new_without_config(GoogleFontDisplay::NAME, pass, fail)
.with_nextjs_plugin(true)
.test_and_snapshot();
}
4 changes: 3 additions & 1 deletion crates/oxc_linter/src/rules/nextjs/google_font_preconnect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,7 @@ fn test() {
"#,
];

Tester::new_without_config(GoogleFontPreconnect::NAME, pass, fail).test_and_snapshot();
Tester::new_without_config(GoogleFontPreconnect::NAME, pass, fail)
.with_nextjs_plugin(true)
.test_and_snapshot();
}
4 changes: 3 additions & 1 deletion crates/oxc_linter/src/rules/nextjs/inline_script_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,5 +246,7 @@ fn test() {
}",
];

Tester::new_without_config(InlineScriptId::NAME, pass, fail).test_and_snapshot();
Tester::new_without_config(InlineScriptId::NAME, pass, fail)
.with_nextjs_plugin(true)
.test_and_snapshot();
}
4 changes: 3 additions & 1 deletion crates/oxc_linter/src/rules/nextjs/next_script_for_ga.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,5 +310,7 @@ fn test() {
}",
];

Tester::new_without_config(NextScriptForGa::NAME, pass, fail).test_and_snapshot();
Tester::new_without_config(NextScriptForGa::NAME, pass, fail)
.with_nextjs_plugin(true)
.test_and_snapshot();
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,7 @@ fn test() {
",
];

Tester::new_without_config(NoAssignModuleVariable::NAME, pass, fail).test_and_snapshot();
Tester::new_without_config(NoAssignModuleVariable::NAME, pass, fail)
.with_nextjs_plugin(true)
.test_and_snapshot();
}
Original file line number Diff line number Diff line change
Expand Up @@ -209,5 +209,7 @@ fn test() {
"#,
];

Tester::new_without_config(NoAsyncClientComponent::NAME, pass, fail).test_and_snapshot();
Tester::new_without_config(NoAsyncClientComponent::NAME, pass, fail)
.with_nextjs_plugin(true)
.test_and_snapshot();
}
4 changes: 3 additions & 1 deletion crates/oxc_linter/src/rules/nextjs/no_css_tags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,5 +153,7 @@ fn test() {
</div>"#,
];

Tester::new_without_config(NoCssTags::NAME, pass, fail).test_and_snapshot();
Tester::new_without_config(NoCssTags::NAME, pass, fail)
.with_nextjs_plugin(true)
.test_and_snapshot();
}
10 changes: 9 additions & 1 deletion crates/oxc_linter/src/tester.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub struct Tester {
import_plugin: bool,
jest_plugin: bool,
jsx_a11y_plugin: bool,
nextjs_plugin: bool,
}

impl Tester {
Expand Down Expand Up @@ -73,6 +74,7 @@ impl Tester {
import_plugin: false,
jest_plugin: false,
jsx_a11y_plugin: false,
nextjs_plugin: false,
}
}

Expand Down Expand Up @@ -121,6 +123,11 @@ impl Tester {
self
}

pub fn with_nextjs_plugin(mut self, yes: bool) -> Self {
self.nextjs_plugin = yes;
self
}

pub fn expect_fix<S: Into<String>>(mut self, expect_fix: Vec<(S, S, Option<Value>)>) -> Self {
self.expect_fix =
expect_fix.into_iter().map(|(s1, s2, r)| (s1.into(), s2.into(), r)).collect::<Vec<_>>();
Expand Down Expand Up @@ -187,7 +194,8 @@ impl Tester {
.with_fix(is_fix)
.with_import_plugin(self.import_plugin)
.with_jest_plugin(self.jest_plugin)
.with_jsx_a11y_plugin(self.jsx_a11y_plugin);
.with_jsx_a11y_plugin(self.jsx_a11y_plugin)
.with_nextjs_plugin(self.nextjs_plugin);
let linter = Linter::from_options(options)
.unwrap()
.with_rules(vec![rule])
Expand Down

0 comments on commit 4aeb0a8

Please sign in to comment.