From e12eba2a3376262685f795fc5ca945efe3744cdb Mon Sep 17 00:00:00 2001 From: Sandip Date: Mon, 19 Jan 2026 10:46:12 +0545 Subject: [PATCH] Added Pinia Custom Eslint Check --- eslint.config.js | 10 ++++ eslint/eslint-pinia-plugin-local.cjs | 9 ++++ eslint/eslint-pinia.cjs | 73 ++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 eslint/eslint-pinia-plugin-local.cjs create mode 100644 eslint/eslint-pinia.cjs diff --git a/eslint.config.js b/eslint.config.js index bc41bad..7885ef8 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -5,6 +5,7 @@ import pluginVue from "eslint-plugin-vue"; import prettier from "eslint-plugin-prettier/recommended"; import vueConfigTypescript from "@vue/eslint-config-typescript"; import vueConfigPrettier from "@vue/eslint-config-prettier"; +import localPlugin from "./eslint/eslint-pinia-plugin-local.cjs"; /** @type {import('eslint').Linter.Config[]} */ export default [ @@ -82,4 +83,13 @@ export default [ "prettier/prettier": ["warn", { singleQuote: false }], }, }, + // pinia + { + plugins: { + local: localPlugin, + }, + rules: { + "local/no-duplicate-pinia-store-ids": "error", + }, + }, ]; diff --git a/eslint/eslint-pinia-plugin-local.cjs b/eslint/eslint-pinia-plugin-local.cjs new file mode 100644 index 0000000..652769a --- /dev/null +++ b/eslint/eslint-pinia-plugin-local.cjs @@ -0,0 +1,9 @@ +"use strict"; + +const noDuplicatePiniaStoreIds = require("./eslint-pinia.cjs"); + +module.exports = { + rules: { + "no-duplicate-pinia-store-ids": noDuplicatePiniaStoreIds, + }, +}; diff --git a/eslint/eslint-pinia.cjs b/eslint/eslint-pinia.cjs new file mode 100644 index 0000000..a17224a --- /dev/null +++ b/eslint/eslint-pinia.cjs @@ -0,0 +1,73 @@ +"use strict"; + +const storeIds = new Map(); + +module.exports = { + meta: { + type: "problem", + docs: { + description: "Disallow duplicate Pinia defineStore ids", + }, + schema: [], + messages: { + duplicate: "Pinia store id '{{id}}' is already defined in {{file}}.", + }, + }, + + create(context) { + const filename = context.getFilename(); + + function reportIfDuplicate(id, node) { + const existingFile = storeIds.get(id); + + if (existingFile === filename) { + return; + } + + if (existingFile) { + context.report({ + node, + messageId: "duplicate", + data: { + id, + file: existingFile, + }, + }); + return; + } + + storeIds.set(id, filename); + } + + return { + CallExpression(node) { + if ( + node.callee.type === "Identifier" && + node.callee.name === "defineStore" && + node.arguments.length > 0 + ) { + const firstArg = node.arguments[0]; + + if (firstArg.type === "Literal" && typeof firstArg.value === "string") { + reportIfDuplicate(firstArg.value, firstArg); + } + + if (firstArg.type === "ObjectExpression") { + const idProp = firstArg.properties.find( + (p) => + p.type === "Property" && + p.key.type === "Identifier" && + p.key.name === "id" && + p.value.type === "Literal" && + typeof p.value.value === "string" + ); + + if (idProp) { + reportIfDuplicate(idProp.value.value, idProp.value); + } + } + } + }, + }; + }, +};