eslint-plugin-ai-guardrail
v0.1.0
Published
AI生成コードの典型的なミスを検出するESLintプラグイン
Maintainers
Readme
eslint-plugin-ai-guardrail
AI生成コードの典型的なミスを検出するESLintプラグイン。
LLMが生成するコードに頻出する問題パターン(存在しないパッケージのimport、非推奨API使用、セキュリティリスクなど)を自動検出します。
Installation
npm install -D eslint-plugin-ai-guardrailUsage
ESLint設定(flat config)
eslint.config.js に recommended 設定を追加:
import aiGuardrail from "eslint-plugin-ai-guardrail";
export default [
aiGuardrail.configs.recommended,
// ... 他の設定
];個別にルールを設定する場合:
import aiGuardrail from "eslint-plugin-ai-guardrail";
export default [
{
plugins: {
"ai-guardrail": aiGuardrail,
},
rules: {
"ai-guardrail/no-hallucinated-imports": "error",
"ai-guardrail/no-deprecated-api": "warn",
"ai-guardrail/no-insecure-eval": "error",
"ai-guardrail/no-unused-catch": "warn",
"ai-guardrail/no-console-in-prod": "warn",
},
},
];CLI
ESLint設定なしで直接実行:
npx ai-guardrail check src/
npx ai-guardrail check .Rules
no-hallucinated-imports
package.json の dependencies に登録されていないパッケージの import を検出します。AIが存在しないパッケージ名を「幻覚」するケースに対応。
// NG: package.json に登録されていない
import foo from "nonexistent-package";
const bar = require("imaginary-lib");
// OK: 登録済みのパッケージ
import express from "express";
// OK: Node.js 組み込みモジュール
import fs from "node:fs";
// OK: 相対パス
import utils from "./utils";no-deprecated-api
Node.js / React / Next.js の非推奨APIの使用を警告し、代替APIを提案します。
// NG: 非推奨API
url.parse("http://example.com"); // → new URL()
ReactDOM.render(<App />, root); // → ReactDOM.createRoot().render()
util.isArray([1, 2]); // → Array.isArray()
// OK: 推奨API
const u = new URL("http://example.com");
ReactDOM.createRoot(root).render(<App />);
Array.isArray([1, 2]);no-insecure-eval
eval() や new Function() の使用を検出します。これらはコードインジェクション攻撃のリスクがあります。
// NG: セキュリティリスク
eval("alert(1)");
new Function("a", "return a + 1");
window.eval("code");
// OK: 安全な代替
JSON.parse('{"a":1}');no-unused-catch
catch ブロックでエラーを適切に処理していないパターンを検出します。空の catch や console.log のみのケースを警告。
// NG: エラーを無視
try { foo(); } catch (e) { }
try { foo(); } catch (e) { console.log(e); }
// OK: 適切なエラーハンドリング
try { foo(); } catch (e) { throw e; }
try { foo(); } catch (e) { handleError(e); }
try { foo(); } catch (e) { console.error(e); reportError(e); }no-console-in-prod
console.log / console.warn / console.error の残存を検出します。
// NG: 本番コードに残すべきでない
console.log("debug");
console.warn("warning");
// OK: allow オプションで許可
// ルール設定: ["warn", { "allow": ["error"] }]
console.error("critical error");Development
# 依存パッケージのインストール
npm install
# ビルド
npm run build
# テスト
npm test
# テスト(watchモード)
npm run test:watchPublishing to npm (for maintainers)
Prerequisites
- Create GitHub repository:
https://github.com/imudak/ai-code-guardrail - Push code to GitHub:
git remote add origin https://github.com/imudak/ai-code-guardrail.git git push -u origin main - Login to npm:
npm login
Publish steps
Update version in
package.json(follow Semantic Versioning):npm version patch # 0.1.0 → 0.1.1 (bug fixes) npm version minor # 0.1.0 → 0.2.0 (new features, backward compatible) npm version major # 0.1.0 → 1.0.0 (breaking changes)Update
CHANGELOG.mdwith release notesVerify package contents:
npm pack --dry-runBuild before publishing (automatic via
prepublishOnlyscript):npm run buildPublish to npm:
npm publishCreate GitHub release:
- Go to https://github.com/imudak/ai-code-guardrail/releases/new
- Tag:
v0.1.0(match package.json version) - Title:
v0.1.0 - Description: Copy from CHANGELOG.md
Verify installation
After publishing, test installation:
mkdir test-install
cd test-install
npm init -y
npm install eslint-plugin-ai-guardrailLicense
MIT
