Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

Commit

Permalink
feat(rome_js_analyze): noNamespace (#4283)
Browse files Browse the repository at this point in the history
Co-authored-by: Victorien ELVINGER <victorien@elvinger.fr>
  • Loading branch information
noftaly and Conaclos authored Mar 10, 2023
1 parent c288c35 commit a5d1251
Show file tree
Hide file tree
Showing 14 changed files with 430 additions and 73 deletions.
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 @@ -98,6 +98,7 @@ define_categories! {
"lint/nursery/noSvgWithoutTitle": "https://docs.rome.tools/lint/rules/noSvgWithoutTitle",
"lint/nursery/noUselessCatch": "https://docs.rome.tools/lint/rules/noUselessCatch",
"lint/nursery/noParameterAssign": "https://docs.rome.tools/lint/rules/noParameterAssign",
"lint/nursery/noNamespace": "https://docs.rome.tools/lint/rules/noNamespace",
// Insert new nursery rule here
"lint/nursery/noRedeclaration": "https://docs.rome.tools/lint/rules/noRedeclaration",
"lint/nursery/useNamespaceKeyword": "https://docs.rome.tools/lint/rules/useNamespaceKeyword",
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.

83 changes: 83 additions & 0 deletions crates/rome_js_analyze/src/analyzers/nursery/no_namespace.rs
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."
}),
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module foo {}
declare module foo {}

namespace foo {}
declare namespace foo {}
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.
```


Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/* should not generate diagnostics */

export {};

declare global {}

declare module "foo" {}
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" {}

```


Loading

0 comments on commit a5d1251

Please sign in to comment.