@yuu1111/oxc-config 0.0.0-stage → 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.ja.md ADDED
@@ -0,0 +1,137 @@
1
+ [English](README.md)
2
+
3
+ # @yuu1111/oxc-config
4
+
5
+ 共有の [Oxlint](https://oxc.rs/docs/guide/usage/linter) と [Oxfmt](https://oxc.rs/docs/guide/usage/formatter) 設定
6
+
7
+ ## Install
8
+
9
+ ```bash
10
+ bun add -D @yuu1111/oxc-config oxlint oxfmt
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ `.oxlintrc.json` で `base` をextendsし、Projectに必要なrule層を足す
16
+
17
+ ```json
18
+ {
19
+ "$schema": "./node_modules/oxlint/configuration_schema.json",
20
+ "extends": [
21
+ "./node_modules/@yuu1111/oxc-config/oxlint/base.json",
22
+ "./node_modules/@yuu1111/oxc-config/oxlint/core.json"
23
+ ]
24
+ }
25
+ ```
26
+
27
+ Oxlintの `extends` はpackage specifierを受け取らず、file pathだけを解決する
28
+ そのため `node_modules` 経由のpathを書く
29
+
30
+ Oxfmtは `oxfmt.config.ts` でpresetをimportする
31
+
32
+ ```ts
33
+ import { yamlOnly } from "@yuu1111/oxc-config/oxfmt";
34
+
35
+ export default { ...yamlOnly };
36
+ ```
37
+
38
+ ## Presets
39
+
40
+ | Preset | 用途 |
41
+ | ------------------ | ------------------------------------------------ |
42
+ | `oxlint/base` | `correctness` と `typescript/no-deprecated` |
43
+ | `oxlint/core` | 汎用のrule 10個 |
44
+ | `oxlint/network` | `fetch` のrule 2つ |
45
+ | `oxlint/discord` | Discordのrule 1つ |
46
+ | `oxlint/scoped` | Project側で対象層を選ぶrule 2つを読み込むだけ |
47
+ | `oxfmt` `base` | Biomeの共有設定と揃えた書式 |
48
+ | `oxfmt` `yamlOnly` | `base` の対象をYAMLだけにし、Biomeと対象を分ける |
49
+
50
+ `oxlint/*` のruleはすべて1つのJS plugin `yuu1111` に入り、presetはそのruleを有効にするだけになる
51
+ `@yuu1111/biome-config` の `plugins/*` と同じ検出をGritQLではなくJS pluginで行い、Biomeのlintより速く走る
52
+
53
+ ### oxlint/core
54
+
55
+ | Rule | 重大度 | Default | 検出対象 |
56
+ | ------------------------------- | ------- | ------- | ------------------------------------------------------------------------------------------------------------------- |
57
+ | `no-reexports` | warning | true | 既存bindingを転送するだけのexport(`export default value`、`export const a = b`、`export type Alias = Imported`) |
58
+ | `no-placeholder-implementation` | error | true | `Not implemented`、`TODO`、`coming soon` を含むerrorのthrow |
59
+ | `no-incomplete-implementation` | warning | true | `null`・`[]`・`{}` へ置き換えるcatch、logだけのcatch |
60
+ | `no-double-assertion` | error | true | `as unknown as T` |
61
+ | `no-type-safety-bypass` | warning | true | `Record<string, unknown>` のassertion、未検証のJSON assertion、静的な `Reflect.get`、receiver無しの `Reflect.apply` |
62
+ | `no-unsafe-errno-assertion` | warning | true | `NodeJS.ErrnoException` のassertion経由で `code` を読むコード |
63
+ | `no-meaningless-test` | warning | true | 空の `test`・`it` callback |
64
+ | `no-tautological-assertion` | error | true | identifierやliteralを自分自身と比較する `toBe`・`toEqual`・`toStrictEqual` |
65
+ | `no-useless-abstraction` | warning | true | 同じ引数を別の関数へ渡すだけの名前付き単一引数arrow function |
66
+ | `no-ternary-statement` | warning | true | `flag ? enable() : disable()` のような文としてのternary |
67
+
68
+ Biomeのpluginは1つのruleにerrorとwarningを混ぜていたが、Oxlintはruleごとに重大度を1つ持つため、errorの検出を別のruleへ分けた
69
+ 意図的な例外は理由を添えて抑制する
70
+
71
+ ```ts
72
+ // oxlint-disable-next-line yuu1111/no-incomplete-implementation -- An optional cache miss is an expected fallback
73
+ cacheRequest.catch(() => null);
74
+ ```
75
+
76
+ ### oxlint/network
77
+
78
+ | Rule | 重大度 | Default | 検出対象 |
79
+ | ------------------------------- | ------- | ------- | ------------------------------------------------------------------------- |
80
+ | `require-fetch-abort-signal` | warning | true | `signal` optionを省いた `fetch` 呼び出し |
81
+ | `require-abort-timeout-cleanup` | warning | true | `setTimeout` callbackで `fetch` をabortしながらそのtimerをclearしない関数 |
82
+
83
+ ### oxlint/discord
84
+
85
+ | Rule | 重大度 | Default | 検出対象 |
86
+ | ----------------------------------- | ------- | ------- | ------------------------------------------------------------ |
87
+ | `no-unsafe-dynamic-discord-message` | warning | true | 明示的な `allowedMentions` policy無しで送る動的なmessage内容 |
88
+
89
+ ### oxlint/scoped
90
+
91
+ | Rule | 重大度 | Default | 検出対象 |
92
+ | -------------------- | ------ | ------- | ------------------------------------------------------------------------------ |
93
+ | `no-adapter-import` | error | false | `integrations/` か `adapters/` を含むpathのimport |
94
+ | `no-direct-response` | error | false | 対象層での `new Response(...)`、`Response.json(...)`、`Response.redirect(...)` |
95
+
96
+ 対象層はProjectごとに違うため、presetはpluginを読み込むだけでruleを有効にしない
97
+ `overrides` で対象層を選んで有効にする
98
+
99
+ ```json
100
+ {
101
+ "extends": [
102
+ "./node_modules/@yuu1111/oxc-config/oxlint/base.json",
103
+ "./node_modules/@yuu1111/oxc-config/oxlint/scoped.json"
104
+ ],
105
+ "overrides": [
106
+ {
107
+ "files": [
108
+ "src/modules/authorization/**"
109
+ ],
110
+ "rules": {
111
+ "yuu1111/no-adapter-import": "error"
112
+ }
113
+ }
114
+ ]
115
+ }
116
+ ```
117
+
118
+ ### oxfmt
119
+
120
+ | Setting | 値 |
121
+ | ---------------- | ----------------------------------------- |
122
+ | `useTabs` | `true` |
123
+ | `singleQuote` | `false` |
124
+ | `endOfLine` | `lf` |
125
+ | `ignorePatterns` | `yamlOnly` だけ、YAML以外をすべて除外する |
126
+
127
+ `yamlOnly` は `*` で一度除外し、directoryを辿れるように戻したうえで `.yaml` と `.yml` だけを対象へ戻す
128
+ Oxfmtだけでformatする場合は `base` を使い、Projectの値を足す
129
+
130
+ ```ts
131
+ import { base } from "@yuu1111/oxc-config/oxfmt";
132
+
133
+ export default {
134
+ ...base,
135
+ ignorePatterns: ["dist/**"],
136
+ };
137
+ ```
package/README.md CHANGED
@@ -1,3 +1,136 @@
1
- # Temporary Holding Version
1
+ [日本語](README.ja.md)
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ # @yuu1111/oxc-config
4
+
5
+ Shared [Oxlint](https://oxc.rs/docs/guide/usage/linter) and [Oxfmt](https://oxc.rs/docs/guide/usage/formatter) configuration.
6
+
7
+ ## Install
8
+
9
+ ```bash
10
+ bun add -D @yuu1111/oxc-config oxlint oxfmt
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ Extend `base` in `.oxlintrc.json` and add the rule layers the project needs:
16
+
17
+ ```json
18
+ {
19
+ "$schema": "./node_modules/oxlint/configuration_schema.json",
20
+ "extends": [
21
+ "./node_modules/@yuu1111/oxc-config/oxlint/base.json",
22
+ "./node_modules/@yuu1111/oxc-config/oxlint/core.json"
23
+ ]
24
+ }
25
+ ```
26
+
27
+ Oxlint's `extends` resolves file paths only and rejects package specifiers, so write the path through `node_modules`.
28
+
29
+ Oxfmt imports a preset from `oxfmt.config.ts`:
30
+
31
+ ```ts
32
+ import { yamlOnly } from "@yuu1111/oxc-config/oxfmt";
33
+
34
+ export default { ...yamlOnly };
35
+ ```
36
+
37
+ ## Presets
38
+
39
+ | Preset | Use case |
40
+ | ------------------ | ---------------------------------------------------------------- |
41
+ | `oxlint/base` | `correctness` and `typescript/no-deprecated` |
42
+ | `oxlint/core` | 10 general-purpose rules |
43
+ | `oxlint/network` | 2 rules for `fetch` |
44
+ | `oxlint/discord` | 1 rule for Discord |
45
+ | `oxlint/scoped` | Loads 2 rules that the project turns on for its own layers |
46
+ | `oxfmt` `base` | Formatting aligned with the shared Biome configuration |
47
+ | `oxfmt` `yamlOnly` | `base` restricted to YAML so that it does not overlap with Biome |
48
+
49
+ Every `oxlint/*` rule lives in one JS plugin named `yuu1111`, and a preset only turns rules on.
50
+ The rules detect what the `plugins/*` layers of `@yuu1111/biome-config` detect, with a JS plugin instead of GritQL, and run faster than the Biome lint.
51
+
52
+ ### oxlint/core
53
+
54
+ | Rule | Severity | Default | Detects |
55
+ | ------------------------------- | -------- | ------- | ----------------------------------------------------------------------------------------------------------------------------- |
56
+ | `no-reexports` | warning | true | Exports that only forward an existing binding (`export default value`, `export const a = b`, `export type Alias = Imported`) |
57
+ | `no-placeholder-implementation` | error | true | Throwing an error that says `Not implemented`, `TODO`, or `coming soon` |
58
+ | `no-incomplete-implementation` | warning | true | Catches that replace a failure with `null`, `[]`, or `{}`, and catches that only log |
59
+ | `no-double-assertion` | error | true | `as unknown as T` |
60
+ | `no-type-safety-bypass` | warning | true | `Record<string, unknown>` assertions, unchecked JSON assertions, static `Reflect.get`, and `Reflect.apply` without a receiver |
61
+ | `no-unsafe-errno-assertion` | warning | true | Reading `code` through a `NodeJS.ErrnoException` assertion |
62
+ | `no-meaningless-test` | warning | true | Empty `test` and `it` callbacks |
63
+ | `no-tautological-assertion` | error | true | `toBe`, `toEqual`, and `toStrictEqual` comparing an identifier or literal with itself |
64
+ | `no-useless-abstraction` | warning | true | Named single-argument arrow functions that only pass the argument on |
65
+ | `no-ternary-statement` | warning | true | Ternaries used as statements, such as `flag ? enable() : disable()` |
66
+
67
+ The Biome plugins mixed errors and warnings in one rule, while Oxlint gives each rule one severity, so the error cases are separate rules here.
68
+ Suppress an intentional exception with a reason:
69
+
70
+ ```ts
71
+ // oxlint-disable-next-line yuu1111/no-incomplete-implementation -- An optional cache miss is an expected fallback
72
+ cacheRequest.catch(() => null);
73
+ ```
74
+
75
+ ### oxlint/network
76
+
77
+ | Rule | Severity | Default | Detects |
78
+ | ------------------------------- | -------- | ------- | ------------------------------------------------------------------------------ |
79
+ | `require-fetch-abort-signal` | warning | true | `fetch` calls without a `signal` option |
80
+ | `require-abort-timeout-cleanup` | warning | true | Functions that abort a `fetch` from a `setTimeout` callback but never clear it |
81
+
82
+ ### oxlint/discord
83
+
84
+ | Rule | Severity | Default | Detects |
85
+ | ----------------------------------- | -------- | ------- | ------------------------------------------------------------------------- |
86
+ | `no-unsafe-dynamic-discord-message` | warning | true | Dynamic message content sent without an explicit `allowedMentions` policy |
87
+
88
+ ### oxlint/scoped
89
+
90
+ | Rule | Severity | Default | Detects |
91
+ | -------------------- | -------- | ------- | ------------------------------------------------------------------------------------ |
92
+ | `no-adapter-import` | error | false | Imports from a path containing `integrations/` or `adapters/` |
93
+ | `no-direct-response` | error | false | `new Response(...)`, `Response.json(...)`, and `Response.redirect(...)` in the layer |
94
+
95
+ The target layer differs per project, so the preset only loads the plugin and leaves these rules off.
96
+ Turn them on for the layer with `overrides`:
97
+
98
+ ```json
99
+ {
100
+ "extends": [
101
+ "./node_modules/@yuu1111/oxc-config/oxlint/base.json",
102
+ "./node_modules/@yuu1111/oxc-config/oxlint/scoped.json"
103
+ ],
104
+ "overrides": [
105
+ {
106
+ "files": [
107
+ "src/modules/authorization/**"
108
+ ],
109
+ "rules": {
110
+ "yuu1111/no-adapter-import": "error"
111
+ }
112
+ }
113
+ ]
114
+ }
115
+ ```
116
+
117
+ ### oxfmt
118
+
119
+ | Setting | Value |
120
+ | ---------------- | ---------------------------------------------- |
121
+ | `useTabs` | `true` |
122
+ | `singleQuote` | `false` |
123
+ | `endOfLine` | `lf` |
124
+ | `ignorePatterns` | `yamlOnly` only, excluding everything but YAML |
125
+
126
+ `yamlOnly` excludes everything with `*`, re-includes directories so they can be walked, and then re-includes `.yaml` and `.yml`.
127
+ A project that formats with Oxfmt alone uses `base` and adds its own values:
128
+
129
+ ```ts
130
+ import { base } from "@yuu1111/oxc-config/oxfmt";
131
+
132
+ export default {
133
+ ...base,
134
+ ignorePatterns: ["dist/**"],
135
+ };
136
+ ```
package/oxfmt.d.ts ADDED
@@ -0,0 +1,19 @@
1
+ /**
2
+ * presetが持つOxfmtの設定 `oxfmt` の型へ依存するとoxlintだけを入れたprojectで型解決が壊れる
3
+ */
4
+ interface OxfmtPreset {
5
+ useTabs: boolean;
6
+ singleQuote: boolean;
7
+ endOfLine: "lf";
8
+ ignorePatterns?: string[];
9
+ }
10
+
11
+ /**
12
+ * Biomeの共有設定と揃えた書式
13
+ */
14
+ export declare const base: OxfmtPreset;
15
+
16
+ /**
17
+ * Biomeと併用するときにYAMLだけを対象にする書式
18
+ */
19
+ export declare const yamlOnly: OxfmtPreset;
package/oxfmt.js ADDED
@@ -0,0 +1,10 @@
1
+ export const base = {
2
+ useTabs: true,
3
+ singleQuote: false,
4
+ endOfLine: "lf",
5
+ };
6
+
7
+ export const yamlOnly = {
8
+ ...base,
9
+ ignorePatterns: ["*", "!*/", "!*.yaml", "!*.yml"],
10
+ };
@@ -0,0 +1,8 @@
1
+ {
2
+ "categories": {
3
+ "correctness": "error"
4
+ },
5
+ "rules": {
6
+ "typescript/no-deprecated": "error"
7
+ }
8
+ }
@@ -0,0 +1,17 @@
1
+ {
2
+ "jsPlugins": [
3
+ "./plugin.js"
4
+ ],
5
+ "rules": {
6
+ "yuu1111/no-reexports": "warn",
7
+ "yuu1111/no-placeholder-implementation": "error",
8
+ "yuu1111/no-incomplete-implementation": "warn",
9
+ "yuu1111/no-double-assertion": "error",
10
+ "yuu1111/no-type-safety-bypass": "warn",
11
+ "yuu1111/no-unsafe-errno-assertion": "warn",
12
+ "yuu1111/no-meaningless-test": "warn",
13
+ "yuu1111/no-tautological-assertion": "error",
14
+ "yuu1111/no-useless-abstraction": "warn",
15
+ "yuu1111/no-ternary-statement": "warn"
16
+ }
17
+ }
@@ -0,0 +1,8 @@
1
+ {
2
+ "jsPlugins": [
3
+ "./plugin.js"
4
+ ],
5
+ "rules": {
6
+ "yuu1111/no-unsafe-dynamic-discord-message": "warn"
7
+ }
8
+ }
@@ -0,0 +1,9 @@
1
+ {
2
+ "jsPlugins": [
3
+ "./plugin.js"
4
+ ],
5
+ "rules": {
6
+ "yuu1111/require-fetch-abort-signal": "warn",
7
+ "yuu1111/require-abort-timeout-cleanup": "warn"
8
+ }
9
+ }
@@ -0,0 +1,666 @@
1
+ const DIAGNOSTIC_KEYS = new Set(["parent", "range", "start", "end", "loc"]);
2
+
3
+ function isNode(value) {
4
+ return (
5
+ typeof value === "object" &&
6
+ value !== null &&
7
+ typeof value.type === "string"
8
+ );
9
+ }
10
+
11
+ function some(node, predicate) {
12
+ if (predicate(node)) {
13
+ return true;
14
+ }
15
+ for (const [key, value] of Object.entries(node)) {
16
+ if (DIAGNOSTIC_KEYS.has(key)) {
17
+ continue;
18
+ }
19
+ const children = Array.isArray(value) ? value : [value];
20
+ for (const child of children) {
21
+ if (isNode(child) && some(child, predicate)) {
22
+ return true;
23
+ }
24
+ }
25
+ }
26
+ return false;
27
+ }
28
+
29
+ function isIdentifier(node, name) {
30
+ return (
31
+ node?.type === "Identifier" && (name === undefined || node.name === name)
32
+ );
33
+ }
34
+
35
+ function propertyName(node) {
36
+ if (node?.type !== "MemberExpression") {
37
+ return undefined;
38
+ }
39
+ if (!node.computed && node.property.type === "Identifier") {
40
+ return node.property.name;
41
+ }
42
+ if (
43
+ node.computed &&
44
+ node.property.type === "Literal" &&
45
+ typeof node.property.value === "string"
46
+ ) {
47
+ return node.property.value;
48
+ }
49
+ return undefined;
50
+ }
51
+
52
+ function isMemberCall(node, objectName, method) {
53
+ return (
54
+ node?.type === "CallExpression" &&
55
+ propertyName(node.callee) === method &&
56
+ isIdentifier(node.callee.object, objectName)
57
+ );
58
+ }
59
+
60
+ function typeArgumentsOf(node) {
61
+ return (node.typeArguments ?? node.typeParameters)?.params ?? [];
62
+ }
63
+
64
+ function isTypeReference(node, name) {
65
+ return node?.type === "TSTypeReference" && isIdentifier(node.typeName, name);
66
+ }
67
+
68
+ function keyName(property) {
69
+ if (property.type !== "Property") {
70
+ return undefined;
71
+ }
72
+ if (!property.computed && property.key.type === "Identifier") {
73
+ return property.key.name;
74
+ }
75
+ if (
76
+ property.key.type === "Literal" &&
77
+ typeof property.key.value === "string"
78
+ ) {
79
+ return property.key.value;
80
+ }
81
+ return undefined;
82
+ }
83
+
84
+ function findProperty(object, name) {
85
+ return object.properties.find((property) => keyName(property) === name);
86
+ }
87
+
88
+ function isEmptyValue(node) {
89
+ return (
90
+ (node?.type === "Literal" && node.value === null && node.raw === "null") ||
91
+ (node?.type === "ArrayExpression" && node.elements.length === 0) ||
92
+ (node?.type === "ObjectExpression" && node.properties.length === 0)
93
+ );
94
+ }
95
+
96
+ function isEmptyBlock(node, sourceCode) {
97
+ return (
98
+ node?.type === "BlockStatement" &&
99
+ sourceCode.getText(node).replace(/\s/g, "") === "{}"
100
+ );
101
+ }
102
+
103
+ function isFunction(node) {
104
+ return (
105
+ node?.type === "ArrowFunctionExpression" ||
106
+ node?.type === "FunctionExpression"
107
+ );
108
+ }
109
+
110
+ function nearestFunctionBody(node) {
111
+ for (let current = node.parent; current; current = current.parent) {
112
+ if (
113
+ current.type === "FunctionDeclaration" ||
114
+ current.type === "FunctionExpression" ||
115
+ current.type === "ArrowFunctionExpression"
116
+ ) {
117
+ return current.body.type === "BlockStatement" ? current.body : undefined;
118
+ }
119
+ }
120
+ return undefined;
121
+ }
122
+
123
+ function rule(description, create) {
124
+ return { meta: { type: "problem", docs: { description } }, create };
125
+ }
126
+
127
+ const noReexports = rule(
128
+ "Disallow exports that only forward an existing binding",
129
+ (context) => {
130
+ const report = (node) =>
131
+ context.report({ node, message: "Re-export is prohibited" });
132
+ return {
133
+ ExportDefaultDeclaration(node) {
134
+ if (isIdentifier(node.declaration)) {
135
+ report(node);
136
+ }
137
+ },
138
+ ExportNamedDeclaration(node) {
139
+ const declaration = node.declaration;
140
+ if (declaration?.type === "VariableDeclaration") {
141
+ if (
142
+ declaration.declarations.some((declarator) =>
143
+ isIdentifier(declarator.init),
144
+ )
145
+ ) {
146
+ report(node);
147
+ }
148
+ return;
149
+ }
150
+ if (
151
+ declaration?.type === "TSTypeAliasDeclaration" &&
152
+ declaration.typeParameters == null &&
153
+ declaration.typeAnnotation.type === "TSTypeReference" &&
154
+ isIdentifier(declaration.typeAnnotation.typeName) &&
155
+ typeArgumentsOf(declaration.typeAnnotation).length === 0
156
+ ) {
157
+ report(node);
158
+ }
159
+ },
160
+ };
161
+ },
162
+ );
163
+
164
+ const PLACEHOLDER = /not[ -]?implemented|todo|coming soon/i;
165
+
166
+ const noPlaceholderImplementation = rule(
167
+ "Disallow placeholder implementations",
168
+ (context) => ({
169
+ ThrowStatement(node) {
170
+ if (
171
+ node.argument?.type === "NewExpression" &&
172
+ isIdentifier(node.argument.callee) &&
173
+ PLACEHOLDER.test(context.sourceCode.getText(node))
174
+ ) {
175
+ context.report({
176
+ node,
177
+ message: "Remove the placeholder implementation",
178
+ });
179
+ }
180
+ },
181
+ }),
182
+ );
183
+
184
+ const SILENT_FALLBACK =
185
+ "Do not leave incomplete implementations or silently replace failures";
186
+ const CONSOLE_METHODS = new Set(["debug", "error", "info", "log", "warn"]);
187
+
188
+ const noIncompleteImplementation = rule(
189
+ "Disallow handlers that silently replace failures",
190
+ (context) => ({
191
+ CallExpression(node) {
192
+ if (
193
+ propertyName(node.callee) !== "catch" ||
194
+ node.arguments.length !== 1
195
+ ) {
196
+ return;
197
+ }
198
+ const [handler] = node.arguments;
199
+ if (
200
+ handler.type === "ArrowFunctionExpression" &&
201
+ handler.params.length <= 1 &&
202
+ handler.params.every((param) => isIdentifier(param)) &&
203
+ handler.body.type !== "BlockStatement" &&
204
+ isEmptyValue(handler.body)
205
+ ) {
206
+ context.report({ node, message: SILENT_FALLBACK });
207
+ }
208
+ },
209
+ TryStatement(node) {
210
+ const body = node.handler?.body.body;
211
+ if (
212
+ node.finalizer == null &&
213
+ node.handler?.param != null &&
214
+ body?.length === 1 &&
215
+ body[0].type === "ReturnStatement" &&
216
+ isEmptyValue(body[0].argument)
217
+ ) {
218
+ context.report({ node, message: SILENT_FALLBACK });
219
+ }
220
+ },
221
+ CatchClause(node) {
222
+ const statements = node.body.body;
223
+ if (
224
+ statements.length !== 1 ||
225
+ statements[0].type !== "ExpressionStatement"
226
+ ) {
227
+ return;
228
+ }
229
+ const call = statements[0].expression;
230
+ if (
231
+ call.type === "CallExpression" &&
232
+ isIdentifier(call.callee.object, "console") &&
233
+ CONSOLE_METHODS.has(propertyName(call.callee))
234
+ ) {
235
+ context.report({ node, message: SILENT_FALLBACK });
236
+ }
237
+ },
238
+ }),
239
+ );
240
+
241
+ const noDoubleAssertion = rule(
242
+ "Disallow chained type assertions through unknown",
243
+ (context) => ({
244
+ TSAsExpression(node) {
245
+ if (
246
+ node.expression.type === "TSAsExpression" &&
247
+ node.expression.typeAnnotation.type === "TSUnknownKeyword"
248
+ ) {
249
+ context.report({
250
+ node,
251
+ message: "Do not bypass type safety with a chained assertion",
252
+ });
253
+ }
254
+ },
255
+ }),
256
+ );
257
+
258
+ function isJsonCall(node) {
259
+ return (
260
+ node?.type === "CallExpression" &&
261
+ propertyName(node.callee) === "json" &&
262
+ node.arguments.length === 0
263
+ );
264
+ }
265
+
266
+ const noTypeSafetyBypass = rule(
267
+ "Disallow unchecked assertions and needless reflection",
268
+ (context) => ({
269
+ TSAsExpression(node) {
270
+ const type = node.typeAnnotation;
271
+ const expression = node.expression;
272
+ const [first, second] = typeArgumentsOf(type);
273
+ if (
274
+ isTypeReference(type, "Record") &&
275
+ first?.type === "TSStringKeyword" &&
276
+ second?.type === "TSUnknownKeyword"
277
+ ) {
278
+ context.report({
279
+ node,
280
+ message:
281
+ "Validate the object shape instead of asserting a generic dictionary",
282
+ });
283
+ return;
284
+ }
285
+ if (
286
+ isMemberCall(expression, "JSON", "parse") &&
287
+ type.type !== "TSUnknownKeyword"
288
+ ) {
289
+ context.report({
290
+ node,
291
+ message: "Validate parsed JSON before asserting its type",
292
+ });
293
+ return;
294
+ }
295
+ if (
296
+ (isJsonCall(expression) && isTypeReference(type, "Promise")) ||
297
+ (expression.type === "AwaitExpression" &&
298
+ isJsonCall(expression.argument) &&
299
+ type.type !== "TSUnknownKeyword")
300
+ ) {
301
+ context.report({
302
+ node,
303
+ message: "Validate the JSON response before asserting its type",
304
+ });
305
+ }
306
+ },
307
+ CallExpression(node) {
308
+ const args = node.arguments;
309
+ if (
310
+ isMemberCall(node, "Reflect", "get") &&
311
+ args.length === 2 &&
312
+ args[1].type === "Literal" &&
313
+ typeof args[1].value === "string"
314
+ ) {
315
+ context.report({
316
+ node,
317
+ message:
318
+ "Use direct property access when the property name is static",
319
+ });
320
+ return;
321
+ }
322
+ if (
323
+ isMemberCall(node, "Reflect", "apply") &&
324
+ args.length === 3 &&
325
+ isIdentifier(args[1], "undefined") &&
326
+ args[2].type === "ArrayExpression"
327
+ ) {
328
+ context.report({
329
+ node,
330
+ message: "Call the function directly when no receiver is required",
331
+ });
332
+ }
333
+ },
334
+ }),
335
+ );
336
+
337
+ const noUnsafeErrnoAssertion = rule(
338
+ "Disallow reading errno fields through an unchecked assertion",
339
+ (context) => ({
340
+ MemberExpression(node) {
341
+ const object = node.object;
342
+ if (
343
+ !node.optional &&
344
+ propertyName(node) === "code" &&
345
+ object.type === "TSAsExpression" &&
346
+ object.typeAnnotation.type === "TSTypeReference" &&
347
+ context.sourceCode.getText(object.typeAnnotation) ===
348
+ "NodeJS.ErrnoException"
349
+ ) {
350
+ context.report({
351
+ node,
352
+ message: "Validate the error shape before reading errno properties",
353
+ });
354
+ }
355
+ },
356
+ }),
357
+ );
358
+
359
+ const TEST_FUNCTIONS = new Set(["test", "it"]);
360
+ const SAME_VALUE_MATCHERS = new Set(["toBe", "toEqual", "toStrictEqual"]);
361
+
362
+ const noMeaninglessTest = rule("Disallow empty tests", (context) => ({
363
+ CallExpression(node) {
364
+ const [, body] = node.arguments;
365
+ if (
366
+ isIdentifier(node.callee) &&
367
+ TEST_FUNCTIONS.has(node.callee.name) &&
368
+ isFunction(body) &&
369
+ body.params.length === 0 &&
370
+ isEmptyBlock(body.body, context.sourceCode)
371
+ ) {
372
+ context.report({ node, message: "Remove empty tests" });
373
+ }
374
+ },
375
+ }));
376
+
377
+ const noTautologicalAssertion = rule(
378
+ "Disallow assertions that compare a value with itself",
379
+ (context) => ({
380
+ CallExpression(node) {
381
+ const expectCall = node.callee.object;
382
+ if (
383
+ !SAME_VALUE_MATCHERS.has(propertyName(node.callee)) ||
384
+ node.arguments.length !== 1 ||
385
+ expectCall?.type !== "CallExpression" ||
386
+ !isIdentifier(expectCall.callee, "expect") ||
387
+ expectCall.arguments.length !== 1
388
+ ) {
389
+ return;
390
+ }
391
+ const [actual] = expectCall.arguments;
392
+ const [expected] = node.arguments;
393
+ const comparable = (value) =>
394
+ value.type === "Identifier" ||
395
+ (value.type === "Literal" && value.regex === undefined);
396
+ if (
397
+ comparable(actual) &&
398
+ context.sourceCode.getText(actual) ===
399
+ context.sourceCode.getText(expected)
400
+ ) {
401
+ context.report({ node, message: "Remove tautological assertions" });
402
+ }
403
+ },
404
+ }),
405
+ );
406
+
407
+ const noUselessAbstraction = rule(
408
+ "Disallow pass-through wrappers",
409
+ (context) => ({
410
+ VariableDeclaration(node) {
411
+ if (node.kind !== "const" || node.declarations.length !== 1) {
412
+ return;
413
+ }
414
+ const wrapper = node.declarations[0].init;
415
+ const [param] = wrapper?.params ?? [];
416
+ if (
417
+ wrapper?.type !== "ArrowFunctionExpression" ||
418
+ wrapper.params.length !== 1 ||
419
+ !isIdentifier(param) ||
420
+ param.typeAnnotation != null
421
+ ) {
422
+ return;
423
+ }
424
+ const body =
425
+ wrapper.async && wrapper.body.type === "AwaitExpression"
426
+ ? wrapper.body.argument
427
+ : wrapper.body;
428
+ if (
429
+ body.type === "CallExpression" &&
430
+ body.arguments.length === 1 &&
431
+ isIdentifier(body.arguments[0], wrapper.params[0].name)
432
+ ) {
433
+ context.report({
434
+ node,
435
+ message: "Remove pass-through wrappers that add no behavior",
436
+ });
437
+ }
438
+ },
439
+ }),
440
+ );
441
+
442
+ const noTernaryStatement = rule(
443
+ "Disallow ternaries used as statements",
444
+ (context) => ({
445
+ ExpressionStatement(node) {
446
+ if (node.expression.type === "ConditionalExpression") {
447
+ context.report({
448
+ node,
449
+ message:
450
+ "Use an if/else statement instead of a ternary operator for side effects",
451
+ });
452
+ }
453
+ },
454
+ }),
455
+ );
456
+
457
+ const requireFetchAbortSignal = rule(
458
+ "Require an AbortSignal on fetch calls",
459
+ (context) => ({
460
+ CallExpression(node) {
461
+ if (!isIdentifier(node.callee, "fetch")) {
462
+ return;
463
+ }
464
+ const [, init] = node.arguments;
465
+ if (
466
+ node.arguments.length === 1 ||
467
+ (node.arguments.length === 2 &&
468
+ init.type === "ObjectExpression" &&
469
+ findProperty(init, "signal") === undefined)
470
+ ) {
471
+ context.report({
472
+ node,
473
+ message: "Pass an AbortSignal to external fetch calls",
474
+ });
475
+ }
476
+ },
477
+ }),
478
+ );
479
+
480
+ function abortedController(init) {
481
+ const [callback] = init?.arguments ?? [];
482
+ if (
483
+ init?.type !== "CallExpression" ||
484
+ !isIdentifier(init.callee, "setTimeout") ||
485
+ init.arguments.length !== 2 ||
486
+ callback.type !== "ArrowFunctionExpression" ||
487
+ callback.params.length !== 0 ||
488
+ callback.body.type !== "CallExpression" ||
489
+ propertyName(callback.body.callee) !== "abort" ||
490
+ !isIdentifier(callback.body.callee.object)
491
+ ) {
492
+ return undefined;
493
+ }
494
+ return callback.body.callee.object.name;
495
+ }
496
+
497
+ const requireAbortTimeoutCleanup = rule(
498
+ "Require clearing the timer that aborts a fetch",
499
+ (context) => ({
500
+ VariableDeclaration(node) {
501
+ if (node.kind !== "const" || node.declarations.length !== 1) {
502
+ return;
503
+ }
504
+ const [declarator] = node.declarations;
505
+ const controller = abortedController(declarator.init);
506
+ const scope = nearestFunctionBody(node);
507
+ if (
508
+ controller === undefined ||
509
+ !isIdentifier(declarator.id) ||
510
+ scope === undefined
511
+ ) {
512
+ return;
513
+ }
514
+ const timer = declarator.id.name;
515
+ const usesSignal = some(scope, (candidate) => {
516
+ if (
517
+ !isIdentifier(candidate.callee, "fetch") ||
518
+ candidate.arguments?.[1]?.type !== "ObjectExpression"
519
+ ) {
520
+ return false;
521
+ }
522
+ const signal = findProperty(candidate.arguments[1], "signal")?.value;
523
+ return (
524
+ propertyName(signal) === "signal" &&
525
+ isIdentifier(signal.object, controller)
526
+ );
527
+ });
528
+ const clears = some(
529
+ scope,
530
+ (candidate) =>
531
+ candidate.type === "CallExpression" &&
532
+ isIdentifier(candidate.callee, "clearTimeout") &&
533
+ isIdentifier(candidate.arguments[0], timer),
534
+ );
535
+ if (usesSignal && !clears) {
536
+ context.report({
537
+ node,
538
+ message: "Clear the AbortController timeout after using its signal",
539
+ });
540
+ }
541
+ },
542
+ }),
543
+ );
544
+
545
+ const DISCORD_METHODS = new Set([
546
+ "reply",
547
+ "send",
548
+ "editReply",
549
+ "followUp",
550
+ "update",
551
+ ]);
552
+
553
+ function isCreateHash(node) {
554
+ return (
555
+ node?.type === "CallExpression" &&
556
+ (isIdentifier(node.callee, "createHash") ||
557
+ propertyName(node.callee) === "createHash")
558
+ );
559
+ }
560
+
561
+ function isDynamic(node) {
562
+ return node?.type === "Identifier" || node?.type === "TemplateLiteral";
563
+ }
564
+
565
+ const noUnsafeDynamicDiscordMessage = rule(
566
+ "Require an allowedMentions policy for dynamic Discord content",
567
+ (context) => ({
568
+ CallExpression(node) {
569
+ if (
570
+ !DISCORD_METHODS.has(propertyName(node.callee)) ||
571
+ node.callee.computed ||
572
+ isCreateHash(node.callee.object)
573
+ ) {
574
+ return;
575
+ }
576
+ if (node.arguments.length !== 1) {
577
+ return;
578
+ }
579
+ const [payload] = node.arguments;
580
+ if (isDynamic(payload)) {
581
+ context.report({
582
+ node,
583
+ message:
584
+ "Wrap dynamic Discord content in a payload that disables mentions",
585
+ });
586
+ return;
587
+ }
588
+ if (
589
+ payload.type === "ObjectExpression" &&
590
+ isDynamic(findProperty(payload, "content")?.value) &&
591
+ findProperty(payload, "allowedMentions") === undefined
592
+ ) {
593
+ context.report({
594
+ node,
595
+ message: "Set allowedMentions when sending dynamic Discord content",
596
+ });
597
+ }
598
+ },
599
+ }),
600
+ );
601
+
602
+ const ADAPTER_PATH = /(?:integrations|adapters)\//;
603
+
604
+ const noAdapterImport = rule(
605
+ "Disallow importing adapter modules from a scoped layer",
606
+ (context) => ({
607
+ ImportDeclaration(node) {
608
+ if (ADAPTER_PATH.test(node.source.value)) {
609
+ context.report({
610
+ node,
611
+ message: "Import the port instead of the adapter module",
612
+ });
613
+ }
614
+ },
615
+ }),
616
+ );
617
+
618
+ const noDirectResponse = rule(
619
+ "Disallow constructing a Response outside the shared helper",
620
+ (context) => {
621
+ const report = (node) =>
622
+ context.report({
623
+ node,
624
+ message: "Build the response with the shared response helper",
625
+ });
626
+ return {
627
+ NewExpression(node) {
628
+ if (isIdentifier(node.callee, "Response")) {
629
+ report(node);
630
+ }
631
+ },
632
+ CallExpression(node) {
633
+ const count = node.arguments.length;
634
+ if (
635
+ (isMemberCall(node, "Response", "json") ||
636
+ isMemberCall(node, "Response", "redirect")) &&
637
+ count >= 1 &&
638
+ count <= 2
639
+ ) {
640
+ report(node);
641
+ }
642
+ },
643
+ };
644
+ },
645
+ );
646
+
647
+ export default {
648
+ meta: { name: "yuu1111" },
649
+ rules: {
650
+ "no-reexports": noReexports,
651
+ "no-placeholder-implementation": noPlaceholderImplementation,
652
+ "no-incomplete-implementation": noIncompleteImplementation,
653
+ "no-double-assertion": noDoubleAssertion,
654
+ "no-type-safety-bypass": noTypeSafetyBypass,
655
+ "no-unsafe-errno-assertion": noUnsafeErrnoAssertion,
656
+ "no-meaningless-test": noMeaninglessTest,
657
+ "no-tautological-assertion": noTautologicalAssertion,
658
+ "no-useless-abstraction": noUselessAbstraction,
659
+ "no-ternary-statement": noTernaryStatement,
660
+ "require-fetch-abort-signal": requireFetchAbortSignal,
661
+ "require-abort-timeout-cleanup": requireAbortTimeoutCleanup,
662
+ "no-unsafe-dynamic-discord-message": noUnsafeDynamicDiscordMessage,
663
+ "no-adapter-import": noAdapterImport,
664
+ "no-direct-response": noDirectResponse,
665
+ },
666
+ };
@@ -0,0 +1,5 @@
1
+ {
2
+ "jsPlugins": [
3
+ "./plugin.js"
4
+ ]
5
+ }
package/package.json CHANGED
@@ -1,6 +1,43 @@
1
- {
2
- "name": "@yuu1111/oxc-config",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
1
+ {
2
+ "name": "@yuu1111/oxc-config",
3
+ "version": "0.0.0",
4
+ "description": "Shared Oxlint and Oxfmt configuration",
5
+ "homepage": "https://yuu1111.github.io/configs/packages/config/oxc-config/",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/yuu1111/configs.git",
9
+ "directory": "packages/config/oxc-config"
10
+ },
11
+ "type": "module",
12
+ "exports": {
13
+ "./oxfmt": {
14
+ "types": "./oxfmt.d.ts",
15
+ "default": "./oxfmt.js"
16
+ },
17
+ "./oxlint/plugin": "./oxlint/plugin.js",
18
+ "./package.json": "./package.json"
19
+ },
20
+ "files": [
21
+ "README.ja.md",
22
+ "oxfmt.d.ts",
23
+ "oxfmt.js",
24
+ "oxlint"
25
+ ],
26
+ "keywords": [
27
+ "oxlint",
28
+ "oxfmt",
29
+ "config"
30
+ ],
31
+ "peerDependencies": {
32
+ "oxfmt": ">=0.74.0",
33
+ "oxlint": ">=1.89.0"
34
+ },
35
+ "peerDependenciesMeta": {
36
+ "oxfmt": {
37
+ "optional": true
38
+ },
39
+ "oxlint": {
40
+ "optional": true
41
+ }
42
+ }
43
+ }