Skip to content

Commit 8fb55c3

Browse files
authored
refactor(linter): cleanup docs and simplify code of eslint/no-fallthrough (#11331)
- ident example code with two spaces as recommended in #6050 - remove `/*oxlint no-fallthrough: "error"*/` in example code - shorten implementation and make it easier to understand
1 parent a0ee946 commit 8fb55c3

File tree

1 file changed

+87
-104
lines changed

1 file changed

+87
-104
lines changed

crates/oxc_linter/src/rules/eslint/no_fallthrough.rs

Lines changed: 87 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,11 @@ declare_oxc_lint!(
8787
///
8888
/// ```js
8989
/// switch(foo) {
90-
/// case 1:
91-
/// doSomething();
90+
/// case 1:
91+
/// doSomething();
9292
///
9393
/// case 2:
94-
/// doSomethingElse();
94+
/// doSomethingElse();
9595
/// }
9696
/// ```
9797
///
@@ -101,12 +101,12 @@ declare_oxc_lint!(
101101
///
102102
/// ```js
103103
/// switch(foo) {
104-
/// case 1:
105-
/// doSomething();
106-
/// break;
104+
/// case 1:
105+
/// doSomething();
106+
/// break;
107107
///
108-
/// case 2:
109-
/// doSomethingElse();
108+
/// case 2:
109+
/// doSomethingElse();
110110
/// }
111111
/// ```
112112
///
@@ -118,41 +118,41 @@ declare_oxc_lint!(
118118
///
119119
/// ```js
120120
/// switch(foo) {
121-
/// case 1:
122-
/// doSomething();
123-
/// // falls through
121+
/// case 1:
122+
/// doSomething();
123+
/// // falls through
124124
///
125-
/// case 2:
126-
/// doSomethingElse();
125+
/// case 2:
126+
/// doSomethingElse();
127127
/// }
128128
///
129129
/// switch(foo) {
130-
/// case 1:
131-
/// doSomething();
132-
/// // fall through
130+
/// case 1:
131+
/// doSomething();
132+
/// // fall through
133133
///
134-
/// case 2:
135-
/// doSomethingElse();
134+
/// case 2:
135+
/// doSomethingElse();
136136
/// }
137137
///
138138
/// switch(foo) {
139-
/// case 1:
140-
/// doSomething();
141-
/// // fallsthrough
139+
/// case 1:
140+
/// doSomething();
141+
/// // fallsthrough
142142
///
143-
/// case 2:
144-
/// doSomethingElse();
143+
/// case 2:
144+
/// doSomethingElse();
145145
/// }
146146
///
147147
/// switch(foo) {
148-
/// case 1: {
149-
/// doSomething();
150-
/// // falls through
151-
/// }
152-
///
153-
/// case 2: {
154-
/// doSomethingElse();
155-
/// }
148+
/// case 1: {
149+
/// doSomething();
150+
/// // falls through
151+
/// }
152+
///
153+
/// case 2: {
154+
/// doSomethingElse();
155+
/// }
156156
/// }
157157
/// ```
158158
///
@@ -164,79 +164,75 @@ declare_oxc_lint!(
164164
///
165165
/// Examples of **incorrect** code for this rule:
166166
/// ```js
167-
/// /*oxlint no-fallthrough: "error"*/
168-
///
169167
/// switch(foo) {
170-
/// case 1:
171-
/// doSomething();
168+
/// case 1:
169+
/// doSomething();
172170
///
173-
/// case 2:
174-
/// doSomething();
171+
/// case 2:
172+
/// doSomething();
175173
/// }
176174
/// ```
177175
///
178176
/// Examples of **correct** code for this rule:
179177
/// ```js
180-
/// /*oxlint no-fallthrough: "error"*/
181-
///
182178
/// switch(foo) {
183-
/// case 1:
184-
/// doSomething();
185-
/// break;
179+
/// case 1:
180+
/// doSomething();
181+
/// break;
186182
///
187-
/// case 2:
188-
/// doSomething();
183+
/// case 2:
184+
/// doSomething();
189185
/// }
190186
///
191187
/// function bar(foo) {
192-
/// switch(foo) {
193-
/// case 1:
194-
/// doSomething();
195-
/// return;
196-
///
197-
/// case 2:
198-
/// doSomething();
199-
/// }
188+
/// switch(foo) {
189+
/// case 1:
190+
/// doSomething();
191+
/// return;
192+
///
193+
/// case 2:
194+
/// doSomething();
195+
/// }
200196
/// }
201197
///
202198
/// switch(foo) {
203-
/// case 1:
204-
/// doSomething();
205-
/// throw new Error("Boo!");
199+
/// case 1:
200+
/// doSomething();
201+
/// throw new Error("Boo!");
206202
///
207-
/// case 2:
208-
/// doSomething();
203+
/// case 2:
204+
/// doSomething();
209205
/// }
210206
///
211207
/// switch(foo) {
212-
/// case 1:
213-
/// case 2:
214-
/// doSomething();
208+
/// case 1:
209+
/// case 2:
210+
/// doSomething();
215211
/// }
216212
///
217213
/// switch(foo) {
218-
/// case 1: case 2:
219-
/// doSomething();
214+
/// case 1: case 2:
215+
/// doSomething();
220216
/// }
221217
///
222218
/// switch(foo) {
223-
/// case 1:
224-
/// doSomething();
225-
/// // falls through
219+
/// case 1:
220+
/// doSomething();
221+
/// // falls through
226222
///
227-
/// case 2:
228-
/// doSomething();
223+
/// case 2:
224+
/// doSomething();
229225
/// }
230226
///
231227
/// switch(foo) {
232-
/// case 1: {
233-
/// doSomething();
234-
/// // falls through
235-
/// }
236-
///
237-
/// case 2: {
238-
/// doSomethingElse();
239-
/// }
228+
/// case 1: {
229+
/// doSomething();
230+
/// // falls through
231+
/// }
232+
///
233+
/// case 2: {
234+
/// doSomethingElse();
235+
/// }
240236
/// }
241237
/// ```
242238
///
@@ -278,42 +274,29 @@ impl Rule for NoFallthrough {
278274
let fallthroughs: FxHashSet<BlockNodeId> = neighbors_filtered_by_edge_weight(
279275
graph,
280276
switch_id,
281-
&|e| match e {
277+
&|edge_type| match edge_type {
282278
EdgeType::Normal | EdgeType::Jump | EdgeType::Error(ErrorEdgeKind::Explicit) => {
283279
None
284280
}
285281
_ => Some(None),
286282
},
287-
&mut |node, last_cond: Option<BlockNodeId>| {
288-
let node = *node;
289-
283+
&mut |&node, last_cond: Option<BlockNodeId>| {
290284
if node == switch_id {
291-
return (last_cond, true);
292-
}
293-
if node == default_or_exit {
294-
return (last_cond, false);
295-
}
296-
if tests.contains_key(&node) {
297-
return (last_cond, true);
298-
}
299-
if cfg.basic_block(node).is_unreachable() {
300-
return (None, false);
301-
}
302-
303-
let fallthrough = graph
304-
.edges_directed(node, Direction::Outgoing)
305-
.find(|it| {
306-
let target = it.target();
307-
if let Some(default) = default {
308-
if default == target {
309-
return true;
310-
}
311-
}
312-
tests.contains_key(&target)
313-
})
314-
.map(|e| e.target());
285+
(last_cond, true)
286+
} else if node == default_or_exit {
287+
(last_cond, false)
288+
} else if tests.contains_key(&node) {
289+
(last_cond, true)
290+
} else if cfg.basic_block(node).is_unreachable() {
291+
(None, false)
292+
} else {
293+
let fallthrough = graph
294+
.edges_directed(node, Direction::Outgoing)
295+
.map(|edge| edge.target())
296+
.find(|target| Some(*target) == default || tests.contains_key(target));
315297

316-
(fallthrough, fallthrough.is_none())
298+
(fallthrough, fallthrough.is_none())
299+
}
317300
},
318301
)
319302
.into_iter()

0 commit comments

Comments
 (0)