I have multiple Json files. People are writing to these files manually. I want to be able to detect the duplicate values inside each file and throw and error. I want to utilize eslint to do this and integrate the vscode format on save and fix on save functionalities. Basically have done the prettier, eslint, typescript setup where it works with the existing rules pretty fine. I created my own local plugin and that is where things get funky. I am using Vscode as the editor.
Here's below my important files.
Here's the json file I have with the duplicate values.
** jsonFolder\b.json **
{
"key1": "value1",
"key2": "value1",
"key3": "value2"
}
Here's the vscode settings to enforce the auto format and auto fix find the errors on save
** .vscode\settings.json **
{
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
}
}
**Here's the plugin code **
plugins\eslint-plugin-no-duplicate-values.js
const { JSONPath } = require('jsonpath-plus');
const fs = require('fs');
const path = require('path');
module.exports = {
rules: {
'no-duplicate-values': {
meta: {
type: 'problem',
docs: {
description: 'Disallow duplicate values in JSON files',
category: 'Possible Errors',
recommended: 'error',
url: 'https://eslint.org/docs/rules/no-duplicate-values',
},
schema: [],
},
create: function (context) {
return {
Program: function (node) {
const folder = path.resolve(__dirname, '../jsonFolder');
fs.readdir(folder, (err, files) => {
files.forEach((file) => {
if (path.extname(file) === '.json') {
const filePath = path.join(folder, file);
const contents = fs.readFileSync(filePath, 'utf8');
let values = [];
JSONPath({ path: '$..*', json: contents }).forEach(
(value) => {
if (values.includes(value)) {
context.report({
node: node,
message: `Duplicate value found in file ${file}: ${value}`,
});
} else {
values.push(value);
}
},
);
}
});
});
},
};
},
},
},
};
**Here's the package.json for the plugin **
plugins\package.json
{
"name": "eslint-plugin-no-duplicate-values",
"version": "1.0.0",
"description": "",
"main": "eslint-plugin-no-duplicate-values.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
**Here's the package.json for the whole project in the root **
package.json
{
"name": "prettier-eslint-typescript-custom-rule",
"version": "1.0.0",
"description": "",
"main": "index.js",
"dependencies": {},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^5.50.0",
"@typescript-eslint/parser": "^5.50.0",
"cypress": "^12.5.1",
"eslint": "^8.33.0",
"eslint-config-prettier": "^8.6.0",
"eslint-plugin-no-duplicate-values": "file:plugins",
"jsonpath-plus": "^7.2.0",
"lodash": "^4.17.21",
"prettier": "^2.8.3",
"typescript": "^4.9.5"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
**Here's the eslintrc.json file **
.eslintrc.json
{
"env": {
"browser": true,
"es2021": true
},
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"prettier"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"plugins": ["@typescript-eslint", "no-duplicate-values"],
"rules": { "no-duplicate-values/no-duplicate-values": "error" }
}
**Here's the prettierrc.json file **
.prettierrc.json
{
"semi": true,
"trailingComma": "all",
"singleQuote": true
}
Now when I run it, it just does not find the duplicate values in the json files. I can see the other issues being found. Please see the screenshot below. eslint rule being found
Can someone help me figure out what I am doing wrong, please?
I have tried to create a plugin, followed the guides online. I am new to this and not sure how good I can test it out. I am not getting any errors, but the code also just doesn't do what is intended. Rule doesn't seem to trigger any errors in the auto detecting and also when I ran
npx eslint --ext .json jsonFolder\b.json
nothing comes back as there are no issues.
Asking help humbly, and if someone can help I would appreciate it greatly. Thanks in advance for those who read it till the end.