oxc-config-ai-guardrails
v1.2.0
Published
AI guardrails for the oxc toolchain: strict oxlint presets and an oxfmt profile that keep AI-generated code small, flat and functional
Maintainers
Readme
oxc-config-ai-guardrails
Strict oxlint presets and an oxfmt profile that keep AI-generated code small, flat and honest about its types.
🚀 Quick start · 📦 Presets · 🛡️ Rules · ❓ FAQ · 🤝 Contributing
A coding agent writes code that compiles and looks finished. It also writes 300-line functions, nested if blocks, any parameters, module mocks that hide the real code, and TODO comments that nobody returns to. This package makes oxlint reject those patterns, so the agent must fix them before you review the code.
- ✅ 92 rules, each one proven to fire.
npm run verifylints deliberate violations and fails if a rule stays silent. - ⚡ Native oxlint only. There are no ESLint plugins and no
jsPlugins. The package has 2 peer dependencies. - 💬 Comments go on their own line. A comment after code on the same line fails. Placeholder comments (
TODO,FIXME,XXX,HACK), blanket disable comments and@ts-ignorealso fail.
👀 Example
An agent writes this:
export async function loadUsers(ids: string[], cache: any) {
const users = [];
for (let i = 0; i < ids.length; i++) {
const user = await fetch(`/api/users/${ids[i]}`).then((r) => r.json());
if (user) {
// TODO: handle inactive users
users.push(Reflect.get(user, 'profile'));
}
}
return users;
}oxlint with the base preset rejects it (the output below is shortened):
1:8 error eslint(func-style): Expected a function expression.
1:23 error typescript(explicit-module-boundary-types): Missing return type on function
1:48 error typescript(explicit-module-boundary-types): Argument is explicitly typed as `any`
1:55 error typescript(no-explicit-any): Unexpected `any`. Specify a different type.
3:35 error eslint(no-plusplus): Unary operator '++' used.
4:18 error eslint(no-await-in-loop): Unexpected `await` inside a loop.
5:5 error eslint(max-depth): Blocks are nested too deeply (2). Maximum allowed is 1.
6:7 error eslint(no-warning-comments): Unexpected 'todo' comment: TODO: handle inactive users
7:18 error eslint(no-restricted-properties): 'Reflect.get' is restricted from being used.🚀 Quick start
1. Install
npm i -D oxlint oxfmt oxc-config-ai-guardrailsAdd oxlint-tsgolint only if you want the type-aware preset:
npm i -D oxlint-tsgolint2. Configure oxlint
.oxlintrc.json:
{
"$schema": "./node_modules/oxlint/configuration_schema.json",
"extends": [
"./node_modules/oxc-config-ai-guardrails/configs/base.json",
"./node_modules/oxc-config-ai-guardrails/configs/typescript.json",
"./node_modules/oxc-config-ai-guardrails/configs/react.json"
],
"options": {
"reportUnusedDisableDirectives": "error"
}
}reportUnusedDisableDirectives reports disable comments that no longer suppress anything. oxlint reads it only from your root config, so the presets cannot set it for you.
Or oxlint.config.ts:
import { defineConfig } from 'oxlint';
import { base, react, typescript } from 'oxc-config-ai-guardrails';
export default defineConfig({
extends: [base, typescript, react],
options: { reportUnusedDisableDirectives: 'error' },
});3. Configure oxfmt
oxfmt has no extends, so import the profile from oxfmt.config.ts:
import { defineConfig } from 'oxfmt';
import { oxfmt } from 'oxc-config-ai-guardrails';
export default defineConfig(oxfmt);4. Add scripts
{
"scripts": {
"lint": "oxlint",
"lint:types": "oxlint --type-aware",
"fmt": "oxfmt",
"fmt:check": "oxfmt --check"
}
}📦 Presets
| Preset | Contents | Needs |
|---|---|---|
| configs/base.json | 71 syntax rules, plus the correctness, suspicious and perf categories for the core, typescript, unicorn, import, oxc, vitest and promise plugins | oxlint |
| configs/typescript.json | 17 type-aware rules | oxlint-tsgolint and --type-aware |
| configs/react.json | 4 React rules, the jsx-a11y plugin, and a 100-line budget for .tsx and .jsx functions | oxlint |
| configs/oxfmt.json | 120 columns, single quotes, sorted imports | oxfmt |
Use only the presets that you need. For a Node project with no React, leave out react.json.
base and typescript relax their noisiest rules in *.test.*, *.spec.*, test/, tests/ and __tests__/ files.
🛡️ What the rules enforce
📏 Size budgets
| Rule | Limit | Test files |
|---|---|---|
| max-lines | 200 lines for each file | off |
| max-lines-per-function | 50 lines (100 in .tsx/.jsx) | off |
| max-statements | 20 for each function | off |
| max-params | 3 | on |
| max-depth | 1 level of nested blocks | on |
| complexity | 10 | on |
| max-nested-callbacks | 2 | off |
Blank lines do not count against the line limits.
no-else-return (also bans else if), no-lonely-if, no-nested-ternary, no-continue, no-labels, no-plusplus, no-void, curly, no-await-in-loop, no-loop-func, guard-for-in, no-fallthrough.
A ternary is legal. A ternary inside a ternary is not.
func-style (expressions only), prefer-arrow-callback, arrow-body-style, no-var, prefer-const, no-param-reassign (properties too), max-classes-per-file: 0, oxc/no-this-in-exported-function.
Test files can declare classes.
| Rule | Rejects |
|---|---|
| typescript/no-explicit-any | any |
| typescript/no-non-null-assertion | value! |
| typescript/ban-ts-comment | @ts-ignore, @ts-nocheck, and @ts-expect-error without a description |
| typescript/explicit-module-boundary-types | exported functions without parameter and return types |
| typescript/no-restricted-types | object, Record<string, unknown> and Record<string, any> |
| typescript/no-empty-object-type | {}, which accepts every non-nullish value |
| typescript/consistent-indexed-object-style | { [key: string]: T }, rewritten to Record so the rule above can check it |
| no-restricted-properties | Reflect.get and Reflect.apply, which turn an unsound access into any |
| typescript/no-unsafe-* (type-aware) | values of type any that move through calls, assignments and returns |
| typescript/no-unsafe-type-assertion (type-aware) | as casts that the compiler cannot prove |
| Rule | Rejects |
|---|---|
| vitest/no-restricted-vi-methods | vi.mock, vi.doMock and vi.stubGlobal. The rule follows import aliases. |
| no-restricted-properties | jest.mock, jest.retryTimes and Playwright page.waitForTimeout |
| vitest/no-conditional-in-test | if, switch and ?: in a test body |
| vitest/no-large-snapshots | inline snapshots longer than 50 lines |
| vitest/padding-around-test-blocks, vitest/padding-around-after-all-blocks | a test or afterAll block with no blank line before it |
A module mock or a stubbed global hides the real dependency, so a test can pass while the code is broken. Inject the dependency instead. A retry or a fixed wait hides a flaky test.
The vitest plugin also turns on its correctness and suspicious rules, for example no-disabled-tests, expect-expect, no-conditional-expect and require-to-throw-message. These rules also check Jest test files.
no-restricted-properties matches identifier names, so an aliased jest or page is not caught.
- Syntax:
promise/prefer-await-to-then(useawait, not.then()chains),eqeqeq(== nullstays legal),no-throw-literal,array-callback-return. Thepromiseplugin also turns on itscorrectnessandsuspiciousrules, for exampleno-multiple-resolved. - Type-aware:
typescript/no-floating-promises,typescript/no-misused-promises,typescript/await-thenable,typescript/require-await,typescript/switch-exhaustiveness-check,typescript/no-unnecessary-condition,typescript/no-unnecessary-type-assertion,typescript/prefer-nullish-coalescing,typescript/prefer-optional-chain,typescript/no-deprecated,typescript/no-implied-eval.
- Imports:
import/no-cycle,import/no-duplicates,import/first,import/newline-after-import,import/no-namespace,import/no-mutable-exports,typescript/consistent-type-imports. - Modern APIs: 11
unicornrules, for exampleprefer-at,prefer-array-find,prefer-includes,prefer-number-propertiesandthrow-new-error. Alsoprefer-exponentiation-operator. - Safety:
no-eval,no-new-func,no-script-url,oxc/no-async-endpoint-handlers. - Comments:
no-inline-commentsbans a comment after code on the same line. Bundler annotations such as/* @__PURE__ */stay legal. Other tool comments on the same line, for example/* webpackChunkName: "x" */, need// oxlint-disable-next-line no-inline-comments.no-warning-commentsbansTODO,FIXME,XXXandHACK.unicorn/no-abusive-eslint-disablebansoxlint-disableandeslint-disablecomments that do not name a rule.
react/rules-of-hooks, react/exhaustive-deps, react/no-unstable-nested-components, react/no-array-index-key.
The jsx-a11y plugin turns on its accessibility rules, for example alt-text, click-events-have-key-events and no-static-element-interactions. react/react-in-jsx-scope is off, because the JSX transform of React 17 and later does not need React in scope.
❓ FAQ
Rules in your own config override the extended presets:
{
"extends": ["./node_modules/oxc-config-ai-guardrails/configs/base.json"],
"rules": {
"max-lines": ["error", { "max": 300, "skipBlankLines": true }],
"no-continue": "off"
}
}typescript/explicit-module-boundary-types treats an exported component like every other exported function. The props type and the return type are the public contract of the component:
export const Button = ({ label }: Props): ReactElement => <button>{label}</button>;The rule does not check .js and .jsx files, or functions that you do not export.
oxlint has two traps:
- A bare rule key can resolve to a type-aware rule.
no-implied-evalexists only as a tsgolint rule, so it never runs without--type-aware. This package keeps it inconfigs/typescript.jsonfor that reason. - oxlint ignores unknown rule options. A typo in an option does not fail the run. Write a file that must fail, and check that oxlint reports it.
🤝 Contributing
Every rule must fire on a fixture. The verify script enforces this:
npm install
npm run verifyscripts/verify.mjs lints fixtures/ and checks these claims:
- Every rule in its manifest is in a preset, and every enabled rule is in the manifest.
- Every rule in the manifest fires on a fixture.
- Each plugin that adds only category rules (
vitest,promise,jsx-a11y) loads: one of its category rules fires. - The test-file overrides relax what they promise, and
prefer-conststill fires in the same file. - An 85-line component passes, a 110-line component fails, and a 51-line non-JSX function fails.
- 3 prose comments after code fail. Bundler annotations,
== nulland a plain ternary stay legal. - Each fixture that claims 4 to 6 use has at least one diagnostic, so a broken filename match fails.
To add a rule:
- Add it to a file in
configs/. - Add it to
MANIFESTinscripts/verify.mjs. - Add a fixture in
fixtures/that breaks the rule. - Run
npm run verify.
The files in fixtures/ are deliberate rule violations. Do not copy them.
