Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
7c9c1f5
bugfix(linter): add option "allowTypeImports" for rule "no-restricted…
Sysix Dec 14, 2024
acbff7b
fix(linter): rule `no-restricted-imports` support `regex` option insi…
Sysix Dec 23, 2024
a955782
fix(linter): rule no-restricted-imports support regex option inside p…
Sysix Dec 23, 2024
0a19f9a
fix(linter): rule no-restricted-imports support `import_name_pattern`…
Sysix Dec 23, 2024
5296bb4
fix(linter): rule no-restricted-imports support missing options
Sysix Dec 23, 2024
f088c10
fix(linter): rule `no-restricted-imports` support missing options
Sysix Dec 23, 2024
9d1caab
fix(linter): rule `no-restricted-imports`: improve diagnostics
Sysix Dec 25, 2024
531c132
[autofix.ci] apply automated fixes
autofix-ci[bot] Dec 25, 2024
0da9b9d
fix(linter): rule no-restricted-imports support missing options - rem…
Sysix Dec 25, 2024
63f3113
fix(linter): rule no-restricted-imports: improve diagnostics
Sysix Dec 26, 2024
0152d10
fix(linter): rule no-restricted-imports: improve diagnostics
Sysix Dec 26, 2024
b6b185a
fix(linter): rule no-restricted-imports: improve diagnostics
Sysix Dec 26, 2024
e069fcb
fix(linter): rule no-restricted-imports: improve diagnostics
Sysix Dec 26, 2024
6567e39
fix(linter): rule no-restricted-imports: improve diagnostics
Sysix Dec 26, 2024
a029467
fix(linter): rule no-restricted-imports: improve diagnostics
Sysix Dec 26, 2024
33e4842
fix(linter): rule no-restricted-imports: improve diagnostics
Sysix Dec 26, 2024
87d8da7
fix(linter): rule no-restricted-imports support missing options - bui…
Sysix Dec 26, 2024
12911f4
fix(linter): rule no-restricted-imports: improve diagnostics
Sysix Dec 30, 2024
304c2d8
fix(linter): rule no-restricted-imports: improve diagnostics
Sysix Dec 30, 2024
b833ef8
fix(linter): rule no-restricted-imports: improve diagnostics
Sysix Dec 30, 2024
3ba7bd4
[autofix.ci] apply automated fixes
autofix-ci[bot] Dec 30, 2024
ece4df6
fix(linter): rule no-restricted-imports: improve diagnostics
Sysix Dec 30, 2024
2cb4ef0
Merge branch 'main' into fix-linter-no-restricted-imports-regex-option
Sysix Dec 31, 2024
5bd8d88
Merge branch 'fix-linter-no-restricted-imports-regex-option' into fix…
Sysix Dec 31, 2024
4109692
fix(linter): rule no-restricted-imports: improve diagnostics
Sysix Dec 31, 2024
f8caf13
[autofix.ci] apply automated fixes
autofix-ci[bot] Dec 31, 2024
16adf00
fix(linter): rule no-restricted-imports: improve diagnostics
Sysix Dec 31, 2024
b694eef
fix(linter): rule no-restricted-imports: improve diagnostics
Sysix Dec 31, 2024
f485633
fix(linter): rule no-restricted-imports: improve diagnostics
Sysix Dec 31, 2024
6d8c597
fix(linter): rule no-restricted-imports: improve diagnostics
Sysix Dec 31, 2024
071c062
fix(linter): rule no-restricted-imports: improve diagnostics
Sysix Dec 31, 2024
5f8d1b4
fix(linter): rule no-restricted-imports: improve diagnostics
Sysix Jan 3, 2025
373e5f9
Merge branch 'fix-linter-no-restricted-imports-improve-diagnostics' i…
Sysix Jan 3, 2025
61f30b3
fix(linter): add option "allowTypeImports" for rule "no-restricted-im…
Sysix Jan 3, 2025
49f9186
fix(linter): add option "allowTypeImports" for rule "no-restricted-im…
Sysix Jan 3, 2025
bb73b60
Merge branch 'main' into bugfix-linter-add-option-allow-type-imports-…
Sysix Feb 10, 2025
8a02b84
fix(linter): add option "allowTypeImports" for rule "no-restricted-im…
Sysix Feb 10, 2025
a2aea8c
fix(linter): add option "allowTypeImports" for rule "no-restricted-im…
Sysix Feb 22, 2025
375a326
fix(linter): add option "allowTypeImports" for rule "no-restricted-im…
Sysix Feb 22, 2025
e5ef7e1
[autofix.ci] apply automated fixes
autofix-ci[bot] Feb 22, 2025
db3cdea
Merge branch 'bugfix-linter-add-option-allow-type-imports-for-no-rest…
Sysix Feb 22, 2025
305b3ce
fix(linter): add option "allowTypeImports" for rule "no-restricted-im…
Sysix Feb 22, 2025
a21da15
[autofix.ci] apply automated fixes
autofix-ci[bot] Feb 22, 2025
b420291
Merge branch 'main' into bugfix-linter-add-option-allow-type-imports-…
Sysix Feb 22, 2025
be27b34
fix(linter): add option "allowTypeImports" for rule "no-restricted-im…
Sysix Feb 23, 2025
4f9e1d5
Merge branch 'bugfix-linter-add-option-allow-type-imports-for-no-rest…
Sysix Feb 23, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix(linter): add option "allowTypeImports" for rule "no-restricted-im…
…ports"
  • Loading branch information
Sysix committed Feb 23, 2025
commit be27b3487146ee193589f3cbd74b9a7e02d62339
181 changes: 85 additions & 96 deletions crates/oxc_linter/src/rules/eslint/no_restricted_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,95 +352,56 @@ enum NameSpanAllowedResult {
NameDisallowed,
}

fn is_name_span_allowed_in_path(name: &CompactStr, path: &RestrictedPath) -> NameSpanAllowedResult {
// fast check if this name is allowed
if path.allow_import_names.as_ref().is_some_and(|allowed| allowed.contains(name)) {
return NameSpanAllowedResult::Allowed;
}
#[derive(PartialEq, Debug)]
enum ImportNameResult {
Allowed,
GeneralDisallowed,
DefaultDisallowed,
NameDisallowed(NameSpan),
}

if path.import_names.as_ref().is_none() {
// when no importNames and no allowImportNames option is provided, no import in general is allowed
if path.allow_import_names.is_some() {
return NameSpanAllowedResult::NameDisallowed;
impl RestrictedPath {
fn is_name_span_allowed(&self, name: &CompactStr) -> NameSpanAllowedResult {
// fast check if this name is allowed
if self.allow_import_names.as_ref().is_some_and(|allowed| allowed.contains(name)) {
return NameSpanAllowedResult::Allowed;
}

return NameSpanAllowedResult::GeneralDisallowed;
}

// the name is found is the importNames list
if path.import_names.as_ref().is_some_and(|disallowed| disallowed.contains(name)) {
return NameSpanAllowedResult::NameDisallowed;
}

// we allow it
NameSpanAllowedResult::Allowed
}

fn is_name_span_allowed_in_pattern(
name: &CompactStr,
pattern: &RestrictedPattern,
) -> NameSpanAllowedResult {
// fast check if this name is allowed
if pattern.allow_import_names.as_ref().is_some_and(|allowed| allowed.contains(name)) {
return NameSpanAllowedResult::Allowed;
}
if self.import_names.as_ref().is_none() {
// when no importNames and no allowImportNames option is provided, no import in general is allowed
if self.allow_import_names.is_some() {
return NameSpanAllowedResult::NameDisallowed;
}

// fast check if this name is allowed
if pattern.get_allow_import_name_pattern_result(name) {
return NameSpanAllowedResult::Allowed;
}
return NameSpanAllowedResult::GeneralDisallowed;
}

// when no importNames or importNamePattern option is provided, no import in general is allowed
if pattern.import_names.as_ref().is_none() && pattern.import_name_pattern.is_none() {
if pattern.allow_import_names.is_some() || pattern.allow_import_name_pattern.is_some() {
// the name is found is the importNames list
if self.import_names.as_ref().is_some_and(|disallowed| disallowed.contains(name)) {
return NameSpanAllowedResult::NameDisallowed;
}

return NameSpanAllowedResult::GeneralDisallowed;
}

// the name is found is the importNames list
if pattern.import_names.as_ref().is_some_and(|disallowed| disallowed.contains(name)) {
return NameSpanAllowedResult::NameDisallowed;
// we allow it
NameSpanAllowedResult::Allowed
}

// the name is found is the importNamePattern
if pattern.get_import_name_pattern_result(name) {
return NameSpanAllowedResult::NameDisallowed;
}

// we allow it
NameSpanAllowedResult::Allowed
}

#[derive(PartialEq, Debug)]
enum ImportNameResult {
Allowed,
GeneralDisallowed,
DefaultDisallowed,
NameDisallowed(NameSpan),
}

impl RestrictedPath {
fn get_import_name_result(&self, name: &ImportImportName, is_type: bool) -> ImportNameResult {
if is_type && self.allow_type_imports.is_some_and(|x| x) {
return ImportNameResult::Allowed;
}

match &name {
ImportImportName::Name(import) => {
match is_name_span_allowed_in_path(&import.name, self) {
NameSpanAllowedResult::NameDisallowed => {
ImportNameResult::NameDisallowed(import.clone())
}
NameSpanAllowedResult::GeneralDisallowed => ImportNameResult::GeneralDisallowed,
NameSpanAllowedResult::Allowed => ImportNameResult::Allowed,
ImportImportName::Name(import) => match self.is_name_span_allowed(&import.name) {
NameSpanAllowedResult::NameDisallowed => {
ImportNameResult::NameDisallowed(import.clone())
}
}
NameSpanAllowedResult::GeneralDisallowed => ImportNameResult::GeneralDisallowed,
NameSpanAllowedResult::Allowed => ImportNameResult::Allowed,
},
ImportImportName::Default(span) => {
let name = CompactStr::new("default");

match is_name_span_allowed_in_path(&name, self) {
match self.is_name_span_allowed(&name) {
NameSpanAllowedResult::NameDisallowed => {
ImportNameResult::NameDisallowed(NameSpan::new(name, *span))
}
Expand All @@ -458,15 +419,13 @@ impl RestrictedPath {
}

match &name {
ExportImportName::Name(import) => {
match is_name_span_allowed_in_path(&import.name, self) {
NameSpanAllowedResult::NameDisallowed => {
ImportNameResult::NameDisallowed(import.clone())
}
NameSpanAllowedResult::GeneralDisallowed => ImportNameResult::GeneralDisallowed,
NameSpanAllowedResult::Allowed => ImportNameResult::Allowed,
ExportImportName::Name(import) => match self.is_name_span_allowed(&import.name) {
NameSpanAllowedResult::NameDisallowed => {
ImportNameResult::NameDisallowed(import.clone())
}
}
NameSpanAllowedResult::GeneralDisallowed => ImportNameResult::GeneralDisallowed,
NameSpanAllowedResult::Allowed => ImportNameResult::Allowed,
},
ExportImportName::All | ExportImportName::AllButDefault => {
ImportNameResult::DefaultDisallowed
}
Expand All @@ -486,7 +445,7 @@ impl RestrictedPath {
let name = literal.value.into_compact_str();
let unused_name = &CompactStr::from("__<>import_name_that_cant_be_used<>__");

match is_name_span_allowed_in_path(unused_name, self) {
match self.is_name_span_allowed(unused_name) {
NameSpanAllowedResult::NameDisallowed => {
ImportNameResult::NameDisallowed(NameSpan::new(name, literal.span))
}
Expand All @@ -497,24 +456,56 @@ impl RestrictedPath {
}

impl RestrictedPattern {
fn is_name_span_allowed(&self, name: &CompactStr) -> NameSpanAllowedResult {
// fast check if this name is allowed
if self.allow_import_names.as_ref().is_some_and(|allowed| allowed.contains(name)) {
return NameSpanAllowedResult::Allowed;
}

// fast check if this name is allowed
if self.get_allow_import_name_pattern_result(name) {
return NameSpanAllowedResult::Allowed;
}

// when no importNames or importNamePattern option is provided, no import in general is allowed
if self.import_names.as_ref().is_none() && self.import_name_pattern.is_none() {
if self.allow_import_names.is_some() || self.allow_import_name_pattern.is_some() {
return NameSpanAllowedResult::NameDisallowed;
}

return NameSpanAllowedResult::GeneralDisallowed;
}

// the name is found is the importNames list
if self.import_names.as_ref().is_some_and(|disallowed| disallowed.contains(name)) {
return NameSpanAllowedResult::NameDisallowed;
}

// the name is found is the importNamePattern
if self.get_import_name_pattern_result(name) {
return NameSpanAllowedResult::NameDisallowed;
}

// we allow it
NameSpanAllowedResult::Allowed
}

fn get_import_name_result(&self, name: &ImportImportName, is_type: bool) -> ImportNameResult {
if is_type && self.allow_type_imports.is_some_and(|x| x) {
return ImportNameResult::Allowed;
}

match &name {
ImportImportName::Name(import) => {
match is_name_span_allowed_in_pattern(&import.name, self) {
NameSpanAllowedResult::NameDisallowed => {
ImportNameResult::NameDisallowed(import.clone())
}
NameSpanAllowedResult::GeneralDisallowed => ImportNameResult::GeneralDisallowed,
NameSpanAllowedResult::Allowed => ImportNameResult::Allowed,
ImportImportName::Name(import) => match self.is_name_span_allowed(&import.name) {
NameSpanAllowedResult::NameDisallowed => {
ImportNameResult::NameDisallowed(import.clone())
}
}
NameSpanAllowedResult::GeneralDisallowed => ImportNameResult::GeneralDisallowed,
NameSpanAllowedResult::Allowed => ImportNameResult::Allowed,
},
ImportImportName::Default(span) => {
let name: CompactStr = CompactStr::new("default");
match is_name_span_allowed_in_pattern(&name, self) {
match self.is_name_span_allowed(&name) {
NameSpanAllowedResult::NameDisallowed => {
ImportNameResult::NameDisallowed(NameSpan::new(name, *span))
}
Expand All @@ -532,15 +523,13 @@ impl RestrictedPattern {
}

match &name {
ExportImportName::Name(import) => {
match is_name_span_allowed_in_pattern(&import.name, self) {
NameSpanAllowedResult::NameDisallowed => {
ImportNameResult::NameDisallowed(import.clone())
}
NameSpanAllowedResult::GeneralDisallowed => ImportNameResult::GeneralDisallowed,
NameSpanAllowedResult::Allowed => ImportNameResult::Allowed,
ExportImportName::Name(import) => match self.is_name_span_allowed(&import.name) {
NameSpanAllowedResult::NameDisallowed => {
ImportNameResult::NameDisallowed(import.clone())
}
}
NameSpanAllowedResult::GeneralDisallowed => ImportNameResult::GeneralDisallowed,
NameSpanAllowedResult::Allowed => ImportNameResult::Allowed,
},
ExportImportName::All | ExportImportName::AllButDefault => {
ImportNameResult::DefaultDisallowed
}
Expand All @@ -560,7 +549,7 @@ impl RestrictedPattern {
let name = literal.value.into_compact_str();
let unused_name = &CompactStr::from("__<>import_name_that_cant_be_used<>__");

match is_name_span_allowed_in_pattern(unused_name, self) {
match self.is_name_span_allowed(unused_name) {
NameSpanAllowedResult::NameDisallowed => {
ImportNameResult::NameDisallowed(NameSpan::new(name, literal.span))
}
Expand Down