Skip to content

Commit

Permalink
perf(linter): improve no_restricted_imports rule performances
Browse files Browse the repository at this point in the history
  • Loading branch information
Spoutnik97 committed Dec 4, 2024
1 parent d3f2c74 commit 00be669
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 57 deletions.
70 changes: 34 additions & 36 deletions crates/oxc_linter/src/rules/eslint/no_restricted_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,11 @@ use crate::{context::LintContext, module_record::ImportImportName, rule::Rule};
fn no_restricted_imports_diagnostic(
ctx: &LintContext,
span: Span,
message: &Option<String>,
message: Option<String>,
source: &str,
) {
let msg = message.as_ref().map_or_else(
|| format!("'{source}' import is restricted from being used."),
std::clone::Clone::clone,
);

let msg =
message.unwrap_or_else(|| format!("'{source}' import is restricted from being used."));
ctx.diagnostic(
OxcDiagnostic::warn(msg).with_help("Remove the import statement.").with_label(span),
);
Expand Down Expand Up @@ -91,11 +88,11 @@ impl Rule for NoRestrictedImports {
let module_record = ctx.module_record();
let mut side_effect_import_map: FxHashMap<&CompactStr, Vec<Span>> = FxHashMap::default();

for entry in &module_record.import_entries {
let source = entry.module_request.name();
let span = entry.module_request.span();
for path in &self.paths {
for entry in &module_record.import_entries {
let source = entry.module_request.name();
let span = entry.module_request.span();

for path in &self.paths {
if source == path.name.as_str() {
if let Some(import_names) = &path.import_names {
match &entry.import_name {
Expand All @@ -104,65 +101,66 @@ impl Rule for NoRestrictedImports {
no_restricted_imports_diagnostic(
ctx,
span,
&path.message,
path.message.clone(),
source,
);
return;
}
}
ImportImportName::Default(_) | ImportImportName::NamespaceObject => {
if !import_names.contains(&entry.local_name.name().to_string()) {
no_restricted_imports_diagnostic(
ctx,
span,
&path.message,
path.message.clone(),
source,
);
return;
}
}
}
} else {
no_restricted_imports_diagnostic(ctx, span, &path.message, source);
no_restricted_imports_diagnostic(ctx, span, path.message.clone(), source);
}
}
}
}

for (source, requests) in &module_record.requested_modules {
for request in requests {
if request.is_import && module_record.import_entries.is_empty() {
side_effect_import_map.entry(source).or_default().push(request.span);
for (source, requests) in &module_record.requested_modules {
for request in requests {
if request.is_import && module_record.import_entries.is_empty() {
side_effect_import_map.entry(source).or_default().push(request.span);
}
}
}
}

for (source, spans) in &side_effect_import_map {
for path in &self.paths {
for (source, spans) in &side_effect_import_map {
if source.as_str() == path.name.as_str() {
for span in spans {
no_restricted_imports_diagnostic(ctx, *span, &path.message, source);
no_restricted_imports_diagnostic(ctx, *span, path.message.clone(), source);
return;
}
}
}
}

for entry in &module_record.local_export_entries {
if let Some(module_request) = &entry.module_request {
let source = module_request.name();
let span = entry.span;
for path in &self.paths {
for entry in &module_record.local_export_entries {
if let Some(module_request) = &entry.module_request {
let source = module_request.name();
let span = entry.span;

if source == path.name.as_str() {
no_restricted_imports_diagnostic(ctx, span, &path.message, source);
no_restricted_imports_diagnostic(ctx, span, path.message.clone(), source);
return;
}
}
}
}
for entry in &module_record.indirect_export_entries {
if let Some(module_request) = &entry.module_request {
let source = module_request.name();
let span = entry.span;
for path in &self.paths {
for entry in &module_record.indirect_export_entries {
if let Some(module_request) = &entry.module_request {
let source = module_request.name();
let span = entry.span;

if source == path.name.as_str() {
no_restricted_imports_diagnostic(ctx, span, &path.message, source);
no_restricted_imports_diagnostic(ctx, span, path.message.clone(), source);
return;
}
}
}
Expand Down
21 changes: 0 additions & 21 deletions crates/oxc_linter/src/snapshots/eslint_no_restricted_imports.snap
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,6 @@ snapshot_kind: text
╰────
help: Remove the import statement.

eslint(no-restricted-imports): Import baz from qux instead.
╭─[no_restricted_imports.tsx:1:31]
1import { foo, bar, baz } from "mod"
· ─────
╰────
help: Remove the import statement.

eslint(no-restricted-imports): Import foo from qux instead.
╭─[no_restricted_imports.tsx:1:31]
1import { foo, bar, baz } from "mod"
· ─────
╰────
help: Remove the import statement.

eslint(no-restricted-imports): Import baz from qux instead.
╭─[no_restricted_imports.tsx:1:31]
1import { foo, bar, baz } from "mod"
· ─────
╰────
help: Remove the import statement.

eslint(no-restricted-imports): Import foo from qux instead.
╭─[no_restricted_imports.tsx:1:31]
1import { foo, bar, baz } from "mod"
Expand Down

0 comments on commit 00be669

Please sign in to comment.