feat(linter): eslint: no-var (#1890)

This feature is primarily used to supplement eslint no-var rules

- doc:
https://github.com/eslint/eslint/blob/main/docs/src/rules/no-var.md
- code: https://github.com/eslint/eslint/blob/main/lib/rules/no-var.js
- test:
https://github.com/eslint/eslint/blob/main/tests/lib/rules/no-var.js

Co-authored-by: 张润钊 <zhangrunzhao@bytedance.com>
This commit is contained in:
zhangrunzhao 2024-01-05 19:52:38 +08:00 committed by GitHub
parent 6c5b22f728
commit 55a87b2e02
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 462 additions and 0 deletions

View file

@ -88,6 +88,7 @@ mod eslint {
pub mod no_unused_private_class_members;
pub mod no_useless_catch;
pub mod no_useless_escape;
pub mod no_var;
pub mod require_yield;
pub mod use_isnan;
pub mod valid_typeof;
@ -336,6 +337,7 @@ oxc_macros::declare_all_lint_rules! {
eslint::no_unused_private_class_members,
eslint::no_useless_catch,
eslint::no_useless_escape,
eslint::no_var,
eslint::require_yield,
eslint::use_isnan,
eslint::valid_typeof,

View file

@ -0,0 +1,111 @@
use oxc_ast::{ast::VariableDeclarationKind, 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(no-var): Unexpected var, use let or const instead.")]
#[diagnostic(severity(warning), help("Replace var with let or const"))]
struct NoVarDiagnostic(#[label] pub Span);
#[derive(Debug, Default, Clone)]
pub struct NoVar;
// doc: https://github.com/eslint/eslint/blob/main/docs/src/rules/no-var.md
// code: https://github.com/eslint/eslint/blob/main/lib/rules/no-var.js
// test: https://github.com/eslint/eslint/blob/main/tests/lib/rules/no-var.js
declare_oxc_lint!(
/// ### What it does
/// ECMAScript 6 allows programmers to create variables with block scope instead of function scope using the `let` and `const` keywords.
/// Block scope is common in many other programming languages and helps programmers avoid mistakes
///
/// ### Why is this bad?
/// Using `var` in an es6 environment triggers this error
///
/// ### Example
/// ```javascript
/// // error
/// var x = "y";
/// var CONFIG = {};
///
/// // success
/// let x = "y";
/// const CONFIG = {};
/// ```
NoVar,
correctness
);
impl Rule for NoVar {
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
if let AstKind::VariableDeclaration(dec) = node.kind() {
if dec.kind == VariableDeclarationKind::Var {
ctx.diagnostic(NoVarDiagnostic(Span::new(dec.span.start, dec.span.start + 3)));
}
}
}
}
#[test]
fn test() {
use crate::tester::Tester;
let pass = vec![("const JOE = 'schmoe';", None), ("let moo = 'car';", None)];
let fail = vec![
("var foo = bar;", None),
("var foo = bar, toast = most;", None),
("var foo = bar; let toast = most;", None),
("for (var a of b) { console.log(a); }", None),
("for (var a in b) { console.log(a); }", None),
("for (let a of b) { var c = 1; console.log(c); }", None),
("for (var i = 0; i < list.length; ++i) { foo(i) }", None),
("for (var i = 0, i = 0; false;);", None),
("var i = 0; for (var i = 1; false;); console.log(i);", None),
("var a, b, c; var a;", None),
("var a; if (b) { var a; }", None),
("if (foo) { var a, b, c; } a;", None),
("for (var i = 0; i < 10; ++i) {} i;", None),
("for (var a in obj) {} a;", None),
("for (var a of list) {} a;", None),
("switch (a) { case 0: var b = 1 }", None),
("for (var a of b) { arr.push(() => a); }", None),
("for (let a of b) { var c; console.log(c); c = 'hello'; }", None),
("var a = a", None),
("var {a = a} = {}", None),
("var {a = b, b} = {}", None),
("var {a, b = a} = {}", None),
("var a = b, b = 1", None),
("var a = b; var b = 1", None),
("function foo() { a } var a = 1; foo()", None),
("if (foo) var bar = 1;", None),
("var foo = 1", None),
("{ var foo = 1 }", None),
("if (true) { var foo = 1 }", None),
("var foo = 1", None),
("declare var foo = 2;", None),
("function foo() { var let; }", None),
("function foo() { var { let } = {}; }", None),
(
"var fx = function (i = 0) { if (i < 5) { return fx(i + 1); } console.log(i); }; fx();",
None,
),
("var foo = function () { foo() };", None),
("var foo = () => foo();", None),
("var foo = (function () { foo(); })();", None),
("var foo = bar(function () { foo(); });", None),
("var bar = foo, foo = function () { foo(); };", None),
("var bar = foo; var foo = function () { foo(); };", None),
("var { foo = foo } = function () { foo(); };", None),
("var { bar = foo, foo } = function () { foo(); };", None),
("var bar = function () { foo(); }; var foo = function() {};", None),
];
Tester::new(NoVar::NAME, pass, fail).test_and_snapshot();
}

View file

@ -0,0 +1,349 @@
---
source: crates/oxc_linter/src/tester.rs
assertion_line: 144
expression: no_var
---
⚠ eslint(no-var): Unexpected var, use let or const instead.
╭─[no_var.tsx:1:1]
1 │ var foo = bar;
· ───
╰────
help: Replace var with let or const
⚠ eslint(no-var): Unexpected var, use let or const instead.
╭─[no_var.tsx:1:1]
1 │ var foo = bar, toast = most;
· ───
╰────
help: Replace var with let or const
⚠ eslint(no-var): Unexpected var, use let or const instead.
╭─[no_var.tsx:1:1]
1 │ var foo = bar; let toast = most;
· ───
╰────
help: Replace var with let or const
⚠ eslint(no-var): Unexpected var, use let or const instead.
╭─[no_var.tsx:1:1]
1 │ for (var a of b) { console.log(a); }
· ───
╰────
help: Replace var with let or const
⚠ eslint(no-var): Unexpected var, use let or const instead.
╭─[no_var.tsx:1:1]
1 │ for (var a in b) { console.log(a); }
· ───
╰────
help: Replace var with let or const
⚠ eslint(no-var): Unexpected var, use let or const instead.
╭─[no_var.tsx:1:1]
1 │ for (let a of b) { var c = 1; console.log(c); }
· ───
╰────
help: Replace var with let or const
⚠ eslint(no-var): Unexpected var, use let or const instead.
╭─[no_var.tsx:1:1]
1 │ for (var i = 0; i < list.length; ++i) { foo(i) }
· ───
╰────
help: Replace var with let or const
⚠ eslint(no-var): Unexpected var, use let or const instead.
╭─[no_var.tsx:1:1]
1 │ for (var i = 0, i = 0; false;);
· ───
╰────
help: Replace var with let or const
⚠ eslint(no-var): Unexpected var, use let or const instead.
╭─[no_var.tsx:1:1]
1 │ var i = 0; for (var i = 1; false;); console.log(i);
· ───
╰────
help: Replace var with let or const
⚠ eslint(no-var): Unexpected var, use let or const instead.
╭─[no_var.tsx:1:1]
1 │ var i = 0; for (var i = 1; false;); console.log(i);
· ───
╰────
help: Replace var with let or const
⚠ eslint(no-var): Unexpected var, use let or const instead.
╭─[no_var.tsx:1:1]
1 │ var a, b, c; var a;
· ───
╰────
help: Replace var with let or const
⚠ eslint(no-var): Unexpected var, use let or const instead.
╭─[no_var.tsx:1:1]
1 │ var a, b, c; var a;
· ───
╰────
help: Replace var with let or const
⚠ eslint(no-var): Unexpected var, use let or const instead.
╭─[no_var.tsx:1:1]
1 │ var a; if (b) { var a; }
· ───
╰────
help: Replace var with let or const
⚠ eslint(no-var): Unexpected var, use let or const instead.
╭─[no_var.tsx:1:1]
1 │ var a; if (b) { var a; }
· ───
╰────
help: Replace var with let or const
⚠ eslint(no-var): Unexpected var, use let or const instead.
╭─[no_var.tsx:1:1]
1 │ if (foo) { var a, b, c; } a;
· ───
╰────
help: Replace var with let or const
⚠ eslint(no-var): Unexpected var, use let or const instead.
╭─[no_var.tsx:1:1]
1 │ for (var i = 0; i < 10; ++i) {} i;
· ───
╰────
help: Replace var with let or const
⚠ eslint(no-var): Unexpected var, use let or const instead.
╭─[no_var.tsx:1:1]
1 │ for (var a in obj) {} a;
· ───
╰────
help: Replace var with let or const
⚠ eslint(no-var): Unexpected var, use let or const instead.
╭─[no_var.tsx:1:1]
1 │ for (var a of list) {} a;
· ───
╰────
help: Replace var with let or const
⚠ eslint(no-var): Unexpected var, use let or const instead.
╭─[no_var.tsx:1:1]
1 │ switch (a) { case 0: var b = 1 }
· ───
╰────
help: Replace var with let or const
⚠ eslint(no-var): Unexpected var, use let or const instead.
╭─[no_var.tsx:1:1]
1 │ for (var a of b) { arr.push(() => a); }
· ───
╰────
help: Replace var with let or const
⚠ eslint(no-var): Unexpected var, use let or const instead.
╭─[no_var.tsx:1:1]
1 │ for (let a of b) { var c; console.log(c); c = 'hello'; }
· ───
╰────
help: Replace var with let or const
⚠ eslint(no-var): Unexpected var, use let or const instead.
╭─[no_var.tsx:1:1]
1 │ var a = a
· ───
╰────
help: Replace var with let or const
⚠ eslint(no-var): Unexpected var, use let or const instead.
╭─[no_var.tsx:1:1]
1 │ var {a = a} = {}
· ───
╰────
help: Replace var with let or const
⚠ eslint(no-var): Unexpected var, use let or const instead.
╭─[no_var.tsx:1:1]
1 │ var {a = b, b} = {}
· ───
╰────
help: Replace var with let or const
⚠ eslint(no-var): Unexpected var, use let or const instead.
╭─[no_var.tsx:1:1]
1 │ var {a, b = a} = {}
· ───
╰────
help: Replace var with let or const
⚠ eslint(no-var): Unexpected var, use let or const instead.
╭─[no_var.tsx:1:1]
1 │ var a = b, b = 1
· ───
╰────
help: Replace var with let or const
⚠ eslint(no-var): Unexpected var, use let or const instead.
╭─[no_var.tsx:1:1]
1 │ var a = b; var b = 1
· ───
╰────
help: Replace var with let or const
⚠ eslint(no-var): Unexpected var, use let or const instead.
╭─[no_var.tsx:1:1]
1 │ var a = b; var b = 1
· ───
╰────
help: Replace var with let or const
⚠ eslint(no-var): Unexpected var, use let or const instead.
╭─[no_var.tsx:1:1]
1 │ function foo() { a } var a = 1; foo()
· ───
╰────
help: Replace var with let or const
⚠ eslint(no-var): Unexpected var, use let or const instead.
╭─[no_var.tsx:1:1]
1 │ if (foo) var bar = 1;
· ───
╰────
help: Replace var with let or const
⚠ eslint(no-var): Unexpected var, use let or const instead.
╭─[no_var.tsx:1:1]
1 │ var foo = 1
· ───
╰────
help: Replace var with let or const
⚠ eslint(no-var): Unexpected var, use let or const instead.
╭─[no_var.tsx:1:1]
1 │ { var foo = 1 }
· ───
╰────
help: Replace var with let or const
⚠ eslint(no-var): Unexpected var, use let or const instead.
╭─[no_var.tsx:1:1]
1 │ if (true) { var foo = 1 }
· ───
╰────
help: Replace var with let or const
⚠ eslint(no-var): Unexpected var, use let or const instead.
╭─[no_var.tsx:1:1]
1 │ var foo = 1
· ───
╰────
help: Replace var with let or const
⚠ eslint(no-var): Unexpected var, use let or const instead.
╭─[no_var.tsx:1:1]
1 │ declare var foo = 2;
· ───
╰────
help: Replace var with let or const
⚠ eslint(no-var): Unexpected var, use let or const instead.
╭─[no_var.tsx:1:1]
1 │ function foo() { var let; }
· ───
╰────
help: Replace var with let or const
⚠ eslint(no-var): Unexpected var, use let or const instead.
╭─[no_var.tsx:1:1]
1 │ function foo() { var { let } = {}; }
· ───
╰────
help: Replace var with let or const
⚠ eslint(no-var): Unexpected var, use let or const instead.
╭─[no_var.tsx:1:1]
1 │ var fx = function (i = 0) { if (i < 5) { return fx(i + 1); } console.log(i); }; fx();
· ───
╰────
help: Replace var with let or const
⚠ eslint(no-var): Unexpected var, use let or const instead.
╭─[no_var.tsx:1:1]
1 │ var foo = function () { foo() };
· ───
╰────
help: Replace var with let or const
⚠ eslint(no-var): Unexpected var, use let or const instead.
╭─[no_var.tsx:1:1]
1 │ var foo = () => foo();
· ───
╰────
help: Replace var with let or const
⚠ eslint(no-var): Unexpected var, use let or const instead.
╭─[no_var.tsx:1:1]
1 │ var foo = (function () { foo(); })();
· ───
╰────
help: Replace var with let or const
⚠ eslint(no-var): Unexpected var, use let or const instead.
╭─[no_var.tsx:1:1]
1 │ var foo = bar(function () { foo(); });
· ───
╰────
help: Replace var with let or const
⚠ eslint(no-var): Unexpected var, use let or const instead.
╭─[no_var.tsx:1:1]
1 │ var bar = foo, foo = function () { foo(); };
· ───
╰────
help: Replace var with let or const
⚠ eslint(no-var): Unexpected var, use let or const instead.
╭─[no_var.tsx:1:1]
1 │ var bar = foo; var foo = function () { foo(); };
· ───
╰────
help: Replace var with let or const
⚠ eslint(no-var): Unexpected var, use let or const instead.
╭─[no_var.tsx:1:1]
1 │ var bar = foo; var foo = function () { foo(); };
· ───
╰────
help: Replace var with let or const
⚠ eslint(no-var): Unexpected var, use let or const instead.
╭─[no_var.tsx:1:1]
1 │ var { foo = foo } = function () { foo(); };
· ───
╰────
help: Replace var with let or const
⚠ eslint(no-var): Unexpected var, use let or const instead.
╭─[no_var.tsx:1:1]
1 │ var { bar = foo, foo } = function () { foo(); };
· ───
╰────
help: Replace var with let or const
⚠ eslint(no-var): Unexpected var, use let or const instead.
╭─[no_var.tsx:1:1]
1 │ var bar = function () { foo(); }; var foo = function() {};
· ───
╰────
help: Replace var with let or const
⚠ eslint(no-var): Unexpected var, use let or const instead.
╭─[no_var.tsx:1:1]
1 │ var bar = function () { foo(); }; var foo = function() {};
· ───
╰────
help: Replace var with let or const