Added Pinia Custom Eslint Check

This commit is contained in:
Sandip Ghimire 2026-01-19 10:46:12 +05:45
parent 013a6cd5aa
commit e12eba2a33
3 changed files with 92 additions and 0 deletions

View File

@ -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",
},
},
];

View File

@ -0,0 +1,9 @@
"use strict";
const noDuplicatePiniaStoreIds = require("./eslint-pinia.cjs");
module.exports = {
rules: {
"no-duplicate-pinia-store-ids": noDuplicatePiniaStoreIds,
},
};

73
eslint/eslint-pinia.cjs Normal file
View File

@ -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);
}
}
}
},
};
},
};