Skip to content

Commit

Permalink
comment: make span public
Browse files Browse the repository at this point in the history
  • Loading branch information
lucab committed Jul 9, 2024
1 parent e8f7dbe commit 0c7e4fc
Show file tree
Hide file tree
Showing 22 changed files with 81 additions and 97 deletions.
10 changes: 2 additions & 8 deletions crates/oxc_ast/src/trivia.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use oxc_span::Span;
#[derive(Debug, Clone, Copy)]
pub struct Comment {
pub kind: CommentKind,
/// The span of the comment text (leading/trailing delimiters not included).
span: Span,
/// The span of the comment text (without leading/trailing delimiters).
pub span: Span,
}

impl Comment {
Expand All @@ -22,12 +22,6 @@ impl Comment {
let span = Span::new(start, end);
Self { kind, span }
}

/// Return the span of the comment text (without delimiters).
#[inline]
pub fn span(&self) -> &Span {
&self.span
}
}

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
Expand Down
15 changes: 5 additions & 10 deletions crates/oxc_codegen/src/annotation_comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@ pub fn get_leading_annotate_comment<const MINIFY: bool>(
let maybe_leading_comment = codegen.try_get_leading_comment(node_start);
let comment = maybe_leading_comment?;
let real_end = match comment.kind {
CommentKind::SingleLine => comment.span().end,
CommentKind::MultiLine => comment.span().end + 2,
CommentKind::SingleLine => comment.span.end,
CommentKind::MultiLine => comment.span.end + 2,
};
let source_code = codegen.source_text;
let content_between = &source_code[real_end as usize..node_start as usize];
// Used for VariableDeclaration (Rollup only respects "const" and only for the first one)
if content_between.chars().all(|ch| ch.is_ascii_whitespace()) {
let comment_content =
&source_code[comment.span().start as usize..comment.span().end as usize];
let comment_content = &source_code[comment.span.start as usize..comment.span.end as usize];
if MATCHER.find_iter(&comment_content).next().is_some() {
return Some(*comment);
}
Expand All @@ -38,17 +37,13 @@ pub fn print_comment<const MINIFY: bool>(comment: Comment, p: &mut Codegen<{ MIN
match comment.kind {
CommentKind::SingleLine => {
p.print_str("//");
p.print_range_of_source_code(
comment.span().start as usize..comment.span().end as usize,
);
p.print_range_of_source_code(comment.span.start as usize..comment.span.end as usize);
p.print_soft_newline();
p.print_indent();
}
CommentKind::MultiLine => {
p.print_str("/*");
p.print_range_of_source_code(
comment.span().start as usize..comment.span().end as usize,
);
p.print_range_of_source_code(comment.span.start as usize..comment.span.end as usize);
p.print_str("*/");
p.print_soft_space();
}
Expand Down
37 changes: 18 additions & 19 deletions crates/oxc_linter/src/disable_directives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ impl<'a> DisableDirectivesBuilder<'a> {
// for matching disable and enable pairs.
// Wrongly ordered matching pairs are not taken into consideration.
for comment in self.trivias.clone().comments() {
let text = comment.span().source_text(self.source_text);
let text = comment.span.source_text(self.source_text);
let text = text.trim_start();

if let Some(text) =
Expand All @@ -103,52 +103,52 @@ impl<'a> DisableDirectivesBuilder<'a> {
// `eslint-disable`
if text.trim().is_empty() {
if self.disable_all_start.is_none() {
self.disable_all_start = Some(comment.span().end);
self.disable_all_start = Some(comment.span.end);
}
self.disable_all_comments.push(*comment.span());
self.disable_all_comments.push(comment.span);
continue;
}

// `eslint-disable-next-line`
if let Some(text) = text.strip_prefix("-next-line") {
// Get the span up to the next new line
let stop = self.source_text[comment.span().end as usize..]
let stop = self.source_text[comment.span.end as usize..]
.lines()
.take(2)
.fold(comment.span().end, |acc, line| acc + line.len() as u32);
.fold(comment.span.end, |acc, line| acc + line.len() as u32);
if text.trim().is_empty() {
self.add_interval(comment.span().end, stop, DisabledRule::All);
self.disable_all_comments.push(*comment.span());
self.add_interval(comment.span.end, stop, DisabledRule::All);
self.disable_all_comments.push(comment.span);
} else {
// `eslint-disable-next-line rule_name1, rule_name2`
let mut rules = vec![];
Self::get_rule_names(text, |rule_name| {
self.add_interval(
comment.span().end,
comment.span.end,
stop,
DisabledRule::Single(rule_name),
);
rules.push(rule_name);
});
self.disable_rule_comments
.push(DisableRuleComment { span: *comment.span(), rules });
.push(DisableRuleComment { span: comment.span, rules });
}
continue;
}

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

// `eslint-disable-line`
if text.trim().is_empty() {
self.add_interval(start, stop, DisabledRule::All);
self.disable_all_comments.push(*comment.span());
self.disable_all_comments.push(comment.span);
} else {
// `eslint-disable-line rule-name1, rule-name2`
let mut rules = vec![];
Expand All @@ -157,19 +157,18 @@ impl<'a> DisableDirectivesBuilder<'a> {
rules.push(rule_name);
});
self.disable_rule_comments
.push(DisableRuleComment { span: *comment.span(), rules });
.push(DisableRuleComment { span: comment.span, rules });
}
continue;
}

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

continue;
}
Expand All @@ -180,15 +179,15 @@ impl<'a> DisableDirectivesBuilder<'a> {
// `eslint-enable`
if text.trim().is_empty() {
if let Some(start) = self.disable_all_start.take() {
self.add_interval(start, comment.span().start, DisabledRule::All);
self.add_interval(start, comment.span.start, DisabledRule::All);
}
} else {
// `eslint-enable rule-name1, rule-name2`
Self::get_rule_names(text, |rule_name| {
if let Some(start) = self.disable_start_map.remove(rule_name) {
self.add_interval(
start,
comment.span().start,
comment.span.start,
DisabledRule::Single(rule_name),
);
}
Expand Down
3 changes: 1 addition & 2 deletions crates/oxc_linter/src/rules/eslint/default_case.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ impl Rule for DefaultCase {
.comments_range(last_case.span.start..switch.span.end)
.last()
.is_some_and(|comment| {
let raw =
comment.span().source_text(ctx.semantic().source_text()).trim();
let raw = comment.span.source_text(ctx.semantic().source_text()).trim();
match &self.comment_pattern {
Some(comment_pattern) => comment_pattern.is_match(raw),
None => raw.eq_ignore_ascii_case("no default"),
Expand Down
14 changes: 6 additions & 8 deletions crates/oxc_linter/src/rules/eslint/max_lines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl Rule for MaxLines {
let mut comment_lines: usize = 0;
for comment in ctx.semantic().trivias().comments() {
if comment.kind.is_single_line() {
let comment_line = ctx.source_text()[..comment.span().start as usize]
let comment_line = ctx.source_text()[..comment.span.start as usize]
.lines()
.next_back()
.unwrap_or("");
Expand All @@ -93,20 +93,18 @@ impl Rule for MaxLines {
}
} else {
let mut start_line =
ctx.source_text()[..comment.span().start as usize].lines().count();
let comment_start_line = ctx.source_text()[..comment.span().start as usize]
ctx.source_text()[..comment.span.start as usize].lines().count();
let comment_start_line = ctx.source_text()[..comment.span.start as usize]
.lines()
.next_back()
.unwrap_or("");
if !line_has_just_comment(comment_start_line, "/*") {
start_line += 1;
}
let mut end_line =
ctx.source_text()[..=comment.span().end as usize].lines().count();
let comment_end_line = ctx.source_text()[comment.span().end as usize..]
.lines()
.next()
.unwrap_or("");
ctx.source_text()[..=comment.span.end as usize].lines().count();
let comment_end_line =
ctx.source_text()[comment.span.end as usize..].lines().next().unwrap_or("");
if line_has_just_comment(comment_end_line, "*/") {
end_line += 1;
}
Expand Down
3 changes: 1 addition & 2 deletions crates/oxc_linter/src/rules/eslint/no_fallthrough.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,7 @@ impl NoFallthrough {
.trivias()
.comments_range(range)
.map(|comment| {
&semantic.source_text()
[comment.span().start as usize..comment.span().end as usize]
&semantic.source_text()[comment.span.start as usize..comment.span.end as usize]
})
.last()
.map(str::trim);
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_linter/src/rules/jest/no_commented_out_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ impl Rule for NoCommentedOutTests {
let comments = ctx.semantic().trivias().comments();
let source_text = ctx.semantic().source_text();
let commented_tests = comments.filter_map(|comment| {
let text = comment.span().source_text(source_text);
let text = comment.span.source_text(source_text);
if RE.is_match(text) {
Some(*comment.span())
Some(comment.span)
} else {
None
}
Expand Down
12 changes: 6 additions & 6 deletions crates/oxc_linter/src/rules/typescript/ban_ts_comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ impl Rule for BanTsComment {
fn run_once(&self, ctx: &LintContext) {
let comments = ctx.semantic().trivias().comments();
for comm in comments {
let raw = ctx.source_range(*comm.span());
let raw = ctx.source_range(comm.span);
if let Some(captures) = find_ts_comment_directive(raw, comm.kind.is_single_line()) {
// safe to unwrap, if capture success, it can always capture one of the four directives
let (directive, description) = (captures.0, captures.1);
Expand All @@ -163,16 +163,16 @@ impl Rule for BanTsComment {
if *on {
if directive == "ignore" {
ctx.diagnostic_with_fix(
ignore_instead_of_expect_error(*comm.span()),
ignore_instead_of_expect_error(comm.span),
|fixer| {
fixer.replace(
*comm.span(),
comm.span,
raw.replace("@ts-ignore", "@ts-expect-error"),
)
},
);
} else {
ctx.diagnostic(comment(directive, *comm.span()));
ctx.diagnostic(comment(directive, comm.span));
}
}
}
Expand All @@ -182,7 +182,7 @@ impl Rule for BanTsComment {
ctx.diagnostic(comment_requires_description(
directive,
self.minimum_description_length,
*comm.span(),
comm.span,
));
}

Expand All @@ -191,7 +191,7 @@ impl Rule for BanTsComment {
ctx.diagnostic(comment_description_not_match_pattern(
directive,
&re.to_string(),
*comm.span(),
comm.span,
));
}
}
Expand Down
6 changes: 3 additions & 3 deletions crates/oxc_linter/src/rules/typescript/ban_tslint_comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ impl Rule for BanTslintComment {
let source_text_len = ctx.semantic().source_text().len();

for comment in comments {
let raw = comment.span().source_text(ctx.semantic().source_text());
let raw = comment.span.source_text(ctx.semantic().source_text());

if is_tslint_comment_directive(raw) {
let comment_span = get_full_comment(
source_text_len,
comment.span().start,
comment.span().end,
comment.span.start,
comment.span.end,
comment.kind.is_multi_line(),
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ fn check_member(member: &TSSignature, node: &AstNode<'_>, ctx: &LintContext<'_>)
.semantic()
.trivias()
.comments_range(node_start..node_end)
.map(|comment| (*comment, comment.span()));
.map(|comment| (*comment, comment.span));

let comments_text = {
let mut comments_vec: Vec<String> = vec![];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,22 @@ impl Rule for PreferTsExpectError {
let comments = ctx.semantic().trivias().comments();

for comment in comments {
let raw = comment.span().source_text(ctx.semantic().source_text());
let raw = comment.span.source_text(ctx.semantic().source_text());

if !is_valid_ts_ignore_present(comment.kind, raw) {
continue;
}

if comment.kind.is_single_line() {
let comment_span = Span::new(comment.span().start - 2, comment.span().end);
let comment_span = Span::new(comment.span.start - 2, comment.span.end);
ctx.diagnostic_with_fix(prefer_ts_expect_error_diagnostic(comment_span), |fixer| {
fixer.replace(
comment_span,
format!("//{}", raw.replace("@ts-ignore", "@ts-expect-error")),
)
});
} else {
let comment_span = Span::new(comment.span().start - 2, comment.span().end + 2);
let comment_span = Span::new(comment.span.start - 2, comment.span.end + 2);
ctx.diagnostic_with_fix(prefer_ts_expect_error_diagnostic(comment_span), |fixer| {
fixer.replace(
comment_span,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,21 +114,21 @@ impl Rule for TripleSlashReference {

for comment in ctx.semantic().trivias().comments_range(0..comments_range_end) {
let raw = &ctx.semantic().source_text()
[comment.span().start as usize..comment.span().end as usize];
[comment.span.start as usize..comment.span.end as usize];
if let Some((group1, group2)) = get_attr_key_and_value(raw) {
if (group1 == "types" && self.types == TypesOption::Never)
|| (group1 == "path" && self.path == PathOption::Never)
|| (group1 == "lib" && self.lib == LibOption::Never)
{
ctx.diagnostic(triple_slash_reference_diagnostic(
&group2,
Span::new(comment.span().start - 2, comment.span().end),
Span::new(comment.span.start - 2, comment.span.end),
));
}

if group1 == "types" && self.types == TypesOption::PreferImport {
refs_for_import
.insert(group2, Span::new(comment.span().start - 2, comment.span().end));
.insert(group2, Span::new(comment.span.start - 2, comment.span.end));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/unicorn/no_empty_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ fn has_triple_slash_directive(ctx: &LintContext<'_>) -> bool {
if !comment.kind.is_single_line() {
continue;
}
let text = comment.span().source_text(ctx.source_text());
let text = comment.span.source_text(ctx.source_text());
if text.starts_with("///") {
return true;
}
Expand Down
Loading

0 comments on commit 0c7e4fc

Please sign in to comment.