Skip to content

Commit

Permalink
[patch] no-extraneous-dependencies: Add package.json cache
Browse files Browse the repository at this point in the history
  • Loading branch information
fa93hws authored and ljharb committed Nov 18, 2020
1 parent dd4e416 commit 8c1a65d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -10,6 +10,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
- [`export`]/TypeScript: properly detect export specifiers as children of a TS module block ([#1889], thanks [@andreubotella])
- [`order`]: ignore non-module-level requires ([#1940], thanks [@golopot])
- [`no-webpack-loader-syntax`]/TypeScript: avoid crash on missing name ([#1947], thanks @leonardodino)
- [`no-extraneous-dependencies`]: Add package.json cache ([#1948], thanks @fa93hws)

## [2.22.1] - 2020-09-27
### Fixed
Expand Down Expand Up @@ -737,6 +738,7 @@ for info on changes for earlier releases.

[`memo-parser`]: ./memo-parser/README.md

[#1948]: https://github.com/benmosher/eslint-plugin-import/pull/1948
[#1947]: https://github.com/benmosher/eslint-plugin-import/pull/1947
[#1940]: https://github.com/benmosher/eslint-plugin-import/pull/1940
[#1889]: https://github.com/benmosher/eslint-plugin-import/pull/1889
Expand Down
13 changes: 10 additions & 3 deletions src/rules/no-extraneous-dependencies.js
Expand Up @@ -7,6 +7,8 @@ import moduleVisitor from 'eslint-module-utils/moduleVisitor'
import importType from '../core/importType'
import docsUrl from '../docsUrl'

const depFieldCache = new Map()

function hasKeys(obj = {}) {
return Object.keys(obj).length > 0
}
Expand Down Expand Up @@ -49,9 +51,14 @@ function getDependencies(context, packageDir) {
if (paths.length > 0) {
// use rule config to find package.json
paths.forEach(dir => {
const _packageContent = extractDepFields(
JSON.parse(fs.readFileSync(path.join(dir, 'package.json'), 'utf8'))
)
const packageJsonPath = path.join(dir, 'package.json')
if (!depFieldCache.has(packageJsonPath)) {
const depFields = extractDepFields(
JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'))
)
depFieldCache.set(packageJsonPath, depFields)
}
const _packageContent = depFieldCache.get(packageJsonPath)
Object.keys(packageContent).forEach(depsKey =>
Object.assign(packageContent[depsKey], _packageContent[depsKey])
)
Expand Down

0 comments on commit 8c1a65d

Please sign in to comment.