-
-
Notifications
You must be signed in to change notification settings - Fork 503
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(noExportedImports): add lint rule (#3097)
- Loading branch information
Showing
20 changed files
with
356 additions
and
132 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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
85 changes: 85 additions & 0 deletions
85
crates/biome_js_analyze/src/lint/nursery/no_exported_imports.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,85 @@ | ||
use biome_analyze::{context::RuleContext, declare_rule, Rule, RuleDiagnostic}; | ||
use biome_console::markup; | ||
use biome_js_semantic::CanBeImportedExported; | ||
use biome_js_syntax::AnyJsImportSpecifier; | ||
use biome_rowan::AstNode; | ||
|
||
use crate::services::semantic::Semantic; | ||
|
||
declare_rule! { | ||
/// Disallow exporting an imported variable. | ||
/// | ||
/// In JavaScript, you can re-export a variable either by using `export from` or | ||
/// by first importing the variable and then exporting it with a regular `export`. | ||
/// | ||
/// You may prefer to use the first approach, as it clearly communicates the intention | ||
/// to re-export an import, and can make static analysis easier. | ||
/// | ||
/// ## Examples | ||
/// | ||
/// ### Invalid | ||
/// | ||
/// ```js,expect_diagnostic | ||
/// import { A } from "mod"; | ||
/// export { A }; | ||
/// ``` | ||
/// | ||
/// ```js,expect_diagnostic | ||
/// import * as ns from "mod"; | ||
/// export { ns }; | ||
/// ``` | ||
/// | ||
/// ```js,expect_diagnostic | ||
/// import D from "mod"; | ||
/// export { D }; | ||
/// ``` | ||
/// | ||
/// ### Valid | ||
/// | ||
/// ```js | ||
/// export { A } from "mod"; | ||
/// export * as ns from "mod"; | ||
/// export { default as D } from "mod"; | ||
/// ``` | ||
/// | ||
pub NoExportedImports { | ||
version: "next", | ||
name: "noExportedImports", | ||
language: "js", | ||
recommended: false, | ||
} | ||
} | ||
|
||
impl Rule for NoExportedImports { | ||
type Query = Semantic<AnyJsImportSpecifier>; | ||
type State = (); | ||
type Signals = Option<Self::State>; | ||
type Options = (); | ||
|
||
fn run(ctx: &RuleContext<Self>) -> Self::Signals { | ||
let specifier = ctx.query(); | ||
let local_name = specifier.local_name().ok()?; | ||
let local_name = local_name.as_js_identifier_binding()?; | ||
if local_name.is_exported(ctx.model()) { | ||
Some(()) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
fn diagnostic(ctx: &RuleContext<Self>, _state: &Self::State) -> Option<RuleDiagnostic> { | ||
let specifier = ctx.query(); | ||
Some( | ||
RuleDiagnostic::new( | ||
rule_category!(), | ||
specifier.range(), | ||
markup! { | ||
"An import should not be exported. Use "<Emphasis>"export from"</Emphasis>"instead." | ||
}, | ||
) | ||
.note(markup! { | ||
<Emphasis>"export from"</Emphasis>" makes it clearer that the intention is to re-export a variable." | ||
}), | ||
) | ||
} | ||
} |
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
8 changes: 8 additions & 0 deletions
8
crates/biome_js_analyze/tests/specs/nursery/noExportedImports/invalid.js
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,8 @@ | ||
import { A } from "mod"; | ||
export { A }; | ||
|
||
import * as ns from "mod"; | ||
export { ns }; | ||
|
||
import D from "mod"; | ||
export { D }; |
64 changes: 64 additions & 0 deletions
64
crates/biome_js_analyze/tests/specs/nursery/noExportedImports/invalid.js.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,64 @@ | ||
--- | ||
source: crates/biome_js_analyze/tests/spec_tests.rs | ||
expression: invalid.js | ||
--- | ||
# Input | ||
```jsx | ||
import { A } from "mod"; | ||
export { A }; | ||
|
||
import * as ns from "mod"; | ||
export { ns }; | ||
|
||
import D from "mod"; | ||
export { D }; | ||
``` | ||
|
||
# Diagnostics | ||
``` | ||
invalid.js:1:10 lint/nursery/noExportedImports ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! An import should not be exported. Use export frominstead. | ||
> 1 │ import { A } from "mod"; | ||
│ ^ | ||
2 │ export { A }; | ||
3 │ | ||
i export from makes it clearer that the intention is to re-export a variable. | ||
``` | ||
|
||
``` | ||
invalid.js:4:8 lint/nursery/noExportedImports ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! An import should not be exported. Use export frominstead. | ||
2 │ export { A }; | ||
3 │ | ||
> 4 │ import * as ns from "mod"; | ||
│ ^^^^^^^ | ||
5 │ export { ns }; | ||
6 │ | ||
i export from makes it clearer that the intention is to re-export a variable. | ||
``` | ||
|
||
``` | ||
invalid.js:7:8 lint/nursery/noExportedImports ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! An import should not be exported. Use export frominstead. | ||
5 │ export { ns }; | ||
6 │ | ||
> 7 │ import D from "mod"; | ||
│ ^ | ||
8 │ export { D }; | ||
i export from makes it clearer that the intention is to re-export a variable. | ||
``` |
3 changes: 3 additions & 0 deletions
3
crates/biome_js_analyze/tests/specs/nursery/noExportedImports/valid.js
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 @@ | ||
export { A } from "mod"; | ||
export * as ns from "mod"; | ||
export { default as D } from "mod"; |
Oops, something went wrong.