Skip to content
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

[patch] no-extraneous-dependencies: Add package.json cache #1948

Merged
merged 1 commit into from Nov 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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