Skip to content

Commit

Permalink
fix(linter): rule no-restricted-imports: improve diagnostics
Browse files Browse the repository at this point in the history
  • Loading branch information
Sysix committed Dec 31, 2024
1 parent f8caf13 commit 16adf00
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 25 deletions.
47 changes: 24 additions & 23 deletions crates/oxc_linter/src/rules/eslint/no_restricted_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -894,25 +894,30 @@ fn get_diagnostic_from_is_skip_able_result_pattern(
diagnostic
}
IsSkipAbleResult::NameDisallowed(name_span) => {
let diagnostic =
if let Some(allow_import_name_pattern) = &pattern.allow_import_name_pattern {
diagnostic_allowed_import_name_pattern(
name_span.clone().span(),
pattern.message.clone(),
name_span.name(),
source,
allow_import_name_pattern.as_str(),
)
} else {
diagnostic_pattern_and_import_name(
name_span.clone().span(),
pattern.message.clone(),
name_span.name(),
source,
)
};

diagnostic
if let Some(allow_import_names) = &pattern.allow_import_names {
diagnostic_allowed_import_name(
name_span.clone().span(),
pattern.message.clone(),
name_span.name(),
source,
allow_import_names.join(", ").as_str(),
)
} else if let Some(allow_import_name_pattern) = &pattern.allow_import_name_pattern {
diagnostic_allowed_import_name_pattern(
name_span.clone().span(),
pattern.message.clone(),
name_span.name(),
source,
allow_import_name_pattern.as_str(),
)
} else {
diagnostic_pattern_and_import_name(
name_span.clone().span(),
pattern.message.clone(),
name_span.name(),
source,
)
}
}
IsSkipAbleResult::Allowed => unreachable!("should be filtered out by parent function"),
}
Expand Down Expand Up @@ -2160,8 +2165,6 @@ fn test() {
}]
}])),
),
// expected: 'DisallowedObject' import from 'foo' is restricted because only 'AllowedObject' import(s) is/are allowed.
// got: 'DisallowedObject' import from 'foo' is restricted from being used by a pattern.
(
r#"import { AllowedObject, DisallowedObject } from "foo";"#,
Some(serde_json::json!([{
Expand All @@ -2171,8 +2174,6 @@ fn test() {
}]
}])),
),
// expected: 'DisallowedObject' import from 'foo' is restricted because only 'AllowedObject' import(s) is/are allowed.
// got: 'DisallowedObject' import from 'foo' is restricted from being used by a pattern.
(
r#"import { AllowedObject, DisallowedObject } from "foo";"#,
Some(serde_json::json!([{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -716,13 +716,13 @@ snapshot_kind: text
╰────
help: Only "AllowedObject" is allowed to be imported from "foo".

eslint(no-restricted-imports): 'DisallowedObject' import from 'foo' is restricted from being used by a pattern.
eslint(no-restricted-imports): 'DisallowedObject' import from 'foo' is restricted because only AllowedObject import(s) is/are allowed.
╭─[no_restricted_imports.tsx:1:25]
1import { AllowedObject, DisallowedObject } from "foo";
· ────────────────
╰────

eslint(no-restricted-imports): 'DisallowedObject' import from 'foo' is restricted from being used by a pattern.
eslint(no-restricted-imports): 'DisallowedObject' import from 'foo' is restricted because only AllowedObject import(s) is/are allowed.
╭─[no_restricted_imports.tsx:1:25]
1import { AllowedObject, DisallowedObject } from "foo";
· ────────────────
Expand Down

0 comments on commit 16adf00

Please sign in to comment.