mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
feat(linter) eslint-plugin-next no-assign-module-variable (#1935)
This commit is contained in:
parent
0f15099f6f
commit
c06d939460
3 changed files with 93 additions and 0 deletions
|
|
@ -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! {
|
||||
|
|
@ -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,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
|
|
@ -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
|
||||
|
||||
|
||||
Loading…
Reference in a new issue