@brutalway/oxlint-config-typescript
v0.8.3
Published
Oxlint config for TypeScript projects: no comments, no barrel files, files named after their export, types and constants in dedicated files.
Maintainers
Readme
@brutalway/oxlint-config-typescript
Oxlint config for TypeScript projects. The other @brutalway/oxlint-config-* packages extend it.
Install
npm install --save-dev oxlint @brutalway/oxlint-config-typescriptimport { typescriptConfig } from '@brutalway/oxlint-config-typescript';
import { defineConfig } from 'oxlint';
export default defineConfig({
extends: [typescriptConfig],
});Two plugins it loads (eslint-plugin-check-file, eslint-plugin-no-comments) depend on ESLint 10.14, which pulls minimatch 10 (GHSA-g3hv-75wg-m249). npm only reads overrides from the root package.json, so add this to yours:
"overrides": {
"eslint-plugin-no-comments": { "minimatch": "^11.0.1" }
}Usage
Run the type-aware rules
The typescript/no-floating-promises family of rules needs oxlint --type-aware and the oxlint-tsgolint package. The rules marked "needs --type-aware" below only run with it.
Allow console in a CLI
console is limited to console.error, except in logger.ts and xLogger.ts, where a project writes the one logger the rest of its code goes through. A CLI turns no-console off in an override.
Keep a narrowing inside a declared function
A declared function is hoisted, so TypeScript drops the narrowing of a captured variable inside it. Move the null check into a helper that returns the narrowed value.
Rules
It turns on the correctness and suspicious categories, a set of typescript/ and import/ rules, and these conventions:
- no comments, except tool directives (
oxlint-,eslint-,@ts-expect-error, triple-slash references, shebang) - no
index.tsand no barrel file - no
types.ts: name itxTypes.ts - nothing in a shared feature, at any depth, that belongs to a feature: no orchestrator component (
XContainer.tsx,XLayout.tsx,XPage.tsx) and noxRoutes.ts - no
xConfig.tsx: a config is plain data inxConfig.ts, and its JSX goes in a component.xContent.tsxstays allowed for content written in JSX. - file names in camelCase or PascalCase
- a file with a single export is named after it (@brutalway/eslint-plugin-file-named-after-export)
- no
export default(import/no-default-export), so a module is imported under the name it exports and never renamed at the import. The files a tool loads by their default export are exempt:*.config.tsand*.config.mts(Vite, Vitest, Playwright, tsdown, Oxlint, i18next),plugin.tsandxPlugin.ts(OxlintjsPlugins, ESLint plugins),.d.tsfiles, which describe a module, andfixtures/.React.lazyreads a default export too: map the named export in the import,lazy(() => import('#src/x/SettingsPage.tsx').then((module) => ({ default: module.SettingsPage }))) - named functions are declarations (
function area() {}), arrow functions stay for callbacks - an
ifwithout braces fits on one line (curlyset tomulti-line) - object shapes use
type,interfaceis only accepted to augment a global such asWindowinsidedeclare global, or a package such asfastifyinsidedeclare module 'fastify' - a
switchover a union covers every case (typescript/switch-exhaustiveness-check, needs--type-aware) - exported types live in
xTypes.tsfiles, exported constants inxConfig.tsfiles. AnxTypes.tsonly exports types, anxConfig.ts(orxContent.ts) only exports constants. Exported functions, classes, and objects that hold a function at any depth ({ notes: { all: () => ['notes'] } }, with or withoutas constorsatisfies) can live anywhere, and so canexport const x = defineX(...). AnxSchemas.tsonly exports Zod schemas, which are constants too: it is exempt from that rule and from the file-name rule, and the types inferred from it go in anxTypes.ts. - one way to write each thing: function members of a type as properties (
area: () => number), nopublickeyword (writeprivateandprotected, leave public implicit),for...ofinstead offorEach,replaceAllfor a global regex,Number.parseFloatandNumber.NaNinstead of the globals,import type { X }instead ofimport { type X } - regexes carry the
uflag, ternaries are not nested,!!xand+xare written as explicit comparisons or calls, parameters are not reassigned consoleonly forconsole.error- no
@ts-ignoreand no@ts-nocheck, and a@ts-expect-errorsays why (// @ts-expect-error: the types of hls.js omit isSupported): an error silenced without a reason stays silenced after the code changes (typescript/ban-ts-comment) - no code in a string:
eval,new Function('...'), andsetTimeout('...')orsetInterval('...'), which take a function instead (no-eval,no-new-func,typescript/no-implied-eval, the last one needs--type-aware) - class members never reassigned are
readonly, deprecated APIs are reported, and avoidexpression is only accepted as the body of an arrow (() => save()). These three need--type-aware. - a function whose cognitive complexity is above 15 is reported, with Sonar's own rule:
sonarjs/cognitive-complexity, the S3776 rule ofeslint-plugin-sonarjs - a function holds 25 lines at most, blank lines and comments not counted (
max-lines-per-function), and blocks and callbacks nest 3 levels deep at most (max-depth,max-nested-callbacks). The limits come from the Norminette of 42, which caps a function at 25 lines. Tests have no length limit, since adescribeblock holds all of its cases - a function takes 4 parameters at most (
max-params) and declares 5 local variables at most (function-size/max-variables, a rule of this package). Each name bound by a destructuring counts, a nested function is counted on its own, and tests are exempt. Both limits come from the Norminette - a file declares 5 top-level functions at most (
function-size/max-functions): function declarations and functions assigned to a top-levelconst, exported or not. A function nested in another one, a callback and an object method do not count. The limit comes from the Norminette, which caps a.cfile at 5 functions
Tests (*.test.ts, *.spec.ts, e2e/) skip the naming and placement rules, the length limit and the variable limit.
Why
The whole file-name blocklist for shared/ lives in this config, so a preset that extends several configs cannot lose part of it.
The whole eslint-plugin-sonarjs plugin does not load under Oxlint with TypeScript 7 (its ts-api-utils finds the project's TypeScript, which has no JavaScript API), so src/cognitiveComplexityPlugin.ts loads the file of sonarjs/cognitive-complexity alone. eslint-plugin-sonarjs is pinned to an exact version, since the path is internal to it, and a fixture test fails if it moves.
