Skip to content

Commit d059a20

Browse files
committed
perf: look module_record instead of all node
1 parent 8bcaf33 commit d059a20

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

crates/oxc_linter/src/rules/react/only_export_components.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -263,9 +263,11 @@ impl Rule for OnlyExportComponents {
263263
}
264264

265265
fn run_once(&self, ctx: &LintContext<'_>) {
266-
let react_is_in_scope = ctx.semantic().nodes().iter().any(|node| {
267-
matches!(node.kind(), AstKind::ImportDeclaration(import_decl) if import_decl.source.value == "react")
268-
});
266+
let react_is_in_scope = ctx
267+
.module_record()
268+
.import_entries
269+
.iter()
270+
.any(|entry| entry.module_request.name == "react");
269271
if self.check_js && !react_is_in_scope {
270272
return;
271273
}
@@ -349,27 +351,32 @@ impl OnlyExportComponents {
349351

350352
fn analyze_exports(&self, ctx: &LintContext, react_hocs: &[&str]) -> ExportAnalysis {
351353
let mut analysis = ExportAnalysis::default();
354+
let module_record = ctx.module_record();
355+
356+
let has_any_exports = !module_record.local_export_entries.is_empty()
357+
|| !module_record.star_export_entries.is_empty()
358+
|| !module_record.indirect_export_entries.is_empty();
359+
if !has_any_exports {
360+
return analysis;
361+
}
362+
363+
analysis.has_exports = true;
352364

353365
for node in ctx.semantic().nodes() {
354366
match node.kind() {
355367
AstKind::ExportAllDeclaration(export_all) if export_all.export_kind.is_value() => {
356-
analysis.has_exports = true;
357368
ctx.diagnostic(export_all_components_diagnostic(export_all.span));
358369
}
359370
AstKind::ExportDefaultDeclaration(export_default) => {
360-
analysis.has_exports = true;
361371
let result = self.analyze_export_default(export_default, react_hocs);
362-
363372
if let Some(span) = result.anonymous_span {
364373
ctx.diagnostic(anonymous_components_diagnostic(span));
365374
}
366-
367375
analysis.merge(result);
368376
}
369377
AstKind::ExportNamedDeclaration(export_named)
370378
if export_named.export_kind.is_value() =>
371379
{
372-
analysis.has_exports = true;
373380
let result = self.analyze_export_named(ctx, export_named, react_hocs);
374381
analysis.merge(result);
375382
}

0 commit comments

Comments
 (0)