-
-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathindex.js
More file actions
299 lines (275 loc) · 7.29 KB
/
index.js
File metadata and controls
299 lines (275 loc) · 7.29 KB
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import globals from 'globals';
import confusingBrowserGlobals from 'confusing-browser-globals';
import stylistic from '@stylistic/eslint-plugin';
import css from '@eslint/css'; // eslint-disable-line no-unused-vars
import pluginUnicorn from 'eslint-plugin-unicorn';
import pluginImport, {createNodeResolver} from 'eslint-plugin-import-x';
import pluginN from 'eslint-plugin-n';
import pluginComments from '@eslint-community/eslint-plugin-eslint-comments';
/// import pluginPromise from 'eslint-plugin-promise';
import pluginAva from 'eslint-plugin-ava';
import {fixupPluginRules} from '@eslint/compat';
import {javascriptRules} from './source/javascript-rules.js';
import {pluginsRules} from './source/plugins-rules.js';
import {jsonConfig, json5Config, jsoncConfig} from './source/json.js';
import {getHtmlConfig} from './source/html.js';
import {getMarkdownConfig} from './source/markdown.js';
import {getRegexpConfig} from './source/regexp.js';
import {getJsdocConfigs} from './source/jsdoc.js';
import noUseExtendNativeRule from './source/rules/no-use-extend-native.js';
// Dynamically import TypeScript-related packages so that `typescript` is not
// required when users don't have it installed (JavaScript-only projects).
let ts;
try {
ts = await import('./source/typescript.js');
} catch (error) {
if (!isMissingTypeScriptError(error)) {
throw error;
}
}
export const tsExtensions = [
'ts',
'tsx',
'mts',
'cts',
];
export const jsExtensions = [
'js',
'jsx',
'mjs',
'cjs',
];
export const frameworkExtensions = [
'vue',
'svelte',
'astro',
];
export const htmlExtensions = [
'html',
];
export const mdExtensions = [
'md',
];
export const allExtensions = [
...jsExtensions,
...tsExtensions,
...frameworkExtensions,
...htmlExtensions,
...mdExtensions,
];
const baseExtensions = [
...jsExtensions,
...frameworkExtensions,
];
export const jsFilesGlob = `**/*.{${jsExtensions.join(',')}}`;
export const tsFilesGlob = `**/*.{${tsExtensions.join(',')}}`;
export const allFilesGlob = `**/*.{${allExtensions.join(',')}}`;
export const typescriptParser = ts?.parser;
export const defaultIgnores = [
'**/node_modules/**',
'**/bower_components/**',
'flow-typed/**',
'coverage/**',
'{tmp,temp}/**',
'**/*.min.js',
'vendor/**',
'dist/**',
'tap-snapshots/*.{cjs,js}',
];
const pluginNoUseExtendNative = {
rules: {
'no-use-extend-native': noUseExtendNativeRule,
},
};
const missingTypeScriptParser = {
parse() {
throw new Error('Install `typescript` to lint TypeScript files with eslint-config-xo.');
},
};
function isMissingTypeScriptError(error) {
return error instanceof Error
&& (error.code === 'ERR_MODULE_NOT_FOUND' || error.code === 'MODULE_NOT_FOUND')
&& /'typescript'/v.test(error.message);
}
function getOptionRules({
space = false,
semicolon = true,
typescript = false,
} = {}) {
const rules = {};
if (space) {
const spaces = typeof space === 'number' ? space : 2;
rules['@stylistic/indent'] = ['error', spaces, {SwitchCase: 1}];
rules['@stylistic/indent-binary-ops'] = ['error', spaces];
} else if (space === false) {
rules['@stylistic/indent'] = ['error', 'tab', {SwitchCase: 1}];
rules['@stylistic/indent-binary-ops'] = ['error', 'tab'];
}
if (semicolon === false) {
rules['@stylistic/semi'] = ['error', 'never'];
rules['@stylistic/semi-spacing'] = ['error', {before: false, after: true}];
if (typescript) {
rules['@stylistic/member-delimiter-style'] = [
'error',
{
multiline: {delimiter: 'none'},
singleline: {delimiter: 'comma', requireLast: false},
},
];
}
}
return rules;
}
export default function eslintConfigXo({
browser = false,
space = false,
semicolon = true,
} = {}) {
const lintedExtensions = ts ? [...baseExtensions, ...tsExtensions] : baseExtensions;
const config = {
name: 'xo/base',
languageOptions: {
globals: {
...globals.es2021,
...(browser ? globals.browser : globals.node),
},
ecmaVersion: 'latest',
sourceType: 'module',
parserOptions: {
ecmaFeatures: {
jsx: true,
},
},
},
linterOptions: {
reportUnusedDisableDirectives: 'error',
reportUnusedInlineConfigs: 'error',
},
plugins: {
'@stylistic': stylistic,
...(ts ? {'@typescript-eslint': ts.plugin} : {}),
unicorn: pluginUnicorn,
'import-x': pluginImport,
'@eslint-community/eslint-comments': pluginComments,
'no-use-extend-native': pluginNoUseExtendNative,
ava: pluginAva,
// TODO: Remove `fixupPluginRules` wrapping when this plugin supports ESLint 10 natively.
n: fixupPluginRules(pluginN),
/// promise: fixupPluginRules(pluginPromise),
},
files: [
`**/*.{${lintedExtensions.join(',')}}`,
],
settings: {
'import-x/extensions': lintedExtensions,
'import-x/core-modules': [
'electron',
'atom',
],
'import-x/parsers': {
espree: jsExtensions,
...(ts ? {'@typescript-eslint/parser': tsExtensions} : {}),
},
'import-x/resolver-next': [
createNodeResolver(),
...(ts ? [ts.createTypeScriptImportResolver()] : []),
],
},
rules: {
...pluginsRules,
...javascriptRules,
'no-restricted-globals': browser
? ['error', ...confusingBrowserGlobals]
: [
'error',
{
globals: [
'event',
// TODO: Enable this in 2028.
// {
// name: 'Buffer',
// message: 'Use Uint8Array instead. See: https://sindresorhus.com/blog/goodbye-nodejs-buffer',
// },
{
name: 'atob',
message: 'This API is deprecated. Use https://github.com/sindresorhus/uint8array-extras instead.',
},
{
name: 'btoa',
message: 'This API is deprecated. Use https://github.com/sindresorhus/uint8array-extras instead.',
},
],
checkGlobalObject: true,
},
],
...getOptionRules({space, semicolon}),
},
};
const typescriptConfigs = ts?.getConfigs({
optionRules: getOptionRules({space, semicolon, typescript: true}),
tsExtensions,
}) ?? [];
const missingTypeScriptConfig = [];
if (!ts) {
missingTypeScriptConfig.push({
name: 'xo/missing-typescript',
files: [
tsFilesGlob,
],
ignores: [
'**/*.d.ts',
'**/*.d.mts',
'**/*.d.cts',
],
languageOptions: {
parser: missingTypeScriptParser,
},
});
}
return [
{
ignores: defaultIgnores,
},
...pluginAva.configs.recommended,
config,
jsonConfig,
json5Config,
jsoncConfig,
getRegexpConfig({files: [`**/*.{${lintedExtensions.join(',')}}`]}),
...getJsdocConfigs({
files: [`**/*.{${lintedExtensions.join(',')}}`],
tsFiles: ts ? [tsFilesGlob] : undefined,
}),
getHtmlConfig({space}),
getMarkdownConfig(),
...missingTypeScriptConfig,
// Disabled for now until it becomes more stable.
// {
// plugins: {
// css,
// },
// files: [
// '**/*.css',
// ],
// language: 'css/css',
// rules: {
// 'css/font-family-fallbacks': 'error',
// 'css/no-duplicate-imports': 'error',
// 'css/no-duplicate-keyframe-selectors': 'error',
// 'css/no-empty-blocks': 'error',
// 'css/no-invalid-at-rule-placement': 'error',
// 'css/no-invalid-at-rules': 'error',
// 'css/no-invalid-named-grid-areas': 'error',
// 'css/no-invalid-properties': 'error',
// 'css/no-unmatchable-selectors': 'error',
// },
// },
...typescriptConfigs,
{
files: ['xo.config.{js,ts}'],
rules: {
'import-x/no-anonymous-default-export': 'off',
},
},
];
}