Added Pinia Custom Eslint Check
This commit is contained in:
parent
013a6cd5aa
commit
e12eba2a33
|
|
@ -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",
|
||||
},
|
||||
},
|
||||
];
|
||||
|
|
|
|||
9
eslint/eslint-pinia-plugin-local.cjs
Normal file
9
eslint/eslint-pinia-plugin-local.cjs
Normal 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
73
eslint/eslint-pinia.cjs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
};
|
||||
},
|
||||
};
|
||||
Loading…
Reference in New Issue
Block a user