-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.spec.ts
248 lines (230 loc) · 6.43 KB
/
index.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import * as parser from '@typescript-eslint/parser';
import { TSESLint } from '@typescript-eslint/utils';
import { readFileSync } from 'fs';
import path from 'path';
import type { Options } from '../src/rules/config';
import rule from '../src/rules/config';
const ruleTester = new TSESLint.RuleTester({
parserOptions: {
ecmaVersion: 6,
sourceType: 'module',
ecmaFeatures: {},
/**
* Project is needed to generate the parserServices
* within @typescript-eslint/parser
*/
project: './tests/fixtures/fixture-project/tsconfig.json',
warnOnUnsupportedTypeScriptVersion: false,
},
parser: require.resolve('@typescript-eslint/parser'),
});
/**
* Inline rules should be supported
*/
const tslintRulesConfig: Options = [
{
rules: {
semicolon: [true, 'always'],
},
},
];
/**
* Custom rules directories should be supported
*/
const tslintRulesDirectoryConfig: Options = [
{
rulesDirectory: ['./tests/fixtures/test-tslint-rules-directory'],
rules: {
'always-fail': {
severity: 'error',
},
},
},
];
const TEST_PROJECT_PATH = path.resolve(
__dirname,
'fixtures',
'test-project',
'tsconfig.json',
);
ruleTester.run('tslint/config', rule, {
valid: [
{
code: 'var foo = true;',
options: tslintRulesConfig,
filename: './tests/fixtures/fixture-project/1.ts',
},
{
filename: './tests/fixtures/test-project/file-spec.ts',
code: readFileSync(
'./tests/fixtures/test-project/file-spec.ts',
'utf8',
).replace(/\n/g, ' '),
parserOptions: {
project: TEST_PROJECT_PATH,
},
options: tslintRulesConfig,
},
{
code: 'throw "should be ok because rule is not loaded";',
options: tslintRulesConfig,
filename: './tests/fixtures/fixture-project/2.ts',
},
],
invalid: [
{
options: [{ lintFile: './tests/fixtures/test-project/tslint.json' }],
code: 'throw "err" // no-string-throw',
output: 'throw new Error("err") // no-string-throw',
filename: './tests/fixtures/fixture-project/3.ts',
errors: [
{
messageId: 'failure',
data: {
message:
'Throwing plain strings (not instances of Error) gives no stack traces',
ruleName: 'no-string-throw',
},
},
],
},
{
code: 'var foo = true // semicolon',
options: tslintRulesConfig,
output: 'var foo = true; // semicolon',
filename: './tests/fixtures/fixture-project/4.ts',
errors: [
{
messageId: 'failure',
data: {
message: 'Missing semicolon',
ruleName: 'semicolon',
},
line: 1,
column: 15,
},
],
},
{
code: 'var foo = true // fail',
options: tslintRulesDirectoryConfig,
output: null,
filename: './tests/fixtures/fixture-project/5.ts',
errors: [
{
messageId: 'failure',
data: {
message: 'failure',
ruleName: 'always-fail',
},
line: 1,
column: 1,
},
],
},
{
filename: './tests/fixtures/test-project/source.ts',
code: readFileSync(
'./tests/fixtures/test-project/source.ts',
'utf8',
).replace(/\n/g, ' '),
parserOptions: {
project: TEST_PROJECT_PATH,
},
output: `const a: string = 1; const sum = 1 + '2'; `,
options: [
{
rulesDirectory: [
path.join(
path.dirname(require.resolve('tslint/package.json')),
'lib',
'rules',
),
],
rules: { 'restrict-plus-operands': true },
},
],
errors: [
{
messageId: 'failure',
data: {
message:
'Operands of \'+\' operation must either be both strings or both numbers or both bigints, but found 1 + "2". Consider using template literals.',
ruleName: 'restrict-plus-operands',
},
},
],
},
],
});
if (process.env.TYPESCRIPT_ESLINT_EXPERIMENTAL_TSSERVER !== 'true') {
describe('tslint/error', () => {
function setupLinter(): TSESLint.Linter {
const linter = new TSESLint.Linter();
linter.defineRule('tslint/config', rule);
linter.defineParser('@typescript-eslint/parser', parser);
return linter;
}
it('should error on missing project', () => {
const linter = setupLinter();
expect(() =>
linter.verify('foo;', {
rules: {
'tslint/config': [2, tslintRulesConfig],
},
parser: '@typescript-eslint/parser',
}),
).toThrow(
'You have used a rule which requires parserServices to be generated. You must therefore provide a value for the "parserOptions.project" property for @typescript-eslint/parser.',
);
});
it('should error on default parser', () => {
const linter = setupLinter();
expect(() =>
linter.verify('foo;', {
parserOptions: {
project: TEST_PROJECT_PATH,
},
rules: {
'tslint/config': [2, tslintRulesConfig],
},
}),
).toThrow(
'You have used a rule which requires parserServices to be generated. You must therefore provide a value for the "parserOptions.project" property for @typescript-eslint/parser.',
);
});
it('should not crash if there are no tslint rules specified', () => {
const linter = new TSESLint.Linter();
jest.spyOn(console, 'warn').mockImplementation();
linter.defineRule('tslint/config', rule);
linter.defineParser('@typescript-eslint/parser', parser);
const filePath = path.resolve(
__dirname,
'fixtures',
'test-project',
'extra.ts',
);
expect(() =>
linter.verify(
'foo;',
{
parserOptions: {
project: TEST_PROJECT_PATH,
},
rules: {
'tslint/config': [2, {}],
},
parser: '@typescript-eslint/parser',
},
filePath,
),
).not.toThrow();
expect(console.warn).toHaveBeenCalledWith(
expect.stringContaining(
`Tried to lint ${filePath} but found no valid, enabled rules for this file type and file path in the resolved configuration.`,
),
);
jest.resetAllMocks();
});
});
}