Path: blob/main/tests/src/rules/no-deprecated.js
829 views
import { test, SYNTAX_CASES, getTSParsers } from '../utils';12import { RuleTester } from 'eslint';34const ruleTester = new RuleTester();5const rule = require('rules/no-deprecated');67ruleTester.run('no-deprecated', rule, {8valid: [9test({ code: "import { x } from './fake' " }),10test({ code: "import bar from './bar'" }),1112test({ code: "import { fine } from './deprecated'" }),13test({ code: "import { _undocumented } from './deprecated'" }),1415test({16code: "import { fn } from './deprecated'",17settings: { 'import/docstyle': ['tomdoc'] },18}),1920test({21code: "import { fine } from './tomdoc-deprecated'",22settings: { 'import/docstyle': ['tomdoc'] },23}),24test({25code: "import { _undocumented } from './tomdoc-deprecated'",26settings: { 'import/docstyle': ['tomdoc'] },27}),2829// naked namespace is fine30test({ code: "import * as depd from './deprecated'" }),31test({ code: "import * as depd from './deprecated'; console.log(depd.fine())" }),32test({ code: "import { deepDep } from './deep-deprecated'" }),33test({ code: "import { deepDep } from './deep-deprecated'; console.log(deepDep.fine())" }),3435// redefined36test({37code: "import { deepDep } from './deep-deprecated'; function x(deepDep) { console.log(deepDep.MY_TERRIBLE_ACTION) }",38}),394041...SYNTAX_CASES,42],43invalid: [4445// reports on parse errors even without specifiers46test({ code: "import './malformed.js'", errors: 1 }),4748test({49code: "import { fn } from './deprecated'",50errors: ["Deprecated: please use 'x' instead."],51}),5253test({54code: "import TerribleClass from './deprecated'",55errors: ['Deprecated: this is awful, use NotAsBadClass.'],56}),5758test({59code: "import { MY_TERRIBLE_ACTION } from './deprecated'",60errors: ['Deprecated: please stop sending/handling this action type.'],61}),6263test({64code: "import { fn } from './deprecated'",65settings: { 'import/docstyle': ['jsdoc', 'tomdoc'] },66errors: ["Deprecated: please use 'x' instead."],67}),6869test({70code: "import { fn } from './tomdoc-deprecated'",71settings: { 'import/docstyle': ['tomdoc'] },72errors: ['Deprecated: This function is terrible.'],73}),7475test({76code: "import TerribleClass from './tomdoc-deprecated'",77settings: { 'import/docstyle': ['tomdoc'] },78errors: ['Deprecated: this is awful, use NotAsBadClass.'],79}),8081test({82code: "import { MY_TERRIBLE_ACTION } from './tomdoc-deprecated'",83settings: { 'import/docstyle': ['tomdoc'] },84errors: ['Deprecated: Please stop sending/handling this action type.'],85}),8687// ignore redeclares88test({89code: "import { MY_TERRIBLE_ACTION } from './deprecated'; function shadow(MY_TERRIBLE_ACTION) { console.log(MY_TERRIBLE_ACTION); }",90errors: ['Deprecated: please stop sending/handling this action type.'],91}),9293// ignore non-deprecateds94test({95code: "import { MY_TERRIBLE_ACTION, fine } from './deprecated'; console.log(fine)",96errors: ['Deprecated: please stop sending/handling this action type.'],97}),9899// reflag on subsequent usages100test({101code: "import { MY_TERRIBLE_ACTION } from './deprecated'; console.log(MY_TERRIBLE_ACTION)",102errors: [103{ type: 'ImportSpecifier', message: 'Deprecated: please stop sending/handling this action type.' },104{ type: 'Identifier', message: 'Deprecated: please stop sending/handling this action type.' },105],106}),107108// don't flag other members109test({110code: "import { MY_TERRIBLE_ACTION } from './deprecated'; console.log(someOther.MY_TERRIBLE_ACTION)",111errors: [112{ type: 'ImportSpecifier', message: 'Deprecated: please stop sending/handling this action type.' },113],114}),115116// flag it even with members117test({118code: "import { MY_TERRIBLE_ACTION } from './deprecated'; console.log(MY_TERRIBLE_ACTION.whatever())",119errors: [120{ type: 'ImportSpecifier', message: 'Deprecated: please stop sending/handling this action type.' },121{ type: 'Identifier', message: 'Deprecated: please stop sending/handling this action type.' },122],123}),124125// works for function calls too126test({127code: "import { MY_TERRIBLE_ACTION } from './deprecated'; console.log(MY_TERRIBLE_ACTION(this, is, the, worst))",128errors: [129{ type: 'ImportSpecifier', message: 'Deprecated: please stop sending/handling this action type.' },130{ type: 'Identifier', message: 'Deprecated: please stop sending/handling this action type.' },131],132}),133134// deprecated full module135test({136code: "import Thing from './deprecated-file'",137errors: [138{ type: 'ImportDeclaration', message: 'Deprecated: this module is the worst.' },139],140}),141142// don't flag as part of other member expressions143test({144code: "import Thing from './deprecated-file'; console.log(other.Thing)",145errors: [146{ type: 'ImportDeclaration', message: 'Deprecated: this module is the worst.' },147],148}),149150// namespace following151test({152code: "import * as depd from './deprecated'; console.log(depd.MY_TERRIBLE_ACTION)",153errors: [154{ type: 'Identifier', message: 'Deprecated: please stop sending/handling this action type.' },155],156}),157test({158code: "import * as deep from './deep-deprecated'; console.log(deep.deepDep.MY_TERRIBLE_ACTION)",159errors: [160{ type: 'Identifier', message: 'Deprecated: please stop sending/handling this action type.' },161],162}),163test({164code: "import { deepDep } from './deep-deprecated'; console.log(deepDep.MY_TERRIBLE_ACTION)",165errors: [166{ type: 'Identifier', message: 'Deprecated: please stop sending/handling this action type.' },167],168}),169test({170code: "import { deepDep } from './deep-deprecated'; function x(deepNDep) { console.log(deepDep.MY_TERRIBLE_ACTION) }",171errors: [172{ type: 'Identifier', message: 'Deprecated: please stop sending/handling this action type.' },173],174}),175],176});177178ruleTester.run('no-deprecated: hoisting', rule, {179valid: [180181test({182code: "function x(deepDep) { console.log(deepDep.MY_TERRIBLE_ACTION) } import { deepDep } from './deep-deprecated'",183}),184185],186187invalid: [188189test({190code: "console.log(MY_TERRIBLE_ACTION); import { MY_TERRIBLE_ACTION } from './deprecated'",191errors: [192{ type: 'Identifier', message: 'Deprecated: please stop sending/handling this action type.' },193{ type: 'ImportSpecifier', message: 'Deprecated: please stop sending/handling this action type.' },194],195}),196197],198});199200describe('TypeScript', function () {201getTSParsers().forEach((parser) => {202const parserConfig = {203parser,204settings: {205'import/parsers': { [parser]: ['.ts'] },206'import/resolver': { 'eslint-import-resolver-typescript': true },207},208};209210ruleTester.run(parser, rule, {211valid: [212test(Object.assign({213code: 'import * as hasDeprecated from \'./ts-deprecated.ts\'',214}, parserConfig)),215],216invalid: [217test(Object.assign({218code: 'import { foo } from \'./ts-deprecated.ts\'; console.log(foo())',219errors: [220{ type: 'ImportSpecifier', message: 'Deprecated: don\'t use this!' },221{ type: 'Identifier', message: 'Deprecated: don\'t use this!' },222] },223parserConfig)),224],225});226});227});228229230