-
-
Notifications
You must be signed in to change notification settings - Fork 446
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(linter) eslint-plugin-next no-assign-module-variable
- Loading branch information
Showing
3 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
crates/oxc_linter/src/rules/nextjs/no_assign_module_variable.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
14
crates/oxc_linter/src/snapshots/no_assign_module_variable.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 │ | ||
2 │ let module = {}; | ||
· ────── | ||
3 │ | ||
╰──── | ||
help: See https://nextjs.org/docs/messages/no-assign-module-variable | ||
|
||
|