-
-
Notifications
You must be signed in to change notification settings - Fork 540
/
Copy pathno_plusplus.rs
190 lines (174 loc) · 6.36 KB
/
no_plusplus.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
use oxc_ast::{ast::UpdateOperator, AstKind};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;
use crate::{context::LintContext, rule::Rule, AstNode};
fn no_plusplus_diagnostic(span: Span, operator: UpdateOperator) -> OxcDiagnostic {
let diagnostic = OxcDiagnostic::warn(format!(
"Unary operator '{operator}' used.",
operator = operator.as_str()
))
.with_label(span);
match operator {
UpdateOperator::Increment => {
diagnostic.with_help("Use the assignment operator `+=` instead.")
}
UpdateOperator::Decrement => {
diagnostic.with_help("Use the assignment operator `-=` instead.")
}
}
}
#[derive(Debug, Default, Clone)]
pub struct NoPlusplus {
/// Whether to allow `++` and `--` in for loop afterthoughts.
allow_for_loop_afterthoughts: bool,
}
declare_oxc_lint!(
/// ### What it does
///
/// Disallow the unary operators `++`` and `--`.
///
/// ### Why is this bad?
///
/// Because the unary `++` and `--` operators are subject to automatic semicolon insertion, differences in whitespace
/// can change the semantics of source code. For example, these two code blocks are not equivalent:
///
/// ```js
/// var i = 10;
/// var j = 20;
///
/// i ++
/// j
/// // => i = 11, j = 20
/// ```
///
/// ```js
/// var i = 10;
/// var j = 20;
///
/// i
/// ++
/// j
/// // => i = 10, j = 21
/// ```
///
/// ### Examples
///
/// Examples of **incorrect** code for this rule:
/// ```js
/// var x = 0; x++;
/// var y = 0; y--;
/// for (let i = 0; i < l; i++) {
/// doSomething(i);
/// }
/// ```
///
/// Examples of **correct** code for this rule:
/// ```js
/// var x = 0; x += 1;
/// var y = 0; y -= 1;
/// for (let i = 0; i < l; i += 1) {
/// doSomething(i);
/// }
/// ```
NoPlusplus,
restriction,
pending
);
impl Rule for NoPlusplus {
fn from_configuration(value: serde_json::Value) -> Self {
let obj = value.get(0);
Self {
allow_for_loop_afterthoughts: obj
.and_then(|v| v.get("allowForLoopAfterthoughts"))
.and_then(serde_json::Value::as_bool)
.unwrap_or(false),
}
}
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
let AstKind::UpdateExpression(expr) = node.kind() else {
return;
};
if self.allow_for_loop_afterthoughts && is_for_loop_afterthought(node, ctx).unwrap_or(false)
{
return;
}
ctx.diagnostic(no_plusplus_diagnostic(expr.span, expr.operator));
}
}
/// Determines whether the given node is considered to be a for loop "afterthought" by the logic of this rule.
/// In particular, it returns `true` if the given node is either:
/// - The update node of a `ForStatement`: for (;; i++) {}
/// - An operand of a sequence expression that is the update node: for (;; foo(), i++) {}
/// - An operand of a sequence expression that is child of another sequence expression, etc.,
/// up to the sequence expression that is the update node: for (;; foo(), (bar(), (baz(), i++))) {}
fn is_for_loop_afterthought(node: &AstNode, ctx: &LintContext) -> Option<bool> {
let mut cur = ctx.nodes().parent_node(node.id())?;
while let AstKind::SequenceExpression(_) | AstKind::ParenthesizedExpression(_) = cur.kind() {
cur = ctx.nodes().parent_node(cur.id())?;
}
Some(matches!(cur.kind(), AstKind::ForStatement(stmt) if stmt.update.is_some()))
}
#[test]
fn test() {
use crate::tester::Tester;
let pass = vec![
("var foo = 0; foo=+1;", None),
("var foo = 0; foo+=1;", None),
("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),
("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();
}