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

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 11 additions & 0 deletions .changeset/no-ternary.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
"@biomejs/biome": patch
---

Added the nursery rule [`noTernary`](https://biomejs.dev/linter/rules/no-ternary/). Disallow ternary operators.

**Invalid:**

```js
const foo = isBar ? baz : qux;
```
12 changes: 12 additions & 0 deletions crates/biome_cli/src/execute/migrate/eslint_any_rule_to_biome.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

161 changes: 91 additions & 70 deletions crates/biome_configuration/src/analyzer/linter/rules.rs

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions crates/biome_diagnostics_categories/src/categories.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ define_categories! {
"lint/nursery/noReactForwardRef": "https://biomejs.dev/linter/rules/no-react-forward-ref",
"lint/nursery/noShadow": "https://biomejs.dev/linter/rules/no-shadow",
"lint/nursery/noSyncScripts": "https://biomejs.dev/linter/rules/no-sync-scripts",
"lint/nursery/noTernary": "https://biomejs.dev/linter/rules/no-ternary",
"lint/nursery/noUnknownAttribute": "https://biomejs.dev/linter/rules/no-unknown-attribute",
"lint/nursery/noUnnecessaryConditions": "https://biomejs.dev/linter/rules/no-unnecessary-conditions",
"lint/nursery/noUnresolvedImports": "https://biomejs.dev/linter/rules/no-unresolved-imports",
Expand Down
3 changes: 2 additions & 1 deletion crates/biome_js_analyze/src/lint/nursery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub mod no_parameters_only_used_in_recursion;
pub mod no_react_forward_ref;
pub mod no_shadow;
pub mod no_sync_scripts;
pub mod no_ternary;
pub mod no_unknown_attribute;
pub mod no_unnecessary_conditions;
pub mod no_unresolved_imports;
Expand All @@ -39,4 +40,4 @@ pub mod use_sorted_classes;
pub mod use_spread;
pub mod use_vue_define_macros_order;
pub mod use_vue_multi_word_component_names;
declare_lint_group! { pub Nursery { name : "nursery" , rules : [self :: no_continue :: NoContinue , self :: no_deprecated_imports :: NoDeprecatedImports , self :: no_empty_source :: NoEmptySource , self :: no_floating_promises :: NoFloatingPromises , self :: no_for_in :: NoForIn , self :: no_import_cycles :: NoImportCycles , self :: no_increment_decrement :: NoIncrementDecrement , self :: no_jsx_literals :: NoJsxLiterals , self :: no_misused_promises :: NoMisusedPromises , self :: no_next_async_client_component :: NoNextAsyncClientComponent , self :: no_parameters_only_used_in_recursion :: NoParametersOnlyUsedInRecursion , self :: no_react_forward_ref :: NoReactForwardRef , self :: no_shadow :: NoShadow , self :: no_sync_scripts :: NoSyncScripts , self :: no_unknown_attribute :: NoUnknownAttribute , self :: no_unnecessary_conditions :: NoUnnecessaryConditions , self :: no_unresolved_imports :: NoUnresolvedImports , self :: no_unused_expressions :: NoUnusedExpressions , self :: no_useless_catch_binding :: NoUselessCatchBinding , self :: no_useless_undefined :: NoUselessUndefined , self :: no_vue_data_object_declaration :: NoVueDataObjectDeclaration , self :: no_vue_duplicate_keys :: NoVueDuplicateKeys , self :: no_vue_reserved_keys :: NoVueReservedKeys , self :: no_vue_reserved_props :: NoVueReservedProps , self :: use_array_sort_compare :: UseArraySortCompare , self :: use_consistent_arrow_return :: UseConsistentArrowReturn , self :: use_exhaustive_switch_cases :: UseExhaustiveSwitchCases , self :: use_explicit_type :: UseExplicitType , self :: use_find :: UseFind , self :: use_max_params :: UseMaxParams , self :: use_qwik_method_usage :: UseQwikMethodUsage , self :: use_qwik_valid_lexical_scope :: UseQwikValidLexicalScope , self :: use_sorted_classes :: UseSortedClasses , self :: use_spread :: UseSpread , self :: use_vue_define_macros_order :: UseVueDefineMacrosOrder , self :: use_vue_multi_word_component_names :: UseVueMultiWordComponentNames ,] } }
declare_lint_group! { pub Nursery { name : "nursery" , rules : [self :: no_continue :: NoContinue , self :: no_deprecated_imports :: NoDeprecatedImports , self :: no_empty_source :: NoEmptySource , self :: no_floating_promises :: NoFloatingPromises , self :: no_for_in :: NoForIn , self :: no_import_cycles :: NoImportCycles , self :: no_increment_decrement :: NoIncrementDecrement , self :: no_jsx_literals :: NoJsxLiterals , self :: no_misused_promises :: NoMisusedPromises , self :: no_next_async_client_component :: NoNextAsyncClientComponent , self :: no_parameters_only_used_in_recursion :: NoParametersOnlyUsedInRecursion , self :: no_react_forward_ref :: NoReactForwardRef , self :: no_shadow :: NoShadow , self :: no_sync_scripts :: NoSyncScripts , self :: no_ternary :: NoTernary , self :: no_unknown_attribute :: NoUnknownAttribute , self :: no_unnecessary_conditions :: NoUnnecessaryConditions , self :: no_unresolved_imports :: NoUnresolvedImports , self :: no_unused_expressions :: NoUnusedExpressions , self :: no_useless_catch_binding :: NoUselessCatchBinding , self :: no_useless_undefined :: NoUselessUndefined , self :: no_vue_data_object_declaration :: NoVueDataObjectDeclaration , self :: no_vue_duplicate_keys :: NoVueDuplicateKeys , self :: no_vue_reserved_keys :: NoVueReservedKeys , self :: no_vue_reserved_props :: NoVueReservedProps , self :: use_array_sort_compare :: UseArraySortCompare , self :: use_consistent_arrow_return :: UseConsistentArrowReturn , self :: use_exhaustive_switch_cases :: UseExhaustiveSwitchCases , self :: use_explicit_type :: UseExplicitType , self :: use_find :: UseFind , self :: use_max_params :: UseMaxParams , self :: use_qwik_method_usage :: UseQwikMethodUsage , self :: use_qwik_valid_lexical_scope :: UseQwikValidLexicalScope , self :: use_sorted_classes :: UseSortedClasses , self :: use_spread :: UseSpread , self :: use_vue_define_macros_order :: UseVueDefineMacrosOrder , self :: use_vue_multi_word_component_names :: UseVueMultiWordComponentNames ,] } }
69 changes: 69 additions & 0 deletions crates/biome_js_analyze/src/lint/nursery/no_ternary.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use biome_analyze::{
Ast, Rule, RuleDiagnostic, RuleSource, context::RuleContext, declare_lint_rule,
};
use biome_console::markup;
use biome_js_syntax::JsConditionalExpression;
use biome_rowan::AstNode;
use biome_rule_options::no_ternary::NoTernaryOptions;

declare_lint_rule! {
/// Disallow ternary operators.
///
/// The ternary operator is used to conditionally assign a value to a variable.
/// Some believe that the use of ternary operators leads to unclear code.
///
/// ## Examples
///
/// ### Invalid
///
/// ```js,expect_diagnostic
/// const foo = isBar ? baz : qux;
/// ```
///
/// ### Valid
///
/// ```js
/// let foo;
///
/// if (isBar) {
/// foo = baz;
/// } else {
/// foo = qux;
/// }
/// ```
///
pub NoTernary {
version: "next",
name: "noTernary",
language: "js",
recommended: false,
sources: &[RuleSource::Eslint("no-ternary").same()],
}
}

impl Rule for NoTernary {
type Query = Ast<JsConditionalExpression>;
type State = ();
type Signals = Option<Self::State>;
type Options = NoTernaryOptions;

fn run(_ctx: &RuleContext<Self>) -> Self::Signals {
Some(())
}

fn diagnostic(ctx: &RuleContext<Self>, _state: &Self::State) -> Option<RuleDiagnostic> {
let node = ctx.query();
Some(
RuleDiagnostic::new(
rule_category!(),
node.range(),
markup! {
"Unexpected ternary operator."
},
)
.note(markup! {
"Ternary operators can lead to unclear code. Use if-else statement instead."
}),
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/* should generate diagnostics */
const foo = isBar ? baz : qux;

function quux() {
return foo ? bar() : baz();
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
source: crates/biome_js_analyze/tests/spec_tests.rs
expression: invalid.js
---
# Input
```js
/* should generate diagnostics */
const foo = isBar ? baz : qux;

function quux() {
return foo ? bar() : baz();
}


```

# Diagnostics
```
invalid.js:2:13 lint/nursery/noTernary ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

i Unexpected ternary operator.

1 │ /* should generate diagnostics */
> 2 │ const foo = isBar ? baz : qux;
│ ^^^^^^^^^^^^^^^^^
3 │
4 │ function quux() {

i Ternary operators can lead to unclear code. Use if-else statement instead.


```

```
invalid.js:5:9 lint/nursery/noTernary ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

i Unexpected ternary operator.

4 │ function quux() {
> 5 │ return foo ? bar() : baz();
│ ^^^^^^^^^^^^^^^^^^^
6 │ }
7 │

i Ternary operators can lead to unclear code. Use if-else statement instead.


```
16 changes: 16 additions & 0 deletions crates/biome_js_analyze/tests/specs/nursery/noTernary/valid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* should not generate diagnostics */
let foo;

if (isBar) {
foo = baz;
} else {
foo = qux;
}

function quux() {
if (foo) {
return bar();
} else {
return baz();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
source: crates/biome_js_analyze/tests/spec_tests.rs
expression: valid.js
---
# Input
```js
/* should not generate diagnostics */
let foo;

if (isBar) {
foo = baz;
} else {
foo = qux;
}

function quux() {
if (foo) {
return bar();
} else {
return baz();
}
}

```
1 change: 1 addition & 0 deletions crates/biome_rule_options/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ pub mod no_svg_without_title;
pub mod no_switch_declarations;
pub mod no_sync_scripts;
pub mod no_template_curly_in_string;
pub mod no_ternary;
pub mod no_then_property;
pub mod no_this_in_static;
pub mod no_ts_ignore;
Expand Down
6 changes: 6 additions & 0 deletions crates/biome_rule_options/src/no_ternary.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use biome_deserialize_macros::{Deserializable, Merge};
use serde::{Deserialize, Serialize};
#[derive(Default, Clone, Debug, Deserialize, Deserializable, Merge, Eq, PartialEq, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "camelCase", deny_unknown_fields, default)]
pub struct NoTernaryOptions {}
14 changes: 14 additions & 0 deletions packages/@biomejs/backend-jsonrpc/src/workspace.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions packages/@biomejs/biome/configuration_schema.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading