refactor(linter): use ctx.source_range(comment.content_span()) API (#7155)

This commit is contained in:
Boshen 2024-11-06 06:25:05 +00:00
parent 3da9443b0e
commit 8c0a362b8b
8 changed files with 8 additions and 9 deletions

View file

@ -79,7 +79,7 @@ impl Rule for DefaultCase {
.comments_range(last_case.span.start..switch.span.end) .comments_range(last_case.span.start..switch.span.end)
.last() .last()
.is_some_and(|comment| { .is_some_and(|comment| {
let raw = comment.content_span().source_text(ctx.source_text()).trim(); let raw = ctx.source_range(comment.content_span()).trim();
match &self.comment_pattern { match &self.comment_pattern {
Some(comment_pattern) => comment_pattern.is_match(raw), Some(comment_pattern) => comment_pattern.is_match(raw),
None => raw.eq_ignore_ascii_case("no default"), None => raw.eq_ignore_ascii_case("no default"),

View file

@ -382,7 +382,7 @@ impl NoFallthrough {
let is_fallthrough_comment_in_range = |range: Range<u32>| { let is_fallthrough_comment_in_range = |range: Range<u32>| {
let comment = semantic let comment = semantic
.comments_range(range) .comments_range(range)
.map(|comment| comment.content_span().source_text(semantic.source_text())) .map(|comment| ctx.source_range(comment.content_span()))
.last() .last()
.map(str::trim); .map(str::trim);

View file

@ -60,9 +60,8 @@ impl Rule for NoCommentedOutTests {
Regex::new(r#"(?mu)^\s*[xf]?(test|it|describe)(\.\w+|\[['"]\w+['"]\])?\s*\("#).unwrap(); Regex::new(r#"(?mu)^\s*[xf]?(test|it|describe)(\.\w+|\[['"]\w+['"]\])?\s*\("#).unwrap();
} }
let comments = ctx.semantic().comments(); let comments = ctx.semantic().comments();
let source_text = ctx.source_text();
let commented_tests = comments.iter().filter_map(|comment| { let commented_tests = comments.iter().filter_map(|comment| {
let text = comment.content_span().source_text(source_text); let text = ctx.source_range(comment.content_span());
if RE.is_match(text) { if RE.is_match(text) {
Some(comment.content_span()) Some(comment.content_span())
} else { } else {

View file

@ -36,7 +36,7 @@ impl Rule for BanTslintComment {
let comments = ctx.semantic().comments(); let comments = ctx.semantic().comments();
let source_text_len = ctx.semantic().source_text().len(); let source_text_len = ctx.semantic().source_text().len();
for comment in comments { for comment in comments {
let raw = comment.content_span().source_text(ctx.source_text()); let raw = ctx.source_range(comment.content_span());
if is_tslint_comment_directive(raw) { if is_tslint_comment_directive(raw) {
let comment_span = get_full_comment(source_text_len, comment.span); let comment_span = get_full_comment(source_text_len, comment.span);
ctx.diagnostic_with_fix( ctx.diagnostic_with_fix(

View file

@ -51,7 +51,7 @@ impl Rule for PreferTsExpectError {
let comments = ctx.semantic().comments(); let comments = ctx.semantic().comments();
for comment in comments { for comment in comments {
let raw = comment.content_span().source_text(ctx.source_text()); let raw = ctx.source_range(comment.content_span());
if !is_valid_ts_ignore_present(*comment, raw) { if !is_valid_ts_ignore_present(*comment, raw) {
continue; continue;

View file

@ -115,7 +115,7 @@ impl Rule for TripleSlashReference {
let mut refs_for_import = FxHashMap::default(); let mut refs_for_import = FxHashMap::default();
for comment in ctx.semantic().comments_range(0..comments_range_end) { for comment in ctx.semantic().comments_range(0..comments_range_end) {
let raw = comment.content_span().source_text(ctx.source_text()); let raw = ctx.source_range(comment.content_span());
if let Some((group1, group2)) = get_attr_key_and_value(raw) { if let Some((group1, group2)) = get_attr_key_and_value(raw) {
if (group1 == "types" && self.types == TypesOption::Never) if (group1 == "types" && self.types == TypesOption::Never)
|| (group1 == "path" && self.path == PathOption::Never) || (group1 == "path" && self.path == PathOption::Never)

View file

@ -73,7 +73,7 @@ fn has_triple_slash_directive(ctx: &LintContext<'_>) -> bool {
if !comment.is_line() { if !comment.is_line() {
continue; continue;
} }
let text = comment.content_span().source_text(ctx.source_text()); let text = ctx.source_range(comment.content_span());
if text.starts_with("///") { if text.starts_with("///") {
return true; return true;
} }

View file

@ -226,7 +226,7 @@ pub fn has_pure_notation(span: Span, ctx: &LintContext) -> bool {
let Some(comment) = ctx.semantic().comments_range(..span.start).next_back() else { let Some(comment) = ctx.semantic().comments_range(..span.start).next_back() else {
return false; return false;
}; };
let raw = comment.content_span().source_text(ctx.source_text()); let raw = ctx.source_range(comment.content_span());
raw.contains("@__PURE__") || raw.contains("#__PURE__") raw.contains("@__PURE__") || raw.contains("#__PURE__")
} }