Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(linter): false positive in typescript/ban-tslint-comment #8094

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions crates/oxc_linter/src/rules/typescript/ban_tslint_comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,10 @@ declare_oxc_lint!(
impl Rule for BanTslintComment {
fn run_once(&self, ctx: &LintContext) {
let comments = ctx.semantic().comments();
let source_text_len = ctx.semantic().source_text().len();
for comment in comments {
let raw = ctx.source_range(comment.content_span());
if is_tslint_comment_directive(raw) {
let comment_span = get_full_comment(source_text_len, comment.span);
let comment_span = get_full_comment(ctx, comment.span);
ctx.diagnostic_with_fix(
ban_tslint_comment_diagnostic(raw.trim(), comment_span),
|fixer| fixer.delete_range(comment_span),
Expand All @@ -57,12 +56,17 @@ fn is_tslint_comment_directive(raw: &str) -> bool {
ENABLE_DISABLE_REGEX.is_match(raw)
}

fn get_full_comment(source_text_len: usize, span: Span) -> Span {
fn get_full_comment(ctx: &LintContext, span: Span) -> Span {
let mut span = span;

let source_size = u32::try_from(ctx.source_text().len()).unwrap();
let tokens = ctx.source_range(Span::new(span.end, source_size));

// Take into account new line at the end of the comment
if source_text_len > span.end as usize {
if matches!(tokens.chars().next(), Some('\n')) {
span.end += 1;
}

span
}

Expand Down Expand Up @@ -111,6 +115,8 @@ fn test() {
None,
),
(r"/* tslint:disable-line */", r"", None),
// Issue: <https://github.com/oxc-project/oxc/issues/8090>
(r"/*tslint:disable*/É", r"É", None),
];

Tester::new(BanTslintComment::NAME, BanTslintComment::CATEGORY, pass, fail)
Expand Down
Loading