Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epidemian
GitHub Repository: epidemian/eslint-plugin-import
Path: blob/main/tests/src/rules/no-deprecated.js
829 views
1
import { test, SYNTAX_CASES, getTSParsers } from '../utils';
2
3
import { RuleTester } from 'eslint';
4
5
const ruleTester = new RuleTester();
6
const rule = require('rules/no-deprecated');
7
8
ruleTester.run('no-deprecated', rule, {
9
valid: [
10
test({ code: "import { x } from './fake' " }),
11
test({ code: "import bar from './bar'" }),
12
13
test({ code: "import { fine } from './deprecated'" }),
14
test({ code: "import { _undocumented } from './deprecated'" }),
15
16
test({
17
code: "import { fn } from './deprecated'",
18
settings: { 'import/docstyle': ['tomdoc'] },
19
}),
20
21
test({
22
code: "import { fine } from './tomdoc-deprecated'",
23
settings: { 'import/docstyle': ['tomdoc'] },
24
}),
25
test({
26
code: "import { _undocumented } from './tomdoc-deprecated'",
27
settings: { 'import/docstyle': ['tomdoc'] },
28
}),
29
30
// naked namespace is fine
31
test({ code: "import * as depd from './deprecated'" }),
32
test({ code: "import * as depd from './deprecated'; console.log(depd.fine())" }),
33
test({ code: "import { deepDep } from './deep-deprecated'" }),
34
test({ code: "import { deepDep } from './deep-deprecated'; console.log(deepDep.fine())" }),
35
36
// redefined
37
test({
38
code: "import { deepDep } from './deep-deprecated'; function x(deepDep) { console.log(deepDep.MY_TERRIBLE_ACTION) }",
39
}),
40
41
42
...SYNTAX_CASES,
43
],
44
invalid: [
45
46
// reports on parse errors even without specifiers
47
test({ code: "import './malformed.js'", errors: 1 }),
48
49
test({
50
code: "import { fn } from './deprecated'",
51
errors: ["Deprecated: please use 'x' instead."],
52
}),
53
54
test({
55
code: "import TerribleClass from './deprecated'",
56
errors: ['Deprecated: this is awful, use NotAsBadClass.'],
57
}),
58
59
test({
60
code: "import { MY_TERRIBLE_ACTION } from './deprecated'",
61
errors: ['Deprecated: please stop sending/handling this action type.'],
62
}),
63
64
test({
65
code: "import { fn } from './deprecated'",
66
settings: { 'import/docstyle': ['jsdoc', 'tomdoc'] },
67
errors: ["Deprecated: please use 'x' instead."],
68
}),
69
70
test({
71
code: "import { fn } from './tomdoc-deprecated'",
72
settings: { 'import/docstyle': ['tomdoc'] },
73
errors: ['Deprecated: This function is terrible.'],
74
}),
75
76
test({
77
code: "import TerribleClass from './tomdoc-deprecated'",
78
settings: { 'import/docstyle': ['tomdoc'] },
79
errors: ['Deprecated: this is awful, use NotAsBadClass.'],
80
}),
81
82
test({
83
code: "import { MY_TERRIBLE_ACTION } from './tomdoc-deprecated'",
84
settings: { 'import/docstyle': ['tomdoc'] },
85
errors: ['Deprecated: Please stop sending/handling this action type.'],
86
}),
87
88
// ignore redeclares
89
test({
90
code: "import { MY_TERRIBLE_ACTION } from './deprecated'; function shadow(MY_TERRIBLE_ACTION) { console.log(MY_TERRIBLE_ACTION); }",
91
errors: ['Deprecated: please stop sending/handling this action type.'],
92
}),
93
94
// ignore non-deprecateds
95
test({
96
code: "import { MY_TERRIBLE_ACTION, fine } from './deprecated'; console.log(fine)",
97
errors: ['Deprecated: please stop sending/handling this action type.'],
98
}),
99
100
// reflag on subsequent usages
101
test({
102
code: "import { MY_TERRIBLE_ACTION } from './deprecated'; console.log(MY_TERRIBLE_ACTION)",
103
errors: [
104
{ type: 'ImportSpecifier', message: 'Deprecated: please stop sending/handling this action type.' },
105
{ type: 'Identifier', message: 'Deprecated: please stop sending/handling this action type.' },
106
],
107
}),
108
109
// don't flag other members
110
test({
111
code: "import { MY_TERRIBLE_ACTION } from './deprecated'; console.log(someOther.MY_TERRIBLE_ACTION)",
112
errors: [
113
{ type: 'ImportSpecifier', message: 'Deprecated: please stop sending/handling this action type.' },
114
],
115
}),
116
117
// flag it even with members
118
test({
119
code: "import { MY_TERRIBLE_ACTION } from './deprecated'; console.log(MY_TERRIBLE_ACTION.whatever())",
120
errors: [
121
{ type: 'ImportSpecifier', message: 'Deprecated: please stop sending/handling this action type.' },
122
{ type: 'Identifier', message: 'Deprecated: please stop sending/handling this action type.' },
123
],
124
}),
125
126
// works for function calls too
127
test({
128
code: "import { MY_TERRIBLE_ACTION } from './deprecated'; console.log(MY_TERRIBLE_ACTION(this, is, the, worst))",
129
errors: [
130
{ type: 'ImportSpecifier', message: 'Deprecated: please stop sending/handling this action type.' },
131
{ type: 'Identifier', message: 'Deprecated: please stop sending/handling this action type.' },
132
],
133
}),
134
135
// deprecated full module
136
test({
137
code: "import Thing from './deprecated-file'",
138
errors: [
139
{ type: 'ImportDeclaration', message: 'Deprecated: this module is the worst.' },
140
],
141
}),
142
143
// don't flag as part of other member expressions
144
test({
145
code: "import Thing from './deprecated-file'; console.log(other.Thing)",
146
errors: [
147
{ type: 'ImportDeclaration', message: 'Deprecated: this module is the worst.' },
148
],
149
}),
150
151
// namespace following
152
test({
153
code: "import * as depd from './deprecated'; console.log(depd.MY_TERRIBLE_ACTION)",
154
errors: [
155
{ type: 'Identifier', message: 'Deprecated: please stop sending/handling this action type.' },
156
],
157
}),
158
test({
159
code: "import * as deep from './deep-deprecated'; console.log(deep.deepDep.MY_TERRIBLE_ACTION)",
160
errors: [
161
{ type: 'Identifier', message: 'Deprecated: please stop sending/handling this action type.' },
162
],
163
}),
164
test({
165
code: "import { deepDep } from './deep-deprecated'; console.log(deepDep.MY_TERRIBLE_ACTION)",
166
errors: [
167
{ type: 'Identifier', message: 'Deprecated: please stop sending/handling this action type.' },
168
],
169
}),
170
test({
171
code: "import { deepDep } from './deep-deprecated'; function x(deepNDep) { console.log(deepDep.MY_TERRIBLE_ACTION) }",
172
errors: [
173
{ type: 'Identifier', message: 'Deprecated: please stop sending/handling this action type.' },
174
],
175
}),
176
],
177
});
178
179
ruleTester.run('no-deprecated: hoisting', rule, {
180
valid: [
181
182
test({
183
code: "function x(deepDep) { console.log(deepDep.MY_TERRIBLE_ACTION) } import { deepDep } from './deep-deprecated'",
184
}),
185
186
],
187
188
invalid: [
189
190
test({
191
code: "console.log(MY_TERRIBLE_ACTION); import { MY_TERRIBLE_ACTION } from './deprecated'",
192
errors: [
193
{ type: 'Identifier', message: 'Deprecated: please stop sending/handling this action type.' },
194
{ type: 'ImportSpecifier', message: 'Deprecated: please stop sending/handling this action type.' },
195
],
196
}),
197
198
],
199
});
200
201
describe('TypeScript', function () {
202
getTSParsers().forEach((parser) => {
203
const parserConfig = {
204
parser,
205
settings: {
206
'import/parsers': { [parser]: ['.ts'] },
207
'import/resolver': { 'eslint-import-resolver-typescript': true },
208
},
209
};
210
211
ruleTester.run(parser, rule, {
212
valid: [
213
test(Object.assign({
214
code: 'import * as hasDeprecated from \'./ts-deprecated.ts\'',
215
}, parserConfig)),
216
],
217
invalid: [
218
test(Object.assign({
219
code: 'import { foo } from \'./ts-deprecated.ts\'; console.log(foo())',
220
errors: [
221
{ type: 'ImportSpecifier', message: 'Deprecated: don\'t use this!' },
222
{ type: 'Identifier', message: 'Deprecated: don\'t use this!' },
223
] },
224
parserConfig)),
225
],
226
});
227
});
228
});
229
230