Skip to content

Commit

Permalink
fix: allow incorrect file path in extraneous deps check
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Jul 1, 2024
1 parent 0f07c36 commit 3a6dee2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/rules/no-extraneous-dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,11 @@ function extractDepFields(pkg: PackageJson) {

function getPackageDepFields(packageJsonPath: string, throwAtRead: boolean) {
if (!depFieldCache.has(packageJsonPath)) {
const depFields = extractDepFields(readJSON(packageJsonPath, throwAtRead)!)
depFieldCache.set(packageJsonPath, depFields)
const packageJson = readJSON<PackageJson>(packageJsonPath, throwAtRead);

Check failure on line 58 in src/rules/no-extraneous-dependencies.ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 20 and ESLint 8.56 on ubuntu-latest

Delete `;`

Check failure on line 58 in src/rules/no-extraneous-dependencies.ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 20 and ESLint 8 on ubuntu-latest

Delete `;`
if (packageJson) {
const depFields = extractDepFields(packageJson);

Check failure on line 60 in src/rules/no-extraneous-dependencies.ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 20 and ESLint 8.56 on ubuntu-latest

Delete `;`

Check failure on line 60 in src/rules/no-extraneous-dependencies.ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 20 and ESLint 8 on ubuntu-latest

Delete `;`
depFieldCache.set(packageJsonPath, depFields);

Check failure on line 61 in src/rules/no-extraneous-dependencies.ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 20 and ESLint 8.56 on ubuntu-latest

Delete `;`

Check failure on line 61 in src/rules/no-extraneous-dependencies.ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 20 and ESLint 8 on ubuntu-latest

Delete `;`
}
}
return depFieldCache.get(packageJsonPath)
}
Expand All @@ -83,10 +86,12 @@ function getDependencies(context: RuleContext, packageDir?: string | string[]) {
// use rule config to find package.json
for (const dir of paths) {
const packageJsonPath = path.resolve(dir, 'package.json')
const packageContent_ = getPackageDepFields(packageJsonPath, true)!
for (const depsKey of Object.keys(packageContent)) {
const key = depsKey as keyof PackageDeps
Object.assign(packageContent[key], packageContent_[key])
const packageContent_ = getPackageDepFields(packageJsonPath, paths.length === 1)

Check failure on line 89 in src/rules/no-extraneous-dependencies.ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 20 and ESLint 8.56 on ubuntu-latest

Replace `packageJsonPath,·paths.length·===·1` with `⏎··········packageJsonPath,⏎··········paths.length·===·1,⏎········`

Check failure on line 89 in src/rules/no-extraneous-dependencies.ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 20 and ESLint 8 on ubuntu-latest

Replace `packageJsonPath,·paths.length·===·1` with `⏎··········packageJsonPath,⏎··········paths.length·===·1,⏎········`
if (packageContent_) {
for (const depsKey of Object.keys(packageContent)) {
const key = depsKey as keyof PackageDeps
Object.assign(packageContent[key], packageContent_[key])
}
}
}
} else {
Expand Down
11 changes: 11 additions & 0 deletions test/rules/no-extraneous-dependencies.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ const packageDirBundledDepsAsObject = testFilePath(
const packageDirBundledDepsRaceCondition = testFilePath(
'bundled-dependencies/race-condition',
)
const emptyPackageDir = testFilePath(

Check failure on line 51 in test/rules/no-extraneous-dependencies.spec.ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 20 and ESLint 8.56 on ubuntu-latest

Replace `⏎··'empty-folder',⏎` with `'empty-folder'`

Check failure on line 51 in test/rules/no-extraneous-dependencies.spec.ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 20 and ESLint 8 on ubuntu-latest

Replace `⏎··'empty-folder',⏎` with `'empty-folder'`
'empty-folder',
)

ruleTester.run('no-extraneous-dependencies', rule, {
valid: [
Expand Down Expand Up @@ -130,6 +133,14 @@ ruleTester.run('no-extraneous-dependencies', rule, {
code: 'import leftpad from "left-pad";',
options: [{ packageDir: packageDirMonoRepoRoot }],
}),
test({
code: 'import leftpad from "left-pad";',
options: [{ packageDir: [emptyPackageDir, packageDirMonoRepoRoot] }],
}),
test({
code: 'import leftpad from "left-pad";',
options: [{ packageDir: [packageDirMonoRepoRoot, emptyPackageDir] }],
}),
test({
code: 'import react from "react";',
options: [
Expand Down

0 comments on commit 3a6dee2

Please sign in to comment.