Skip to content

Commit 0c7e4fc

Browse files
committed
comment: make span public
1 parent e8f7dbe commit 0c7e4fc

File tree

22 files changed

+81
-97
lines changed

22 files changed

+81
-97
lines changed

crates/oxc_ast/src/trivia.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ use oxc_span::Span;
1212
#[derive(Debug, Clone, Copy)]
1313
pub struct Comment {
1414
pub kind: CommentKind,
15-
/// The span of the comment text (leading/trailing delimiters not included).
16-
span: Span,
15+
/// The span of the comment text (without leading/trailing delimiters).
16+
pub span: Span,
1717
}
1818

1919
impl Comment {
@@ -22,12 +22,6 @@ impl Comment {
2222
let span = Span::new(start, end);
2323
Self { kind, span }
2424
}
25-
26-
/// Return the span of the comment text (without delimiters).
27-
#[inline]
28-
pub fn span(&self) -> &Span {
29-
&self.span
30-
}
3125
}
3226

3327
#[derive(Debug, Clone, Copy, Eq, PartialEq)]

crates/oxc_codegen/src/annotation_comment.rs

+5-10
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,14 @@ pub fn get_leading_annotate_comment<const MINIFY: bool>(
1616
let maybe_leading_comment = codegen.try_get_leading_comment(node_start);
1717
let comment = maybe_leading_comment?;
1818
let real_end = match comment.kind {
19-
CommentKind::SingleLine => comment.span().end,
20-
CommentKind::MultiLine => comment.span().end + 2,
19+
CommentKind::SingleLine => comment.span.end,
20+
CommentKind::MultiLine => comment.span.end + 2,
2121
};
2222
let source_code = codegen.source_text;
2323
let content_between = &source_code[real_end as usize..node_start as usize];
2424
// Used for VariableDeclaration (Rollup only respects "const" and only for the first one)
2525
if content_between.chars().all(|ch| ch.is_ascii_whitespace()) {
26-
let comment_content =
27-
&source_code[comment.span().start as usize..comment.span().end as usize];
26+
let comment_content = &source_code[comment.span.start as usize..comment.span.end as usize];
2827
if MATCHER.find_iter(&comment_content).next().is_some() {
2928
return Some(*comment);
3029
}
@@ -38,17 +37,13 @@ pub fn print_comment<const MINIFY: bool>(comment: Comment, p: &mut Codegen<{ MIN
3837
match comment.kind {
3938
CommentKind::SingleLine => {
4039
p.print_str("//");
41-
p.print_range_of_source_code(
42-
comment.span().start as usize..comment.span().end as usize,
43-
);
40+
p.print_range_of_source_code(comment.span.start as usize..comment.span.end as usize);
4441
p.print_soft_newline();
4542
p.print_indent();
4643
}
4744
CommentKind::MultiLine => {
4845
p.print_str("/*");
49-
p.print_range_of_source_code(
50-
comment.span().start as usize..comment.span().end as usize,
51-
);
46+
p.print_range_of_source_code(comment.span.start as usize..comment.span.end as usize);
5247
p.print_str("*/");
5348
p.print_soft_space();
5449
}

crates/oxc_linter/src/disable_directives.rs

+18-19
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ impl<'a> DisableDirectivesBuilder<'a> {
9494
// for matching disable and enable pairs.
9595
// Wrongly ordered matching pairs are not taken into consideration.
9696
for comment in self.trivias.clone().comments() {
97-
let text = comment.span().source_text(self.source_text);
97+
let text = comment.span.source_text(self.source_text);
9898
let text = text.trim_start();
9999

100100
if let Some(text) =
@@ -103,52 +103,52 @@ impl<'a> DisableDirectivesBuilder<'a> {
103103
// `eslint-disable`
104104
if text.trim().is_empty() {
105105
if self.disable_all_start.is_none() {
106-
self.disable_all_start = Some(comment.span().end);
106+
self.disable_all_start = Some(comment.span.end);
107107
}
108-
self.disable_all_comments.push(*comment.span());
108+
self.disable_all_comments.push(comment.span);
109109
continue;
110110
}
111111

112112
// `eslint-disable-next-line`
113113
if let Some(text) = text.strip_prefix("-next-line") {
114114
// Get the span up to the next new line
115-
let stop = self.source_text[comment.span().end as usize..]
115+
let stop = self.source_text[comment.span.end as usize..]
116116
.lines()
117117
.take(2)
118-
.fold(comment.span().end, |acc, line| acc + line.len() as u32);
118+
.fold(comment.span.end, |acc, line| acc + line.len() as u32);
119119
if text.trim().is_empty() {
120-
self.add_interval(comment.span().end, stop, DisabledRule::All);
121-
self.disable_all_comments.push(*comment.span());
120+
self.add_interval(comment.span.end, stop, DisabledRule::All);
121+
self.disable_all_comments.push(comment.span);
122122
} else {
123123
// `eslint-disable-next-line rule_name1, rule_name2`
124124
let mut rules = vec![];
125125
Self::get_rule_names(text, |rule_name| {
126126
self.add_interval(
127-
comment.span().end,
127+
comment.span.end,
128128
stop,
129129
DisabledRule::Single(rule_name),
130130
);
131131
rules.push(rule_name);
132132
});
133133
self.disable_rule_comments
134-
.push(DisableRuleComment { span: *comment.span(), rules });
134+
.push(DisableRuleComment { span: comment.span, rules });
135135
}
136136
continue;
137137
}
138138

139139
// `eslint-disable-line`
140140
if let Some(text) = text.strip_prefix("-line") {
141141
// Get the span between the preceding newline to this comment
142-
let start = self.source_text[..=comment.span().start as usize]
142+
let start = self.source_text[..=comment.span.start as usize]
143143
.lines()
144144
.next_back()
145-
.map_or(0, |line| comment.span().start - (line.len() as u32 - 1));
146-
let stop = comment.span().start;
145+
.map_or(0, |line| comment.span.start - (line.len() as u32 - 1));
146+
let stop = comment.span.start;
147147

148148
// `eslint-disable-line`
149149
if text.trim().is_empty() {
150150
self.add_interval(start, stop, DisabledRule::All);
151-
self.disable_all_comments.push(*comment.span());
151+
self.disable_all_comments.push(comment.span);
152152
} else {
153153
// `eslint-disable-line rule-name1, rule-name2`
154154
let mut rules = vec![];
@@ -157,19 +157,18 @@ impl<'a> DisableDirectivesBuilder<'a> {
157157
rules.push(rule_name);
158158
});
159159
self.disable_rule_comments
160-
.push(DisableRuleComment { span: *comment.span(), rules });
160+
.push(DisableRuleComment { span: comment.span, rules });
161161
}
162162
continue;
163163
}
164164

165165
// `eslint-disable rule-name1, rule-name2`
166166
let mut rules = vec![];
167167
Self::get_rule_names(text, |rule_name| {
168-
self.disable_start_map.entry(rule_name).or_insert(comment.span().end);
168+
self.disable_start_map.entry(rule_name).or_insert(comment.span.end);
169169
rules.push(rule_name);
170170
});
171-
self.disable_rule_comments
172-
.push(DisableRuleComment { span: *comment.span(), rules });
171+
self.disable_rule_comments.push(DisableRuleComment { span: comment.span, rules });
173172

174173
continue;
175174
}
@@ -180,15 +179,15 @@ impl<'a> DisableDirectivesBuilder<'a> {
180179
// `eslint-enable`
181180
if text.trim().is_empty() {
182181
if let Some(start) = self.disable_all_start.take() {
183-
self.add_interval(start, comment.span().start, DisabledRule::All);
182+
self.add_interval(start, comment.span.start, DisabledRule::All);
184183
}
185184
} else {
186185
// `eslint-enable rule-name1, rule-name2`
187186
Self::get_rule_names(text, |rule_name| {
188187
if let Some(start) = self.disable_start_map.remove(rule_name) {
189188
self.add_interval(
190189
start,
191-
comment.span().start,
190+
comment.span.start,
192191
DisabledRule::Single(rule_name),
193192
);
194193
}

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ impl Rule for DefaultCase {
7373
.comments_range(last_case.span.start..switch.span.end)
7474
.last()
7575
.is_some_and(|comment| {
76-
let raw =
77-
comment.span().source_text(ctx.semantic().source_text()).trim();
76+
let raw = comment.span.source_text(ctx.semantic().source_text()).trim();
7877
match &self.comment_pattern {
7978
Some(comment_pattern) => comment_pattern.is_match(raw),
8079
None => raw.eq_ignore_ascii_case("no default"),

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

+6-8
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ impl Rule for MaxLines {
8484
let mut comment_lines: usize = 0;
8585
for comment in ctx.semantic().trivias().comments() {
8686
if comment.kind.is_single_line() {
87-
let comment_line = ctx.source_text()[..comment.span().start as usize]
87+
let comment_line = ctx.source_text()[..comment.span.start as usize]
8888
.lines()
8989
.next_back()
9090
.unwrap_or("");
@@ -93,20 +93,18 @@ impl Rule for MaxLines {
9393
}
9494
} else {
9595
let mut start_line =
96-
ctx.source_text()[..comment.span().start as usize].lines().count();
97-
let comment_start_line = ctx.source_text()[..comment.span().start as usize]
96+
ctx.source_text()[..comment.span.start as usize].lines().count();
97+
let comment_start_line = ctx.source_text()[..comment.span.start as usize]
9898
.lines()
9999
.next_back()
100100
.unwrap_or("");
101101
if !line_has_just_comment(comment_start_line, "/*") {
102102
start_line += 1;
103103
}
104104
let mut end_line =
105-
ctx.source_text()[..=comment.span().end as usize].lines().count();
106-
let comment_end_line = ctx.source_text()[comment.span().end as usize..]
107-
.lines()
108-
.next()
109-
.unwrap_or("");
105+
ctx.source_text()[..=comment.span.end as usize].lines().count();
106+
let comment_end_line =
107+
ctx.source_text()[comment.span.end as usize..].lines().next().unwrap_or("");
110108
if line_has_just_comment(comment_end_line, "*/") {
111109
end_line += 1;
112110
}

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,7 @@ impl NoFallthrough {
213213
.trivias()
214214
.comments_range(range)
215215
.map(|comment| {
216-
&semantic.source_text()
217-
[comment.span().start as usize..comment.span().end as usize]
216+
&semantic.source_text()[comment.span.start as usize..comment.span.end as usize]
218217
})
219218
.last()
220219
.map(str::trim);

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ impl Rule for NoCommentedOutTests {
5353
let comments = ctx.semantic().trivias().comments();
5454
let source_text = ctx.semantic().source_text();
5555
let commented_tests = comments.filter_map(|comment| {
56-
let text = comment.span().source_text(source_text);
56+
let text = comment.span.source_text(source_text);
5757
if RE.is_match(text) {
58-
Some(*comment.span())
58+
Some(comment.span)
5959
} else {
6060
None
6161
}

crates/oxc_linter/src/rules/typescript/ban_ts_comment.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ impl Rule for BanTsComment {
142142
fn run_once(&self, ctx: &LintContext) {
143143
let comments = ctx.semantic().trivias().comments();
144144
for comm in comments {
145-
let raw = ctx.source_range(*comm.span());
145+
let raw = ctx.source_range(comm.span);
146146
if let Some(captures) = find_ts_comment_directive(raw, comm.kind.is_single_line()) {
147147
// safe to unwrap, if capture success, it can always capture one of the four directives
148148
let (directive, description) = (captures.0, captures.1);
@@ -163,16 +163,16 @@ impl Rule for BanTsComment {
163163
if *on {
164164
if directive == "ignore" {
165165
ctx.diagnostic_with_fix(
166-
ignore_instead_of_expect_error(*comm.span()),
166+
ignore_instead_of_expect_error(comm.span),
167167
|fixer| {
168168
fixer.replace(
169-
*comm.span(),
169+
comm.span,
170170
raw.replace("@ts-ignore", "@ts-expect-error"),
171171
)
172172
},
173173
);
174174
} else {
175-
ctx.diagnostic(comment(directive, *comm.span()));
175+
ctx.diagnostic(comment(directive, comm.span));
176176
}
177177
}
178178
}
@@ -182,7 +182,7 @@ impl Rule for BanTsComment {
182182
ctx.diagnostic(comment_requires_description(
183183
directive,
184184
self.minimum_description_length,
185-
*comm.span(),
185+
comm.span,
186186
));
187187
}
188188

@@ -191,7 +191,7 @@ impl Rule for BanTsComment {
191191
ctx.diagnostic(comment_description_not_match_pattern(
192192
directive,
193193
&re.to_string(),
194-
*comm.span(),
194+
comm.span,
195195
));
196196
}
197197
}

crates/oxc_linter/src/rules/typescript/ban_tslint_comment.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ impl Rule for BanTslintComment {
3939
let source_text_len = ctx.semantic().source_text().len();
4040

4141
for comment in comments {
42-
let raw = comment.span().source_text(ctx.semantic().source_text());
42+
let raw = comment.span.source_text(ctx.semantic().source_text());
4343

4444
if is_tslint_comment_directive(raw) {
4545
let comment_span = get_full_comment(
4646
source_text_len,
47-
comment.span().start,
48-
comment.span().end,
47+
comment.span.start,
48+
comment.span.end,
4949
comment.kind.is_multi_line(),
5050
);
5151

crates/oxc_linter/src/rules/typescript/prefer_function_type.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ fn check_member(member: &TSSignature, node: &AstNode<'_>, ctx: &LintContext<'_>)
162162
.semantic()
163163
.trivias()
164164
.comments_range(node_start..node_end)
165-
.map(|comment| (*comment, comment.span()));
165+
.map(|comment| (*comment, comment.span));
166166

167167
let comments_text = {
168168
let mut comments_vec: Vec<String> = vec![];

crates/oxc_linter/src/rules/typescript/prefer_ts_expect_error.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,22 @@ impl Rule for PreferTsExpectError {
4646
let comments = ctx.semantic().trivias().comments();
4747

4848
for comment in comments {
49-
let raw = comment.span().source_text(ctx.semantic().source_text());
49+
let raw = comment.span.source_text(ctx.semantic().source_text());
5050

5151
if !is_valid_ts_ignore_present(comment.kind, raw) {
5252
continue;
5353
}
5454

5555
if comment.kind.is_single_line() {
56-
let comment_span = Span::new(comment.span().start - 2, comment.span().end);
56+
let comment_span = Span::new(comment.span.start - 2, comment.span.end);
5757
ctx.diagnostic_with_fix(prefer_ts_expect_error_diagnostic(comment_span), |fixer| {
5858
fixer.replace(
5959
comment_span,
6060
format!("//{}", raw.replace("@ts-ignore", "@ts-expect-error")),
6161
)
6262
});
6363
} else {
64-
let comment_span = Span::new(comment.span().start - 2, comment.span().end + 2);
64+
let comment_span = Span::new(comment.span.start - 2, comment.span.end + 2);
6565
ctx.diagnostic_with_fix(prefer_ts_expect_error_diagnostic(comment_span), |fixer| {
6666
fixer.replace(
6767
comment_span,

crates/oxc_linter/src/rules/typescript/triple_slash_reference.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -114,21 +114,21 @@ impl Rule for TripleSlashReference {
114114

115115
for comment in ctx.semantic().trivias().comments_range(0..comments_range_end) {
116116
let raw = &ctx.semantic().source_text()
117-
[comment.span().start as usize..comment.span().end as usize];
117+
[comment.span.start as usize..comment.span.end as usize];
118118
if let Some((group1, group2)) = get_attr_key_and_value(raw) {
119119
if (group1 == "types" && self.types == TypesOption::Never)
120120
|| (group1 == "path" && self.path == PathOption::Never)
121121
|| (group1 == "lib" && self.lib == LibOption::Never)
122122
{
123123
ctx.diagnostic(triple_slash_reference_diagnostic(
124124
&group2,
125-
Span::new(comment.span().start - 2, comment.span().end),
125+
Span::new(comment.span.start - 2, comment.span.end),
126126
));
127127
}
128128

129129
if group1 == "types" && self.types == TypesOption::PreferImport {
130130
refs_for_import
131-
.insert(group2, Span::new(comment.span().start - 2, comment.span().end));
131+
.insert(group2, Span::new(comment.span.start - 2, comment.span.end));
132132
}
133133
}
134134
}

crates/oxc_linter/src/rules/unicorn/no_empty_file.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ fn has_triple_slash_directive(ctx: &LintContext<'_>) -> bool {
7373
if !comment.kind.is_single_line() {
7474
continue;
7575
}
76-
let text = comment.span().source_text(ctx.source_text());
76+
let text = comment.span.source_text(ctx.source_text());
7777
if text.starts_with("///") {
7878
return true;
7979
}

0 commit comments

Comments
 (0)