Skip to content

Commit 9751ea8

Browse files
committed
chore: match resource names to those used by the pattern resource names
1 parent 2c649da commit 9751ea8

File tree

3 files changed

+65
-68
lines changed

3 files changed

+65
-68
lines changed

crates/biome_js_analyze/src/lint/nursery/no_restricted_imports.rs

+29-30
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ declare_lint_rule! {
585585
pub struct RestrictedImportsOptions {
586586
/// A list of import paths that should trigger the rule.
587587
#[serde(skip_serializing_if = "FxHashMap::is_empty")]
588-
paths: FxHashMap<Box<str>, CustomRestrictedImport>,
588+
paths: FxHashMap<Box<str>, Paths>,
589589

590590
/// A list of gitignore-style patterns or regular expressions that should trigger the rule.
591591
#[serde(skip_serializing_if = "Option::is_none")]
@@ -633,7 +633,7 @@ impl ImportRestrictionStatus {
633633
)]
634634
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
635635
#[serde(rename_all = "camelCase", deny_unknown_fields, default)]
636-
pub struct CustomRestrictedImportOptions {
636+
pub struct PathOptions {
637637
/// The message to display when this module is imported.
638638
#[serde(skip_serializing_if = "str::is_empty")]
639639
message: Box<str>,
@@ -647,7 +647,7 @@ pub struct CustomRestrictedImportOptions {
647647
allow_import_names: Box<[Box<str>]>,
648648
}
649649

650-
impl CustomRestrictedImportOptions {
650+
impl PathOptions {
651651
pub fn has_import_name_patterns(&self) -> bool {
652652
!self.import_names.is_empty() || !self.allow_import_names.is_empty()
653653
}
@@ -703,27 +703,27 @@ impl CustomRestrictedImportOptions {
703703
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
704704
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
705705
#[serde(untagged)]
706-
pub enum CustomRestrictedImport {
706+
pub enum Paths {
707707
/// The message to display when this module is imported.
708708
Plain(Box<str>),
709709
/// Additional options to configure the message and allowed/disallowed import names.
710-
WithOptions(CustomRestrictedImportOptions),
710+
WithOptions(PathOptions),
711711
}
712712

713-
impl From<CustomRestrictedImport> for CustomRestrictedImportOptions {
714-
fn from(options: CustomRestrictedImport) -> Self {
715-
match options {
716-
CustomRestrictedImport::Plain(message) => Self {
713+
impl From<Paths> for PathOptions {
714+
fn from(paths: Paths) -> Self {
715+
match paths {
716+
Paths::Plain(message) => Self {
717717
message,
718718
import_names: [].into(),
719719
allow_import_names: [].into(),
720720
},
721-
CustomRestrictedImport::WithOptions(options) => options,
721+
Paths::WithOptions(path_options) => path_options,
722722
}
723723
}
724724
}
725725

726-
impl Deserializable for CustomRestrictedImport {
726+
impl Deserializable for Paths {
727727
fn deserialize(
728728
ctx: &mut impl DeserializationContext,
729729
value: &impl DeserializableValue,
@@ -814,7 +814,7 @@ impl Deserializable for Patterns {
814814

815815
struct RestrictedImportVisitor<'a> {
816816
import_source: &'a str,
817-
restricted_import: CustomRestrictedImportOptions,
817+
path_options: PathOptions,
818818
results: Vec<RestrictedImportMessage>,
819819
}
820820

@@ -1213,7 +1213,7 @@ impl RestrictedImportVisitor<'_> {
12131213
fn visit_imported_identifier(&mut self, name_token: &SyntaxToken<JsLanguage>) -> Option<()> {
12141214
// TODO: inner_string_text removes quotes but does not e.g. decode escape sequences.
12151215
// If the imported name uses e.g. Unicode escape sequences, this may cause
1216-
// problems because restricted_import.(allow_)import_names contains decoded
1216+
// problems because path_options.(allow_)import_names contains decoded
12171217
// strings, while inner_string_text(name_token) returns encoded strings.
12181218
self.visit_special_import_token(name_token, inner_string_text(name_token).text())
12191219
}
@@ -1225,19 +1225,19 @@ impl RestrictedImportVisitor<'_> {
12251225
import_node: &SyntaxNode<JsLanguage>,
12261226
name_or_alias: &str,
12271227
) -> Option<()> {
1228-
let status = self.restricted_import.is_import_allowed(name_or_alias);
1228+
let status = self.path_options.is_import_allowed(name_or_alias);
12291229
if status.is_allowed() {
12301230
return None;
12311231
}
12321232
self.results.push(RestrictedImportMessage {
12331233
location: import_node.text_trimmed_range(),
1234-
message: self.restricted_import.get_message_for_restriction(
1234+
message: self.path_options.get_message_for_restriction(
12351235
self.import_source,
12361236
name_or_alias,
12371237
status.reason(),
12381238
),
12391239
import_source: self.import_source.to_string(),
1240-
allowed_import_names: self.restricted_import.allow_import_names.clone(),
1240+
allowed_import_names: self.path_options.allow_import_names.clone(),
12411241
});
12421242
Some(())
12431243
}
@@ -1249,19 +1249,19 @@ impl RestrictedImportVisitor<'_> {
12491249
import_token: &SyntaxToken<JsLanguage>,
12501250
name_or_alias: &str,
12511251
) -> Option<()> {
1252-
let status = self.restricted_import.is_import_allowed(name_or_alias);
1252+
let status = self.path_options.is_import_allowed(name_or_alias);
12531253
if status.is_allowed() {
12541254
return None;
12551255
}
12561256
self.results.push(RestrictedImportMessage {
12571257
location: import_token.text_trimmed_range(),
1258-
message: self.restricted_import.get_message_for_restriction(
1258+
message: self.path_options.get_message_for_restriction(
12591259
self.import_source,
12601260
name_or_alias,
12611261
status.reason(),
12621262
),
12631263
import_source: self.import_source.to_string(),
1264-
allowed_import_names: self.restricted_import.allow_import_names.clone(),
1264+
allowed_import_names: self.path_options.allow_import_names.clone(),
12651265
});
12661266
Some(())
12671267
}
@@ -1292,17 +1292,16 @@ impl Rule for NoRestrictedImports {
12921292
let import_source = import_source_text.text();
12931293
let options = ctx.options();
12941294

1295-
if let Some(restricted_import_settings) = options.paths.get(import_source) {
1296-
let restricted_import: CustomRestrictedImportOptions =
1297-
restricted_import_settings.clone().into();
1295+
if let Some(paths) = options.paths.get(import_source) {
1296+
let path_options: PathOptions = paths.clone().into();
12981297

12991298
match node {
13001299
AnyJsImportLike::JsModuleSource(module_source_node) => {
1301-
if !restricted_import.has_import_name_patterns() {
1300+
if !path_options.has_import_name_patterns() {
13021301
// All imports disallowed, add diagnostic to the import source
13031302
vec![RestrictedImportMessage {
13041303
location: module_name.text_trimmed_range(),
1305-
message: restricted_import.get_message_for_restriction(
1304+
message: path_options.get_message_for_restriction(
13061305
import_source,
13071306
"",
13081307
ImportRestrictionCause::ImportSource,
@@ -1314,7 +1313,7 @@ impl Rule for NoRestrictedImports {
13141313
// Check (and possibly report) each imported name individually
13151314
let mut visitor = RestrictedImportVisitor {
13161315
import_source,
1317-
restricted_import,
1316+
path_options,
13181317
results: vec![],
13191318
};
13201319
visitor.visit_import(module_source_node);
@@ -1326,11 +1325,11 @@ impl Rule for NoRestrictedImports {
13261325
// which exports are being used/whether this should be considered a
13271326
// namespace import, a side-effect import (the two of which may
13281327
// be difficult to distinguish) or a collection of named imports.
1329-
if !restricted_import.has_import_name_patterns() {
1328+
if !path_options.has_import_name_patterns() {
13301329
// All imports disallowed, add diagnostic to the import source
13311330
vec![RestrictedImportMessage {
13321331
location: module_name.text_trimmed_range(),
1333-
message: restricted_import.get_message_for_restriction(
1332+
message: path_options.get_message_for_restriction(
13341333
import_source,
13351334
"",
13361335
ImportRestrictionCause::ImportSource,
@@ -1342,23 +1341,23 @@ impl Rule for NoRestrictedImports {
13421341
// Check (and possibly report) each imported name individually
13431342
let mut visitor = RestrictedImportVisitor {
13441343
import_source,
1345-
restricted_import,
1344+
path_options,
13461345
results: vec![],
13471346
};
13481347
visitor.visit_import_call(import_call);
13491348
visitor.results
13501349
}
13511350
}
13521351
AnyJsImportLike::JsCallExpression(_expression) => {
1353-
let status = restricted_import
1352+
let status = path_options
13541353
.is_import_allowed(RestrictedImportVisitor::DEFAULT_IMPORT_ALIAS);
13551354

13561355
if status.is_forbidden() {
13571356
// require() calls can only import the default import, so
13581357
// there are no individual import names to check or report on.
13591358
vec![RestrictedImportMessage {
13601359
location: module_name.text_trimmed_range(),
1361-
message: restricted_import.get_message_for_restriction(
1360+
message: path_options.get_message_for_restriction(
13621361
import_source,
13631362
"",
13641363
ImportRestrictionCause::ImportSource,

packages/@biomejs/backend-jsonrpc/src/workspace.ts

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@biomejs/biome/configuration_schema.json

+33-35
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)