This repository has been archived by the owner on Aug 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 656
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rome_js_analyze): noNamespace (#4283)
Co-authored-by: Victorien ELVINGER <victorien@elvinger.fr>
- Loading branch information
Showing
14 changed files
with
430 additions
and
73 deletions.
There are no files selected for viewing
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
83 changes: 83 additions & 0 deletions
83
crates/rome_js_analyze/src/analyzers/nursery/no_namespace.rs
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,83 @@ | ||
use rome_analyze::{context::RuleContext, declare_rule, Ast, Rule, RuleDiagnostic}; | ||
use rome_console::markup; | ||
use rome_js_syntax::TsModuleDeclaration; | ||
use rome_rowan::AstNode; | ||
|
||
declare_rule! { | ||
/// Disallow the use of TypeScript's `namespace`s. | ||
/// | ||
/// Namespaces are an old way to organize your code in TypeScript. | ||
/// They are not recommended anymore and should be replaced by ES6 modules | ||
/// (the `import`/`export` syntax). | ||
/// | ||
/// Source: https://typescript-eslint.io/rules/no-namespace | ||
/// | ||
/// ## Examples | ||
/// | ||
/// ### Invalid | ||
/// | ||
/// ```ts,expect_diagnostic | ||
/// module foo {} | ||
/// ``` | ||
/// | ||
/// ```ts,expect_diagnostic | ||
/// declare module foo {} | ||
/// ``` | ||
/// | ||
/// ```ts,expect_diagnostic | ||
/// namespace foo {} | ||
/// ``` | ||
/// | ||
/// ```ts,expect_diagnostic | ||
/// declare namespace foo {} | ||
/// ``` | ||
/// | ||
/// ## Valid | ||
/// | ||
/// ```ts | ||
/// import foo from 'foo'; | ||
/// export { bar }; | ||
/// ``` | ||
/// | ||
/// ```ts | ||
/// declare global {} | ||
/// ``` | ||
/// | ||
/// ```ts | ||
/// declare module 'foo' {} | ||
/// ``` | ||
/// | ||
pub(crate) NoNamespace { | ||
version: "12.0.0", | ||
name: "noNamespace", | ||
recommended: true, | ||
} | ||
} | ||
|
||
impl Rule for NoNamespace { | ||
type Query = Ast<TsModuleDeclaration>; | ||
type State = (); | ||
type Signals = Option<Self::State>; | ||
type Options = (); | ||
|
||
fn run(_: &RuleContext<Self>) -> Self::Signals { | ||
Some(()) | ||
} | ||
|
||
fn diagnostic(ctx: &RuleContext<Self>, _: &Self::State) -> Option<RuleDiagnostic> { | ||
let node = ctx.query(); | ||
|
||
Some( | ||
RuleDiagnostic::new( | ||
rule_category!(), | ||
node.syntax().text_trimmed_range(), | ||
markup! { | ||
"TypeScript's namespaces are an oudated way to organize code." | ||
}, | ||
) | ||
.note(markup! { | ||
"Prefer the ES6 modules (import/export) over namespaces." | ||
}), | ||
) | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
crates/rome_js_analyze/tests/specs/nursery/noNamespace/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,5 @@ | ||
module foo {} | ||
declare module foo {} | ||
|
||
namespace foo {} | ||
declare namespace foo {} |
79 changes: 79 additions & 0 deletions
79
crates/rome_js_analyze/tests/specs/nursery/noNamespace/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,79 @@ | ||
--- | ||
source: crates/rome_js_analyze/tests/spec_tests.rs | ||
expression: invalid.ts | ||
--- | ||
# Input | ||
```js | ||
module foo {} | ||
declare module foo {} | ||
|
||
namespace foo {} | ||
declare namespace foo {} | ||
|
||
``` | ||
|
||
# Diagnostics | ||
``` | ||
invalid.ts:1:1 lint/nursery/noNamespace ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! TypeScript's namespaces are an oudated way to organize code. | ||
> 1 │ module foo {} | ||
│ ^^^^^^^^^^^^^ | ||
2 │ declare module foo {} | ||
3 │ | ||
i Prefer the ES6 modules (import/export) over namespaces. | ||
``` | ||
|
||
``` | ||
invalid.ts:2:9 lint/nursery/noNamespace ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! TypeScript's namespaces are an oudated way to organize code. | ||
1 │ module foo {} | ||
> 2 │ declare module foo {} | ||
│ ^^^^^^^^^^^^^ | ||
3 │ | ||
4 │ namespace foo {} | ||
i Prefer the ES6 modules (import/export) over namespaces. | ||
``` | ||
|
||
``` | ||
invalid.ts:4:1 lint/nursery/noNamespace ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! TypeScript's namespaces are an oudated way to organize code. | ||
2 │ declare module foo {} | ||
3 │ | ||
> 4 │ namespace foo {} | ||
│ ^^^^^^^^^^^^^^^^ | ||
5 │ declare namespace foo {} | ||
6 │ | ||
i Prefer the ES6 modules (import/export) over namespaces. | ||
``` | ||
|
||
``` | ||
invalid.ts:5:9 lint/nursery/noNamespace ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! TypeScript's namespaces are an oudated way to organize code. | ||
4 │ namespace foo {} | ||
> 5 │ declare namespace foo {} | ||
│ ^^^^^^^^^^^^^^^^ | ||
6 │ | ||
i Prefer the ES6 modules (import/export) over namespaces. | ||
``` | ||
|
||
|
7 changes: 7 additions & 0 deletions
7
crates/rome_js_analyze/tests/specs/nursery/noNamespace/valid.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,7 @@ | ||
/* should not generate diagnostics */ | ||
|
||
export {}; | ||
|
||
declare global {} | ||
|
||
declare module "foo" {} |
17 changes: 17 additions & 0 deletions
17
crates/rome_js_analyze/tests/specs/nursery/noNamespace/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,17 @@ | ||
--- | ||
source: crates/rome_js_analyze/tests/spec_tests.rs | ||
expression: valid.ts | ||
--- | ||
# Input | ||
```js | ||
/* should not generate diagnostics */ | ||
|
||
export {}; | ||
|
||
declare global {} | ||
|
||
declare module "foo" {} | ||
|
||
``` | ||
|
||
|
Oops, something went wrong.