+
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 @@ -45,6 +45,7 @@ define_dategories! {
"lint/nursery/noAccessKey": "https://docs.rome.tools/lint/rules/noAccessKey",
"lint/nursery/noAssignInExpressions": "https://docs.rome.tools/lint/rules/noAssignInExpressions",
"lint/nursery/noWith": "https://docs.rome.tools/lint/rules/noWith",
"lint/nursery/noExtraSemicolons": "https://docs.rome.tools/lint/rules/noExtraSemicolons",
"lint/nursery/noBannedTypes":"https://docs.rome.tools/lint/rules/noBannedTypes",
"lint/nursery/noClassAssign": "https://docs.rome.tools/lint/rules/noClassAssign",
"lint/nursery/noCommaOperator": "https://docs.rome.tools/lint/rules/noCommaOperator",
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.

155 changes: 155 additions & 0 deletions crates/rome_js_analyze/src/analyzers/nursery/no_extra_semicolons.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
use rome_analyze::context::RuleContext;
use rome_analyze::{declare_rule, ActionCategory, Ast, Rule, RuleDiagnostic};
use rome_console::markup;
use rome_diagnostics::Applicability;
use rome_js_syntax::{JsEmptyClassMember, JsEmptyStatement};

use rome_rowan::{declare_node_union, AstNode, BatchMutationExt};

use crate::JsRuleAction;

declare_rule! {
/// Typing mistakes and misunderstandings about where semicolons are required can lead to semicolons that are unnecessary.
/// While not technically an error, extra semicolons can cause confusion when reading code.
///
/// This rule disallows unnecessary semicolons.
///
/// ## Examples
///
/// ### Invalid
///
///```js,expect_diagnostic
/// const x = 5;;
///```
///
///```js,expect_diagnostic
/// function buzz() {
/// const x = 10;;
/// }
///```
///
///```js,expect_diagnostic
/// function foo() {
/// // code
/// };
///```
///
///```js,expect_diagnostic
/// class C {
/// field;;
///
/// method() {
/// // code
/// }
///
/// static {
/// // code
/// }
/// }
/// ```
///
/// ```js,expect_diagnostic
/// class C {
/// field;
///
/// method() {
/// // code
/// };
///
/// static {
/// // code
/// }
/// }
/// ```
///
/// ```js,expect_diagnostic
/// class C {
/// field;
///
/// method() {
/// // code
/// }
///
/// static {
/// // code
/// };
/// }
/// ```
///
/// ```js,expect_diagnostic
/// class C {
/// field;
///
/// method() {
/// // code
/// }
///
/// static {
/// // code
/// }
/// };
/// ```
pub(crate) NoExtraSemicolons {
version: "12.0.0",
name: "noExtraSemicolons",
recommended: true,
}
}

declare_node_union! {
pub(crate) AnyJsExtraSemicolon = JsEmptyStatement | JsEmptyClassMember
}

impl Rule for NoExtraSemicolons {
type Query = Ast<AnyJsExtraSemicolon>;
type State = AnyJsExtraSemicolon;
type Signals = Option<Self::State>;
type Options = ();

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

match node {
AnyJsExtraSemicolon::JsEmptyStatement(stmt) => {
let parent = stmt.syntax().parent()?;
if parent.kind().is_list() {
Some(AnyJsExtraSemicolon::JsEmptyStatement(stmt.clone()))
} else {
None
}
}
AnyJsExtraSemicolon::JsEmptyClassMember(stmt) => {
Some(AnyJsExtraSemicolon::JsEmptyClassMember(stmt.clone()))
}
}
}

fn diagnostic(ctx: &RuleContext<Self>, _: &Self::State) -> Option<RuleDiagnostic> {
let node = ctx.query();
Some(RuleDiagnostic::new(
rule_category!(),
node.range(),
markup! {
"Unnecessary semicolon."
},
))
}

fn action(ctx: &RuleContext<Self>, node_replace: &Self::State) -> Option<JsRuleAction> {
let mut mutation = ctx.root().begin();
match node_replace {
AnyJsExtraSemicolon::JsEmptyStatement(stmt) => {
mutation.remove_node(stmt.clone());
}
AnyJsExtraSemicolon::JsEmptyClassMember(stmt) => {
mutation.remove_node(stmt.clone());
}
}
Some(JsRuleAction {
category: ActionCategory::QuickFix,
applicability: Applicability::MaybeIncorrect,
message: markup! { "Remove unnecessary semicolon." }.to_owned(),
mutation,
})
}
}
143 changes: 143 additions & 0 deletions crates/rome_js_analyze/tests/specs/nursery/noExtraSemicolons.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
// invalid

const x = 10;
if (x > 5) {
x = 5;
};

while(true);;

while(true) {};

loop1:
for (let i = 0; i < 5; i++) {
str = str + i;;
};

loop1:
for (let i = 0; i < 5; i++) {
if (i === 1) {
continue loop1;
}
str = str + i;
};

function baz() { ; }

function buzz() {
const x = 10;;
}

for(;true;);;

for(;true;);for(;true;);for(;true;);for(;true;);for(;true;);;

{
const x = 5;;
}

function foo() {
// code
};

class C {
field;;

method() {
// code
}

static {
// code
}
}

class C {
field;

method() {
// code
};

static {
// code
}
}

class C {
field;

method() {
// code
}

static {
// code
};
}

class C {
field;

method() {
// code
}

static {
// code
}
};

// valid

const x = 10;
if (x > 5) {
x = 5;
};

while(true);

while(true) {}

loop1:
for (let i = 0; i < 5; i++) {
str = str + i;
}

loop1:
for (let i = 0; i < 5; i++) {
if (i === 1) {
continue loop1;
}
str = str + i;
}

function baz() {}

function buzz() {
const x = 10;
}

for(;true;);

for(;true;);for(;true;);for(;true;);for(;true;);for(;true;);

{
const x = 5;
}

const foo = function() {
// code
};

class C {
field;

method() {
// code
}

static {
// code
}
}
Loading
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载