Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7295f3f

Browse files
committed
feat(lint): rule noUnnecessaryConditions
1 parent 5f328e1 commit 7295f3f

File tree

26 files changed

+3439
-101
lines changed

26 files changed

+3439
-101
lines changed

.changeset/cold-seals-ring.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
"@biomejs/biome": patch
3+
---
4+
5+
Added the new nursery rule `noUnnecessararyConditions`, which detects whenever some conditions don't
6+
change during the life cycle of the program, and truthy or false, hence deemed redundant.
7+
8+
For example, the following snippets will trigger the rule:
9+
10+
```js
11+
// Always truthy literal conditions
12+
if (true) {
13+
console.log("always runs");
14+
}
15+
```
16+
17+
```ts
18+
// Unnecessary condition on constrained string type
19+
function foo(arg: 'bar' | 'baz') {
20+
if (arg) { // This check is unnecessary
21+
}
22+
}
23+
```

crates/biome_cli/src/execute/migrate/eslint_any_rule_to_biome.rs

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/biome_configuration/src/analyzer/linter/rules.rs

Lines changed: 93 additions & 72 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/biome_css_analyze/tests/spec_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ pub(crate) fn analyze_and_snap(
173173
"css",
174174
);
175175

176-
assert_diagnostics_expectation_comment(input_file, root.syntax(), diagnostics.len());
176+
assert_diagnostics_expectation_comment(input_file, root.syntax(), diagnostics);
177177
}
178178

179179
fn check_code_action(

crates/biome_diagnostics_categories/src/categories.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ define_categories! {
171171
"lint/nursery/noNoninteractiveElementInteractions": "https://biomejs.dev/linter/rules/no-noninteractive-element-interactions",
172172
"lint/nursery/noProcessGlobal": "https://biomejs.dev/linter/rules/no-process-global",
173173
"lint/nursery/noQuickfixBiome": "https://biomejs.dev/linter/rules/no-quickfix-biome",
174+
"lint/nursery/noQwikUseVisibleTask": "https://biomejs.dev/linter/rules/no-qwik-use-visible-task",
174175
"lint/nursery/noReactPropAssign": "https://biomejs.dev/linter/rules/no-react-prop-assign",
175176
"lint/nursery/noReactSpecificProps": "https://biomejs.dev/linter/rules/no-react-specific-props",
176177
"lint/nursery/noRestrictedElements": "https://biomejs.dev/linter/rules/no-restricted-elements",
@@ -187,17 +188,17 @@ define_categories! {
187188
"lint/nursery/noUnknownSelectorPseudoElement": "https://biomejs.dev/linter/rules/no-unknown-selector-pseudo-element",
188189
"lint/nursery/noUnknownUnit": "https://biomejs.dev/linter/rules/no-unknown-unit",
189190
"lint/nursery/noUnmatchableAnbSelector": "https://biomejs.dev/linter/rules/no-unmatchable-anb-selector",
191+
"lint/nursery/noUnnecessaryConditions": "https://biomejs.dev/linter/rules/no-unnecessary-conditions",
190192
"lint/nursery/noUnresolvedImports": "https://biomejs.dev/linter/rules/no-unresolved-imports",
191193
"lint/nursery/noUnusedFunctionParameters": "https://biomejs.dev/linter/rules/no-unused-function-parameters",
192194
"lint/nursery/noUnwantedPolyfillio": "https://biomejs.dev/linter/rules/no-unwanted-polyfillio",
193-
"lint/nursery/noQwikUseVisibleTask": "https://biomejs.dev/linter/rules/no-qwik-use-visible-task",
194195
"lint/nursery/noUselessBackrefInRegex": "https://biomejs.dev/linter/rules/no-useless-backref-in-regex",
195196
"lint/nursery/noUselessEscapeInString": "https://biomejs.dev/linter/rules/no-useless-escape-in-string",
196197
"lint/nursery/noUselessUndefined": "https://biomejs.dev/linter/rules/no-useless-undefined",
197-
"lint/nursery/useQwikClasslist": "https://biomejs.dev/linter/rules/use-qwik-classlist",
198198
"lint/nursery/noVueReservedKeys": "https://biomejs.dev/linter/rules/no-vue-reserved-keys",
199199
"lint/nursery/noVueReservedProps": "https://biomejs.dev/linter/rules/no-vue-reserved-props",
200200
"lint/nursery/useAdjacentGetterSetter": "https://biomejs.dev/linter/rules/use-adjacent-getter-setter",
201+
"lint/nursery/useAnchorHref": "https://biomejs.dev/linter/rules/use-anchor-href",
201202
"lint/nursery/useBiomeSuppressionComment": "https://biomejs.dev/linter/rules/use-biome-suppression-comment",
202203
"lint/nursery/useConsistentObjectDefinition": "https://biomejs.dev/linter/rules/use-consistent-object-definition",
203204
"lint/nursery/useConsistentResponse": "https://biomejs.dev/linter/rules/use-consistent-response",
@@ -206,20 +207,20 @@ define_categories! {
206207
"lint/nursery/useExplicitType": "https://biomejs.dev/linter/rules/use-explicit-type",
207208
"lint/nursery/useExportsLast": "https://biomejs.dev/linter/rules/use-exports-last",
208209
"lint/nursery/useForComponent": "https://biomejs.dev/linter/rules/use-for-component",
209-
"lint/nursery/useReactFunctionComponents": "https://biomejs.dev/linter/rules/use-react-function-components",
210210
"lint/nursery/useGoogleFontPreconnect": "https://biomejs.dev/linter/rules/use-google-font-preconnect",
211+
"lint/nursery/useImageSize": "https://biomejs.dev/linter/rules/use-image-size",
211212
"lint/nursery/useImportRestrictions": "https://biomejs.dev/linter/rules/use-import-restrictions",
212213
"lint/nursery/useIndexOf": "https://biomejs.dev/linter/rules/use-index-of",
213214
"lint/nursery/useIterableCallbackReturn": "https://biomejs.dev/linter/rules/use-iterable-callback-return",
214-
"lint/nursery/useImageSize": "https://biomejs.dev/linter/rules/use-image-size",
215215
"lint/nursery/useJsonImportAttribute": "https://biomejs.dev/linter/rules/use-json-import-attribute",
216216
"lint/nursery/useJsxCurlyBraceConvention": "https://biomejs.dev/linter/rules/use-jsx-curly-brace-convention",
217-
"lint/nursery/useAnchorHref": "https://biomejs.dev/linter/rules/use-anchor-href",
218217
"lint/nursery/useNamedOperation": "https://biomejs.dev/linter/rules/use-named-operation",
219218
"lint/nursery/useNamingConvention": "https://biomejs.dev/linter/rules/use-naming-convention",
220219
"lint/nursery/useNumericSeparators": "https://biomejs.dev/linter/rules/use-numeric-separators",
221220
"lint/nursery/useObjectSpread": "https://biomejs.dev/linter/rules/use-object-spread",
222221
"lint/nursery/useParseIntRadix": "https://biomejs.dev/linter/rules/use-parse-int-radix",
222+
"lint/nursery/useQwikClasslist": "https://biomejs.dev/linter/rules/use-qwik-classlist",
223+
"lint/nursery/useReactFunctionComponents": "https://biomejs.dev/linter/rules/use-react-function-components",
223224
"lint/nursery/useReadonlyClassProperties": "https://biomejs.dev/linter/rules/use-readonly-class-properties",
224225
"lint/nursery/useSingleJsDocAsterisk": "https://biomejs.dev/linter/rules/use-single-js-doc-asterisk",
225226
"lint/nursery/useSortedClasses": "https://biomejs.dev/linter/rules/use-sorted-classes",

crates/biome_graphql_analyze/tests/spec_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ pub(crate) fn analyze_and_snap(
145145
"graphql",
146146
);
147147

148-
assert_diagnostics_expectation_comment(input_file, root.syntax(), diagnostics.len());
148+
assert_diagnostics_expectation_comment(input_file, root.syntax(), diagnostics);
149149
}
150150

151151
fn check_code_action(

crates/biome_js_analyze/src/lint/nursery.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ pub mod no_secrets;
2424
pub mod no_shadow;
2525
pub mod no_ts_ignore;
2626
pub mod no_unassigned_variables;
27+
pub mod no_unnecessary_conditions;
2728
pub mod no_unresolved_imports;
2829
pub mod no_unwanted_polyfillio;
2930
pub mod no_useless_backref_in_regex;
@@ -55,4 +56,4 @@ pub mod use_sorted_classes;
5556
pub mod use_symbol_description;
5657
pub mod use_unified_type_signature;
5758
pub mod use_unique_element_ids;
58-
declare_lint_group! { pub Nursery { name : "nursery" , rules : [self :: no_await_in_loop :: NoAwaitInLoop , self :: no_bitwise_operators :: NoBitwiseOperators , self :: no_constant_binary_expression :: NoConstantBinaryExpression , self :: no_destructured_props :: NoDestructuredProps , self :: no_excessive_lines_per_function :: NoExcessiveLinesPerFunction , self :: no_floating_promises :: NoFloatingPromises , self :: no_global_dirname_filename :: NoGlobalDirnameFilename , self :: no_implicit_coercion :: NoImplicitCoercion , self :: no_import_cycles :: NoImportCycles , self :: no_magic_numbers :: NoMagicNumbers , self :: no_misused_promises :: NoMisusedPromises , self :: no_nested_component_definitions :: NoNestedComponentDefinitions , self :: no_noninteractive_element_interactions :: NoNoninteractiveElementInteractions , self :: no_process_global :: NoProcessGlobal , self :: no_qwik_use_visible_task :: NoQwikUseVisibleTask , self :: no_react_prop_assign :: NoReactPropAssign , self :: no_restricted_elements :: NoRestrictedElements , self :: no_secrets :: NoSecrets , self :: no_shadow :: NoShadow , self :: no_ts_ignore :: NoTsIgnore , self :: no_unassigned_variables :: NoUnassignedVariables , self :: no_unresolved_imports :: NoUnresolvedImports , self :: no_unwanted_polyfillio :: NoUnwantedPolyfillio , self :: no_useless_backref_in_regex :: NoUselessBackrefInRegex , self :: no_useless_escape_in_string :: NoUselessEscapeInString , self :: no_useless_undefined :: NoUselessUndefined , self :: no_vue_reserved_keys :: NoVueReservedKeys , self :: no_vue_reserved_props :: NoVueReservedProps , self :: use_adjacent_getter_setter :: UseAdjacentGetterSetter , self :: use_anchor_href :: UseAnchorHref , self :: use_consistent_object_definition :: UseConsistentObjectDefinition , self :: use_consistent_response :: UseConsistentResponse , self :: use_exhaustive_switch_cases :: UseExhaustiveSwitchCases , self :: use_explicit_type :: UseExplicitType , self :: use_exports_last :: UseExportsLast , self :: use_for_component :: UseForComponent , self :: use_google_font_preconnect :: UseGoogleFontPreconnect , self :: use_image_size :: UseImageSize , self :: use_index_of :: UseIndexOf , self :: use_iterable_callback_return :: UseIterableCallbackReturn , self :: use_json_import_attribute :: UseJsonImportAttribute , self :: use_numeric_separators :: UseNumericSeparators , self :: use_object_spread :: UseObjectSpread , self :: use_parse_int_radix :: UseParseIntRadix , self :: use_qwik_classlist :: UseQwikClasslist , self :: use_react_function_components :: UseReactFunctionComponents , self :: use_readonly_class_properties :: UseReadonlyClassProperties , self :: use_single_js_doc_asterisk :: UseSingleJsDocAsterisk , self :: use_sorted_classes :: UseSortedClasses , self :: use_symbol_description :: UseSymbolDescription , self :: use_unified_type_signature :: UseUnifiedTypeSignature , self :: use_unique_element_ids :: UseUniqueElementIds ,] } }
59+
declare_lint_group! { pub Nursery { name : "nursery" , rules : [self :: no_await_in_loop :: NoAwaitInLoop , self :: no_bitwise_operators :: NoBitwiseOperators , self :: no_constant_binary_expression :: NoConstantBinaryExpression , self :: no_destructured_props :: NoDestructuredProps , self :: no_excessive_lines_per_function :: NoExcessiveLinesPerFunction , self :: no_floating_promises :: NoFloatingPromises , self :: no_global_dirname_filename :: NoGlobalDirnameFilename , self :: no_implicit_coercion :: NoImplicitCoercion , self :: no_import_cycles :: NoImportCycles , self :: no_magic_numbers :: NoMagicNumbers , self :: no_misused_promises :: NoMisusedPromises , self :: no_nested_component_definitions :: NoNestedComponentDefinitions , self :: no_noninteractive_element_interactions :: NoNoninteractiveElementInteractions , self :: no_process_global :: NoProcessGlobal , self :: no_qwik_use_visible_task :: NoQwikUseVisibleTask , self :: no_react_prop_assign :: NoReactPropAssign , self :: no_restricted_elements :: NoRestrictedElements , self :: no_secrets :: NoSecrets , self :: no_shadow :: NoShadow , self :: no_ts_ignore :: NoTsIgnore , self :: no_unassigned_variables :: NoUnassignedVariables , self :: no_unnecessary_conditions :: NoUnnecessaryConditions , self :: no_unresolved_imports :: NoUnresolvedImports , self :: no_unwanted_polyfillio :: NoUnwantedPolyfillio , self :: no_useless_backref_in_regex :: NoUselessBackrefInRegex , self :: no_useless_escape_in_string :: NoUselessEscapeInString , self :: no_useless_undefined :: NoUselessUndefined , self :: no_vue_reserved_keys :: NoVueReservedKeys , self :: no_vue_reserved_props :: NoVueReservedProps , self :: use_adjacent_getter_setter :: UseAdjacentGetterSetter , self :: use_anchor_href :: UseAnchorHref , self :: use_consistent_object_definition :: UseConsistentObjectDefinition , self :: use_consistent_response :: UseConsistentResponse , self :: use_exhaustive_switch_cases :: UseExhaustiveSwitchCases , self :: use_explicit_type :: UseExplicitType , self :: use_exports_last :: UseExportsLast , self :: use_for_component :: UseForComponent , self :: use_google_font_preconnect :: UseGoogleFontPreconnect , self :: use_image_size :: UseImageSize , self :: use_index_of :: UseIndexOf , self :: use_iterable_callback_return :: UseIterableCallbackReturn , self :: use_json_import_attribute :: UseJsonImportAttribute , self :: use_numeric_separators :: UseNumericSeparators , self :: use_object_spread :: UseObjectSpread , self :: use_parse_int_radix :: UseParseIntRadix , self :: use_qwik_classlist :: UseQwikClasslist , self :: use_react_function_components :: UseReactFunctionComponents , self :: use_readonly_class_properties :: UseReadonlyClassProperties , self :: use_single_js_doc_asterisk :: UseSingleJsDocAsterisk , self :: use_sorted_classes :: UseSortedClasses , self :: use_symbol_description :: UseSymbolDescription , self :: use_unified_type_signature :: UseUnifiedTypeSignature , self :: use_unique_element_ids :: UseUniqueElementIds ,] } }

0 commit comments

Comments
 (0)