@disphere/code-quality-config
v0.4.0
Published
Provides opinionated configuration for npm plugins that deal with code quality.
Readme
@disphere/code-quality-config
Opinionated, shared ESLint (flat config) and Prettier configuration. It bundles the plugins it uses, so a project only needs the peer dependencies.
What you get:
- ESLint:
@eslint/jsrecommended, type-checkedtypescript-eslint,eslint-plugin-nfor Node code, React (@eslint-react+ hooks), Vue (eslint-plugin-vue),import-xwith cycle detection, Cypress, and Jest/Vitest/Mocha. Rules that would conflict with Prettier are turned off. - Prettier: 120 columns, single quotes, trailing commas, import sorting, SQL formatting inside tagged template literals, and one sentence per line in Markdown.
- CLI helpers:
lint-reportandwhich-modules, which summarise ESLint output.
Requirements
- Node.js >= 24
eslint^10.8,prettier^3.9.6,typescript>=5.1.6 <6.1.0. TypeScript is needed even in JavaScript-only projects, becausetypescript-eslintloads it.
Installation
npm install --save-dev @disphere/code-quality-config eslint prettier typescript@6TypeScript 7 is not supported yet, because typescript-eslint does not support it.
Declare the Node version in your package.json.
eslint-plugin-n reads it to decide which Node APIs are allowed:
{
"engines": {
"node": ">=24.0.0"
}
}npm audit and minimatch (GHSA-g3hv-75wg-m249)
npm audit may report GHSA-g3hv-75wg-m249, a ReDoS in minimatch <11.0.1.
It comes in through eslint, @eslint/config-array and @typescript-eslint/typescript-estree, which require minimatch 10 themselves, so this package can't fix it. npm overrides only take effect in the root project, so the package can't apply one for you either.
The risk is low: in a lint setup, minimatch only matches the glob patterns from your own ESLint config and ignore files, not untrusted input.
To clear the audit anyway, add an override to your package.json:
{
"overrides": {
"minimatch": "^11.0.5"
}
}ESLint and typescript-eslint both work with minimatch 11; their own fixes require ^11.0.5 and ^11.0.1.
Remove the override once releases of both that depend on minimatch 11 are installed.
ESLint
Create eslint.config.mjs:
import appConfig from '@disphere/code-quality-config/eslint';
export default appConfig({
env: 'node',
tsconfigRootDir: import.meta.dirname, // TypeScript projects only
});All options are optional:
| Option | Values | Default | Description |
| ----------------- | ----------------------------------------------------------- | ----------- | -------------------------------------------------------------------------------------------------------------------------------- |
| env | 'browser' | 'node' | 'both' | 'browser' | Where your code runs. Sets globals; node/both also enable eslint-plugin-n. |
| modules | 'esm' | 'cjs' | 'esm' | How bare .js files are parsed. Must match the type in your package.json. |
| testRunner | 'jest' | 'vitest' | 'mocha' | 'node' | 'none' | 'vitest' | Picks the plugin and globals for test files. 'node' is Node's built-in node:test (see below). 'none' skips the test block. |
| jestVersion | number | 30 | Major Jest version; only read when testRunner is 'jest'. |
| tsconfigRootDir | string | – | Pass import.meta.dirname in TypeScript projects. Type-aware rules use the nearest tsconfig.json. |
| ignores | string[] | [] | Extra ignore globs. dist/, build/, coverage/ and *.min.js are always ignored. |
These globs decide which rules apply to a file:
- TypeScript:
*.ts,*.mts,*.cts,*.tsx - React:
*.jsx,*.tsx - Vue:
*.vue(see Vue) - Tests:
*.test.*,*.spec.*, and files under__tests__/,test/ortests/ - Cypress:
*.cy.*and files undercypress/ - Tooling:
*.config.*andscripts/**. These are treated as Node code even in browser projects.
With testRunner: 'node', test files get Node globals, and calls of test, describe, it and suite (including .skip, .todo and .only) imported from node:test are exempt from @typescript-eslint/no-floating-promises, because the runner awaits their promises itself.
Other floating promises in test files are still reported.
Jest, Vitest and Mocha need no such exemption: their test()/describe()/it() don't return promises.
Variables, arguments and caught errors prefixed with _ are exempt from the unused-variable rules.
curly is enforced with its default all option: every if, else, for, while and do body needs braces.
Existing code can be fixed with eslint --fix.
With TypeScript 6, compilerOptions.types defaults to [], so @types/* packages are no longer included automatically.
If you see type-aware errors such as no-unsafe-call on Node APIs, list the packages explicitly in your tsconfig.json, e.g. "types": ["node"].
Vue
Vue single-file components are linted automatically, with no extra option.
Like the React rules, the Vue rules only apply to matching files (*.vue), so other projects are unaffected.
eslint-plugin-vue'sflat/recommendedpreset (Vue 3) applies to the whole file. Template formatting rules are turned off, because Prettier formats templates.<script>and<script setup>are parsed by the typescript-eslint parser, so the same type-checked rules as in.tsfiles apply. Blocks withoutlang="ts"are linted the same way.- Type-aware rules need the
.vuefiles in atsconfig.json(e.g."include": ["src"]or"src/**/*.vue"). To import components from.tsfiles, declare the usual*.vuemodule shim; seeshims-vue.d.ts. - Custom elements such as Ionic's
<ion-button>are not flagged.vue/no-undef-componentsstays off, because it can't see globally registered or auto-imported components. Enable it yourself withignorePatterns: ['^ion-']if it fits your project.
Prettier formats .vue files out of the box.
Sorting the imports inside <script> needs @vue/compiler-sfc, which every Vue project already has as a dependency of vue.
Prettier
Create .prettierrc.mjs:
import base from '@disphere/code-quality-config/prettier';
export default base;Use .mjs, not .js, so the file is ESM whether or not your package.json sets "type": "module".
Add a .prettierignore as needed.
To extend the config, spread it: export default { ...base, printWidth: 100 };.
Imports are sorted in this order: Node built-ins, third-party packages, @/ aliases, then relative imports.
One sentence per line in Markdown
In *.md files, Prettier puts each sentence on its own line, via prettier-plugin-sentences-per-line.
Changing a sentence then touches one line in the diff.
Paragraphs are never reflowed (proseWrap: 'preserve'): lines are only split, never joined.
- First sentence. Second sentence.
+ First sentence.
+ Second sentence.- A line is split where a word ends in
.,!or?and the next word starts with a capital letter (A–Z). Sentences that start with inline code, a link, emphasis or an umlaut, or follow a bare number (Node 26. Then), are not split, soformat:checkwon't flag them. - Headings and table rows are never split. List items and blockquotes are.
- Abbreviations don't end a sentence:
e.g.,i.e.,etc.,vs., English titles (Dr.,Mr.), common German abbreviations (bzw.,ca.,ggf.,vgl.,Nr.) and single letters, which covers initials andz. B.,d. h.oru. a.. Version numbers such as1.2.3are not split either. - To add abbreviations, override
sentencesPerLineAdditionalAbbreviationsfor*.mdfiles. Keep the shared list, because the option replaces it:
import base from '@disphere/code-quality-config/prettier';
const markdown = base.overrides.find((override) => override.files === '*.md');
markdown.options.sentencesPerLineAdditionalAbbreviations.push('resp.');
export default base;Adopting this reformats existing Markdown once; consider doing it in a dedicated formatting commit.
SQL in template literals
SQL inside template literals tagged mysqlFormat (MySQL dialect) or mssqlFormat (T-SQL) is formatted:
const query = mysqlFormat`
SELECT
id
FROM
users
WHERE
name = :name
`;The tags are only markers for Prettier.
This package doesn't ship them: it is a dev dependency, and the tags are runtime code.
Copy them into your project, e.g. as src/sql.ts:
/*
* Markers for prettier-plugin-embed: templates tagged with these are formatted as SQL. At runtime they only join the
* template back together; `null`/`undefined` values become empty strings.
*/
type SqlValue = string | number | bigint | boolean | null | undefined;
const joinTemplate = (strings: TemplateStringsArray, ...values: SqlValue[]): string =>
strings.reduce((sql, str, i) => sql + str + String(values[i] ?? ''), '');
export const mysqlFormat = joinTemplate;
export const mssqlFormat = joinTemplate;In JavaScript, drop the type annotations and the SqlValue type.
Values are interpolated as-is and are not escaped, so pass user input as query parameters (:name), not via ${}.
Suggested npm scripts
{
"scripts": {
"lint": "eslint",
"lint:report": "eslint --format json | lint-report",
"format": "prettier --write .",
"format:check": "prettier --check ."
}
}lint-report
Reads ESLint JSON output from stdin (or from a file path argument) and prints a summary instead of every message.
- Default: grouped by rule, with error, warning and file counts, and how many are autofixable.
--by=file: ranks the files with the most problems.--max-warnings=N: fails when there are more than N warnings.
It exits with code 1 when there are errors, so it can end a pipeline.
which-modules
Groups the messages of one rule by the module or identifier they name. This shows whether a large count has one cause or many:
npx eslint --format json | npx which-modules no-undefExample
test/consumer/ is a minimal TypeScript project set up as described above.
The package's smoke test installs the packed artifact into a copy of it and runs Prettier, tsc, ESLint and Vitest there.
It also contains the SQL tag snippet from above, in src/sql.ts, and a Vue component (src/GreetingCard.vue) imported from TypeScript.
