Skip to content

Commit

Permalink
feat(js_analyze): add lint/noEnum (#3653)
Browse files Browse the repository at this point in the history
  • Loading branch information
nickfla1 authored Aug 27, 2024
1 parent 17bb2fb commit 2d590ea
Show file tree
Hide file tree
Showing 11 changed files with 293 additions and 111 deletions.
241 changes: 130 additions & 111 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 @@ -125,6 +125,7 @@ define_categories! {
"lint/nursery/noDuplicatedFields": "https://biomejs.dev/linter/rules/no-duplicated-fields",
"lint/nursery/noDynamicNamespaceImportAccess": "https://biomejs.dev/linter/rules/no-dynamic-namespace-import-access",
"lint/nursery/noEmptyBlock": "https://biomejs.dev/linter/rules/no-empty-block",
"lint/nursery/noEnum": "https://biomejs.dev/linter/rules/no-enum",
"lint/nursery/noEvolvingTypes": "https://biomejs.dev/linter/rules/no-evolving-types",
"lint/nursery/noExportedImports": "https://biomejs.dev/linter/rules/no-exported-imports",
"lint/nursery/noImportantInKeyframe": "https://biomejs.dev/linter/rules/no-important-in-keyframe",
Expand Down
2 changes: 2 additions & 0 deletions crates/biome_js_analyze/src/lint/nursery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub mod no_console;
pub mod no_done_callback;
pub mod no_duplicate_else_if;
pub mod no_dynamic_namespace_import_access;
pub mod no_enum;
pub mod no_evolving_types;
pub mod no_exported_imports;
pub mod no_irregular_whitespace;
Expand Down Expand Up @@ -50,6 +51,7 @@ declare_lint_group! {
self :: no_done_callback :: NoDoneCallback ,
self :: no_duplicate_else_if :: NoDuplicateElseIf ,
self :: no_dynamic_namespace_import_access :: NoDynamicNamespaceImportAccess ,
self :: no_enum :: NoEnum ,
self :: no_evolving_types :: NoEvolvingTypes ,
self :: no_exported_imports :: NoExportedImports ,
self :: no_irregular_whitespace :: NoIrregularWhitespace ,
Expand Down
97 changes: 97 additions & 0 deletions crates/biome_js_analyze/src/lint/nursery/no_enum.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
use biome_analyze::{context::RuleContext, declare_lint_rule, Ast, Rule, RuleDiagnostic};
use biome_console::markup;
use biome_js_syntax::{JsFileSource, TsEnumDeclaration};
use biome_rowan::AstNode;

declare_lint_rule! {
/// Disallow TypeScript enum.
///
/// TypeScript enums are not a type-level extension to JavaScript like type annotations or definitions.
/// Users may wish to disable non-type-level extensions to use bundlers or compilers that only strip types.
///
/// Const enums are not covered by this rule since `noConstEnum` already handles them.
/// Enums within the ambient context, including declaration files, are ignores as well.
///
/// ## Examples
///
/// ### Invalid
///
/// ```ts,expect_diagnostic
/// enum Foo {
/// BAR = 'bar',
/// BAZ = 'baz',
/// }
/// ```
///
/// ### Valid
///
/// ```ts
/// const Foo = {
/// BAR: 'bar',
/// BAZ: 'baz',
/// } as const
/// ```
///
/// ```ts
/// type Foo = 'bar' | 'baz'
/// ```
///
/// ```ts
/// const enum Foo {
/// BAR = 'bar',
/// BAZ = 'baz',
/// }
/// ```
///
///
pub NoEnum {
version: "next",
name: "noEnum",
language: "ts",
recommended: false,
}
}

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

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

let source_type = ctx.source_type::<JsFileSource>().language();
let is_declaration = source_type.is_definition_file();

if is_declaration {
return None;
}

let is_const_decl = enum_decl.const_token().is_some();

if is_const_decl || enum_decl.is_ambient() {
return None;
}

Some(())
}

fn diagnostic(ctx: &RuleContext<Self>, _state: &Self::State) -> Option<RuleDiagnostic> {
Some(
RuleDiagnostic::new(
rule_category!(),
ctx.query().range(),
markup! {
"Don't use "<Emphasis>"enum"</Emphasis>
},
)
.note(markup! {
"TypeScript enums are not a type-level extension to JavaScript like type annotations or definitions. Users may wish to disable non-type-level extensions to use bundlers or compilers that only strip types."
})
.note(markup! {
"Use JavaScript objects or TypeScript unions instead."
}),
)
}
}
1 change: 1 addition & 0 deletions crates/biome_js_analyze/src/options.rs

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

3 changes: 3 additions & 0 deletions crates/biome_js_analyze/tests/specs/nursery/noEnum/invalid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
enum MyEnum {
A = 123
}
31 changes: 31 additions & 0 deletions crates/biome_js_analyze/tests/specs/nursery/noEnum/invalid.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
source: crates/biome_js_analyze/tests/spec_tests.rs
expression: invalid.ts
---
# Input
```ts
enum MyEnum {
A = 123
}

```

# Diagnostics
```
invalid.ts:1:1 lint/nursery/noEnum ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! Don't use enum
> 1 │ enum MyEnum {
│ ^^^^^^^^^^^^^
> 2 │ A = 123
> 3 │ }
│ ^
4 │
i TypeScript enums are not a type-level extension to JavaScript like type annotations or definitions. Users may wish to disable non-type-level extensions to use bundlers or compilers that only strip types.
i Use JavaScript objects or TypeScript unions instead.
```
4 changes: 4 additions & 0 deletions crates/biome_js_analyze/tests/specs/nursery/noEnum/valid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* should not generate diagnostics */
const enum MyEnum {
A = 123
}
12 changes: 12 additions & 0 deletions crates/biome_js_analyze/tests/specs/nursery/noEnum/valid.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
source: crates/biome_js_analyze/tests/spec_tests.rs
expression: valid.ts
---
# Input
```ts
/* should not generate diagnostics */
const enum MyEnum {
A = 123
}

```
5 changes: 5 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.

7 changes: 7 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.

0 comments on commit 2d590ea

Please sign in to comment.