-
-
Notifications
You must be signed in to change notification settings - Fork 475
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(js_analyze): add
lint/noEnum
(#3653)
- Loading branch information
Showing
11 changed files
with
293 additions
and
111 deletions.
There are no files selected for viewing
241 changes: 130 additions & 111 deletions
241
crates/biome_configuration/src/analyzer/linter/rules.rs
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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." | ||
}), | ||
) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
3 changes: 3 additions & 0 deletions
3
crates/biome_js_analyze/tests/specs/nursery/noEnum/invalid.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
enum MyEnum { | ||
A = 123 | ||
} |
31 changes: 31 additions & 0 deletions
31
crates/biome_js_analyze/tests/specs/nursery/noEnum/invalid.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
12
crates/biome_js_analyze/tests/specs/nursery/noEnum/valid.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
``` |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.