-
-
Notifications
You must be signed in to change notification settings - Fork 404
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(biome_js_analyze): noRestrictedImports - Added option to allow import from specific locations #2977
base: main
Are you sure you want to change the base?
Conversation
…ort from specific locations
Wouldn't you achieve the same result by using an override where you turn off/change the rule for |
This comment has been minimized.
This comment has been minimized.
CodSpeed Performance ReportMerging #2977 will not alter performanceComparing Summary
|
…all submodules of a module
About using the override option. that is a possibility for the example I given. However, We would need to replicate all the other options of this rule for each folder of each override (e.g.: block the use of loadesh, etc. for each foder). And I realized that the implementation that was used forced the path defined to be exactly the imported module. For instance, imagine we have a folder So I've added a new option: now we can define: "noRestrictedImports": {
"level": "error",
"options": {
"paths": {
"@/features": {
"message": "It is not allowed to reference @/features from here.",
"includeAllSubmodules": true,
"allowedFrom": ["./src/routes"]
}
}
}
} This way all submodules will also be forbidden (eg.: |
} | ||
|
||
if let Some(allowed_from) = &config.allowed_from { | ||
if allowed_from.contains(&file_path.to_string_lossy().to_string()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A couple of things on this line:
.contains
can create false positives. For example, if your configuration is./src/something.js
, a path like../src/something.js
will pass, which is not what you want, right?- I don't think we need to allocate a
String
(via.to_string
) to do a simple check
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
About the the allocation of the .to_string
, I can use:
file_path.to_string_lossy().matches(allowed).count() > 0
And about the false positives, it would be nice to allow something like "./src/**/tests" or other patterns. To allow this, I would need to know the working directory root. because file_path
returns the complete path, and to use the "./" notation as you pointed, I'd need to convert it. Is there any helpers to do that?
crates/biome_js_analyze/src/lint/nursery/no_restricted_imports.rs
Outdated
Show resolved
Hide resolved
crates/biome_js_analyze/tests/specs/nursery/noRestrictedImports/invalid.js
Outdated
Show resolved
Hide resolved
#[derive(Clone, Debug, Default, Deserialize, Deserializable, Eq, PartialEq, Serialize)] | ||
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] | ||
#[serde(rename_all = "camelCase", deny_unknown_fields)] | ||
pub struct RestrictedModuleConfig { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please document each new option that you created? The doc documents well with used for the JSON schema, and we want to document everything that is public.
crates/biome_js_analyze/tests/specs/nursery/noRestrictedImports/invalid.js.snap
Outdated
Show resolved
Hide resolved
The changes that you applied are breaking changes, which is fine because this rule is a nursery. However, I still think we should provide the old option. Not all users will need to provide an object, and the might want to provide only a message. Also, the new options that you added are not documented in the main doc comment. Could you please add more text to describe them? |
About the Option to pass only a message (string) besides the new option that needs an Object, I think It would require a Union Type. However, a union type cannot be used in One option is to create a new lint rule all together, but i think there must be a way. Can you point me in the correct direction? thanks. |
We already have options that are biome/crates/biome_configuration/src/parse/json/linter.rs Lines 65 to 129 in 9e4feb6
It is how we deserialize the configuration of a lint rule, where it can be |
Summary
Sometimes we want to forbid the use of a module in the application but allow in specific places.
One great example of this is when we want to define a linear folder structure and enforce that a folder should not reference the folders above it, but allow referencing all the folders below.
Test Plan
In the example above we would be able to allow using
@/features
module from within./src/routes
but not from other places.