@gd-kirie/alint-config 0.6.2 → 0.6.4
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/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -23,7 +23,7 @@ async function reviewStringifiedRethrows(file, model, usage) {
|
|
|
23
23
|
temperature: 0
|
|
24
24
|
});
|
|
25
25
|
const content = response.text;
|
|
26
|
-
if (typeof content !== "string") throw new
|
|
26
|
+
if (typeof content !== "string") throw new TypeError("alint model response did not include message content.");
|
|
27
27
|
recordUsage(usage, model, response);
|
|
28
28
|
const result = JSON.parse(content);
|
|
29
29
|
if (!Array.isArray(result.findings)) return [];
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../src/rules/no-stringified-rethrow.ts","../src/plugin.ts","../src/config.ts"],"sourcesContent":["import type { ResolvedModel, RuleContext, RuleDefinition, SourceFile } from \"@alint-js/core\";\nimport {
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/rules/no-stringified-rethrow.ts","../src/plugin.ts","../src/config.ts"],"sourcesContent":["import type { ResolvedModel, RuleContext, RuleDefinition, SourceFile } from \"@alint-js/core\";\nimport type { GenerateTextResult } from \"xsai\";\nimport { defineRule } from \"@alint-js/core\";\nimport { generateText } from \"xsai\";\n\nexport interface ErrorWrappingReviewFinding {\n evidence?: unknown;\n line: number;\n message?: string;\n}\n\ninterface ReviewResponse {\n findings?: ErrorWrappingReviewFinding[];\n}\n\nexport const noStringifiedRethrowRule: RuleDefinition = defineRule({\n create: (ctx) => ({\n async onTargetFile({ file }) {\n const model = await ctx.model();\n const findings = await reviewStringifiedRethrows(file, model, ctx.metering);\n\n for (const finding of findings) {\n ctx.report({\n evidence: finding.evidence,\n filePath: file.path,\n loc: {\n start: {\n column: 0,\n line: finding.line,\n },\n },\n message:\n finding.message ??\n \"Do not stringify and rethrow caught errors; preserve the original error as cause.\",\n });\n }\n },\n }),\n});\n\nasync function reviewStringifiedRethrows(\n file: SourceFile,\n model: ResolvedModel,\n usage: RuleContext[\"metering\"],\n): Promise<ErrorWrappingReviewFinding[]> {\n const response = await generateText({\n baseURL: model.provider.endpoint,\n headers: model.provider.headers,\n messages: createReviewMessages(file),\n model: model.id,\n response_format: {\n type: \"json_object\",\n },\n temperature: 0,\n });\n const content = response.text;\n\n if (typeof content !== \"string\") {\n throw new TypeError(\"alint model response did not include message content.\");\n }\n\n recordUsage(usage, model, response);\n\n const result = JSON.parse(content) as ReviewResponse;\n\n if (!Array.isArray(result.findings)) {\n return [];\n }\n\n return result.findings.filter(isReviewFinding);\n}\n\nfunction createReviewMessages(\n file: SourceFile,\n): Array<{ content: string; role: \"system\" | \"user\" }> {\n return [\n {\n content: [\n \"You are reviewing one JavaScript or TypeScript file.\",\n \"\",\n \"Task:\",\n \"Find catch blocks that stringify, format, or otherwise collapse the caught error into a message string before throwing a new error, without preserving the original error as the error cause.\",\n \"\",\n \"Report only cases where the original caught error is not preserved through `cause` or an equivalent custom error cause field.\",\n \"Do not report code that throws `new Error(message, { cause: error })`.\",\n \"Do not report custom error classes that pass or store the caught error as cause.\",\n \"Do not report code that logs an error and rethrows the same original error.\",\n \"\",\n \"Return JSON only with this shape:\",\n \"{\\\"findings\\\":[{\\\"line\\\":1,\\\"message\\\":\\\"short diagnostic\\\",\\\"evidence\\\":{\\\"reason\\\":\\\"short reason\\\"}}]}\",\n \"Use the line number of the throw statement.\",\n \"Return an empty findings array when there is no issue.\",\n ].join(\"\\n\"),\n role: \"system\",\n },\n {\n content: `File: ${file.path}\\n\\n${formatSourceWithLineNumbers(file.text)}`,\n role: \"user\",\n },\n ];\n}\n\nfunction formatSourceWithLineNumbers(source: string): string {\n return source\n .split(\"\\n\")\n .map((line, index) => `${index + 1} | ${line}`)\n .join(\"\\n\");\n}\n\nfunction isReviewFinding(value: unknown): value is ErrorWrappingReviewFinding {\n if (!value || typeof value !== \"object\") {\n return false;\n }\n\n const finding = value as Partial<ErrorWrappingReviewFinding>;\n\n return typeof finding.line === \"number\" && Number.isInteger(finding.line) && finding.line > 0;\n}\n\nfunction recordUsage(\n metering: RuleContext[\"metering\"],\n model: ResolvedModel,\n response: GenerateTextResult,\n): void {\n metering.recordUsage({\n inputTokens: response.usage.inputTokens,\n modelId: model.id,\n outputTokens: response.usage.outputTokens,\n providerId: model.provider.id,\n totalTokens: response.usage.totalTokens,\n });\n}\n","import type { PluginDefinition } from \"@alint-js/core\";\nimport { definePlugin } from \"@alint-js/core\";\nimport { noStringifiedRethrowRule } from \"./rules/no-stringified-rethrow\";\n\nexport const gdKirieAlintPlugin: PluginDefinition = definePlugin({\n rules: {\n \"no-stringified-rethrow\": noStringifiedRethrowRule,\n },\n});\n","import type { AlintConfig } from \"@alint-js/core\";\nimport { defineConfig } from \"@alint-js/core\";\nimport { gdKirieAlintPlugin } from \"./plugin\";\n\nexport const gdKirieAlintConfig: AlintConfig = defineConfig([\n {\n plugins: {\n \"@gd-kirie\": gdKirieAlintPlugin,\n },\n rules: {\n \"@gd-kirie/no-stringified-rethrow\": \"warn\",\n },\n },\n]);\n"],"mappings":";;;AAeA,MAAa,2BAA2C,WAAW,EACjE,SAAS,SAAS,EAChB,MAAM,aAAa,EAAE,QAAQ;CAE3B,MAAM,WAAW,MAAM,0BAA0B,MAAM,MADnC,IAAI,MAAM,GACgC,IAAI,QAAQ;CAE1E,KAAK,MAAM,WAAW,UACpB,IAAI,OAAO;EACT,UAAU,QAAQ;EAClB,UAAU,KAAK;EACf,KAAK,EACH,OAAO;GACL,QAAQ;GACR,MAAM,QAAQ;EAChB,EACF;EACA,SACE,QAAQ,WACR;CACJ,CAAC;AAEL,EACF,GACF,CAAC;AAED,eAAe,0BACb,MACA,OACA,OACuC;CACvC,MAAM,WAAW,MAAM,aAAa;EAClC,SAAS,MAAM,SAAS;EACxB,SAAS,MAAM,SAAS;EACxB,UAAU,q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