Skip to content

Commit

Permalink
fix(lint/useEnumInitailizers): ignore enum in ambient namespaces (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
Conaclos authored Aug 20, 2023
1 parent 7c2ee84 commit 30315b6
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ use rome_analyze::{declare_rule, ActionCategory, Ast, Rule, RuleDiagnostic};
use rome_console::markup;
use rome_diagnostics::Applicability;
use rome_js_factory::make;
use rome_js_syntax::{
AnyJsExpression, AnyJsLiteralExpression, JsSyntaxKind, TsDeclareStatement, TsEnumDeclaration,
TsExportDeclareClause,
};
use rome_js_syntax::{AnyJsExpression, AnyJsLiteralExpression, JsSyntaxKind, TsEnumDeclaration};
use rome_rowan::{AstNode, BatchMutationExt};

declare_rule! {
Expand Down Expand Up @@ -86,9 +83,7 @@ impl Rule for UseEnumInitializers {

fn run(ctx: &RuleContext<Self>) -> Self::Signals {
let enum_declaration = ctx.query();
if enum_declaration.parent::<TsDeclareStatement>().is_some()
|| enum_declaration.parent::<TsExportDeclareClause>().is_some()
{
if enum_declaration.is_ambient() {
// In ambient declarations, enum members without initializers are opaque types.
// They generally represent an enum with complex initializers.
return None;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,13 @@ export enum IndexedColor {
Red = "0",
Green = "1",
Blue,
}
}

export namespace A {
export namespace B {
export enum Enum {
A,
B,
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ export enum IndexedColor {
Green = "1",
Blue,
}

export namespace A {
export namespace B {
export enum Enum {
A,
B,
}
}
}

```

# Diagnostics
Expand Down Expand Up @@ -266,9 +276,56 @@ invalid.ts:34:13 lint/style/useEnumInitializers ━━━━━━━━━━
> 37 │ Blue,
│ ^^^^
38 │ }
39 │
i Allowing implicit initializations for enum members can cause bugs if enum declarations are modified over time.
```

```
invalid.ts:42:21 lint/style/useEnumInitializers FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! This enum declaration contains members that are implicitly initialized.
40 │ export namespace A {
41 │ export namespace B {
> 42 │ export enum Enum {
│ ^^^^
43 │ A,
44 │ B,
i This enum member should be explicitly initialized.
41 │ export namespace B {
42 │ export enum Enum {
> 43 │ A,
│ ^
44 │ B,
45 │ }
i This enum member should be explicitly initialized.
42 │ export enum Enum {
43 │ A,
> 44 │ B,
│ ^
45 │ }
46 │ }
i Allowing implicit initializations for enum members can cause bugs if enum declarations are modified over time.
i Suggested fix: Initialize all enum members.
41 41 │ export namespace B {
42 42 │ export enum Enum {
43 │ - ············A,
44 │ - ············B,
43 │ + ············A·=·0,
44 │ + ············B·=·1,
45 45 │ }
46 46 │ }
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,21 @@ declare enum Weather2 {
Rainy,
Sunny,
}

export declare namespace A {
export namespace B {
export enum Enum {
A,
B,
}
}
}

declare namespace A2 {
export namespace B2 {
export enum Enum {
A,
B,
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,24 @@ declare enum Weather2 {
Sunny,
}

export declare namespace A {
export namespace B {
export enum Enum {
A,
B,
}
}
}

declare namespace A2 {
export namespace B2 {
export enum Enum {
A,
B,
}
}
}

```


20 changes: 20 additions & 0 deletions crates/rome_js_syntax/src/declaration_ext.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use rome_rowan::AstNode;

use crate::{JsSyntaxKind, JsSyntaxNode, TsEnumDeclaration};

impl TsEnumDeclaration {
/// Returns `true` if this enum is an ambient enum or in an ambient context.
pub fn is_ambient(&self) -> bool {
is_in_ambient_context(self.syntax())
}
}

/// Returns `true` if `syntax` is in an ambient context.
fn is_in_ambient_context(syntax: &JsSyntaxNode) -> bool {
syntax.ancestors().any(|x| {
matches!(
x.kind(),
JsSyntaxKind::TS_DECLARE_STATEMENT | JsSyntaxKind::TS_EXPORT_DECLARE_CLAUSE
)
})
}
1 change: 1 addition & 0 deletions crates/rome_js_syntax/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#[macro_use]
mod generated;
pub mod binding_ext;
pub mod declaration_ext;
pub mod directive_ext;
pub mod expr_ext;
pub mod file_source;
Expand Down

0 comments on commit 30315b6

Please sign in to comment.