feat: better type help, esp. with makeEslintConfig

This commit is contained in:
Christopher Willis-Ford 2025-04-02 10:34:53 -07:00
parent 7d7b277676
commit 497bd2880b
6 changed files with 33 additions and 11 deletions

View file

@ -100,21 +100,22 @@ export default makeEslintConfig({
### Further Customization ### Further Customization
The return value of the `makeEslintConfig` function is a standard ESLint configuration array. This means you can The first parameter to `makeEslintConfig` is covered above. Any further parameters passed to `makeEslintConfig` are
customize your configuration further like this: appended to the resulting ESLint configuration array. This means you can customize your configuration further like
this:
```mjs ```mjs
// myProjectRoot/eslint.config.mjs // myProjectRoot/eslint.config.mjs
import { makeEslintConfig } from 'eslint-config-scratch' import { makeEslintConfig } from 'eslint-config-scratch'
export default [ export default makeEslintConfig(
...makeEslintConfig({ {
// Optional: enables rules that use type info, some of which work in JS too // Optional: enables rules that use type info, some of which work in JS too
tsconfigRootDir: import.meta.dirname, tsconfigRootDir: import.meta.dirname,
// Optional: specify global variables available in your environment // Optional: specify global variables available in your environment
globals: 'browser', globals: 'browser',
}), },
// Add custom rules or overrides here // Add custom rules or overrides here
{ {
files: ['*.test.js'], files: ['*.test.js'],
@ -122,9 +123,12 @@ export default [
'no-console': 'off', // Allow console logs in test files 'no-console': 'off', // Allow console logs in test files
}, },
}, },
] )
``` ```
You could concatenate more configuration objects onto the array returned by `makeEslintConfig` with equivalent
results, but this approach offers better editor hints for autocomplete and type checking.
All ESLint configuration options are available this way. You can use this to handle globals yourself if the simplified All ESLint configuration options are available this way. You can use this to handle globals yourself if the simplified
`globals` configuration from above doesn't meet your needs: `globals` configuration from above doesn't meet your needs:
@ -133,11 +137,11 @@ All ESLint configuration options are available this way. You can use this to han
import { makeEslintConfig } from 'eslint-config-scratch' import { makeEslintConfig } from 'eslint-config-scratch'
import globals from 'globals' import globals from 'globals'
export default [ export default makeEslintConfig(
...makeEslintConfig({ {
// Optional: enables rules that use type info, some of which work in JS too // Optional: enables rules that use type info, some of which work in JS too
tsconfigRootDir: import.meta.dirname, tsconfigRootDir: import.meta.dirname,
}), },
{ {
files: ['src/main/**.js'], files: ['src/main/**.js'],
languageOptions: { languageOptions: {
@ -153,7 +157,7 @@ export default [
}, },
}, },
}, },
] )
``` ```
Of course, another option would be to place a different `eslint.config.mjs` file in each subdirectory. If you have Of course, another option would be to place a different `eslint.config.mjs` file in each subdirectory. If you have
@ -173,6 +177,17 @@ can use the rule sets under `legacy/`:
New projects should not use these rule sets. They may disappear in the future. Scratch did not use Prettier at this New projects should not use these rule sets. They may disappear in the future. Scratch did not use Prettier at this
time, so there is no legacy Prettier configuration. time, so there is no legacy Prettier configuration.
Use these rule sets by importing them directly:
```mjs
// myProjectRoot/eslint.config.mjs
import webConfig from 'eslint-config-scratch/legacy/es6'
import { globalIgnores } from 'eslint/config'
/** @returns {import('eslint').Linter.Config[]} */
export default [...webConfig, globalIgnores(['dist/**/*'])]
```
## Committing ## Committing
This project uses [semantic release](https://github.com/semantic-release/semantic-release) This project uses [semantic release](https://github.com/semantic-release/semantic-release)

View file

@ -72,6 +72,8 @@ const flattenGlobals = globalsIn => {
* - a single object as described in the "Specifying Globals" section of the ESLint documentation: * - a single object as described in the "Specifying Globals" section of the ESLint documentation:
* https://eslint.org/docs/latest/use/configure/language-options#using-configuration-files * https://eslint.org/docs/latest/use/configure/language-options#using-configuration-files
* - an array of zero or more elements, each of which can be either of the above * - an array of zero or more elements, each of which can be either of the above
* @param {import('typescript-eslint').InfiniteDepthConfigWithExtends[]} moreConfigs Additional ESLint configurations
* to merge with the base configuration.
* @example * @example
* // eslint.config.mjs * // eslint.config.mjs
* export default makeScratchConfig({tsconfigRootDir: import.meta.dirname, globals: 'node'}) * export default makeScratchConfig({tsconfigRootDir: import.meta.dirname, globals: 'node'})
@ -85,7 +87,7 @@ const flattenGlobals = globalsIn => {
* ] * ]
* @returns {import('typescript-eslint').ConfigArray} An ESLint configuration array. * @returns {import('typescript-eslint').ConfigArray} An ESLint configuration array.
*/ */
const makeEslintConfig = ({ tsconfigRootDir, globals: globalsIn } = {}) => { const makeEslintConfig = ({ tsconfigRootDir, globals: globalsIn } = {}, ...moreConfigs) => {
const flattenedGlobals = flattenGlobals(globalsIn) const flattenedGlobals = flattenGlobals(globalsIn)
return tseslint.config( return tseslint.config(
@ -285,6 +287,7 @@ const makeEslintConfig = ({ tsconfigRootDir, globals: globalsIn } = {}) => {
'symbol-description': ['error'], 'symbol-description': ['error'],
}, },
}, },
...moreConfigs,
// Keep `eslintConfigPrettier` last to turn off rules that conflict with Prettier // Keep `eslintConfigPrettier` last to turn off rules that conflict with Prettier
eslintConfigPrettier, eslintConfigPrettier,
) )

View file

@ -14,6 +14,7 @@ const compat = new FlatCompat({
allConfig: js.configs.all, allConfig: js.configs.all,
}) })
/** @type {import('eslint').Linter.Config[]} */
export default [ export default [
...compat.extends('eslint:recommended'), ...compat.extends('eslint:recommended'),
jsdoc.configs['flat/recommended'], jsdoc.configs['flat/recommended'],

View file

@ -1,5 +1,6 @@
import globals from 'globals' import globals from 'globals'
/** @type {import('eslint').Linter.Config[]} */
export default [ export default [
{ {
languageOptions: { languageOptions: {

View file

@ -12,6 +12,7 @@ const compat = new FlatCompat({
allConfig: js.configs.all, allConfig: js.configs.all,
}) })
/** @type {import('eslint').Linter.Config[]} */
export default [ export default [
...compat.extends('plugin:react/recommended'), ...compat.extends('plugin:react/recommended'),
{ {

View file

@ -1,3 +1,4 @@
/** @type {import('eslint').Linter.Config[]} */
export default [ export default [
{ {
languageOptions: { languageOptions: {