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

feat: add no-ts-ignore rule #1390

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions src/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ pub mod no_this_alias;
pub mod no_this_before_super;
pub mod no_throw_literal;
pub mod no_top_level_await;
pub mod no_ts_ignore;
pub mod no_undef;
pub mod no_unreachable;
pub mod no_unsafe_finally;
Expand Down Expand Up @@ -349,6 +350,7 @@ fn get_all_rules_raw() -> Vec<Box<dyn LintRule>> {
Box::new(no_this_before_super::NoThisBeforeSuper),
Box::new(no_throw_literal::NoThrowLiteral),
Box::new(no_top_level_await::NoTopLevelAwait),
Box::new(no_ts_ignore::NoTsIgnore),
Box::new(no_undef::NoUndef),
Box::new(no_unreachable::NoUnreachable),
Box::new(no_unsafe_finally::NoUnsafeFinally),
Expand Down
121 changes: 121 additions & 0 deletions src/rules/no_ts_ignore.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
use super::{Context, LintRule};
use crate::tags::Tags;
use crate::Program;
use deno_ast::swc::common::comments::CommentKind;
use deno_ast::{SourceRange, SourceRangedForSpanned};
use once_cell::sync::Lazy;
use regex::Regex;

const CODE: &str = "no-ts-ignore";

const MESSAGE: &str = "@ts-ignore is not allowed.";

const HINT: &str = "Remove @ts-ignore and check your type declaration.";

#[derive(Debug)]
pub struct NoTsIgnore;

impl NoTsIgnore {
fn report(&self, range: SourceRange, context: &mut Context) {
context.add_diagnostic_with_hint(range, CODE, MESSAGE, HINT);
}
}

impl LintRule for NoTsIgnore {
fn tags(&self) -> Tags {
&[]
}

fn code(&self) -> &'static str {
CODE
}

fn lint_program_with_ast_view(
&self,
context: &mut Context,
_program: Program,
) {
static IGNORE_REGEX: Lazy<Regex> =
Lazy::new(|| Regex::new(r"@ts-ignore(?::\s*[^\n]*|[^\n]*)?$").unwrap());

for comment in context.all_comments() {
if comment.kind != CommentKind::Line {
continue;
}

if IGNORE_REGEX.is_match(&comment.text) {
self.report(comment.range(), context);
}
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn no_ts_ignore_valid() {
assert_lint_ok! {
NoTsIgnore,
r#"/* @ts-ignore */"#,
r#"/** @ts-ignore */"#,
r#"/*
// @ts-ignore in a block
*/
"#,
};
}

#[test]
fn no_ts_ignore_invalid() {
assert_lint_err! {
NoTsIgnore,
r#"// @ts-ignore"#: [
{
col: 0,
message: MESSAGE,
hint: HINT,
}
],
r#"if (false) {
// @ts-ignore: Unreachable code error
console.log('hello');
}"#: [
{
line: 2,
message: MESSAGE,
hint: HINT,
}
],
r#"// @ts-ignore"# : [
{
col: 0,
message: MESSAGE,
hint: HINT,
},
],
r#"/// @ts-ignore"# : [
{
col: 0,
message: MESSAGE,
hint: HINT,
},
],
r#"//@ts-ignore"# : [
{
col: 0,
message: MESSAGE,
hint: HINT,
},
],
r#"// @ts-ignore "# : [
{
col: 0,
message: MESSAGE,
hint: HINT,
},
],
}
}
}
Loading