Skip to content

Commit b53b053

Browse files
authored
refactor(linter): simplify accessing span of NameSpan (#11305)
Make span of NameSpan public like it already is in oxc_syntax::module_record::NameSpan to make code shorter and more explicit. Also found five unnecessary clones while working on this.
1 parent 54dfbd3 commit b53b053

File tree

11 files changed

+18
-22
lines changed

11 files changed

+18
-22
lines changed

crates/oxc_linter/src/module_record.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ impl fmt::Debug for ModuleRecord {
116116
#[derive(Debug, Clone, PartialEq, Eq)]
117117
pub struct NameSpan {
118118
pub name: CompactStr,
119-
span: Span,
119+
pub span: Span,
120120
}
121121

122122
impl NameSpan {
@@ -127,10 +127,6 @@ impl NameSpan {
127127
pub fn name(&self) -> &str {
128128
self.name.as_str()
129129
}
130-
131-
pub fn span(&self) -> Span {
132-
self.span
133-
}
134130
}
135131

136132
impl<'a> From<&oxc_syntax::module_record::NameSpan<'a>> for NameSpan {
@@ -392,7 +388,7 @@ impl ExportExportName {
392388
/// Attempt to get the [`Span`] of this export name.
393389
pub fn span(&self) -> Option<Span> {
394390
match self {
395-
Self::Name(name) => Some(name.span()),
391+
Self::Name(name) => Some(name.span),
396392
Self::Default(span) => Some(*span),
397393
Self::Null => None,
398394
}

crates/oxc_linter/src/rules/eslint/no_duplicate_imports.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ impl Rule for NoDuplicateImports {
137137

138138
for entry in &module_record.import_entries {
139139
let source = &entry.module_request.name;
140-
let span = entry.module_request.span();
140+
let span = entry.module_request.span;
141141

142142
let same_statement = if let Some(curr_span) = current_span {
143143
curr_span == span

crates/oxc_linter/src/rules/eslint/no_restricted_imports.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,14 +1295,14 @@ fn get_diagnostic_from_import_name_result_path(
12951295
},
12961296
ImportNameResult::NameDisallowed(name_span) => match &path.allow_import_names {
12971297
Some(allow_import_names) => diagnostic_allowed_import_name(
1298-
name_span.clone().span(),
1298+
name_span.span,
12991299
path.message.clone(),
13001300
name_span.name(),
13011301
source,
13021302
allow_import_names.join(", ").as_str(),
13031303
),
13041304
_ => diagnostic_import_name(
1305-
name_span.clone().span(),
1305+
name_span.span,
13061306
path.message.clone(),
13071307
name_span.name(),
13081308
source,
@@ -1363,22 +1363,22 @@ fn get_diagnostic_from_import_name_result_pattern(
13631363
}
13641364
ImportNameResult::NameDisallowed(name_span) => match &pattern.allow_import_names {
13651365
Some(allow_import_names) => diagnostic_allowed_import_name(
1366-
name_span.clone().span(),
1366+
name_span.span,
13671367
pattern.message.clone(),
13681368
name_span.name(),
13691369
source,
13701370
allow_import_names.join(", ").as_str(),
13711371
),
13721372
_ => match &pattern.allow_import_name_pattern {
13731373
Some(allow_import_name_pattern) => diagnostic_allowed_import_name_pattern(
1374-
name_span.clone().span(),
1374+
name_span.span,
13751375
pattern.message.clone(),
13761376
name_span.name(),
13771377
source,
13781378
allow_import_name_pattern.as_str(),
13791379
),
13801380
_ => diagnostic_pattern_and_import_name(
1381-
name_span.clone().span(),
1381+
name_span.span,
13821382
pattern.message.clone(),
13831383
name_span.name(),
13841384
source,

crates/oxc_linter/src/rules/import/export.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ impl Rule for Export {
7474
walk_exported_recursive(remote_module_record, &mut export_names, &mut visited);
7575

7676
if export_names.is_empty() {
77-
ctx.diagnostic(no_named_export(module_request.name(), module_request.span()));
77+
ctx.diagnostic(no_named_export(module_request.name(), module_request.span));
7878
} else {
7979
all_export_names.insert(star_export_entry.span, export_names);
8080
}

crates/oxc_linter/src/rules/import/max_dependencies.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ impl Rule for MaxDependencies {
119119
"File has too many dependencies ({}). Maximum allowed is {}.",
120120
module_count, self.max,
121121
);
122-
ctx.diagnostic(max_dependencies_diagnostic(error, entry.module_request.span()));
122+
ctx.diagnostic(max_dependencies_diagnostic(error, entry.module_request.span));
123123
}
124124
}
125125

crates/oxc_linter/src/rules/import/named.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ impl Rule for Named {
105105
if !remote_module_record.has_module_syntax {
106106
continue;
107107
}
108-
let import_span = import_name.span();
108+
let import_span = import_name.span;
109109
let name = import_name.name();
110110
// Check `import { default as foo } from 'bar'`
111111
if name == "default" && remote_module_record.export_default.is_some() {
@@ -152,7 +152,7 @@ impl Rule for Named {
152152
if remote_module_record.exported_bindings.contains_key(name) {
153153
continue;
154154
}
155-
ctx.diagnostic(named_diagnostic(name, specifier, import_name.span()));
155+
ctx.diagnostic(named_diagnostic(name, specifier, import_name.span));
156156
}
157157
}
158158
}

crates/oxc_linter/src/rules/import/no_duplicates.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ impl Rule for NoDuplicates {
118118
let imports = module_record
119119
.import_entries
120120
.iter()
121-
.filter(|entry| entry.module_request.span() == requested_module.span)
121+
.filter(|entry| entry.module_request.span == requested_module.span)
122122
.collect::<Vec<_>>();
123123
if imports.is_empty() {
124124
import_entries_maps.entry(0).or_default().push(requested_module);

crates/oxc_linter/src/rules/import/no_named_default.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ impl Rule for NoNamedDefault {
4848
return;
4949
};
5050
if import_name.name() == "default" && !entry.is_type {
51-
ctx.diagnostic(no_named_default_diagnostic(import_name.span()));
51+
ctx.diagnostic(no_named_default_diagnostic(import_name.span));
5252
}
5353
});
5454
}

crates/oxc_linter/src/rules/import/no_namespace.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ impl Rule for NoNamespace {
108108
let source = entry.module_request.name();
109109

110110
if self.ignore.is_empty() {
111-
ctx.diagnostic(no_namespace_diagnostic(entry.local_name.span()));
111+
ctx.diagnostic(no_namespace_diagnostic(entry.local_name.span));
112112
} else {
113113
if !source.contains('.') {
114114
return;
@@ -120,7 +120,7 @@ impl Rule for NoNamespace {
120120
return;
121121
}
122122

123-
ctx.diagnostic(no_namespace_diagnostic(entry.local_name.span()));
123+
ctx.diagnostic(no_namespace_diagnostic(entry.local_name.span));
124124
}
125125
}
126126
ImportImportName::Name(_) | ImportImportName::Default(_) => {}

crates/oxc_linter/src/rules/jest/no_mocks_import.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ impl Rule for NoMocksImport {
5151
for import_entry in &module_records.import_entries {
5252
let module_specifier = import_entry.module_request.name();
5353
if contains_mocks_dir(module_specifier) {
54-
ctx.diagnostic(no_mocks_import_diagnostic(import_entry.module_request.span()));
54+
ctx.diagnostic(no_mocks_import_diagnostic(import_entry.module_request.span));
5555
}
5656
}
5757

0 commit comments

Comments
 (0)