Skip to content

Commit 79014ff

Browse files
committed
refactor(syntax): clean up ModuleRecord (#7568)
1 parent bf16668 commit 79014ff

6 files changed

Lines changed: 41 additions & 48 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ impl Rule for NoDuplicateImports {
129129

130130
for (source, requests) in &module_record.requested_modules {
131131
for request in requests {
132-
if request.is_import() && module_record.import_entries.is_empty() {
133-
side_effect_import_map.entry(source).or_default().push(request.span());
132+
if request.is_import && module_record.import_entries.is_empty() {
133+
side_effect_import_map.entry(source).or_default().push(request.span);
134134
}
135135
}
136136
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ impl Rule for NoCycle {
170170
});
171171

172172
if visitor_result.result {
173-
let span = module_record.requested_modules[&stack[0].0][0].span();
173+
let span = module_record.requested_modules[&stack[0].0][0].span;
174174
let help = stack
175175
.iter()
176176
.map(|(specifier, path)| {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ impl Rule for NoDuplicates {
107107
let requested_modules = group
108108
.into_iter()
109109
.flat_map(|(_path, requested_modules)| requested_modules)
110-
.filter(|requested_module| requested_module.is_import());
110+
.filter(|requested_module| requested_module.is_import);
111111
// When prefer_inline is false, 0 is value, 1 is type named, 2 is type namespace and 3 is type default
112112
// When prefer_inline is true, 0 is value and type named, 2 is type // namespace and 3 is type default
113113
let mut import_entries_maps: FxHashMap<u8, Vec<&RequestedModule>> =
@@ -116,7 +116,7 @@ impl Rule for NoDuplicates {
116116
let imports = module_record
117117
.import_entries
118118
.iter()
119-
.filter(|entry| entry.module_request.span() == requested_module.span())
119+
.filter(|entry| entry.module_request.span() == requested_module.span)
120120
.collect::<Vec<_>>();
121121
if imports.is_empty() {
122122
import_entries_maps.entry(0).or_default().push(requested_module);
@@ -154,7 +154,7 @@ impl Rule for NoDuplicates {
154154
fn check_duplicates(ctx: &LintContext, requested_modules: Option<&Vec<&RequestedModule>>) {
155155
if let Some(requested_modules) = requested_modules {
156156
if requested_modules.len() > 1 {
157-
let mut labels = requested_modules.iter().map(|m| m.span());
157+
let mut labels = requested_modules.iter().map(|m| m.span);
158158
let first = labels.next().unwrap(); // we know there is at least one
159159
let module_name = ctx.source_range(first).trim_matches('\'').trim_matches('"');
160160
ctx.diagnostic(no_duplicates_diagnostic(module_name, first, labels));

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl Rule for NoSelfImport {
5050
};
5151
if remote_module_record_ref.value().resolved_absolute_path == *resolved_absolute_path {
5252
for requested_module in requested_modules {
53-
ctx.diagnostic(no_self_import_diagnostic(requested_module.span()));
53+
ctx.diagnostic(no_self_import_diagnostic(requested_module.span));
5454
}
5555
}
5656
}

crates/oxc_linter/src/rules/vitest/no_import_node_test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ impl Rule for NoImportNodeTest {
5656

5757
if let Some(node_test_module) = module_record.requested_modules.get("node:test") {
5858
if let Some(requested_module) = node_test_module.first() {
59-
ctx.diagnostic_with_fix(no_import_node_test(requested_module.span()), |fixer| {
60-
fixer.replace(requested_module.span(), "\"vitest\"")
59+
ctx.diagnostic_with_fix(no_import_node_test(requested_module.span), |fixer| {
60+
fixer.replace(requested_module.span, "\"vitest\"")
6161
});
6262
}
6363
}

crates/oxc_syntax/src/module_record.rs

Lines changed: 32 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
//! [ECMAScript Module Record](https://tc39.es/ecma262/#sec-abstract-module-records)
2-
#![allow(missing_docs)] // fixme
3-
4-
use std::fmt;
52
63
use oxc_allocator::{Allocator, Vec};
74
use oxc_span::{Atom, Span};
@@ -15,6 +12,7 @@ use rustc_hash::FxHashMap;
1512
/// See
1613
/// * <https://tc39.es/ecma262/#table-additional-fields-of-source-text-module-records>
1714
/// * <https://tc39.es/ecma262/#cyclic-module-record>
15+
#[derive(Debug)]
1816
pub struct ModuleRecord<'a> {
1917
/// This module has no import / export statements
2018
pub not_esm: bool,
@@ -74,6 +72,7 @@ pub struct ModuleRecord<'a> {
7472
}
7573

7674
impl<'a> ModuleRecord<'a> {
75+
/// Constructor
7776
pub fn new(allocator: &'a Allocator) -> Self {
7877
Self {
7978
not_esm: true,
@@ -91,30 +90,18 @@ impl<'a> ModuleRecord<'a> {
9190
}
9291
}
9392

94-
impl fmt::Debug for ModuleRecord<'_> {
95-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::fmt::Result {
96-
f.debug_struct("ModuleRecord")
97-
.field("not_esm", &self.not_esm)
98-
.field("import_entries", &self.import_entries)
99-
.field("local_export_entries", &self.local_export_entries)
100-
.field("indirect_export_entries", &self.indirect_export_entries)
101-
.field("star_export_entries", &self.star_export_entries)
102-
.field("exported_bindings", &self.exported_bindings)
103-
.field("exported_bindings_duplicated", &self.exported_bindings_duplicated)
104-
.field("exported_bindings_from_star_export", &self.exported_bindings_from_star_export)
105-
.field("export_default", &self.export_default)
106-
.field("export_default_duplicated", &self.export_default_duplicated)
107-
.finish()
108-
}
109-
}
110-
93+
/// Name and Span
11194
#[derive(Debug, Clone, PartialEq, Eq)]
11295
pub struct NameSpan<'a> {
96+
/// Name
11397
pub name: Atom<'a>,
98+
99+
/// Span
114100
pub span: Span,
115101
}
116102

117103
impl<'a> NameSpan<'a> {
104+
/// Constructor
118105
pub fn new(name: Atom<'a>, span: Span) -> Self {
119106
Self { name, span }
120107
}
@@ -193,16 +180,21 @@ pub struct ImportEntry<'a> {
193180
/// `ImportName` For `ImportEntry`
194181
#[derive(Debug, Clone, PartialEq, Eq)]
195182
pub enum ImportImportName<'a> {
183+
/// Name
196184
Name(NameSpan<'a>),
185+
/// Namespace Object
197186
NamespaceObject,
187+
/// Default
198188
Default(Span),
199189
}
200190

201191
impl ImportImportName<'_> {
192+
/// Is `default`
202193
pub fn is_default(&self) -> bool {
203194
matches!(self, Self::Default(_))
204195
}
205196

197+
/// Is namespace
206198
pub fn is_namespace_object(&self) -> bool {
207199
matches!(self, Self::NamespaceObject)
208200
}
@@ -253,6 +245,7 @@ pub struct ExportEntry<'a> {
253245
/// `ImportName` for `ExportEntry`
254246
#[derive(Debug, Default, Clone, PartialEq, Eq)]
255247
pub enum ExportImportName<'a> {
248+
/// Name
256249
Name(NameSpan<'a>),
257250
/// all is used for export * as ns from "mod" declarations.
258251
All,
@@ -263,11 +256,14 @@ pub enum ExportImportName<'a> {
263256
Null,
264257
}
265258

259+
/// Export Import Name
266260
impl ExportImportName<'_> {
261+
/// Is all
267262
pub fn is_all(&self) -> bool {
268263
matches!(self, Self::All)
269264
}
270265

266+
/// Is all but default
271267
pub fn is_all_but_default(&self) -> bool {
272268
matches!(self, Self::AllButDefault)
273269
}
@@ -276,8 +272,11 @@ impl ExportImportName<'_> {
276272
/// `ExportName` for `ExportEntry`
277273
#[derive(Debug, Default, Clone, PartialEq, Eq)]
278274
pub enum ExportExportName<'a> {
275+
/// Name
279276
Name(NameSpan<'a>),
277+
/// Default
280278
Default(Span),
279+
/// Null
281280
#[default]
282281
Null,
283282
}
@@ -306,9 +305,11 @@ impl ExportExportName<'_> {
306305
/// `LocalName` for `ExportEntry`
307306
#[derive(Debug, Default, Clone, PartialEq, Eq)]
308307
pub enum ExportLocalName<'a> {
308+
/// Name
309309
Name(NameSpan<'a>),
310310
/// `export default name_span`
311311
Default(NameSpan<'a>),
312+
/// Null
312313
#[default]
313314
Null,
314315
}
@@ -333,22 +334,11 @@ impl<'a> ExportLocalName<'a> {
333334
}
334335
}
335336

337+
/// RequestedModule
336338
#[derive(Debug, Clone, Copy)]
337339
pub struct RequestedModule {
338-
span: Span,
339-
is_type: bool,
340-
/// is_import is true if the module is requested by an import statement.
341-
is_import: bool,
342-
}
343-
344-
impl RequestedModule {
345-
pub fn new(span: Span, is_type: bool, is_import: bool) -> Self {
346-
Self { span, is_type, is_import }
347-
}
348-
349-
pub fn span(&self) -> Span {
350-
self.span
351-
}
340+
/// Span
341+
pub span: Span,
352342

353343
/// `true` if a `type` modifier was used in the import statement.
354344
///
@@ -358,13 +348,16 @@ impl RequestedModule {
358348
/// import { type bar } from "bar"; // false, `type` is on specifier
359349
/// import { baz } from "baz"; // false, no `type` modifier
360350
/// ```
361-
pub fn is_type(&self) -> bool {
362-
self.is_type
363-
}
351+
pub is_type: bool,
364352

365353
/// `true` if the module is requested by an import statement.
366-
pub fn is_import(&self) -> bool {
367-
self.is_import
354+
pub is_import: bool,
355+
}
356+
357+
impl RequestedModule {
358+
/// Constructor
359+
pub fn new(span: Span, is_type: bool, is_import: bool) -> Self {
360+
Self { span, is_type, is_import }
368361
}
369362
}
370363

0 commit comments

Comments
 (0)