-
-
Notifications
You must be signed in to change notification settings - Fork 449
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
862 additions
and
36 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
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.
127 changes: 127 additions & 0 deletions
127
crates/rome_js_analyze/src/semantic_analyzers/nursery/use_export_type.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,127 @@ | ||
use crate::{semantic_services::Semantic, JsRuleAction}; | ||
use rome_analyze::{context::RuleContext, declare_rule, ActionCategory, Rule, RuleDiagnostic}; | ||
use rome_console::markup; | ||
use rome_diagnostics::Applicability; | ||
use rome_js_factory::make; | ||
use rome_js_syntax::{ | ||
AnyJsExportNamedSpecifier, JsExportNamedClause, JsExportNamedSpecifierList, JsFileSource, T, | ||
}; | ||
use rome_rowan::{AstNode, AstSeparatedList, BatchMutationExt, TextRange, TriviaPieceKind}; | ||
|
||
declare_rule! { | ||
/// Promotes the use of `export type` for type-only types. | ||
/// | ||
/// _TypeScript_ allows specifying a `type` keyword on an `export` to indicate that | ||
/// the `export` doesn't exist at runtime. | ||
/// This will enable transpilers to safely drop exports of types without looking for its definition. | ||
/// | ||
/// ## Examples | ||
/// | ||
/// ### Invalid | ||
/// | ||
/// ```ts,expect_diagnostic | ||
/// interface I {} | ||
/// export { I }; | ||
/// ``` | ||
/// | ||
/// ```ts,expect_diagnostic | ||
/// type T = number; | ||
/// export { T }; | ||
/// ``` | ||
/// | ||
/// ```ts,expect_diagnostic | ||
/// import type { T } from "./mod.js"; | ||
/// export { T }; | ||
/// ``` | ||
/// | ||
/// ## Valid | ||
/// | ||
/// ```js | ||
/// class C {} | ||
/// function f() {} | ||
/// export { C, f }; | ||
/// ``` | ||
/// | ||
/// This rules applies only to identifiers locally defined. | ||
/// It doesn't warn against a type exported as a value in re-export clause such as: | ||
/// | ||
/// ```ts | ||
/// export { TypeA } from "./mod.ts" | ||
/// ``` | ||
pub(crate) UseExportType { | ||
version: "next", | ||
name: "useExportType", | ||
recommended: true, | ||
} | ||
} | ||
|
||
impl Rule for UseExportType { | ||
type Query = Semantic<AnyJsExportNamedSpecifier>; | ||
type State = TextRange; | ||
type Signals = Option<Self::State>; | ||
type Options = (); | ||
|
||
fn run(ctx: &RuleContext<Self>) -> Self::Signals { | ||
let source_type = ctx.source_type::<JsFileSource>(); | ||
if !source_type.language().is_typescript() { | ||
return None; | ||
} | ||
let specifier = ctx.query(); | ||
let model = ctx.model(); | ||
if specifier.is_type_only() { | ||
return None; | ||
} | ||
let reference = specifier.local_name().ok()?; | ||
let binding = model.binding(&reference)?; | ||
let binding = binding.tree(); | ||
if binding.is_type_only() { | ||
return Some(binding.range()); | ||
} | ||
None | ||
} | ||
|
||
fn diagnostic(ctx: &RuleContext<Self>, declaration: &Self::State) -> Option<RuleDiagnostic> { | ||
Some( | ||
RuleDiagnostic::new( | ||
rule_category!(), | ||
ctx.query().range(), | ||
markup! { | ||
"This export is only a type and should thus use "<Emphasis>"export type"</Emphasis>"." | ||
}, | ||
).detail(declaration, markup! { | ||
"The type is defined here." | ||
}) | ||
) | ||
} | ||
|
||
fn action(ctx: &RuleContext<Self>, _: &Self::State) -> Option<JsRuleAction> { | ||
let specifier = ctx.query(); | ||
let mut mutation = ctx.root().begin(); | ||
let type_token = | ||
Some(make::token(T![type]).with_trailing_trivia([(TriviaPieceKind::Whitespace, " ")])); | ||
if let Some(specifier_list) = specifier.parent::<JsExportNamedSpecifierList>() { | ||
if specifier_list.iter().count() == 1 { | ||
if let Some(export) = specifier_list.parent::<JsExportNamedClause>() { | ||
let new_export = export.clone().with_type_token(type_token); | ||
mutation.replace_node(export, new_export); | ||
return Some(JsRuleAction { | ||
category: ActionCategory::QuickFix, | ||
applicability: Applicability::Always, | ||
message: markup! { "Use "<Emphasis>"export type"</Emphasis>"." }.to_owned(), | ||
mutation, | ||
}); | ||
} | ||
} | ||
} | ||
mutation.replace_node_discard_trivia( | ||
specifier.clone(), | ||
specifier.clone().with_type_token(type_token), | ||
); | ||
Some(JsRuleAction { | ||
category: ActionCategory::QuickFix, | ||
applicability: Applicability::Always, | ||
message: markup! { "Use an inline type export." }.to_owned(), | ||
mutation, | ||
}) | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
crates/rome_js_analyze/tests/specs/nursery/useExportType/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,18 @@ | ||
import { type T1, V1 } from "./mod.ts"; | ||
export { T1, V1 }; | ||
|
||
import type { T2, T3 } from "./mod.ts"; | ||
export { T2, T3 }; | ||
|
||
import type T4 from "./mod.ts"; | ||
export { T4 }; | ||
|
||
import type * as ns from "./mod.ts"; | ||
export { ns }; | ||
|
||
interface Interface {} | ||
type TypeAlias = {} | ||
enum Enum {} | ||
function func() {} | ||
class Class {} | ||
export { Interface, TypeAlias, Enum, func as f, Class }; |
215 changes: 215 additions & 0 deletions
215
crates/rome_js_analyze/tests/specs/nursery/useExportType/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,215 @@ | ||
--- | ||
source: crates/rome_js_analyze/tests/spec_tests.rs | ||
expression: invalid.ts | ||
--- | ||
# Input | ||
```js | ||
import { type T1, V1 } from "./mod.ts"; | ||
export { T1, V1 }; | ||
|
||
import type { T2, T3 } from "./mod.ts"; | ||
export { T2, T3 }; | ||
|
||
import type T4 from "./mod.ts"; | ||
export { T4 }; | ||
|
||
import type * as ns from "./mod.ts"; | ||
export { ns }; | ||
|
||
interface Interface {} | ||
type TypeAlias = {} | ||
enum Enum {} | ||
function func() {} | ||
class Class {} | ||
export { Interface, TypeAlias, Enum, func as f, Class }; | ||
|
||
``` | ||
|
||
# Diagnostics | ||
``` | ||
invalid.ts:2:10 lint/nursery/useExportType FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! This export is only a type and should thus use export type. | ||
1 │ import { type T1, V1 } from "./mod.ts"; | ||
> 2 │ export { T1, V1 }; | ||
│ ^^ | ||
3 │ | ||
4 │ import type { T2, T3 } from "./mod.ts"; | ||
i The type is defined here. | ||
> 1 │ import { type T1, V1 } from "./mod.ts"; | ||
│ ^^ | ||
2 │ export { T1, V1 }; | ||
3 │ | ||
i Safe fix: Use an inline type export. | ||
2 │ export·{·type·T1,·V1·}; | ||
│ +++++ | ||
``` | ||
|
||
``` | ||
invalid.ts:5:10 lint/nursery/useExportType FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! This export is only a type and should thus use export type. | ||
4 │ import type { T2, T3 } from "./mod.ts"; | ||
> 5 │ export { T2, T3 }; | ||
│ ^^ | ||
6 │ | ||
7 │ import type T4 from "./mod.ts"; | ||
i The type is defined here. | ||
2 │ export { T1, V1 }; | ||
3 │ | ||
> 4 │ import type { T2, T3 } from "./mod.ts"; | ||
│ ^^ | ||
5 │ export { T2, T3 }; | ||
6 │ | ||
i Safe fix: Use an inline type export. | ||
5 │ export·{·type·T2,·T3·}; | ||
│ +++++ | ||
``` | ||
|
||
``` | ||
invalid.ts:5:14 lint/nursery/useExportType FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! This export is only a type and should thus use export type. | ||
4 │ import type { T2, T3 } from "./mod.ts"; | ||
> 5 │ export { T2, T3 }; | ||
│ ^^ | ||
6 │ | ||
7 │ import type T4 from "./mod.ts"; | ||
i The type is defined here. | ||
2 │ export { T1, V1 }; | ||
3 │ | ||
> 4 │ import type { T2, T3 } from "./mod.ts"; | ||
│ ^^ | ||
5 │ export { T2, T3 }; | ||
6 │ | ||
i Safe fix: Use an inline type export. | ||
5 │ export·{·T2,·type·T3·}; | ||
│ +++++ | ||
``` | ||
|
||
``` | ||
invalid.ts:8:10 lint/nursery/useExportType FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! This export is only a type and should thus use export type. | ||
7 │ import type T4 from "./mod.ts"; | ||
> 8 │ export { T4 }; | ||
│ ^^ | ||
9 │ | ||
10 │ import type * as ns from "./mod.ts"; | ||
i The type is defined here. | ||
5 │ export { T2, T3 }; | ||
6 │ | ||
> 7 │ import type T4 from "./mod.ts"; | ||
│ ^^ | ||
8 │ export { T4 }; | ||
9 │ | ||
i Safe fix: Use export type. | ||
8 │ export·type·{·T4·}; | ||
│ +++++ | ||
``` | ||
|
||
``` | ||
invalid.ts:11:10 lint/nursery/useExportType FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! This export is only a type and should thus use export type. | ||
10 │ import type * as ns from "./mod.ts"; | ||
> 11 │ export { ns }; | ||
│ ^^ | ||
12 │ | ||
13 │ interface Interface {} | ||
i The type is defined here. | ||
8 │ export { T4 }; | ||
9 │ | ||
> 10 │ import type * as ns from "./mod.ts"; | ||
│ ^^ | ||
11 │ export { ns }; | ||
12 │ | ||
i Safe fix: Use export type. | ||
11 │ export·type·{·ns·}; | ||
│ +++++ | ||
``` | ||
|
||
``` | ||
invalid.ts:18:10 lint/nursery/useExportType FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! This export is only a type and should thus use export type. | ||
16 │ function func() {} | ||
17 │ class Class {} | ||
> 18 │ export { Interface, TypeAlias, Enum, func as f, Class }; | ||
│ ^^^^^^^^^ | ||
19 │ | ||
i The type is defined here. | ||
11 │ export { ns }; | ||
12 │ | ||
> 13 │ interface Interface {} | ||
│ ^^^^^^^^^ | ||
14 │ type TypeAlias = {} | ||
15 │ enum Enum {} | ||
i Safe fix: Use an inline type export. | ||
18 │ export·{·type·Interface,·TypeAlias,·Enum,·func·as·f,·Class·}; | ||
│ +++++ | ||
``` | ||
|
||
``` | ||
invalid.ts:18:21 lint/nursery/useExportType FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! This export is only a type and should thus use export type. | ||
16 │ function func() {} | ||
17 │ class Class {} | ||
> 18 │ export { Interface, TypeAlias, Enum, func as f, Class }; | ||
│ ^^^^^^^^^ | ||
19 │ | ||
i The type is defined here. | ||
13 │ interface Interface {} | ||
> 14 │ type TypeAlias = {} | ||
│ ^^^^^^^^^ | ||
15 │ enum Enum {} | ||
16 │ function func() {} | ||
i Safe fix: Use an inline type export. | ||
18 │ export·{·Interface,·type·TypeAlias,·Enum,·func·as·f,·Class·}; | ||
│ +++++ | ||
``` | ||
|
||
|
Oops, something went wrong.