-
Notifications
You must be signed in to change notification settings - Fork 43
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
Cross-sama
wants to merge
1
commit into
es-tooling:main
Choose a base branch
from
Cross-sama:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
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 {}; | ||
}, | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
}, | ||
}, | ||
}, | ||
]); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 filewe 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