Skip to content

Commit

Permalink
feat(linter): noRestrictedImports add patterns option
Browse files Browse the repository at this point in the history
  • Loading branch information
nekocode committed Oct 15, 2024
1 parent 561b54c commit 4bcffbe
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 7 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/biome_js_analyze/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ biome_suppression = { workspace = true }
biome_unicode_table = { workspace = true }
bitvec = "1.0.1"
enumflags2 = { workspace = true }
ignore = { workspace = true }
natord = { workspace = true }
regex = { workspace = true }
roaring = "0.10.6"
Expand Down
27 changes: 23 additions & 4 deletions crates/biome_js_analyze/src/lint/nursery/no_restricted_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use biome_console::markup;
use biome_deserialize_macros::Deserializable;
use biome_js_syntax::{inner_string_text, AnyJsImportLike};
use biome_rowan::TextRange;
use ignore::gitignore::GitignoreBuilder;
use rustc_hash::FxHashMap;
use serde::{Deserialize, Serialize};

Expand All @@ -19,6 +20,9 @@ declare_lint_rule! {
/// "paths": {
/// "lodash": "Using lodash is not encouraged",
/// "underscore": "Using underscore is not encouraged"
/// },
/// "patterns": {
/// "lodash/*": "Using lodash is not encouraged"
/// }
/// }
/// }
Expand All @@ -44,6 +48,9 @@ pub struct RestrictedImportsOptions {
/// A list of names that should trigger the rule
#[serde(skip_serializing_if = "FxHashMap::is_empty")]
paths: FxHashMap<Box<str>, Box<str>>,
/// A list of gitignore-style patterns that should trigger the rule
#[serde(skip_serializing_if = "FxHashMap::is_empty")]
patterns: FxHashMap<Box<str>, Box<str>>,
}

impl Rule for NoRestrictedImports {
Expand All @@ -60,10 +67,22 @@ impl Rule for NoRestrictedImports {
let module_name = node.module_name_token()?;
let inner_text = inner_string_text(&module_name);

ctx.options()
.paths
.get(inner_text.text())
.map(|message| (module_name.text_trimmed_range(), message.to_string()))
// Check against exact paths
if let Some(message) = ctx.options().paths.get(inner_text.text()) {
return Some((module_name.text_trimmed_range(), message.to_string()));
}

// Check against gitignore-style patterns
for (pattern, message) in &ctx.options().patterns {
let mut builder = GitignoreBuilder::new("");
builder.add_line(None, pattern).unwrap();
let gitignore = builder.build().unwrap();
if gitignore.matched(inner_text.text(), false).is_ignore() {
return Some((module_name.text_trimmed_range(), message.to_string()));
}
}

None
}

fn diagnostic(_ctx: &RuleContext<Self>, (span, text): &Self::State) -> Option<RuleDiagnostic> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
import eslint from 'eslint';
const l = require('lodash');
const g = require('lodash/get');
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ expression: invalid.js
```jsx
import eslint from 'eslint';
const l = require('lodash');
const g = require('lodash/get');

```

Expand All @@ -18,7 +19,7 @@ invalid.js:1:20 lint/nursery/noRestrictedImports ━━━━━━━━━━
> 1 │ import eslint from 'eslint';
│ ^^^^^^^^
2 │ const l = require('lodash');
3 │
3 │ const g = require('lodash/get');
```
Expand All @@ -31,9 +32,22 @@ invalid.js:2:19 lint/nursery/noRestrictedImports ━━━━━━━━━━
1 │ import eslint from 'eslint';
> 2 │ const l = require('lodash');
│ ^^^^^^^^
3 │
3 │ const g = require('lodash/get');
4 │
```

```
invalid.js:3:19 lint/nursery/noRestrictedImports ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! It's not recommended to use lodash
1 │ import eslint from 'eslint';
2 │ const l = require('lodash');
> 3 │ const g = require('lodash/get');
│ ^^^^^^^^^^^^
4 │
```
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
"paths": {
"eslint": "Importing Eslint is forbidden",
"lodash": "It's not recommended to use lodash"
},
"patterns": {
"lodash/*": "It's not recommended to use lodash"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
"options": {
"paths": {
"node:fs": "Importing from node:fs is forbidden"
},
"patterns": {
"lodash/*": "It's not recommended to use lodash"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
declare module "node:fs" {}
declare module "node:fs" {}
declare module "lodash/get" {}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ expression: valid.ts
# Input
```ts
declare module "node:fs" {}
declare module "lodash/get" {}
```
4 changes: 4 additions & 0 deletions packages/@biomejs/backend-jsonrpc/src/workspace.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions packages/@biomejs/biome/configuration_schema.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4bcffbe

Please sign in to comment.