+
Skip to content
This repository was archived by the owner on Aug 31, 2023. It is now read-only.
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
1 change: 1 addition & 0 deletions crates/rome_diagnostics_categories/src/categories.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ define_dategories! {
"lint/nursery/noConstEnum": "https://docs.rome.tools/lint/rules/noConstEnum",
"lint/nursery/noConstructorReturn": "https://docs.rome.tools/lint/rules/noConstructorReturn",
"lint/nursery/noDistractingElements": "https://docs.rome.tools/lint/rules/noDistractingElements",
"lint/nursery/noDuplicateCase": "https://docs.rome.tools/lint/rules/noDuplicateCase",
"lint/nursery/noDuplicateObjectKeys":"https://docs.rome.tools/lint/rules/noDuplicateObjectKeys",
"lint/nursery/noEmptyInterface": "https://docs.rome.tools/lint/rules/noEmptyInterface",
"lint/nursery/noExtraNonNullAssertion":"https://docs.rome.tools/lint/rules/noExtraNonNullAssertion",
Expand Down
3 changes: 2 additions & 1 deletion crates/rome_js_analyze/src/analyzers/nursery.rs

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

132 changes: 132 additions & 0 deletions crates/rome_js_analyze/src/analyzers/nursery/no_duplicate_case.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
use crate::utils::is_node_equal;
use rome_analyze::context::RuleContext;
use rome_analyze::{declare_rule, Ast, Rule, RuleDiagnostic};
use rome_js_syntax::{AnyJsExpression, AnyJsSwitchClause, JsCaseClause, JsSwitchStatement};
use rome_rowan::{AstNode, TextRange};

declare_rule! {
/// Disallow duplicate case labels.
/// If a switch statement has duplicate test expressions in case clauses, it is likely that a programmer copied a case clause but forgot to change the test expression.
///
/// Source: https://eslint.org/docs/latest/rules/no-duplicate-case
///
/// ## Examples
///
/// ### Invalid
///
/// ```js,expect_diagnostic
/// switch (a) {
/// case 1:
/// break;
/// case 1:
/// break;
/// default:
/// break;
/// }
/// ```
///
/// ```js,expect_diagnostic
/// switch (a) {
/// case one:
/// break;
/// case one:
/// break;
/// default:
/// break;
/// }
/// ```
///
/// ```js,expect_diagnostic
/// switch (a) {
/// case "1":
/// break;
/// case "1":
/// break;
/// default:
/// break;
/// }
/// ```
///
/// ### Valid
///
/// ```js
/// switch (a) {
/// case 1:
/// break;
/// case 2:
/// break;
/// default:
/// break;
/// }
/// ```
///
/// ```js
/// switch (a) {
/// case one:
/// break;
/// case two:
/// break;
/// default:
/// break;
/// }
/// ```
///
/// ```js
/// switch (a) {
/// case "1":
/// break;
/// case "2":
/// break;
/// default:
/// break;
/// }
/// ```
pub(crate) NoDuplicateCase {
version: "12.0.0",
name: "noDuplicateCase",
recommended: true,
}
}

impl Rule for NoDuplicateCase {
type Query = Ast<JsSwitchStatement>;
type State = (TextRange, JsCaseClause);
type Signals = Vec<Self::State>;
type Options = ();

fn run(ctx: &RuleContext<Self>) -> Self::Signals {
let node = ctx.query();

let mut defined_tests: Vec<AnyJsExpression> = Vec::new();
let mut signals = Vec::new();

for case in node.cases() {
if let AnyJsSwitchClause::JsCaseClause(case) = case {
if let Ok(test) = case.test() {
let define_test = defined_tests
.iter()
.find(|define_test| is_node_equal(define_test.syntax(), test.syntax()));

match define_test {
Some(define_test) => {
signals.push((define_test.range(), case));
}
None => {
defined_tests.push(test);
}
}
}
}
}

signals
}

fn diagnostic(_: &RuleContext<Self>, state: &Self::State) -> Option<RuleDiagnostic> {
let (first_label_range, case) = state;
case.test().ok().map(|test| {
RuleDiagnostic::new(rule_category!(), test.range(), "Duplicate case label.")
.detail(first_label_range, "The first similar label is here:")
})
}
}
64 changes: 4 additions & 60 deletions crates/rome_js_analyze/src/analyzers/nursery/no_self_compare.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use crate::utils::is_node_equal;
use rome_analyze::context::RuleContext;
use rome_analyze::{declare_rule, Ast, Rule, RuleDiagnostic};
use rome_js_syntax::{
JsBinaryExpression, JsBinaryOperator, JsSyntaxNode, JsSyntaxToken, WalkEvent,
};
use rome_rowan::{AstNode, Direction};
use std::iter;
use rome_js_syntax::{JsBinaryExpression, JsBinaryOperator};
use rome_rowan::AstNode;

declare_rule! {
/// Disallow comparisons where both sides are exactly the same.
Expand Down Expand Up @@ -60,7 +58,7 @@ impl Rule for NoSelfCompare {
let left = node.left().ok()?;
let right = node.right().ok()?;

if is_node_equal(&left.into_syntax(), &right.into_syntax()) {
if is_node_equal(left.syntax(), right.syntax()) {
return Some(());
}

Expand All @@ -75,57 +73,3 @@ impl Rule for NoSelfCompare {
))
}
}

/// Verifies that both nodes are equal by checking their descendants (nodes included) kinds
/// and tokens (same kind and inner token text).
fn is_node_equal(a_node: &JsSyntaxNode, b_node: &JsSyntaxNode) -> bool {
let a_tree = a_node.preorder_with_tokens(Direction::Next);
let b_tree = b_node.preorder_with_tokens(Direction::Next);

for (a_child, b_child) in iter::zip(a_tree, b_tree) {
let a_event = match a_child {
WalkEvent::Enter(event) => event,
WalkEvent::Leave(event) => event,
};

let b_event = match b_child {
WalkEvent::Enter(event) => event,
WalkEvent::Leave(event) => event,
};

if a_event.kind() != b_event.kind() {
return false;
}

let a_token = a_event.as_token();
let b_token = b_event.as_token();

match (a_token, b_token) {
// both are nodes
(None, None) => continue,
// one of them is a node
(None, Some(_)) | (Some(_), None) => return false,
// both are tokens
(Some(a), Some(b)) => {
if !is_token_text_equal(a, b) {
return false;
}
continue;
}
}
}

true
}

/// Verify that tokens' inner text are equal
fn is_token_text_equal(a: &JsSyntaxToken, b: &JsSyntaxToken) -> bool {
static QUOTES: [char; 2] = ['"', '\''];

a.token_text_trimmed()
.trim_start_matches(QUOTES)
.trim_end_matches(QUOTES)
== b.token_text_trimmed()
.trim_start_matches(QUOTES)
.trim_end_matches(QUOTES)
}
61 changes: 58 additions & 3 deletions crates/rome_js_analyze/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use rome_js_factory::make;
use rome_js_syntax::{
AnyJsStatement, JsLanguage, JsModuleItemList, JsStatementList, JsVariableDeclaration,
JsVariableDeclarator, JsVariableDeclaratorList, JsVariableStatement, T,
AnyJsStatement, JsLanguage, JsModuleItemList, JsStatementList, JsSyntaxNode, JsSyntaxToken,
JsVariableDeclaration, JsVariableDeclarator, JsVariableDeclaratorList, JsVariableStatement, T,
};
use rome_rowan::{AstNode, AstSeparatedList, BatchMutation};
use rome_rowan::{AstNode, AstSeparatedList, BatchMutation, Direction, WalkEvent};
use std::borrow::Cow;
use std::iter;

pub mod batch;
pub mod escape;
Expand Down Expand Up @@ -192,6 +193,60 @@ pub(crate) fn remove_declarator(
Some(())
}

/// Verifies that both nodes are equal by checking their descendants (nodes included) kinds
/// and tokens (same kind and inner token text).
pub(crate) fn is_node_equal(a_node: &JsSyntaxNode, b_node: &JsSyntaxNode) -> bool {
let a_tree = a_node.preorder_with_tokens(Direction::Next);
let b_tree = b_node.preorder_with_tokens(Direction::Next);

for (a_child, b_child) in iter::zip(a_tree, b_tree) {
let a_event = match a_child {
WalkEvent::Enter(event) => event,
WalkEvent::Leave(event) => event,
};

let b_event = match b_child {
WalkEvent::Enter(event) => event,
WalkEvent::Leave(event) => event,
};

if a_event.kind() != b_event.kind() {
return false;
}

let a_token = a_event.as_token();
let b_token = b_event.as_token();

match (a_token, b_token) {
// both are nodes
(None, None) => continue,
// one of them is a node
(None, Some(_)) | (Some(_), None) => return false,
// both are tokens
(Some(a), Some(b)) => {
if !is_token_text_equal(a, b) {
return false;
}
continue;
}
}
}

true
}

/// Verify that tokens' inner text are equal
fn is_token_text_equal(a: &JsSyntaxToken, b: &JsSyntaxToken) -> bool {
static QUOTES: [char; 2] = ['"', '\''];

a.token_text_trimmed()
.trim_start_matches(QUOTES)
.trim_end_matches(QUOTES)
== b.token_text_trimmed()
.trim_start_matches(QUOTES)
.trim_end_matches(QUOTES)
}

#[test]
fn ok_to_camel_case() {
assert_eq!(to_camel_case("camelCase"), Cow::Borrowed("camelCase"));
Expand Down
Loading
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载