@homer0/oxlint-config 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,265 @@
1
+ import globals from 'globals';
2
+ import { DEFAULT_IGNORES, TEST_FILES, type TestConvention } from './consts.js';
3
+ import { extensionFragments } from './fragments.js';
4
+ import {
5
+ nextjs as nextjsRules,
6
+ typeAware as typeAwareRules,
7
+ vitest as vitestRules,
8
+ } from './rules/index.js';
9
+ import type {
10
+ ConfigFragment,
11
+ ConfigName,
12
+ CreateConfigSettings,
13
+ GeneratedConfig,
14
+ GeneratedConfigOverride,
15
+ ResolvedTestConfig,
16
+ ResolveTestConfigOptions,
17
+ ResolveConfigComponentsOptions,
18
+ ResolveConfigComponentsResult,
19
+ ResolveTestConfigComponentsOptions,
20
+ CreateTestOverrideOptions,
21
+ } from './types.js';
22
+
23
+ const mergeRules = (...fragments: (ConfigFragment | undefined)[]) =>
24
+ fragments.reduce<Record<string, unknown>>((acc, fragment) => {
25
+ Object.assign(acc, fragment?.rules);
26
+ return acc;
27
+ }, {});
28
+
29
+ const getConfig = (configs: ConfigName[]): ConfigName => {
30
+ if (configs.length !== 1) {
31
+ throw new Error(
32
+ 'Exactly one environment config must be selected: "node" or "browser".',
33
+ );
34
+ }
35
+
36
+ const [config] = configs;
37
+ if (typeof config === 'undefined') {
38
+ throw new Error(
39
+ 'Exactly one environment config must be selected: "node" or "browser".',
40
+ );
41
+ }
42
+
43
+ return config;
44
+ };
45
+
46
+ const resolveTestFiles = (files: string | string[] | undefined): string[] => {
47
+ if (typeof files === 'undefined') return [];
48
+ if (Array.isArray(files)) return files.slice();
49
+ if (Object.hasOwn(TEST_FILES, files))
50
+ return TEST_FILES[files as TestConvention].slice();
51
+
52
+ return [files];
53
+ };
54
+
55
+ const resolveTestConfig = ({
56
+ tests,
57
+ productionConfig,
58
+ productionTs,
59
+ }: ResolveTestConfigOptions): ResolvedTestConfig | undefined => {
60
+ if (!tests) return undefined;
61
+
62
+ if (tests === true) {
63
+ return {
64
+ configs: [productionConfig],
65
+ files: [...TEST_FILES.colocated, ...TEST_FILES.directory],
66
+ framework: 'vitest',
67
+ ts: productionTs,
68
+ };
69
+ }
70
+
71
+ if (typeof tests === 'string') {
72
+ return {
73
+ configs: [productionConfig],
74
+ files: TEST_FILES[tests].slice(),
75
+ framework: 'vitest',
76
+ ts: productionTs,
77
+ };
78
+ }
79
+
80
+ return {
81
+ configs: tests.configs ?? [productionConfig],
82
+ files: resolveTestFiles(tests.files),
83
+ framework: tests.framework || 'vitest',
84
+ ts: tests.ts ?? productionTs,
85
+ };
86
+ };
87
+
88
+ const resolveTestConfigComponents = ({
89
+ config,
90
+ testConfig,
91
+ tests,
92
+ ts,
93
+ }: ResolveTestConfigComponentsOptions) => {
94
+ if (testConfig) {
95
+ return {
96
+ doingTestsSetup: true,
97
+ testConfig,
98
+ };
99
+ }
100
+
101
+ if (!tests) {
102
+ return {
103
+ doingTestsSetup: false,
104
+ testConfig: undefined,
105
+ };
106
+ }
107
+
108
+ return {
109
+ doingTestsSetup: false,
110
+ testConfig: resolveTestConfig({
111
+ tests,
112
+ productionConfig: config,
113
+ productionTs: ts ?? false,
114
+ }),
115
+ };
116
+ };
117
+
118
+ const resolveConfigComponents = (
119
+ options: ResolveConfigComponentsOptions,
120
+ ): ResolveConfigComponentsResult => {
121
+ const {
122
+ configs,
123
+ extensions,
124
+ jsdoc,
125
+ nextjs,
126
+ react,
127
+ ts = true,
128
+ typeAware,
129
+ baseFragment = extensionFragments.base,
130
+ baseExtensionFragment: baseExtensionFragmentOption,
131
+ } = options;
132
+
133
+ const baseExtensionFragment = baseExtensionFragmentOption ?? extensions?.base;
134
+
135
+ const config = getConfig(options.configs);
136
+
137
+ const { doingTestsSetup, testConfig } = resolveTestConfigComponents({
138
+ config,
139
+ testConfig: options.testConfig,
140
+ tests: options.tests,
141
+ ts,
142
+ });
143
+
144
+ const fragments: ConfigFragment[] = [baseFragment, extensionFragments[config]];
145
+
146
+ const plugins = ['import'];
147
+
148
+ const configGlobals: Array<Record<string, boolean | string>> = [];
149
+
150
+ if (configs.includes('node')) {
151
+ plugins.push('node');
152
+ }
153
+
154
+ if (jsdoc) {
155
+ fragments.push(extensionFragments.jsdoc);
156
+ plugins.push('jsdoc');
157
+ }
158
+
159
+ if (ts) {
160
+ fragments.push(extensionFragments.typescript);
161
+ }
162
+
163
+ if (typeAware) {
164
+ fragments.push({ rules: typeAwareRules });
165
+ }
166
+
167
+ if (ts || typeAware) {
168
+ plugins.push('typescript');
169
+ }
170
+
171
+ if (react) {
172
+ fragments.push(extensionFragments.react);
173
+ plugins.push('react', 'jsx-a11y', 'react-perf');
174
+ }
175
+
176
+ if (nextjs) {
177
+ fragments.push({ rules: nextjsRules });
178
+ plugins.push('nextjs');
179
+ }
180
+
181
+ if (testConfig?.framework === 'vitest') {
182
+ plugins.push('vitest');
183
+ if (doingTestsSetup) {
184
+ fragments.push({ rules: vitestRules });
185
+ configGlobals.push(globals.vitest);
186
+ }
187
+ }
188
+
189
+ if (baseExtensionFragment) {
190
+ fragments.push(baseExtensionFragment);
191
+ }
192
+
193
+ if (extensions?.[config]) {
194
+ fragments.push(extensions[config]);
195
+ }
196
+
197
+ if (jsdoc && extensions?.jsdoc) {
198
+ fragments.push(extensions.jsdoc);
199
+ }
200
+
201
+ if (ts && extensions?.typescript) {
202
+ fragments.push(extensions.typescript);
203
+ }
204
+
205
+ if (react && extensions?.react) {
206
+ fragments.push(extensions.react);
207
+ }
208
+
209
+ return {
210
+ config,
211
+ fragments,
212
+ plugins,
213
+ testConfig,
214
+ configGlobals:
215
+ configGlobals.length > 0 ? Object.assign({}, ...configGlobals) : undefined,
216
+ };
217
+ };
218
+
219
+ const createTestOverride = ({
220
+ testConfig,
221
+ extensions,
222
+ }: CreateTestOverrideOptions): GeneratedConfigOverride => {
223
+ const { config, fragments, configGlobals } = resolveConfigComponents({
224
+ configs: testConfig.configs,
225
+ extensions,
226
+ testConfig,
227
+ ts: testConfig.ts,
228
+ baseFragment: extensionFragments.tests,
229
+ baseExtensionFragment: extensions?.tests,
230
+ });
231
+ const rules = mergeRules(...fragments);
232
+
233
+ if (testConfig.files.length === 0) {
234
+ throw new Error('Custom test configuration requires at least one file glob.');
235
+ }
236
+
237
+ return {
238
+ files: testConfig.files,
239
+ globals: {
240
+ ...extensionFragments[config].globals,
241
+ ...configGlobals,
242
+ },
243
+ rules,
244
+ };
245
+ };
246
+
247
+ /** Creates a native Oxlint configuration from one environment and optional policy layers. */
248
+ export const createConfig = (options: CreateConfigSettings): GeneratedConfig => {
249
+ const { fragments, plugins, config, testConfig } = resolveConfigComponents(options);
250
+ const { extensions, ignores, typeAware } = options;
251
+
252
+ const rules = mergeRules(...fragments);
253
+
254
+ return {
255
+ globals: {
256
+ ...extensionFragments.base.globals,
257
+ ...extensionFragments[config].globals,
258
+ },
259
+ ignorePatterns: [...DEFAULT_IGNORES, ...(ignores || [])],
260
+ options: typeAware ? { typeAware: true } : undefined,
261
+ plugins,
262
+ rules,
263
+ overrides: testConfig ? [createTestOverride({ testConfig, extensions })] : undefined,
264
+ };
265
+ };
@@ -0,0 +1,18 @@
1
+ import { createConfig } from './createConfig.js';
2
+ import type { CreateNextjsConfigOptions } from './types.js';
3
+
4
+ const NEXTJS_IGNORES = ['.next/', 'out/', 'build/', 'next-env.d.ts'];
5
+
6
+ /** Creates a native Oxlint configuration for a Next.js TypeScript project. */
7
+ export const createNextjsConfig = ({
8
+ ignores = [],
9
+ ...options
10
+ }: CreateNextjsConfigOptions) =>
11
+ createConfig({
12
+ ...options,
13
+ configs: ['node'],
14
+ ignores: [...NEXTJS_IGNORES, ...ignores],
15
+ nextjs: true,
16
+ react: true,
17
+ ts: true,
18
+ });
@@ -0,0 +1,31 @@
1
+ import globals from 'globals';
2
+ import type { ExtensionFragmentName } from './consts.js';
3
+ import { base, browser, jsdoc, node, react, tests, typescript } from './rules/index.js';
4
+
5
+ /** Policy fragments used by the configuration factories and caller extensions. */
6
+ export const extensionFragments = {
7
+ base: {
8
+ globals: globals.es2023,
9
+ rules: base,
10
+ },
11
+ browser: {
12
+ globals: globals.browser,
13
+ rules: browser,
14
+ },
15
+ jsdoc: {
16
+ rules: jsdoc,
17
+ },
18
+ node: {
19
+ globals: globals.node,
20
+ rules: node,
21
+ },
22
+ react: {
23
+ rules: react,
24
+ },
25
+ tests: {
26
+ rules: tests,
27
+ },
28
+ typescript: {
29
+ rules: typescript,
30
+ },
31
+ } as const satisfies Record<ExtensionFragmentName, object>;
package/src/index.ts ADDED
@@ -0,0 +1,28 @@
1
+ import { createConfig } from './createConfig.js';
2
+ import type { CreateReactConfigOptions } from './types.js';
3
+
4
+ export { createConfig } from './createConfig.js';
5
+ export { createNextjsConfig } from './createNextjsConfig.js';
6
+ export { extensionFragments } from './fragments.js';
7
+ export type {
8
+ ConfigFragment,
9
+ ConfigName,
10
+ CreateConfigOptions,
11
+ CreateNextjsConfigOptions,
12
+ CreateReactConfigOptions,
13
+ ExtensionFragmentName,
14
+ ExtensionFragments,
15
+ GeneratedConfig,
16
+ GeneratedConfigOverride,
17
+ RuleSettings,
18
+ TestConfigOptions,
19
+ TestFramework,
20
+ TestsOption,
21
+ } from './types.js';
22
+
23
+ /** Creates a native Oxlint configuration with React and JSX accessibility policy. */
24
+ export const createReactConfig = (options: CreateReactConfigOptions) =>
25
+ createConfig({
26
+ ...options,
27
+ react: true,
28
+ });