Skip to content

eslint-cdk-plugin

インストール

以下のコマンドを実行してインストールします。

sh
npm install -D eslint-cdk-plugin
sh
yarn add -D eslint-cdk-plugin
sh
pnpm install -D eslint-cdk-plugin

eslint の設定

eslint.config.mjs を以下のように記述します。

🚨 このプラグインは FlatConfig のみをサポートしています。
FlatConfig とは?
🚨 このプラグインは typescript の型情報を使う為 typescript-eslint との併用が必要になります。

ESM を使用する場合

js
// eslint.config.mjs
import eslint from "@eslint/js";
import { defineConfig } from "eslint/config";
import tseslint from "typescript-eslint";
import cdkPlugin from "eslint-cdk-plugin";

export default defineConfig([
  eslint.configs.recommended,
  ...tseslint.configs.recommended,
  {
    files: ["lib/**/*.ts", "bin/*.ts"],
    // ✅ Add plugins
    extends: [cdkPlugin.configs.recommended],
    // ... some configs
  },
]);

CJS を使用する場合

js
// eslint.config.cjs
const eslint = require("@eslint/js");
const { defineConfig } = require("eslint/config");
const tseslint = require("typescript-eslint");
const cdkPlugin = require("eslint-cdk-plugin");

module.exports = defineConfig([
  eslint.configs.recommended,
  ...tseslint.configs.recommended,
  {
    files: ["lib/**/*.ts", "bin/*.ts"],
    // ✅ Add plugins
    extends: [cdkPlugin.configs.recommended],
    // ... some configs
  },
]);

ルールのカスタマイズ

ルールをカスタマイズしたい場合は、eslint.config.mjs を以下のように記述します。
(CJS の場合は eslint.config.cjs を使用し、CommonJS の記法で記述します)

js
// eslint.config.mjs
import eslint from "@eslint/js";
import { defineConfig } from "eslint/config";
import tseslint from "typescript-eslint";
import cdkPlugin from "eslint-cdk-plugin";

export default defineConfig([
  eslint.configs.recommended,
  ...tseslint.configs.recommended,
  {
    files: ["lib/**/*.ts", "bin/*.ts"],
    languageOptions: {
      parserOptions: {
        projectService: true,
        project: "./tsconfig.json",
      },
    },
    // ✅ Add plugins
    plugins: {
      cdk: cdkPlugin,
    },
    // ✅ Add rules (use custom rules)
    rules: {
      "cdk/no-construct-in-interface": "error",
      "cdk/no-construct-stack-suffix": "error",
      "cdk/no-parent-name-construct-id-match": "error",
    },
  },
]);