-
-
Notifications
You must be signed in to change notification settings - Fork 11.8k
Expand file tree
/
Copy pathindex.mjs
More file actions
411 lines (394 loc) · 17.8 KB
/
Copy pathindex.mjs
File metadata and controls
411 lines (394 loc) · 17.8 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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
// The React-app ESLint factory for internal Ghost frontend apps.
//
// Split out from `@internal/cfg-eslint` so a Node lib never loads the React
// plugin set. Because every plugin it needs is a static import (declared as this
// package's own dependencies), `reactAppConfig` is SYNCHRONOUS. The conditional
// options (reactRefresh, i18next, storybook, tailwind) just choose whether to
// *use* an already-loaded plugin — loading an unused plugin is cheap.
//
// Shared atoms come from the base package (`@internal/cfg-eslint`).
import {createRequire} from 'node:module';
import js from '@eslint/js';
import globals from 'globals';
import ghostPlugin from 'eslint-plugin-ghost';
import reactPlugin from 'eslint-plugin-react';
import reactHooksPlugin from 'eslint-plugin-react-hooks';
import * as tseslint from 'typescript-eslint';
import reactRefreshPluginImport from 'eslint-plugin-react-refresh';
import tailwindcssPluginV4 from 'eslint-plugin-tailwindcss';
import i18nextPluginImport from 'eslint-plugin-i18next';
import storybookPluginImport from 'eslint-plugin-storybook';
import {
jsReactAppRules,
mochaRulesOff,
shadeLayeredImportsRule,
sortImportsRule,
strictLinterOptions,
tailwindRulesV4,
tailwindRulesWithConfig,
tsReactAppRules,
viteOnlyExtras
} from '@internal/cfg-eslint';
/**
* Options for {@link reactAppConfig}.
*
* @typedef {object} ReactAppConfigOptions
* @property {boolean} [typescript=true]
* When false: vanilla JS app, no typescript-eslint extends, no
* @typescript-eslint/* rules. LEGACY for sodo-search and announcement-bar
* (should migrate to TS eventually).
* @property {boolean} [reactRefresh=true]
* When false: skip eslint-plugin-react-refresh. Set false for UMD-bundled
* apps (comments-ui, signup-form) and vanilla JS apps — react-refresh is a
* Vite-HMR rule that's meaningless without Vite's HMR runtime.
* @property {boolean} [i18next=false]
* When true: apply eslint-plugin-i18next's flat/recommended preset.
* @property {'plugin' | 'storiesBlock' | null} [storybook=null]
* - `'plugin'`: applies eslint-plugin-storybook's full flat/recommended
* ruleset (story-exports check, prefer-pascal-case, hierarchy-separator,
* no-redundant-story-name, etc.) — this is what you want for any workspace
* with a proper Storybook setup. Used by shade.
* - `'storiesBlock'`: a minimal escape hatch — adds just one rule override
* (`react-hooks/rules-of-hooks: 'off'`) scoped to `**\/*.stories.*` files,
* for workspaces that have Storybook stories but don't want the full
* storybook ruleset. Used by admin-x-design-system.
* - `null` (default): skip Storybook handling entirely.
* @property {string} [tailwindCssPath]
* Absolute path to a Tailwind v4 CSS config. Omit to skip Tailwind. Setting
* both this and `legacyTailwindV3ConfigPath` throws. NOTE: the workspace
* must also have `tailwindcss` as a (dev)Dependency — the
* eslint-plugin-tailwindcss settings-based resolver requires it locally.
* @property {string} [legacyTailwindV3ConfigPath]
* LEGACY escape hatch. Absolute path to a Tailwind v3 JS/CJS config. Used by
* comments-ui and signup-form until they migrate to v4. The migration
* involves theme token rewrites + class rewrites + CDN regression testing
* (multi-day, blocked on no owner) so the rest of the codebase isn't held up.
* @property {boolean} [shadeRestricted=false]
* When true: block barrel imports of `@tryghost/shade` (force layered subpath
* imports). Only relevant for workspaces that import shade.
* @property {boolean} [sortImports=false]
* When true: apply `ghost/sort-imports-es6-autofix/sort-imports-es6`.
* @property {boolean} [legacyJsTsSplit=false]
* LEGACY escape hatch for portal only. Portal is mid-TS-migration with both
* `.js` and `.ts` source files mixed in `src/`. When true, emits two src
* blocks: one for `src/**\/*.{js,jsx}` (vanilla rules) and one for
* `src/**\/*.{ts,tsx}` (TS rules + project: './tsconfig.json'). Remove when
* portal's JS files are fully migrated to TS. When used, pass
* `tsconfigRootDir: import.meta.dirname` so the TS block resolves
* `tsconfig.json` from the workspace, not the factory's directory.
* @property {string} [tsconfigRootDir]
* Workspace directory containing `tsconfig.json` for type-aware lint blocks.
* Only consulted when `legacyJsTsSplit: true`. Defaults to `process.cwd()`
* (works when ESLint is invoked from the workspace directory, which is the
* `pnpm --filter ... exec eslint` pattern).
* @property {string[]} [ignores=['dist/**\/*']]
* Extra ignore globs (replaces the default — pass an array including your
* defaults).
* @property {string[]} [srcGlobs]
* Override src globs. Default depends on typescript flag: TS uses
* `['src/**\/*.{js,ts,cjs,tsx}']`; vanilla JS uses `['src/**\/*.{js,jsx}']`.
* @property {string[]} [testGlobs]
* Override test globs. Default mirrors `srcGlobs` for the `test/` directory.
* Pass `false` to skip the test block entirely (some UMD apps lint a single
* src+test combined block via `srcGlobs`).
* @property {object} [extraSrcRules]
* Per-workspace rule overrides for the src block (highest precedence).
* @property {object} [extraTestRules]
* Per-workspace rule overrides for the test block.
*/
/**
* Build a flat ESLint config for a frontend React app.
*
* @param {ReactAppConfigOptions} [options]
* @returns {import('eslint').Linter.Config[]}
*
* @example
* // apps/posts/eslint.config.js — Vite TS React + Tailwind v4 + shade restriction
* import {reactAppConfig} from '@internal/cfg-eslint-react';
* export default reactAppConfig({
* tailwindCssPath: `${import.meta.dirname}/../admin/src/index.css`,
* shadeRestricted: true
* });
*
* @example
* // apps/announcement-bar/eslint.config.js — vanilla JS React
* import {reactAppConfig} from '@internal/cfg-eslint-react';
* export default reactAppConfig({
* typescript: false,
* reactRefresh: false,
* ignores: ['umd/**\/*', 'dist/**\/*']
* });
*/
const REACT_APP_PARAMS = new Set([
'typescript', 'reactRefresh', 'i18next', 'storybook', 'tailwindCssPath',
'legacyTailwindV3ConfigPath', 'shadeRestricted', 'sortImports',
'legacyJsTsSplit', 'tsconfigRootDir', 'ignores', 'srcGlobs', 'testGlobs',
'extraSrcRules', 'extraTestRules'
]);
export function reactAppConfig(options = {}) {
const unknown = Object.keys(options).filter(k => !REACT_APP_PARAMS.has(k));
if (unknown.length) {
throw new Error(`reactAppConfig: unknown options ${JSON.stringify(unknown)}. Valid keys: ${JSON.stringify([...REACT_APP_PARAMS])}`);
}
const {
typescript = true,
reactRefresh = true,
i18next = false,
storybook = null,
tailwindCssPath,
legacyTailwindV3ConfigPath,
shadeRestricted = false,
sortImports = false,
legacyJsTsSplit = false,
tsconfigRootDir,
ignores = ['dist/**/*'],
srcGlobs,
testGlobs,
extraSrcRules = {},
extraTestRules = {}
} = options;
if (tailwindCssPath && legacyTailwindV3ConfigPath) {
throw new Error('reactAppConfig: pass either tailwindCssPath (v4) or legacyTailwindV3ConfigPath (v3), not both.');
}
if (legacyJsTsSplit && !typescript) {
throw new Error('reactAppConfig: legacyJsTsSplit requires typescript: true (the TS block uses tseslint).');
}
if (legacyJsTsSplit && (srcGlobs || testGlobs !== undefined)) {
// The split branch hardcodes its file globs by extension (.js,.jsx vs
// .ts,.tsx) — if a consumer passes srcGlobs/testGlobs we'd silently
// ignore them. Throw rather than confuse.
throw new Error('reactAppConfig: legacyJsTsSplit does not honor srcGlobs/testGlobs; they would be silently dropped.');
}
if (shadeRestricted && extraSrcRules['no-restricted-imports']) {
// Setting both would silently replace the shade restriction with the
// user's rule (last-key-wins on spread). If a workspace needs both,
// add the user's paths to shadeLayeredImportsRule via a custom
// `no-restricted-imports` value that includes both shade + the
// workspace's paths. Throwing rather than silently losing the shade
// restriction (which is security-shaped).
throw new Error('reactAppConfig: shadeRestricted + extraSrcRules[\'no-restricted-imports\'] would silently override the shade restriction. Merge them in your workspace config.');
}
// Plugins are statically imported at module load. The conditionals below
// pick whether to register/use each optional one.
// typescript-eslint is always available because we use its `config()` helper
// to flatten the `extends:` key (vanilla ESLint flat config doesn't support
// `extends:` natively).
const reactRefreshPlugin = reactRefresh ? reactRefreshPluginImport : null;
// The v4 lane resolves the plugin from this package (main catalog). The
// legacy v3 lane must resolve it from the app instead: importing the root's
// 4.x plugin, whose rules take no per-rule options, would hard-fail ESLint
// schema validation on the v3-style {config} options. The v3 apps pin
// eslint-plugin-tailwindcss via catalog:tailwind3 precisely for this, and
// the tailwind config path lives in the app dir.
const tailwindcssPlugin = legacyTailwindV3ConfigPath
? createRequire(legacyTailwindV3ConfigPath)('eslint-plugin-tailwindcss')
: (tailwindCssPath ? tailwindcssPluginV4 : null);
const i18nextPlugin = i18next ? i18nextPluginImport : null;
const storybookPlugin = storybook === 'plugin' ? storybookPluginImport : null;
const reactFlat = reactPlugin.configs.flat.recommended;
const reactJsxRuntime = reactPlugin.configs.flat['jsx-runtime'];
const i18nextFlat = i18nextPlugin?.configs['flat/recommended'];
const defaultTsSrcGlobs = ['src/**/*.{js,ts,cjs,tsx}'];
const defaultJsSrcGlobs = ['src/**/*.{js,jsx}'];
const defaultTsTestGlobs = ['test/**/*.{js,ts,cjs,tsx}'];
const defaultJsTestGlobs = ['test/**/*.{js,jsx}'];
const baseLanguageOptions = {
...reactFlat.languageOptions,
ecmaVersion: 2022,
sourceType: 'module',
globals: {...globals.browser, ...globals.node}
};
const basePlugins = {
...reactFlat.plugins,
...(i18nextFlat?.plugins ?? {}),
ghost: ghostPlugin,
'react-hooks': reactHooksPlugin,
...(reactRefreshPlugin && {'react-refresh': reactRefreshPlugin}),
...(tailwindcssPlugin && {tailwindcss: tailwindcssPlugin})
};
const baseSettings = {
react: {version: 'detect'},
...(tailwindCssPath && {tailwindcss: {cssConfigPath: tailwindCssPath}})
};
const tailwindRules = tailwindCssPath
? tailwindRulesV4
: (legacyTailwindV3ConfigPath ? tailwindRulesWithConfig(legacyTailwindV3ConfigPath) : {});
// Shared rule layers for any src block (TS or JS variant overlays on top).
const baseSrcRules = {
...js.configs.recommended.rules,
...reactFlat.rules,
...(i18nextFlat?.rules ?? {}),
...reactHooksPlugin.configs.recommended.rules,
// TODO: ~46 legacy violations (~24 across Vite TS apps + 22 in
// comments-ui surfaced when react-hooks plugin was added there + 1 in
// announcement-bar). Real bug-catcher (missing useEffect/useMemo deps);
// cleanup PR will fix per-call-site and flip this to 'error'. Until
// then 'off' is intentional — the plugin's default is 'warn' which
// Ghost's stance forbids.
'react-hooks/exhaustive-deps': 'off',
...(reactRefresh ? viteOnlyExtras : {}),
...(shadeRestricted ? shadeLayeredImportsRule : {}),
...(sortImports ? sortImportsRule : {}),
...tailwindRules
};
// Build src block(s). legacyJsTsSplit produces two src blocks
// (one .js, one .ts); everything else produces one.
const srcBlocks = [];
if (legacyJsTsSplit) {
// Portal — split blocks. The validator above guarantees srcGlobs and
// testGlobs are not set when legacyJsTsSplit is true.
srcBlocks.push({
files: ['src/**/*.{js,jsx}', 'test/**/*.{js,jsx}'],
...js.configs.recommended,
languageOptions: {
...reactFlat.languageOptions,
ecmaVersion: 2022,
sourceType: 'module',
globals: {
...globals.browser,
...globals.vitest,
...globals.jest,
vi: 'readonly',
require: 'readonly'
}
},
plugins: basePlugins,
settings: baseSettings,
rules: {
...baseSrcRules,
...reactJsxRuntime.rules,
...jsReactAppRules,
...extraSrcRules
}
});
srcBlocks.push({
files: ['src/**/*.{ts,tsx}', 'test/**/*.{ts,tsx}'],
extends: [...tseslint.configs.recommended],
languageOptions: {
ecmaVersion: 2022,
sourceType: 'module',
parserOptions: {
ecmaFeatures: {jsx: true},
project: './tsconfig.json',
// BUG-FIX: was `import.meta.dirname` which resolves to the
// factory's directory (repo root), not the workspace.
tsconfigRootDir: tsconfigRootDir ?? process.cwd()
},
globals: {
...globals.browser,
...globals.vitest,
...globals.jest,
vi: 'readonly'
}
},
plugins: basePlugins,
settings: baseSettings,
rules: {
...baseSrcRules, // includes js.recommended + reactFlat + i18nextFlat + react-hooks + viteOnlyExtras + tailwindRules
...reactJsxRuntime.rules,
...tsReactAppRules, // TS branch needs TS-safe defaults (no-undef: off, no-explicit-any: error, react strict rules)
...extraSrcRules
}
});
} else if (typescript) {
srcBlocks.push({
files: srcGlobs ?? defaultTsSrcGlobs,
extends: [...tseslint.configs.recommended],
languageOptions: baseLanguageOptions,
plugins: basePlugins,
settings: baseSettings,
rules: {
...baseSrcRules,
...tsReactAppRules,
...extraSrcRules
}
});
} else {
// Vanilla JS (sodo-search, announcement-bar). LEGACY: should migrate to TS.
srcBlocks.push({
files: srcGlobs ?? defaultJsSrcGlobs,
...js.configs.recommended,
languageOptions: {
...reactFlat.languageOptions,
ecmaVersion: 2022,
sourceType: 'module',
globals: globals.browser
},
plugins: basePlugins,
settings: baseSettings,
rules: {
...baseSrcRules,
...jsReactAppRules,
...extraSrcRules
}
});
}
// Test block — skipped if testGlobs === false.
const testBlocks = [];
if (testGlobs !== false && !legacyJsTsSplit) {
const resolvedTestGlobs = testGlobs ?? (typescript ? defaultTsTestGlobs : defaultJsTestGlobs);
const testLanguageOptions = typescript
? {
ecmaVersion: 2022,
sourceType: 'module',
globals: {
...globals.browser,
...globals.node,
...globals.vitest,
vi: 'readonly'
}
}
: {
...reactFlat.languageOptions,
ecmaVersion: 2022,
sourceType: 'module',
globals: {
...globals.browser,
...globals.vitest,
...globals.jest,
vi: 'readonly'
}
};
// Test blocks need the same plugins as src because the spread rules
// (tsReactAppRules / jsReactAppRules) reference react/*, react-hooks/*,
// etc. — ESLint flat config errors if a rule references a plugin not
// registered in the same block.
testBlocks.push({
files: resolvedTestGlobs,
...(typescript ? {extends: [...tseslint.configs.recommended]} : js.configs.recommended),
languageOptions: testLanguageOptions,
plugins: basePlugins,
settings: baseSettings,
rules: {
...(typescript ? tsReactAppRules : {...js.configs.recommended.rules, ...reactFlat.rules, ...jsReactAppRules}),
...mochaRulesOff(ghostPlugin),
...extraTestRules
}
});
}
// Storybook handling (after src/test so storybook rules win for stories).
const storybookBlocks = [];
if (storybook === 'plugin') {
storybookBlocks.push(...storybookPlugin.configs['flat/recommended']);
storybookBlocks.push({
files: ['**/*.stories.{ts,tsx,js,jsx,mjs,cjs}', '**/*.story.{ts,tsx,js,jsx,mjs,cjs}'],
rules: {
'storybook/hierarchy-separator': 'error',
'storybook/no-redundant-story-name': 'error',
'storybook/prefer-pascal-case': 'error'
}
});
} else if (storybook === 'storiesBlock') {
storybookBlocks.push({
files: ['**/*.stories.{ts,tsx,js,jsx}'],
rules: {'react-hooks/rules-of-hooks': 'off'}
});
}
return tseslint.config(
{ignores},
{files: ['**/*'], ...strictLinterOptions},
...srcBlocks,
...storybookBlocks,
...testBlocks
);
}