feat(rome_js_analyze): useDefaultSwitchClauseLast
MODIFY
crates/rome_diagnostics_categories/src/categories.rs
crates/rome_diagnostics_categories/src/categories.rs
@@ -95,6 +95,7 @@ define_dategories! {
"lint/nursery/noVoidTypeReturn": "https://docs.rome.tools/lint/rules/noVoidTypeReturn",
"lint/nursery/useCamelCase": "https://docs.rome.tools/lint/rules/useCamelCase",
"lint/nursery/useConst":"https://docs.rome.tools/lint/rules/useConst",
+ "lint/nursery/useDefaultSwitchClauseLast":"https://docs.rome.tools/lint/rules/useDefaultSwitchClauseLast",
"lint/nursery/useExhaustiveDependencies": "https://docs.rome.tools/lint/rules/useExhaustiveDependencies",
"lint/nursery/useFlatMap": "https://docs.rome.tools/lint/rules/useFlatMap",
"lint/nursery/useNumericLiterals": "https://docs.rome.tools/lint/rules/useNumericLiterals",

MODIFY
crates/rome_js_analyze/src/analyzers/nursery.rs
crates/rome_js_analyze/src/analyzers/nursery.rs
@@ -17,7 +17,8 @@ mod no_setter_return;
mod no_string_case_mismatch;
mod no_unsafe_finally;
mod no_void_type_return;
+mod use_default_switch_clause_last;
mod use_flat_map;
mod use_numeric_literals;
mod use_valid_for_direction;
-declare_group! { pub (crate) Nursery { name : "nursery" , rules : [self :: no_access_key :: NoAccessKey , self :: no_banned_types :: NoBannedTypes , self :: no_conditional_assignment :: NoConditionalAssignment , self :: no_constructor_return :: NoConstructorReturn , self :: no_distracting_elements :: NoDistractingElements , self :: no_dupe_keys :: NoDupeKeys , self :: no_empty_interface :: NoEmptyInterface , self :: no_explicit_any :: NoExplicitAny , self :: no_extra_non_null_assertion :: NoExtraNonNullAssertion , self :: no_header_scope :: NoHeaderScope , self :: no_invalid_constructor_super :: NoInvalidConstructorSuper , self :: no_precision_loss :: NoPrecisionLoss , self :: no_setter_return :: NoSetterReturn , self :: no_string_case_mismatch :: NoStringCaseMismatch , self :: no_unsafe_finally :: NoUnsafeFinally , self :: no_void_type_return :: NoVoidTypeReturn , self :: use_flat_map :: UseFlatMap , self :: use_numeric_literals :: UseNumericLiterals , self :: use_valid_for_direction :: UseValidForDirection ,] } }
+declare_group! { pub (crate) Nursery { name : "nursery" , rules : [self :: no_access_key :: NoAccessKey , self :: no_banned_types :: NoBannedTypes , self :: no_conditional_assignment :: NoConditionalAssignment , self :: no_constructor_return :: NoConstructorReturn , self :: no_distracting_elements :: NoDistractingElements , self :: no_dupe_keys :: NoDupeKeys , self :: no_empty_interface :: NoEmptyInterface , self :: no_explicit_any :: NoExplicitAny , self :: no_extra_non_null_assertion :: NoExtraNonNullAssertion , self :: no_header_scope :: NoHeaderScope , self :: no_invalid_constructor_super :: NoInvalidConstructorSuper , self :: no_precision_loss :: NoPrecisionLoss , self :: no_setter_return :: NoSetterReturn , self :: no_string_case_mismatch :: NoStringCaseMismatch , self :: no_unsafe_finally :: NoUnsafeFinally , self :: no_void_type_return :: NoVoidTypeReturn , self :: use_default_switch_clause_last :: UseDefaultSwitchClauseLast , self :: use_flat_map :: UseFlatMap , self :: use_numeric_literals :: UseNumericLiterals , self :: use_valid_for_direction :: UseValidForDirection ,] } }

ADD
crates/rome_js_analyze/tests/specs/nursery/useDefaultSwitchClauseLast/invalid.js
+switch (foo) {
+ default:
+ break;
+ case 0:
+ break;
+}
+
+switch (foo) {
+ case 0:
+ break;
+ default:
+ break;
+ case 1:
+ break;
+}
+
+switch (foo) {
+ default:
+ f();
+ case 0:
+ break;
+}
+
+switch (foo) {
+ case 0:
+ break;
+ default:
+ case 1:
+ break;
+}
\ No newline at end of file

ADD
crates/rome_js_analyze/tests/specs/nursery/useDefaultSwitchClauseLast/valid.js
+switch (foo) {
+ case 0:
+ break;
+ case 1:
+ break;
+ default:
+ break;
+}
+
+switch (foo) {
+ case 0:
+ break;
+ case 1:
+ default:
+ break;
+}
+
+switch (foo) {
+ default:
+ break;
+}
+
+switch (foo) {}
+
+switch (foo) {
+ case 0:
+ break;
+}
\ No newline at end of file

ADD
crates/rome_js_analyze/tests/specs/nursery/useDefaultSwitchClauseLast/valid.js.snap
+---
+source: crates/rome_js_analyze/tests/spec_tests.rs
+assertion_line: 73
+expression: valid.js
+---
+# Input
+```js
+switch (foo) {
+ case 0:
+ break;
+ case 1:
+ break;
+ default:
+ break;
+}
+
+switch (foo) {
+ case 0:
+ break;
+ case 1:
+ default:
+ break;
+}
+
+switch (foo) {
+ default:
+ break;
+}
+
+switch (foo) {}
+
+switch (foo) {
+ case 0:
+ break;
+}
+```
+
+

MODIFY
crates/rome_service/src/configuration/linter/rules.rs
crates/rome_service/src/configuration/linter/rules.rs
@@ -769,6 +769,8 @@ struct NurserySchema {
use_camel_case: Option<RuleConfiguration>,
#[doc = "Require const declarations for variables that are never reassigned after declared."]
use_const: Option<RuleConfiguration>,
+ #[doc = "Enforce default clauses in switch statements to be last"]
+ use_default_switch_clause_last: Option<RuleConfiguration>,
#[doc = "Enforce all dependencies are correctly specified."]
use_exhaustive_dependencies: Option<RuleConfiguration>,
#[doc = "Promotes the use of .flatMap() when map().flat() are used together."]
@@ -780,7 +782,7 @@ struct NurserySchema {
}
impl Nursery {
const CATEGORY_NAME: &'static str = "nursery";
- pub(crate) const CATEGORY_RULES: [&'static str; 24] = [
+ pub(crate) const CATEGORY_RULES: [&'static str; 25] = [
"noAccessKey",
"noBannedTypes",
"noConditionalAssignment",
@@ -801,6 +803,7 @@ impl Nursery {
"noVoidTypeReturn",
"useCamelCase",
"useConst",
+ "useDefaultSwitchClauseLast",
"useExhaustiveDependencies",
"useFlatMap",
"useNumericLiterals",

MODIFY
editors/vscode/configuration_schema.json
editors/vscode/configuration_schema.json
}
]
},
+ "useDefaultSwitchClauseLast": {
+ "description": "Enforce default clauses in switch statements to be last",
+ "anyOf": [
+ {
+ "$ref": "#/definitions/RuleConfiguration"
+ },
+ {
+ "type": "null"
+ }
+ ]
+ },
"useExhaustiveDependencies": {
"description": "Enforce all dependencies are correctly specified.",
"anyOf": [

MODIFY
npm/backend-jsonrpc/src/workspace.ts
npm/backend-jsonrpc/src/workspace.ts
@@ -431,6 +431,10 @@ export interface Nursery {
* Require const declarations for variables that are never reassigned after declared.
*/
useConst?: RuleConfiguration;
+ /**
+ * Enforce default clauses in switch statements to be last
+ */
+ useDefaultSwitchClauseLast?: RuleConfiguration;
/**
* Enforce all dependencies are correctly specified.
*/
@@ -671,6 +675,7 @@ export type Category =
| "lint/nursery/noVoidTypeReturn"
| "lint/nursery/useCamelCase"
| "lint/nursery/useConst"
+ | "lint/nursery/useDefaultSwitchClauseLast"
| "lint/nursery/useExhaustiveDependencies"
| "lint/nursery/useFlatMap"
| "lint/nursery/useNumericLiterals"

MODIFY
npm/rome/configuration_schema.json
npm/rome/configuration_schema.json
}
]
},
+ "useDefaultSwitchClauseLast": {
+ "description": "Enforce default clauses in switch statements to be last",
+ "anyOf": [
+ {
+ "$ref": "#/definitions/RuleConfiguration"
+ },
+ {
+ "type": "null"
+ }
+ ]
+ },
"useExhaustiveDependencies": {
"description": "Enforce all dependencies are correctly specified.",
"anyOf": [

MODIFY
website/src/pages/lint/rules/index.mdx
website/src/pages/lint/rules/index.mdx
@@ -565,6 +565,12 @@ Enforce camel case naming convention.
Require <code>const</code> declarations for variables that are never reassigned after declared.
</section>
<section class="rule">
+<h3 data-toc-exclude id="useDefaultSwitchClauseLast">
+ <a href="/lint/rules/useDefaultSwitchClauseLast">useDefaultSwitchClauseLast</a>
+</h3>
+Enforce default clauses in switch statements to be last
+</section>
+<section class="rule">
<h3 data-toc-exclude id="useExhaustiveDependencies">
<a href="/lint/rules/useExhaustiveDependencies">useExhaustiveDependencies</a>
</h3>

feat(rome_js_analyze): useDefaultSwitchClauseLast (#3791)