@ts-kizuna/eslint-plugin 1.49.5
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/LICENSE.MD +9 -0
- package/README.md +23 -0
- package/dist/index.cjs +257 -0
- package/dist/index.d.cts +9 -0
- package/dist/index.d.mts +10 -0
- package/dist/index.mjs +232 -0
- package/package.json +78 -0
package/LICENSE.MD
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Sondre Ørland
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# @ts-kizuna/eslint-plugin
|
|
2
|
+
|
|
3
|
+
`@ts-kizuna/eslint-plugin` catches ts-kizuna mistakes in your editor, as you type. These are things the type system cannot express on its own.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
pnpm add -D @ts-kizuna/eslint-plugin
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
Add the recommended config to your `eslint.config.js`:
|
|
14
|
+
|
|
15
|
+
```js
|
|
16
|
+
import kizuna from '@ts-kizuna/eslint-plugin';
|
|
17
|
+
|
|
18
|
+
export default [kizuna.configs.recommended];
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Documentation
|
|
22
|
+
|
|
23
|
+
[ESLint plugin](https://ts-kizuna.com/docs/eslint)
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
//#region \0rolldown/runtime.js
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
10
|
+
key = keys[i];
|
|
11
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
12
|
+
get: ((k) => from[k]).bind(null, key),
|
|
13
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
19
|
+
value: mod,
|
|
20
|
+
enumerable: true
|
|
21
|
+
}) : target, mod));
|
|
22
|
+
//#endregion
|
|
23
|
+
let _typescript_eslint_parser = require("@typescript-eslint/parser");
|
|
24
|
+
_typescript_eslint_parser = __toESM(_typescript_eslint_parser, 1);
|
|
25
|
+
let node_path = require("node:path");
|
|
26
|
+
node_path = __toESM(node_path, 1);
|
|
27
|
+
let _typescript_eslint_utils = require("@typescript-eslint/utils");
|
|
28
|
+
let typescript = require("typescript");
|
|
29
|
+
typescript = __toESM(typescript, 1);
|
|
30
|
+
let _ts_kizuna_core_authoring_names = require("@ts-kizuna/core/authoring-names");
|
|
31
|
+
//#region package.json
|
|
32
|
+
var name = "@ts-kizuna/eslint-plugin";
|
|
33
|
+
var version = "1.49.4";
|
|
34
|
+
//#endregion
|
|
35
|
+
//#region src/schema-violations.ts
|
|
36
|
+
/**
|
|
37
|
+
* A JSDoc inline tag, `{@link …}`, `{@linkcode …}`, any `{@tag}`.
|
|
38
|
+
*/
|
|
39
|
+
const JSDOC_INLINE_TAG = /\{@[a-zA-Z][\w-]*/;
|
|
40
|
+
/**
|
|
41
|
+
* Walks a schema expression, following `z.object` shapes, nested objects, and identifier
|
|
42
|
+
* references to other schemas (across files), and returns every issue it carries, each
|
|
43
|
+
* paired with the node it sits on. The walk is bounded by a visited set so cycles end.
|
|
44
|
+
*/
|
|
45
|
+
const collectSchemaIssues = (root, resolve) => {
|
|
46
|
+
const violations = [];
|
|
47
|
+
const reported = /* @__PURE__ */ new Set();
|
|
48
|
+
const visited = /* @__PURE__ */ new Set();
|
|
49
|
+
const add = (issue, node, viaReference) => {
|
|
50
|
+
const key = `${issue}@${node.getSourceFile().fileName}:${node.pos}`;
|
|
51
|
+
if (reported.has(key)) return;
|
|
52
|
+
reported.add(key);
|
|
53
|
+
violations.push({
|
|
54
|
+
issue,
|
|
55
|
+
node,
|
|
56
|
+
viaReference
|
|
57
|
+
});
|
|
58
|
+
};
|
|
59
|
+
const checkFieldJsDoc = (property, viaReference) => {
|
|
60
|
+
const deprecatedTags = typescript.default.getJSDocTags(property).filter((tag) => tag.tagName.text === "deprecated");
|
|
61
|
+
if (deprecatedTags.length > 1) add("duplicate-deprecated", property, viaReference);
|
|
62
|
+
for (const tag of deprecatedTags) if (JSDOC_INLINE_TAG.test(typescript.default.getTextOfJSDocComment(tag.comment) ?? "")) add("jsdoc-tag", property, viaReference);
|
|
63
|
+
};
|
|
64
|
+
const walk = (node, viaReference) => {
|
|
65
|
+
if (typescript.default.isPropertyAccessExpression(node) && node.name.text === "coerce" && typescript.default.isIdentifier(node.expression) && node.expression.text === "z") add("coerce", node, viaReference);
|
|
66
|
+
if (typescript.default.isObjectLiteralExpression(node)) {
|
|
67
|
+
for (const property of node.properties) if (typescript.default.isPropertyAssignment(property) || typescript.default.isShorthandPropertyAssignment(property)) checkFieldJsDoc(property, viaReference);
|
|
68
|
+
}
|
|
69
|
+
if (typescript.default.isIdentifier(node)) {
|
|
70
|
+
const target = resolve(node);
|
|
71
|
+
if (target && !visited.has(target)) {
|
|
72
|
+
visited.add(target);
|
|
73
|
+
walk(target, true);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
typescript.default.forEachChild(node, (child) => walk(child, viaReference));
|
|
77
|
+
};
|
|
78
|
+
visited.add(root);
|
|
79
|
+
walk(root, false);
|
|
80
|
+
return violations;
|
|
81
|
+
};
|
|
82
|
+
//#endregion
|
|
83
|
+
//#region src/rules/no-unsupported-schema.ts
|
|
84
|
+
const SCHEMA_KEYS = new Set([
|
|
85
|
+
"body",
|
|
86
|
+
"query",
|
|
87
|
+
"pathParams",
|
|
88
|
+
"headers"
|
|
89
|
+
]);
|
|
90
|
+
const createCheckerResolver = (checker) => (identifier) => {
|
|
91
|
+
const symbol = checker.getSymbolAtLocation(identifier);
|
|
92
|
+
if (!symbol) return void 0;
|
|
93
|
+
return (symbol.flags & typescript.default.SymbolFlags.Alias ? checker.getAliasedSymbol(symbol) : symbol).declarations?.find(typescript.default.isVariableDeclaration)?.initializer;
|
|
94
|
+
};
|
|
95
|
+
const createSourceResolver = () => {
|
|
96
|
+
const sourceFileCache = /* @__PURE__ */ new Map();
|
|
97
|
+
const optionsCache = /* @__PURE__ */ new Map();
|
|
98
|
+
const readSourceFile = (fileName) => {
|
|
99
|
+
if (sourceFileCache.has(fileName)) return sourceFileCache.get(fileName);
|
|
100
|
+
const text = typescript.default.sys.readFile(fileName);
|
|
101
|
+
const sourceFile = text === void 0 ? void 0 : typescript.default.createSourceFile(fileName, text, typescript.default.ScriptTarget.Latest, true);
|
|
102
|
+
sourceFileCache.set(fileName, sourceFile);
|
|
103
|
+
return sourceFile;
|
|
104
|
+
};
|
|
105
|
+
const compilerOptionsFor = (containingFile) => {
|
|
106
|
+
const configPath = typescript.default.findConfigFile(node_path.dirname(containingFile), typescript.default.sys.fileExists);
|
|
107
|
+
const key = configPath ?? "";
|
|
108
|
+
const cached = optionsCache.get(key);
|
|
109
|
+
if (cached) return cached;
|
|
110
|
+
const options = configPath ? typescript.default.parseJsonConfigFileContent(typescript.default.readConfigFile(configPath, typescript.default.sys.readFile).config, typescript.default.sys, node_path.dirname(configPath)).options : {
|
|
111
|
+
allowJs: true,
|
|
112
|
+
moduleResolution: typescript.default.ModuleResolutionKind.Bundler
|
|
113
|
+
};
|
|
114
|
+
optionsCache.set(key, options);
|
|
115
|
+
return options;
|
|
116
|
+
};
|
|
117
|
+
const constNamed = (sourceFile, name) => {
|
|
118
|
+
for (const statement of sourceFile.statements) {
|
|
119
|
+
if (!typescript.default.isVariableStatement(statement)) continue;
|
|
120
|
+
for (const declaration of statement.declarationList.declarations) if (typescript.default.isIdentifier(declaration.name) && declaration.name.text === name && declaration.initializer) return declaration.initializer;
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
const importedFrom = (sourceFile, name) => {
|
|
124
|
+
for (const statement of sourceFile.statements) {
|
|
125
|
+
if (!typescript.default.isImportDeclaration(statement) || !typescript.default.isStringLiteral(statement.moduleSpecifier)) continue;
|
|
126
|
+
const named = statement.importClause?.namedBindings;
|
|
127
|
+
if (!named || !typescript.default.isNamedImports(named)) continue;
|
|
128
|
+
for (const element of named.elements) if (element.name.text === name) return {
|
|
129
|
+
specifier: statement.moduleSpecifier.text,
|
|
130
|
+
exportedName: (element.propertyName ?? element.name).text
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
return (identifier) => {
|
|
135
|
+
const sourceFile = identifier.getSourceFile();
|
|
136
|
+
const local = constNamed(sourceFile, identifier.text);
|
|
137
|
+
if (local) return local;
|
|
138
|
+
const imported = importedFrom(sourceFile, identifier.text);
|
|
139
|
+
if (!imported) return void 0;
|
|
140
|
+
const resolved = typescript.default.resolveModuleName(imported.specifier, sourceFile.fileName, compilerOptionsFor(sourceFile.fileName), typescript.default.sys).resolvedModule?.resolvedFileName;
|
|
141
|
+
if (!resolved) return void 0;
|
|
142
|
+
const target = readSourceFile(resolved);
|
|
143
|
+
return target ? constNamed(target, imported.exportedName) : void 0;
|
|
144
|
+
};
|
|
145
|
+
};
|
|
146
|
+
const MESSAGE_IDS = {
|
|
147
|
+
coerce: ["coerce", "coerceReference"],
|
|
148
|
+
"jsdoc-tag": ["jsdocTag", "jsdocTagReference"],
|
|
149
|
+
"duplicate-deprecated": ["duplicateDeprecated", "duplicateDeprecatedReference"]
|
|
150
|
+
};
|
|
151
|
+
const calleeName = (node) => {
|
|
152
|
+
const { callee } = node;
|
|
153
|
+
if (callee.type === "Identifier") return callee.name;
|
|
154
|
+
if (callee.type === "MemberExpression" && callee.property.type === "Identifier") return callee.property.name;
|
|
155
|
+
};
|
|
156
|
+
const schemaNodesOf = (call) => {
|
|
157
|
+
const name = calleeName(call);
|
|
158
|
+
if (name === _ts_kizuna_core_authoring_names.AUTHORING_NAMES.model) {
|
|
159
|
+
const config = call.arguments[0];
|
|
160
|
+
if (config?.type !== "ObjectExpression") return [];
|
|
161
|
+
const schema = config.properties.find((property) => property.type === "Property" && property.key.type === "Identifier" && property.key.name === "schema");
|
|
162
|
+
return schema ? [schema.value] : [];
|
|
163
|
+
}
|
|
164
|
+
if (name !== _ts_kizuna_core_authoring_names.AUTHORING_NAMES.routes && name !== _ts_kizuna_core_authoring_names.AUTHORING_NAMES.contract) return [];
|
|
165
|
+
const nodes = [];
|
|
166
|
+
const visit = (node) => {
|
|
167
|
+
if (node.type === "Property") {
|
|
168
|
+
const isSchemaField = node.key.type === "Identifier" && SCHEMA_KEYS.has(node.key.name);
|
|
169
|
+
const isStatusResponse = node.key.type === "Literal" && typeof node.key.value === "number";
|
|
170
|
+
if (isSchemaField || isStatusResponse) nodes.push(node.value);
|
|
171
|
+
}
|
|
172
|
+
for (const [key, child] of Object.entries(node)) {
|
|
173
|
+
if (key === "parent") continue;
|
|
174
|
+
if (Array.isArray(child)) child.forEach((item) => item?.type && visit(item));
|
|
175
|
+
else if (child?.type) visit(child);
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
for (const argument of call.arguments) visit(argument);
|
|
179
|
+
return nodes;
|
|
180
|
+
};
|
|
181
|
+
const noUnsupportedSchema = _typescript_eslint_utils.ESLintUtils.RuleCreator.withoutDocs({
|
|
182
|
+
meta: {
|
|
183
|
+
type: "problem",
|
|
184
|
+
docs: { description: "Disallow constructs unsupported by ts-kizuna (z.coerce, @deprecated with JSDoc tags) in contract and model schemas, including imported ones." },
|
|
185
|
+
messages: {
|
|
186
|
+
coerce: "z.coerce is not supported in a ts-kizuna schema. kizuna coerces query, path, and header params automatically, use z.number(), z.date(), or z.bigint() instead.",
|
|
187
|
+
jsdocTag: "This @deprecated message uses JSDoc inline tags like {@link}. kizuna surfaces deprecation text verbatim to generated clients that cannot parse it, use plain text instead (backticks are fine).",
|
|
188
|
+
duplicateDeprecated: "This field has more than one @deprecated tag. kizuna serializes only the first and silently drops the rest, collapse them into a single message.",
|
|
189
|
+
coerceReference: "This schema uses z.coerce, which ts-kizuna does not support. kizuna coerces query, path, and header params automatically, use z.number(), z.date(), or z.bigint() instead.",
|
|
190
|
+
jsdocTagReference: "This schema has a @deprecated message with JSDoc inline tags like {@link}. kizuna surfaces deprecation text verbatim to generated clients that cannot parse it, use plain text instead (backticks are fine).",
|
|
191
|
+
duplicateDeprecatedReference: "This schema has a field with more than one @deprecated tag. kizuna serializes only the first and silently drops the rest, collapse them into a single message."
|
|
192
|
+
},
|
|
193
|
+
schema: []
|
|
194
|
+
},
|
|
195
|
+
defaultOptions: [],
|
|
196
|
+
create(context) {
|
|
197
|
+
const services = _typescript_eslint_utils.ESLintUtils.getParserServices(context, true);
|
|
198
|
+
const resolve = services.program ? createCheckerResolver(services.program.getTypeChecker()) : createSourceResolver();
|
|
199
|
+
const reported = /* @__PURE__ */ new Set();
|
|
200
|
+
return { CallExpression(call) {
|
|
201
|
+
for (const schemaNode of schemaNodesOf(call)) {
|
|
202
|
+
const tsNode = services.esTreeNodeToTSNodeMap.get(schemaNode);
|
|
203
|
+
for (const { issue, node, viaReference } of collectSchemaIssues(tsNode, resolve)) {
|
|
204
|
+
const reportNode = (viaReference ? void 0 : services.tsNodeToESTreeNodeMap.get(node)) ?? schemaNode;
|
|
205
|
+
const messageId = MESSAGE_IDS[issue][viaReference ? 1 : 0];
|
|
206
|
+
const key = `${messageId}@${reportNode.range[0]}`;
|
|
207
|
+
if (reported.has(key)) continue;
|
|
208
|
+
reported.add(key);
|
|
209
|
+
context.report({
|
|
210
|
+
node: reportNode,
|
|
211
|
+
messageId
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
} };
|
|
216
|
+
}
|
|
217
|
+
});
|
|
218
|
+
//#endregion
|
|
219
|
+
//#region src/index.ts
|
|
220
|
+
const plugin = {
|
|
221
|
+
meta: {
|
|
222
|
+
name,
|
|
223
|
+
version,
|
|
224
|
+
namespace: "@ts-kizuna"
|
|
225
|
+
},
|
|
226
|
+
rules: { "no-unsupported-schema": noUnsupportedSchema }
|
|
227
|
+
};
|
|
228
|
+
/**
|
|
229
|
+
* Flat config enabling every ts-kizuna rule.
|
|
230
|
+
*
|
|
231
|
+
* ```js
|
|
232
|
+
* import kizuna from '@ts-kizuna/eslint-plugin';
|
|
233
|
+
*
|
|
234
|
+
* export default [kizuna.configs.recommended];
|
|
235
|
+
* ```
|
|
236
|
+
*/
|
|
237
|
+
const recommended = {
|
|
238
|
+
name: "@ts-kizuna/recommended",
|
|
239
|
+
files: [
|
|
240
|
+
"**/*.ts",
|
|
241
|
+
"**/*.cts",
|
|
242
|
+
"**/*.mts",
|
|
243
|
+
"**/*.tsx"
|
|
244
|
+
],
|
|
245
|
+
plugins: { "@ts-kizuna": plugin },
|
|
246
|
+
languageOptions: {
|
|
247
|
+
parser: _typescript_eslint_parser.default,
|
|
248
|
+
parserOptions: { jsDocParsingMode: "all" }
|
|
249
|
+
},
|
|
250
|
+
rules: { "@ts-kizuna/no-unsupported-schema": "error" }
|
|
251
|
+
};
|
|
252
|
+
const eslintPlugin = {
|
|
253
|
+
...plugin,
|
|
254
|
+
configs: { recommended }
|
|
255
|
+
};
|
|
256
|
+
//#endregion
|
|
257
|
+
module.exports = eslintPlugin;
|
package/dist/index.d.cts
ADDED
package/dist/index.d.mts
ADDED
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
import parser from "@typescript-eslint/parser";
|
|
2
|
+
import * as path from "node:path";
|
|
3
|
+
import { ESLintUtils } from "@typescript-eslint/utils";
|
|
4
|
+
import ts from "typescript";
|
|
5
|
+
import { AUTHORING_NAMES } from "@ts-kizuna/core/authoring-names";
|
|
6
|
+
//#region package.json
|
|
7
|
+
var name = "@ts-kizuna/eslint-plugin";
|
|
8
|
+
var version = "1.49.4";
|
|
9
|
+
//#endregion
|
|
10
|
+
//#region src/schema-violations.ts
|
|
11
|
+
/**
|
|
12
|
+
* A JSDoc inline tag, `{@link …}`, `{@linkcode …}`, any `{@tag}`.
|
|
13
|
+
*/
|
|
14
|
+
const JSDOC_INLINE_TAG = /\{@[a-zA-Z][\w-]*/;
|
|
15
|
+
/**
|
|
16
|
+
* Walks a schema expression, following `z.object` shapes, nested objects, and identifier
|
|
17
|
+
* references to other schemas (across files), and returns every issue it carries, each
|
|
18
|
+
* paired with the node it sits on. The walk is bounded by a visited set so cycles end.
|
|
19
|
+
*/
|
|
20
|
+
const collectSchemaIssues = (root, resolve) => {
|
|
21
|
+
const violations = [];
|
|
22
|
+
const reported = /* @__PURE__ */ new Set();
|
|
23
|
+
const visited = /* @__PURE__ */ new Set();
|
|
24
|
+
const add = (issue, node, viaReference) => {
|
|
25
|
+
const key = `${issue}@${node.getSourceFile().fileName}:${node.pos}`;
|
|
26
|
+
if (reported.has(key)) return;
|
|
27
|
+
reported.add(key);
|
|
28
|
+
violations.push({
|
|
29
|
+
issue,
|
|
30
|
+
node,
|
|
31
|
+
viaReference
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
const checkFieldJsDoc = (property, viaReference) => {
|
|
35
|
+
const deprecatedTags = ts.getJSDocTags(property).filter((tag) => tag.tagName.text === "deprecated");
|
|
36
|
+
if (deprecatedTags.length > 1) add("duplicate-deprecated", property, viaReference);
|
|
37
|
+
for (const tag of deprecatedTags) if (JSDOC_INLINE_TAG.test(ts.getTextOfJSDocComment(tag.comment) ?? "")) add("jsdoc-tag", property, viaReference);
|
|
38
|
+
};
|
|
39
|
+
const walk = (node, viaReference) => {
|
|
40
|
+
if (ts.isPropertyAccessExpression(node) && node.name.text === "coerce" && ts.isIdentifier(node.expression) && node.expression.text === "z") add("coerce", node, viaReference);
|
|
41
|
+
if (ts.isObjectLiteralExpression(node)) {
|
|
42
|
+
for (const property of node.properties) if (ts.isPropertyAssignment(property) || ts.isShorthandPropertyAssignment(property)) checkFieldJsDoc(property, viaReference);
|
|
43
|
+
}
|
|
44
|
+
if (ts.isIdentifier(node)) {
|
|
45
|
+
const target = resolve(node);
|
|
46
|
+
if (target && !visited.has(target)) {
|
|
47
|
+
visited.add(target);
|
|
48
|
+
walk(target, true);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
ts.forEachChild(node, (child) => walk(child, viaReference));
|
|
52
|
+
};
|
|
53
|
+
visited.add(root);
|
|
54
|
+
walk(root, false);
|
|
55
|
+
return violations;
|
|
56
|
+
};
|
|
57
|
+
//#endregion
|
|
58
|
+
//#region src/rules/no-unsupported-schema.ts
|
|
59
|
+
const SCHEMA_KEYS = new Set([
|
|
60
|
+
"body",
|
|
61
|
+
"query",
|
|
62
|
+
"pathParams",
|
|
63
|
+
"headers"
|
|
64
|
+
]);
|
|
65
|
+
const createCheckerResolver = (checker) => (identifier) => {
|
|
66
|
+
const symbol = checker.getSymbolAtLocation(identifier);
|
|
67
|
+
if (!symbol) return void 0;
|
|
68
|
+
return (symbol.flags & ts.SymbolFlags.Alias ? checker.getAliasedSymbol(symbol) : symbol).declarations?.find(ts.isVariableDeclaration)?.initializer;
|
|
69
|
+
};
|
|
70
|
+
const createSourceResolver = () => {
|
|
71
|
+
const sourceFileCache = /* @__PURE__ */ new Map();
|
|
72
|
+
const optionsCache = /* @__PURE__ */ new Map();
|
|
73
|
+
const readSourceFile = (fileName) => {
|
|
74
|
+
if (sourceFileCache.has(fileName)) return sourceFileCache.get(fileName);
|
|
75
|
+
const text = ts.sys.readFile(fileName);
|
|
76
|
+
const sourceFile = text === void 0 ? void 0 : ts.createSourceFile(fileName, text, ts.ScriptTarget.Latest, true);
|
|
77
|
+
sourceFileCache.set(fileName, sourceFile);
|
|
78
|
+
return sourceFile;
|
|
79
|
+
};
|
|
80
|
+
const compilerOptionsFor = (containingFile) => {
|
|
81
|
+
const configPath = ts.findConfigFile(path.dirname(containingFile), ts.sys.fileExists);
|
|
82
|
+
const key = configPath ?? "";
|
|
83
|
+
const cached = optionsCache.get(key);
|
|
84
|
+
if (cached) return cached;
|
|
85
|
+
const options = configPath ? ts.parseJsonConfigFileContent(ts.readConfigFile(configPath, ts.sys.readFile).config, ts.sys, path.dirname(configPath)).options : {
|
|
86
|
+
allowJs: true,
|
|
87
|
+
moduleResolution: ts.ModuleResolutionKind.Bundler
|
|
88
|
+
};
|
|
89
|
+
optionsCache.set(key, options);
|
|
90
|
+
return options;
|
|
91
|
+
};
|
|
92
|
+
const constNamed = (sourceFile, name) => {
|
|
93
|
+
for (const statement of sourceFile.statements) {
|
|
94
|
+
if (!ts.isVariableStatement(statement)) continue;
|
|
95
|
+
for (const declaration of statement.declarationList.declarations) if (ts.isIdentifier(declaration.name) && declaration.name.text === name && declaration.initializer) return declaration.initializer;
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
const importedFrom = (sourceFile, name) => {
|
|
99
|
+
for (const statement of sourceFile.statements) {
|
|
100
|
+
if (!ts.isImportDeclaration(statement) || !ts.isStringLiteral(statement.moduleSpecifier)) continue;
|
|
101
|
+
const named = statement.importClause?.namedBindings;
|
|
102
|
+
if (!named || !ts.isNamedImports(named)) continue;
|
|
103
|
+
for (const element of named.elements) if (element.name.text === name) return {
|
|
104
|
+
specifier: statement.moduleSpecifier.text,
|
|
105
|
+
exportedName: (element.propertyName ?? element.name).text
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
return (identifier) => {
|
|
110
|
+
const sourceFile = identifier.getSourceFile();
|
|
111
|
+
const local = constNamed(sourceFile, identifier.text);
|
|
112
|
+
if (local) return local;
|
|
113
|
+
const imported = importedFrom(sourceFile, identifier.text);
|
|
114
|
+
if (!imported) return void 0;
|
|
115
|
+
const resolved = ts.resolveModuleName(imported.specifier, sourceFile.fileName, compilerOptionsFor(sourceFile.fileName), ts.sys).resolvedModule?.resolvedFileName;
|
|
116
|
+
if (!resolved) return void 0;
|
|
117
|
+
const target = readSourceFile(resolved);
|
|
118
|
+
return target ? constNamed(target, imported.exportedName) : void 0;
|
|
119
|
+
};
|
|
120
|
+
};
|
|
121
|
+
const MESSAGE_IDS = {
|
|
122
|
+
coerce: ["coerce", "coerceReference"],
|
|
123
|
+
"jsdoc-tag": ["jsdocTag", "jsdocTagReference"],
|
|
124
|
+
"duplicate-deprecated": ["duplicateDeprecated", "duplicateDeprecatedReference"]
|
|
125
|
+
};
|
|
126
|
+
const calleeName = (node) => {
|
|
127
|
+
const { callee } = node;
|
|
128
|
+
if (callee.type === "Identifier") return callee.name;
|
|
129
|
+
if (callee.type === "MemberExpression" && callee.property.type === "Identifier") return callee.property.name;
|
|
130
|
+
};
|
|
131
|
+
const schemaNodesOf = (call) => {
|
|
132
|
+
const name = calleeName(call);
|
|
133
|
+
if (name === AUTHORING_NAMES.model) {
|
|
134
|
+
const config = call.arguments[0];
|
|
135
|
+
if (config?.type !== "ObjectExpression") return [];
|
|
136
|
+
const schema = config.properties.find((property) => property.type === "Property" && property.key.type === "Identifier" && property.key.name === "schema");
|
|
137
|
+
return schema ? [schema.value] : [];
|
|
138
|
+
}
|
|
139
|
+
if (name !== AUTHORING_NAMES.routes && name !== AUTHORING_NAMES.contract) return [];
|
|
140
|
+
const nodes = [];
|
|
141
|
+
const visit = (node) => {
|
|
142
|
+
if (node.type === "Property") {
|
|
143
|
+
const isSchemaField = node.key.type === "Identifier" && SCHEMA_KEYS.has(node.key.name);
|
|
144
|
+
const isStatusResponse = node.key.type === "Literal" && typeof node.key.value === "number";
|
|
145
|
+
if (isSchemaField || isStatusResponse) nodes.push(node.value);
|
|
146
|
+
}
|
|
147
|
+
for (const [key, child] of Object.entries(node)) {
|
|
148
|
+
if (key === "parent") continue;
|
|
149
|
+
if (Array.isArray(child)) child.forEach((item) => item?.type && visit(item));
|
|
150
|
+
else if (child?.type) visit(child);
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
for (const argument of call.arguments) visit(argument);
|
|
154
|
+
return nodes;
|
|
155
|
+
};
|
|
156
|
+
const noUnsupportedSchema = ESLintUtils.RuleCreator.withoutDocs({
|
|
157
|
+
meta: {
|
|
158
|
+
type: "problem",
|
|
159
|
+
docs: { description: "Disallow constructs unsupported by ts-kizuna (z.coerce, @deprecated with JSDoc tags) in contract and model schemas, including imported ones." },
|
|
160
|
+
messages: {
|
|
161
|
+
coerce: "z.coerce is not supported in a ts-kizuna schema. kizuna coerces query, path, and header params automatically, use z.number(), z.date(), or z.bigint() instead.",
|
|
162
|
+
jsdocTag: "This @deprecated message uses JSDoc inline tags like {@link}. kizuna surfaces deprecation text verbatim to generated clients that cannot parse it, use plain text instead (backticks are fine).",
|
|
163
|
+
duplicateDeprecated: "This field has more than one @deprecated tag. kizuna serializes only the first and silently drops the rest, collapse them into a single message.",
|
|
164
|
+
coerceReference: "This schema uses z.coerce, which ts-kizuna does not support. kizuna coerces query, path, and header params automatically, use z.number(), z.date(), or z.bigint() instead.",
|
|
165
|
+
jsdocTagReference: "This schema has a @deprecated message with JSDoc inline tags like {@link}. kizuna surfaces deprecation text verbatim to generated clients that cannot parse it, use plain text instead (backticks are fine).",
|
|
166
|
+
duplicateDeprecatedReference: "This schema has a field with more than one @deprecated tag. kizuna serializes only the first and silently drops the rest, collapse them into a single message."
|
|
167
|
+
},
|
|
168
|
+
schema: []
|
|
169
|
+
},
|
|
170
|
+
defaultOptions: [],
|
|
171
|
+
create(context) {
|
|
172
|
+
const services = ESLintUtils.getParserServices(context, true);
|
|
173
|
+
const resolve = services.program ? createCheckerResolver(services.program.getTypeChecker()) : createSourceResolver();
|
|
174
|
+
const reported = /* @__PURE__ */ new Set();
|
|
175
|
+
return { CallExpression(call) {
|
|
176
|
+
for (const schemaNode of schemaNodesOf(call)) {
|
|
177
|
+
const tsNode = services.esTreeNodeToTSNodeMap.get(schemaNode);
|
|
178
|
+
for (const { issue, node, viaReference } of collectSchemaIssues(tsNode, resolve)) {
|
|
179
|
+
const reportNode = (viaReference ? void 0 : services.tsNodeToESTreeNodeMap.get(node)) ?? schemaNode;
|
|
180
|
+
const messageId = MESSAGE_IDS[issue][viaReference ? 1 : 0];
|
|
181
|
+
const key = `${messageId}@${reportNode.range[0]}`;
|
|
182
|
+
if (reported.has(key)) continue;
|
|
183
|
+
reported.add(key);
|
|
184
|
+
context.report({
|
|
185
|
+
node: reportNode,
|
|
186
|
+
messageId
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
} };
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
//#endregion
|
|
194
|
+
//#region src/index.ts
|
|
195
|
+
const plugin = {
|
|
196
|
+
meta: {
|
|
197
|
+
name,
|
|
198
|
+
version,
|
|
199
|
+
namespace: "@ts-kizuna"
|
|
200
|
+
},
|
|
201
|
+
rules: { "no-unsupported-schema": noUnsupportedSchema }
|
|
202
|
+
};
|
|
203
|
+
/**
|
|
204
|
+
* Flat config enabling every ts-kizuna rule.
|
|
205
|
+
*
|
|
206
|
+
* ```js
|
|
207
|
+
* import kizuna from '@ts-kizuna/eslint-plugin';
|
|
208
|
+
*
|
|
209
|
+
* export default [kizuna.configs.recommended];
|
|
210
|
+
* ```
|
|
211
|
+
*/
|
|
212
|
+
const recommended = {
|
|
213
|
+
name: "@ts-kizuna/recommended",
|
|
214
|
+
files: [
|
|
215
|
+
"**/*.ts",
|
|
216
|
+
"**/*.cts",
|
|
217
|
+
"**/*.mts",
|
|
218
|
+
"**/*.tsx"
|
|
219
|
+
],
|
|
220
|
+
plugins: { "@ts-kizuna": plugin },
|
|
221
|
+
languageOptions: {
|
|
222
|
+
parser,
|
|
223
|
+
parserOptions: { jsDocParsingMode: "all" }
|
|
224
|
+
},
|
|
225
|
+
rules: { "@ts-kizuna/no-unsupported-schema": "error" }
|
|
226
|
+
};
|
|
227
|
+
const eslintPlugin = {
|
|
228
|
+
...plugin,
|
|
229
|
+
configs: { recommended }
|
|
230
|
+
};
|
|
231
|
+
//#endregion
|
|
232
|
+
export { eslintPlugin as default };
|
package/package.json
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ts-kizuna/eslint-plugin",
|
|
3
|
+
"version": "1.49.5",
|
|
4
|
+
"description": "ESLint rules for ts-kizuna contracts, flagging Zod schemas it cannot represent",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"ts-kizuna",
|
|
7
|
+
"typescript",
|
|
8
|
+
"zod",
|
|
9
|
+
"openapi",
|
|
10
|
+
"contract-first",
|
|
11
|
+
"type-safe",
|
|
12
|
+
"rest",
|
|
13
|
+
"api",
|
|
14
|
+
"eslint",
|
|
15
|
+
"eslintplugin",
|
|
16
|
+
"eslint-plugin",
|
|
17
|
+
"lint"
|
|
18
|
+
],
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"homepage": "https://ts-kizuna.com/docs/eslint",
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "git+https://github.com/ts-kizuna/kizuna.git",
|
|
24
|
+
"directory": "packages/eslint-plugin"
|
|
25
|
+
},
|
|
26
|
+
"bugs": "https://github.com/ts-kizuna/kizuna/issues",
|
|
27
|
+
"type": "module",
|
|
28
|
+
"sideEffects": false,
|
|
29
|
+
"engines": {
|
|
30
|
+
"node": ">=24"
|
|
31
|
+
},
|
|
32
|
+
"main": "./dist/index.cjs",
|
|
33
|
+
"module": "./dist/index.mjs",
|
|
34
|
+
"types": "./dist/index.d.mts",
|
|
35
|
+
"exports": {
|
|
36
|
+
".": {
|
|
37
|
+
"import": {
|
|
38
|
+
"types": "./dist/index.d.mts",
|
|
39
|
+
"default": "./dist/index.mjs"
|
|
40
|
+
},
|
|
41
|
+
"require": {
|
|
42
|
+
"types": "./dist/index.d.cts",
|
|
43
|
+
"default": "./dist/index.cjs"
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
"kizuna": {
|
|
48
|
+
"entries": {
|
|
49
|
+
".": "server"
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
"files": [
|
|
53
|
+
"dist"
|
|
54
|
+
],
|
|
55
|
+
"publishConfig": {
|
|
56
|
+
"access": "public"
|
|
57
|
+
},
|
|
58
|
+
"dependencies": {
|
|
59
|
+
"@typescript-eslint/parser": "^8.0.0",
|
|
60
|
+
"@typescript-eslint/utils": "^8.0.0"
|
|
61
|
+
},
|
|
62
|
+
"peerDependencies": {
|
|
63
|
+
"eslint": ">=9.0.0",
|
|
64
|
+
"typescript": ">=5.0.0",
|
|
65
|
+
"@ts-kizuna/core": "1.49.5"
|
|
66
|
+
},
|
|
67
|
+
"devDependencies": {
|
|
68
|
+
"eslint": "^9.39.4",
|
|
69
|
+
"tsdown": "^0.21.0",
|
|
70
|
+
"typescript": "^5.6.3",
|
|
71
|
+
"zod": "^4.0.0",
|
|
72
|
+
"@ts-kizuna/core": "1.49.5"
|
|
73
|
+
},
|
|
74
|
+
"scripts": {
|
|
75
|
+
"build": "tsdown src/index.ts --format esm,cjs --dts --clean --external eslint --external typescript --external @typescript-eslint/utils --external @typescript-eslint/parser --external @ts-kizuna/core",
|
|
76
|
+
"typecheck": "tsc --noEmit"
|
|
77
|
+
}
|
|
78
|
+
}
|