Skip to content

Add a castom lint rule to check for duplicates, and eslint itself. #213

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions eslint-rules/no-duplicate-manifest-entries.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import fs from 'fs';
import path from 'path';

const manifestDir = path.resolve(process.cwd(), 'manifests');

function loadManifest(fileName) {
const filePath = path.join(manifestDir, fileName);
return JSON.parse(fs.readFileSync(filePath, 'utf-8'));
}

function getDuplicates(manifests) {
const seen = new Map();
const duplicates = [];

for (const [manifestName, entries] of Object.entries(manifests)) {
for (const moduleName of Object.keys(entries)) {
const key = moduleName.toLowerCase();
if (seen.has(key)) {
duplicates.push({
moduleName,
first: seen.get(key),
second: manifestName,
});
} else {
seen.set(key, manifestName);
}
}
}

return duplicates;
}

export default {
meta: {
type: 'problem',
docs: {
description: 'disallow duplicate modules across manifests',
},
schema: [],
},
create(context) {
// only run once per lint run (on Program)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this create will run per JS file

we should probably run against the JSON files and report the specific file/position while traversing it

that also means we'd need to setup eslint to be able to lint JSON, though.

i.e. it should visit each manifest and check against the already seen set, to then report the exact position in the current manifest being linted

if (context.getFilename().endsWith('.js') === false) return {};

const manifestFiles = fs.readdirSync(manifestDir).filter(f => f.endsWith('.json'));

const manifests = {};
for (const file of manifestFiles) {
manifests[file] = loadManifest(file);
}

const duplicates = getDuplicates(manifests);

if (duplicates.length > 0) {
for (const dup of duplicates) {
context.report({
loc: { line: 1, column: 0 },
message: `Module "${dup.moduleName}" found in both "${dup.first}" and "${dup.second}"`,
});
}
}

return {};
},
};
25 changes: 25 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import js from "@eslint/js";
import globals from "globals";
import tseslint from "typescript-eslint";
import { defineConfig } from "eslint/config";


export default defineConfig([
{ files: ["**/*.{js,mjs,cjs,ts,mts,cts}"], plugins: { js }, extends: ["js/recommended"] },
{ files: ["**/*.{js,mjs,cjs,ts,mts,cts}"], languageOptions: { globals: globals.node } },
tseslint.configs.recommended,
{
files: ['**/*.js'],
rules: {
'no-duplicate-manifest-entries': 'error',
},
plugins: {
'no-duplicate-manifest-entries': {
rules: {
'no-duplicate-manifest-entries': noDuplicateManifestEntries,
},
},
},
},
]);

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"generate-schema": "node scripts/generate-schema.js",
"prepare": "tshy && npm run build:update-manifest-paths",
"test:validate": "node scripts/validate-modules.js",
"sort-manifests": "node scripts/sort-manifests.js"
"sort-manifests": "node scripts/sort-manifests.js",
"lint": "eslint ."
},
"tshy": {
"exports": {
Expand Down