Skip to content

Commit 6dd6f7c

Browse files
committed
refactor(ast): change Comment struct (#5783)
1 parent 512be65 commit 6dd6f7c

File tree

16 files changed

+68
-71
lines changed

16 files changed

+68
-71
lines changed

crates/oxc_ast/src/trivia.rs

+26-29
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,34 @@ use std::{
88

99
use oxc_span::Span;
1010

11+
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
12+
pub enum CommentKind {
13+
Line,
14+
Block,
15+
}
16+
1117
/// Single or multiline comment
1218
#[derive(Debug, Clone, Copy)]
1319
pub struct Comment {
14-
pub kind: CommentKind,
1520
/// The span of the comment text (without leading/trailing delimiters).
1621
pub span: Span,
22+
23+
pub kind: CommentKind,
1724
}
1825

1926
impl Comment {
2027
#[inline]
2128
pub fn new(start: u32, end: u32, kind: CommentKind) -> Self {
2229
let span = Span::new(start, end);
23-
Self { kind, span }
30+
Self { span, kind }
31+
}
32+
33+
pub fn is_line(self) -> bool {
34+
self.kind == CommentKind::Line
35+
}
36+
37+
pub fn is_block(self) -> bool {
38+
self.kind == CommentKind::Block
2439
}
2540

2641
pub fn real_span(&self) -> Span {
@@ -29,36 +44,18 @@ impl Comment {
2944

3045
pub fn real_span_end(&self) -> u32 {
3146
match self.kind {
32-
CommentKind::SingleLine => self.span.end,
47+
CommentKind::Line => self.span.end,
3348
// length of `*/`
34-
CommentKind::MultiLine => self.span.end + 2,
49+
CommentKind::Block => self.span.end + 2,
3550
}
3651
}
3752

3853
pub fn real_span_start(&self) -> u32 {
39-
match self.kind {
40-
CommentKind::SingleLine | CommentKind::MultiLine => self.span.start - 2,
41-
}
54+
self.span.start - 2
4255
}
4356
}
4457

45-
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
46-
pub enum CommentKind {
47-
SingleLine,
48-
MultiLine,
49-
}
50-
51-
impl CommentKind {
52-
#[inline]
53-
pub fn is_single_line(self) -> bool {
54-
self == Self::SingleLine
55-
}
56-
57-
#[inline]
58-
pub fn is_multi_line(self) -> bool {
59-
self == Self::MultiLine
60-
}
61-
}
58+
impl CommentKind {}
6259

6360
/// Sorted set of unique trivia comments, in ascending order by starting position.
6461
pub type SortedComments = Box<[Comment]>;
@@ -192,11 +189,11 @@ mod test {
192189
#[test]
193190
fn test_comments_range() {
194191
let comments: SortedComments = vec![
195-
Comment { span: Span::new(0, 4), kind: CommentKind::SingleLine },
196-
Comment { span: Span::new(5, 9), kind: CommentKind::SingleLine },
197-
Comment { span: Span::new(10, 13), kind: CommentKind::SingleLine },
198-
Comment { span: Span::new(14, 17), kind: CommentKind::SingleLine },
199-
Comment { span: Span::new(18, 23), kind: CommentKind::SingleLine },
192+
Comment::new(0, 4, CommentKind::Line),
193+
Comment::new(5, 9, CommentKind::Line),
194+
Comment::new(10, 13, CommentKind::Line),
195+
Comment::new(14, 17, CommentKind::Line),
196+
Comment::new(18, 23, CommentKind::Line),
200197
]
201198
.into_boxed_slice();
202199
let full_len = comments.len();

crates/oxc_codegen/src/annotation_comment.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,15 @@ impl<'a> Codegen<'a> {
106106
}
107107
self.update_last_consumed_comment_end(real_span_end);
108108
match comment.kind() {
109-
CommentKind::SingleLine => {
109+
CommentKind::Line => {
110110
self.print_str("//");
111111
self.print_range_of_source_code(
112112
comment_span.start as usize..comment_span.end as usize,
113113
);
114114
self.print_soft_newline();
115115
self.print_indent();
116116
}
117-
CommentKind::MultiLine => {
117+
CommentKind::Block => {
118118
self.print_str("/*");
119119
self.print_range_of_source_code(
120120
comment_span.start as usize..comment_span.end as usize,

crates/oxc_diagnostics/src/graphic_reporter.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -642,14 +642,14 @@ impl GraphicalReportHandler {
642642
max_gutter,
643643
line,
644644
labels,
645-
LabelRenderMode::MultiLineFirst,
645+
LabelRenderMode::BlockFirst,
646646
)?;
647647

648648
self.render_multi_line_end_single(
649649
f,
650650
first,
651651
label.style,
652-
LabelRenderMode::MultiLineFirst,
652+
LabelRenderMode::BlockFirst,
653653
)?;
654654
for label_line in rest {
655655
// no line number!
@@ -660,13 +660,13 @@ impl GraphicalReportHandler {
660660
max_gutter,
661661
line,
662662
labels,
663-
LabelRenderMode::MultiLineRest,
663+
LabelRenderMode::BlockRest,
664664
)?;
665665
self.render_multi_line_end_single(
666666
f,
667667
label_line,
668668
label.style,
669-
LabelRenderMode::MultiLineRest,
669+
LabelRenderMode::BlockRest,
670670
)?;
671671
}
672672
}
@@ -764,7 +764,7 @@ impl GraphicalReportHandler {
764764
let applicable = highlights.iter().filter(|hl| line.span_applies_gutter(hl));
765765
for (i, hl) in applicable.enumerate() {
766766
if !line.span_line_only(hl) && line.span_ends(hl) {
767-
if render_mode == LabelRenderMode::MultiLineRest {
767+
if render_mode == LabelRenderMode::BlockRest {
768768
// this is to make multiline labels work. We want to make the right amount
769769
// of horizontal space for them, but not actually draw the lines
770770
let horizontal_space = max_gutter.saturating_sub(i) + 2;
@@ -792,7 +792,7 @@ impl GraphicalReportHandler {
792792
num_repeat
793793
// if we are rendering a multiline label, then leave a bit of space for the
794794
// rcross character
795-
- if render_mode == LabelRenderMode::MultiLineFirst {
795+
- if render_mode == LabelRenderMode::BlockFirst {
796796
1
797797
} else {
798798
0
@@ -1039,9 +1039,9 @@ impl GraphicalReportHandler {
10391039
hl,
10401040
label_line,
10411041
if first {
1042-
LabelRenderMode::MultiLineFirst
1042+
LabelRenderMode::BlockFirst
10431043
} else {
1044-
LabelRenderMode::MultiLineRest
1044+
LabelRenderMode::BlockRest
10451045
},
10461046
)?;
10471047
first = false;
@@ -1090,10 +1090,10 @@ impl GraphicalReportHandler {
10901090
LabelRenderMode::SingleLine => {
10911091
format!("{}{} {}", chars.lbot, chars.hbar.to_string().repeat(2), label,)
10921092
}
1093-
LabelRenderMode::MultiLineFirst => {
1093+
LabelRenderMode::BlockFirst => {
10941094
format!("{}{}{} {}", chars.lbot, chars.hbar, chars.rcross, label,)
10951095
}
1096-
LabelRenderMode::MultiLineRest => {
1096+
LabelRenderMode::BlockRest => {
10971097
format!(" {} {}", chars.vbar, label,)
10981098
}
10991099
};
@@ -1115,10 +1115,10 @@ impl GraphicalReportHandler {
11151115
LabelRenderMode::SingleLine => {
11161116
writeln!(f, "{} {}", self.theme.characters.hbar.style(style), label)?;
11171117
}
1118-
LabelRenderMode::MultiLineFirst => {
1118+
LabelRenderMode::BlockFirst => {
11191119
writeln!(f, "{} {}", self.theme.characters.rcross.style(style), label)?;
11201120
}
1121-
LabelRenderMode::MultiLineRest => {
1121+
LabelRenderMode::BlockRest => {
11221122
writeln!(f, "{} {}", self.theme.characters.vbar.style(style), label)?;
11231123
}
11241124
}
@@ -1206,9 +1206,9 @@ enum LabelRenderMode {
12061206
/// we're rendering a single line label (or not rendering in any special way)
12071207
SingleLine,
12081208
/// we're rendering a multiline label
1209-
MultiLineFirst,
1209+
BlockFirst,
12101210
/// we're rendering the rest of a multiline label
1211-
MultiLineRest,
1211+
BlockRest,
12121212
}
12131213

12141214
#[derive(Debug)]

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ impl Rule for MaxLines {
8383
let comment_lines = if self.skip_comments {
8484
let mut comment_lines: usize = 0;
8585
for comment in ctx.semantic().trivias().comments() {
86-
if comment.kind.is_single_line() {
86+
if comment.is_line() {
8787
let comment_line = ctx.source_text()[..comment.span.start as usize]
8888
.lines()
8989
.next_back()

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,10 @@ impl Rule for BanTsComment {
155155
let comments = ctx.semantic().trivias().comments();
156156
for comm in comments {
157157
let raw = ctx.source_range(comm.span);
158-
if let Some(captures) = find_ts_comment_directive(raw, comm.kind.is_single_line()) {
158+
if let Some(captures) = find_ts_comment_directive(raw, comm.is_line()) {
159159
// safe to unwrap, if capture success, it can always capture one of the four directives
160160
let (directive, description) = (captures.0, captures.1);
161-
if CommentKind::MultiLine == comm.kind
161+
if CommentKind::Block == comm.kind
162162
&& (directive == "check" || directive == "nocheck")
163163
{
164164
continue;

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ impl Rule for BanTslintComment {
4444
source_text_len,
4545
comment.span.start,
4646
comment.span.end,
47-
comment.kind.is_multi_line(),
47+
comment.is_block(),
4848
);
4949

5050
ctx.diagnostic_with_fix(

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,12 @@ fn check_member(member: &TSSignature, node: &AstNode<'_>, ctx: &LintContext<'_>)
173173
[span.start as usize..span.end as usize];
174174

175175
match comment_interface.kind {
176-
CommentKind::SingleLine => {
176+
CommentKind::Line => {
177177
let single_line_comment: String =
178178
format!("//{comment}\n");
179179
comments_vec.push(single_line_comment);
180180
}
181-
CommentKind::MultiLine => {
181+
CommentKind::Block => {
182182
let multi_line_comment: String =
183183
format!("/*{comment}*/\n");
184184
comments_vec.push(multi_line_comment);

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

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use cow_utils::CowUtils;
2-
use oxc_ast::CommentKind;
2+
use oxc_ast::Comment;
33
use oxc_diagnostics::OxcDiagnostic;
44
use oxc_macros::declare_oxc_lint;
55
use oxc_span::Span;
@@ -50,11 +50,11 @@ impl Rule for PreferTsExpectError {
5050
for comment in comments {
5151
let raw = comment.span.source_text(ctx.semantic().source_text());
5252

53-
if !is_valid_ts_ignore_present(comment.kind, raw) {
53+
if !is_valid_ts_ignore_present(*comment, raw) {
5454
continue;
5555
}
5656

57-
if comment.kind.is_single_line() {
57+
if comment.is_line() {
5858
let comment_span = Span::new(comment.span.start - 2, comment.span.end);
5959
ctx.diagnostic_with_fix(prefer_ts_expect_error_diagnostic(comment_span), |fixer| {
6060
fixer.replace(
@@ -79,18 +79,18 @@ impl Rule for PreferTsExpectError {
7979
}
8080
}
8181

82-
fn get_last_comment_line(comment: CommentKind, raw: &str) -> String {
83-
if comment.is_single_line() {
82+
fn get_last_comment_line(comment: Comment, raw: &str) -> String {
83+
if comment.is_line() {
8484
return String::from(raw);
8585
}
8686

8787
return String::from(raw.lines().last().unwrap_or(raw));
8888
}
8989

90-
fn is_valid_ts_ignore_present(comment: CommentKind, raw: &str) -> bool {
90+
fn is_valid_ts_ignore_present(comment: Comment, raw: &str) -> bool {
9191
let line = get_last_comment_line(comment, raw);
9292

93-
if comment.is_single_line() {
93+
if comment.is_line() {
9494
test_single_line_comment(&line)
9595
} else {
9696
test_multi_line_comment(&line)

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ impl Rule for NoEmptyFile {
7070

7171
fn has_triple_slash_directive(ctx: &LintContext<'_>) -> bool {
7272
for comment in ctx.semantic().trivias().comments() {
73-
if !comment.kind.is_single_line() {
73+
if !comment.is_line() {
7474
continue;
7575
}
7676
let text = comment.span.source_text(ctx.source_text());

crates/oxc_linter/src/utils/tree_shaking.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ pub fn get_leading_tree_shaking_comment<'a>(span: Span, ctx: &LintContext<'a>) -
299299
ctx.source_text()[..comment.span.end as usize].lines().next_back().unwrap_or("");
300300
let nothing_before_comment = previous_line
301301
.trim()
302-
.strip_prefix(if comment.kind == CommentKind::SingleLine { "//" } else { "/*" })
302+
.strip_prefix(if comment.kind == CommentKind::Line { "//" } else { "/*" })
303303
.is_some_and(|s| s.trim().is_empty());
304304
if !nothing_before_comment {
305305
return None;

crates/oxc_parser/src/lexer/trivia_builder.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ impl TriviaBuilder {
1717

1818
pub fn add_single_line_comment(&mut self, start: u32, end: u32) {
1919
// skip leading `//`
20-
self.add_comment(Comment::new(start + 2, end, CommentKind::SingleLine));
20+
self.add_comment(Comment::new(start + 2, end, CommentKind::Line));
2121
}
2222

2323
pub fn add_multi_line_comment(&mut self, start: u32, end: u32) {
2424
// skip leading `/*` and trailing `*/`
25-
self.add_comment(Comment::new(start + 2, end - 2, CommentKind::MultiLine));
25+
self.add_comment(Comment::new(start + 2, end - 2, CommentKind::Block));
2626
}
2727

2828
fn add_comment(&mut self, comment: Comment) {

crates/oxc_parser/src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -510,11 +510,11 @@ mod test {
510510
let allocator = Allocator::default();
511511
let source_type = SourceType::default().with_typescript(true);
512512
let sources = [
513-
("// line comment", CommentKind::SingleLine),
514-
("/* line comment */", CommentKind::MultiLine),
513+
("// line comment", CommentKind::Line),
514+
("/* line comment */", CommentKind::Block),
515515
(
516516
"type Foo = ( /* Require properties which are not generated automatically. */ 'bar')",
517-
CommentKind::MultiLine,
517+
CommentKind::Block,
518518
),
519519
];
520520
for (source, kind) in sources {

crates/oxc_prettier/src/comments/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ impl Comment {
1818
Self {
1919
start: span.start,
2020
end: span.end,
21-
is_block: comment.kind.is_multi_line(),
21+
is_block: comment.is_block(),
2222
has_line_suffix: false,
2323
}
2424
}

crates/oxc_semantic/src/jsdoc/builder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ impl<'a> JSDocBuilder<'a> {
147147
}
148148

149149
fn parse_if_jsdoc_comment(&self, comment: &Comment) -> Option<JSDoc<'a>> {
150-
if !comment.kind.is_multi_line() {
150+
if !comment.is_block() {
151151
return None;
152152
}
153153

crates/oxc_wasm/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -420,8 +420,8 @@ impl Oxc {
420420
.comments()
421421
.map(|comment| Comment {
422422
r#type: match comment.kind {
423-
CommentKind::SingleLine => CommentType::Line,
424-
CommentKind::MultiLine => CommentType::Block,
423+
CommentKind::Line => CommentType::Line,
424+
CommentKind::Block => CommentType::Block,
425425
},
426426
value: comment.span.source_text(source_text).to_string(),
427427
start: comment.span.start,

napi/parser/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ fn parse_with_return<'a>(source_text: &'a str, options: &ParserOptions) -> Parse
109109
.comments()
110110
.map(|comment| Comment {
111111
r#type: match comment.kind {
112-
CommentKind::SingleLine => "Line",
113-
CommentKind::MultiLine => "Block",
112+
CommentKind::Line => "Line",
113+
CommentKind::Block => "Block",
114114
},
115115
value: comment.span.source_text(source_text).to_string(),
116116
start: comment.span.start,

0 commit comments

Comments
 (0)