Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: try perf no plusplus #5581

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add no-plusplus rule
  • Loading branch information
camchenry committed Sep 7, 2024
commit 662aadf8c9c13bc805612463022ca69409750351
2 changes: 2 additions & 0 deletions crates/oxc_linter/src/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ mod eslint {
pub mod no_new_wrappers;
pub mod no_nonoctal_decimal_escape;
pub mod no_obj_calls;
pub mod no_plusplus;
pub mod no_proto;
pub mod no_prototype_builtins;
pub mod no_redeclare;
Expand Down Expand Up @@ -543,6 +544,7 @@ oxc_macros::declare_all_lint_rules! {
eslint::no_new_wrappers,
eslint::no_nonoctal_decimal_escape,
eslint::no_obj_calls,
eslint::no_plusplus,
eslint::no_proto,
eslint::no_prototype_builtins,
eslint::no_redeclare,
Expand Down
102 changes: 102 additions & 0 deletions crates/oxc_linter/src/rules/eslint/no_plusplus.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;

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

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

declare_oxc_lint!(
/// ### What it does
///
///
/// ### Why is this bad?
///
///
/// ### Examples
///
/// Examples of **incorrect** code for this rule:
/// ```js
/// FIXME: Tests will fail if examples are missing or syntactically incorrect.
/// ```
///
/// Examples of **correct** code for this rule:
/// ```js
/// FIXME: Tests will fail if examples are missing or syntactically incorrect.
/// ```
NoPlusplus,
restriction,
);

impl Rule for NoPlusplus {
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {}
}

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

let pass = vec![
("var foo = 0; foo=+1;", None),
("var foo = 0; foo=+1;", Some(serde_json::json!([{ "allowForLoopAfterthoughts": true }]))),
(
"for (i = 0; i < l; i++) { console.log(i); }",
Some(serde_json::json!([{ "allowForLoopAfterthoughts": true }])),
),
(
"for (var i = 0, j = i + 1; j < example.length; i++, j++) {}",
Some(serde_json::json!([{ "allowForLoopAfterthoughts": true }])),
),
("for (;; i--, foo());", Some(serde_json::json!([{ "allowForLoopAfterthoughts": true }]))),
("for (;; foo(), --i);", Some(serde_json::json!([{ "allowForLoopAfterthoughts": true }]))),
(
"for (;; foo(), ++i, bar);",
Some(serde_json::json!([{ "allowForLoopAfterthoughts": true }])),
),
(
"for (;; i++, (++j, k--));",
Some(serde_json::json!([{ "allowForLoopAfterthoughts": true }])),
),
(
"for (;; foo(), (bar(), i++), baz());",
Some(serde_json::json!([{ "allowForLoopAfterthoughts": true }])),
),
(
"for (;; (--i, j += 2), bar = j + 1);",
Some(serde_json::json!([{ "allowForLoopAfterthoughts": true }])),
),
(
"for (;; a, (i--, (b, ++j, c)), d);",
Some(serde_json::json!([{ "allowForLoopAfterthoughts": true }])),
),
];

let fail = vec![
("var foo = 0; foo++;", None),
("var foo = 0; foo--;", None),
("for (i = 0; i < l; i++) { console.log(i); }", None),
("for (i = 0; i < l; foo, i++) { console.log(i); }", None),
("var foo = 0; foo++;", Some(serde_json::json!([{ "allowForLoopAfterthoughts": true }]))),
(
"for (i = 0; i < l; i++) { v++; }",
Some(serde_json::json!([{ "allowForLoopAfterthoughts": true }])),
),
("for (i++;;);", Some(serde_json::json!([{ "allowForLoopAfterthoughts": true }]))),
("for (;--i;);", Some(serde_json::json!([{ "allowForLoopAfterthoughts": true }]))),
("for (;;) ++i;", Some(serde_json::json!([{ "allowForLoopAfterthoughts": true }]))),
("for (;; i = j++);", Some(serde_json::json!([{ "allowForLoopAfterthoughts": true }]))),
("for (;; i++, f(--j));", Some(serde_json::json!([{ "allowForLoopAfterthoughts": true }]))),
(
"for (;; foo + (i++, bar));",
Some(serde_json::json!([{ "allowForLoopAfterthoughts": true }])),
),
];

Tester::new(NoPlusplus::NAME, pass, fail).test_and_snapshot();
}