chore(linter): add tests no import/no-deprecated (#2489)

This commit is contained in:
Boshen 2024-02-24 17:50:30 +08:00 committed by GitHub
parent 3e1794d6c0
commit ef5713d600
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 75 additions and 9 deletions

View file

@ -12,13 +12,14 @@ use crate::{context::LintContext, rule::Rule};
#[diagnostic(severity(warning), help(""))]
struct NoDeprecatedDiagnostic(Atom, #[label] pub Span);
/// <https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/namespace.md>
/// <https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/no-deprecated.md>
#[derive(Debug, Default, Clone)]
pub struct NoDeprecated;
declare_oxc_lint!(
/// ### What it does
/// TODO
///
/// Reports use of a deprecated name, as indicated by a JSDoc block with a @deprecated tag or TomDoc Deprecated: comment.
NoDeprecated,
nursery
);
@ -29,14 +30,74 @@ impl Rule for NoDeprecated {
#[test]
fn test() {
// use crate::tester::Tester;
use crate::tester::Tester;
// let pass = vec![];
let pass = vec![
r"import { x } from './fake'",
r"import bar from './bar'",
r"import { fine } from './deprecated'",
r"import { _undocumented } from './deprecated'",
r"import { fn } from './deprecated'",
r"import { fine } from './tomdoc-deprecated'",
r"import { _undocumented } from './tomdoc-deprecated'",
r"import * as depd from './deprecated'",
r"import * as depd from './deprecated'; console.log(depd.fine())",
r"import { deepDep } from './deep-deprecated'",
r"import { deepDep } from './deep-deprecated'; console.log(deepDep.fine())",
r"import { deepDep } from './deep-deprecated'; function x(deepDep) { console.log(deepDep.MY_TERRIBLE_ACTION) }",
r"for (let { foo, bar } of baz) {}",
r"for (let [ foo, bar ] of baz) {}",
r"const { x, y } = bar",
r"const { x, y, ...z } = bar",
r"let x; export { x }",
r"let x; export { x as y }",
r"export const x = null",
r"export var x = null",
r"export let x = null",
r"export default x",
r"export default class x {}",
r#"import json from "./data.json""#,
r#"import foo from "./foobar.json";"#,
r#"import foo from "./foobar";"#,
r#"import { foo } from "./issue-370-commonjs-namespace/bar""#,
r#"export * from "./issue-370-commonjs-namespace/bar""#,
r#"import * as a from "./commonjs-namespace/a"; a.b"#,
r#"import { foo } from "./ignore.invalid.extension""#,
// hoisting
r#"function x(deepDep) { console.log(deepDep.MY_TERRIBLE_ACTION) } import { deepDep } from "./deep-deprecated""#,
// TypeScript
r#"import * as hasDeprecated from "./ts-deprecated.ts""#,
];
// let fail = vec![];
let fail = vec![
// r#"import './malformed.js'"#,
// r#"import { fn } from './deprecated'"#,
// r#"import TerribleClass from './deprecated'"#,
// r#"import { MY_TERRIBLE_ACTION } from './deprecated'"#,
// r#"import { fn } from './deprecated'"#,
// r#"import { fn } from './tomdoc-deprecated'"#,
// r#"import TerribleClass from './tomdoc-deprecated'"#,
// r#"import { MY_TERRIBLE_ACTION } from './tomdoc-deprecated'"#,
// r#"import { MY_TERRIBLE_ACTION } from './deprecated'; function shadow(MY_TERRIBLE_ACTION) { console.log(MY_TERRIBLE_ACTION); }"#,
// r#"import { MY_TERRIBLE_ACTION, fine } from './deprecated'; console.log(fine)"#,
// r#"import { MY_TERRIBLE_ACTION } from './deprecated'; console.log(MY_TERRIBLE_ACTION)"#,
// r#"import { MY_TERRIBLE_ACTION } from './deprecated'; console.log(someOther.MY_TERRIBLE_ACTION)"#,
// r#"import { MY_TERRIBLE_ACTION } from './deprecated'; console.log(MY_TERRIBLE_ACTION.whatever())"#,
// r#"import { MY_TERRIBLE_ACTION } from './deprecated'; console.log(MY_TERRIBLE_ACTION(this, is, the, worst))"#,
// r#"import Thing from './deprecated-file'"#,
// r#"import Thing from './deprecated-file'; console.log(other.Thing)"#,
// r#"import * as depd from './deprecated'; console.log(depd.MY_TERRIBLE_ACTION)"#,
// r#"import * as deep from './deep-deprecated'; console.log(deep.deepDep.MY_TERRIBLE_ACTION)"#,
// r#"import { deepDep } from './deep-deprecated'; console.log(deepDep.MY_TERRIBLE_ACTION)"#,
// r#"import { deepDep } from './deep-deprecated'; function x(deepNDep) { console.log(deepDep.MY_TERRIBLE_ACTION) }"#,
// // hoisting
// r#"console.log(MY_TERRIBLE_ACTION); import { MY_TERRIBLE_ACTION } from "./deprecated""#,
// // TypeScript
// r#"import { foo } from "./ts-deprecated.ts"; console.log(foo())"#,
];
// Tester::new(NoDeprecated::NAME, pass, fail)
// .change_rule_path("index.js")
// .with_import_plugin(true)
// .test_and_snapshot();
Tester::new(NoDeprecated::NAME, pass, fail)
.change_rule_path("index.js")
.with_import_plugin(true)
.test_and_snapshot();
}

View file

@ -0,0 +1,5 @@
---
source: crates/oxc_linter/src/tester.rs
expression: no_deprecated
---