Skip to content

Commit

Permalink
feat(linter) eslint-plugin-next no-assign-module-variable
Browse files Browse the repository at this point in the history
  • Loading branch information
camc314 committed Jan 7, 2024
1 parent b64e1a4 commit abf5f42
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
2 changes: 2 additions & 0 deletions crates/oxc_linter/src/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ mod nextjs {
pub mod google_font_preconnect;
pub mod inline_script_id;
pub mod next_script_for_ga;
pub mod no_assign_module_variable;
}

oxc_macros::declare_all_lint_rules! {
Expand Down Expand Up @@ -515,4 +516,5 @@ oxc_macros::declare_all_lint_rules! {
nextjs::google_font_preconnect,
nextjs::inline_script_id,
nextjs::next_script_for_ga,
nextjs::no_assign_module_variable,
}
77 changes: 77 additions & 0 deletions crates/oxc_linter/src/rules/nextjs/no_assign_module_variable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use oxc_ast::{ast::BindingPatternKind, AstKind};
use oxc_diagnostics::{
miette::{self, Diagnostic},
thiserror::Error,
};
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;

use crate::{context::LintContext, rule::Rule, AstNode};

#[derive(Debug, Error, Diagnostic)]
#[error("eslint-plugin-next(no-assign-module-variable): Do not assign to the variable `module`.")]
#[diagnostic(
severity(warning),
help("See https://nextjs.org/docs/messages/no-assign-module-variable")
)]
struct NoAssignModuleVariableDiagnostic(#[label] pub Span);

#[derive(Debug, Default, Clone)]
pub struct NoAssignModuleVariable;

declare_oxc_lint!(
/// ### What it does
///
///
/// ### Why is this bad?
///
///
/// ### Example
/// ```javascript
/// ```
NoAssignModuleVariable,
correctness
);

impl Rule for NoAssignModuleVariable {
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
let AstKind::VariableDeclaration(variable_decl) = node.kind() else { return };

for decl in &variable_decl.declarations {
let BindingPatternKind::BindingIdentifier(binding_ident) = &decl.id.kind else {
continue;
};

if binding_ident.name == "module" {
ctx.diagnostic(NoAssignModuleVariableDiagnostic(binding_ident.span));
}
}
}
}

#[test]
fn test() {
use crate::tester::Tester;

let pass = vec![
r"
let myModule = {};
export default function MyComponent() {
return <></>
}
",
];

let fail = vec![
r"
let module = {};
export default function MyComponent() {
return <></>
}
",
];

Tester::new_without_config(NoAssignModuleVariable::NAME, pass, fail).test_and_snapshot();
}
14 changes: 14 additions & 0 deletions crates/oxc_linter/src/snapshots/no_assign_module_variable.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
source: crates/oxc_linter/src/tester.rs
expression: no_assign_module_variable
---
eslint-plugin-next(no-assign-module-variable): Do not assign to the variable `module`.
╭─[no_assign_module_variable.tsx:1:1]
1
2let module = {};
· ──────
3
╰────
help: See https://nextjs.org/docs/messages/no-assign-module-variable


0 comments on commit abf5f42

Please sign in to comment.