-
Notifications
You must be signed in to change notification settings - Fork 2
/
configs.spec.ts
241 lines (207 loc) · 6.67 KB
/
configs.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
import ESLint from 'eslint';
import * as fs from 'fs';
import semver from 'semver/preload';
import packageJson from '../package.json';
const configFiles = fs
.readdirSync('.', { withFileTypes: true })
.filter(
value =>
value.isFile() &&
value.name.endsWith('.js') &&
value.name !== '.eslintrc.js'
)
.map(value => value.name);
/**
* Determines the canonical package name for the given eslint `plugin`,
* that can be used to install the plugin using a package manager.
*
* Generally this is done by returning the plugin name with `eslint-plugin-`
* appended to it (if the name does not already start with that string).
*
* Scoped plugins must be explicitly checked for to handle properly;
* Currently the `@typescript-eslint` is the only supported scoped plugin.
*
* @param {string} plugin
*
* @return {string}
*/
const determinePluginPackageName = (plugin: string): string => {
if (plugin.startsWith('eslint-plugin-')) {
return plugin;
}
if (plugin === '@typescript-eslint') {
return `${plugin}/eslint-plugin`;
}
return `eslint-plugin-${plugin}`;
};
const requireConfig = (
config: string
): ESLint.Linter.Config &
Required<Pick<ESLint.Linter.Config, 'plugins' | 'extends' | 'rules'>> => ({
plugins: [],
extends: [],
rules: {},
// eslint-disable-next-line node/global-require,@typescript-eslint/no-require-imports,@typescript-eslint/no-var-requires
...(require(config) as ESLint.Linter.Config)
});
// todo: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/56545
declare module 'eslint' {
// eslint-disable-next-line @typescript-eslint/no-shadow
namespace ESLint {
interface LintResult {
fatalErrorCount: number;
}
}
}
describe('package.json', () => {
it('includes every config file', () => {
expect.hasAssertions();
expect(packageJson.files).toStrictEqual(
expect.arrayContaining(configFiles)
);
});
describe('peer dependencies', () => {
it('includes eslint and prettier as required peer dependencies', () => {
expect.hasAssertions();
expect(Object.keys(packageJson.peerDependencies)).toContain('eslint');
expect(Object.keys(packageJson.peerDependencies)).toContain('prettier');
expect(packageJson.peerDependenciesMeta).toStrictEqual(
expect.not.objectContaining({
eslint: { optional: true },
prettier: { optional: true }
})
);
});
it.each(Object.entries(packageJson.peerDependencies))(
'the constraint for "%s" intersects with the dev constraint',
(name, peerConstraint) => {
expect.hasAssertions();
const devConstraint =
packageJson.devDependencies[
name as keyof typeof packageJson.devDependencies
];
expect(semver.intersects(peerConstraint, devConstraint)).toBe(true);
}
);
});
});
const makeEnabledRulesWarn = (
value: ESLint.Linter.RuleEntry
): ESLint.Linter.RuleEntry => {
if (Array.isArray(value)) {
return [
value[0] !== 'off' ? 'warn' : 'off',
...(value.slice(1) as unknown[])
];
}
return value !== 'off' ? 'warn' : 'off';
};
describe('for each config file', () => {
describe.each(configFiles)('%s config', configFile => {
const config = requireConfig(`./../${configFile}`);
it('is valid', async () => {
expect.hasAssertions();
const baseConfig: ESLint.Linter.Config = {
// default to using the @typescript-eslint/parser in case we have any
// rules that can use the type services, like `jest/unbound-method`
parser: '@typescript-eslint/parser',
...config,
parserOptions: {
// @babel/eslint-parser
requireConfigFile: false,
// @typescript-eslint/parser
project: 'tsconfig.json',
createDefaultProgram: false,
ecmaVersion: 2019,
sourceType: 'module'
},
// make all enabled rules warn, since misconfigured rules will create errors
rules: Object.fromEntries(
Object.entries(config.rules).map(([name, value = 'warn']) => [
name,
makeEnabledRulesWarn(value)
])
)
};
const linter = new ESLint.ESLint({
useEslintrc: false,
baseConfig
});
await expect(
linter.lintText('', { filePath: './test/configs.spec.ts' })
).resolves.toStrictEqual([
expect.objectContaining<Partial<ESLint.ESLint.LintResult>>({
errorCount: 0,
fatalErrorCount: 0
})
]);
});
it('lists any plugins as peer dependencies', () => {
expect.hasAssertions();
expect(Object.keys(packageJson.peerDependencies)).toStrictEqual(
expect.arrayContaining(
config.plugins.map(plugin => determinePluginPackageName(plugin))
)
);
});
if (config.parser) {
it('lists its parser as a peer dependency', () => {
expect.hasAssertions();
expect(Object.keys(packageJson.peerDependencies)).toContain(
config.parser
);
});
}
if (configFile !== 'index.js') {
it('lists any plugins as optional peer dependencies', () => {
expect.hasAssertions();
expect(packageJson.peerDependenciesMeta).toStrictEqual(
expect.objectContaining(
Object.fromEntries(
config.plugins
.filter(plugin => plugin !== 'prettier')
.map(plugin => [
determinePluginPackageName(plugin),
{ optional: true }
])
)
)
);
});
if (config.parser) {
it('lists its parser as an optional peer dependency', () => {
expect.hasAssertions();
expect(Object.keys(packageJson.peerDependencies)).toContain(
config.parser
);
expect(packageJson.peerDependenciesMeta).toHaveProperty(
config.parser as string,
{ optional: true }
);
});
}
}
if (configFile === 'index.js') {
it('lists any plugins as required peer dependencies', () => {
expect.hasAssertions();
expect(packageJson.peerDependenciesMeta).toStrictEqual(
expect.not.objectContaining(
Object.fromEntries(
config.plugins.map(plugin => [
determinePluginPackageName(plugin),
{ optional: true }
])
)
)
);
});
}
if (configFile !== 'jest.js') {
it('should include prettier', () => {
expect.hasAssertions();
expect(config.plugins).toContainEqual('prettier');
expect(config.extends).toContainEqual('plugin:prettier/recommended');
});
}
});
});