Skip to content

Commit b8362da

Browse files
committed
chore: Add package.json cache to no-extraneous-dependencies rule
1 parent dd4e416 commit b8362da

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/rules/no-extraneous-dependencies.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import moduleVisitor from 'eslint-module-utils/moduleVisitor'
77
import importType from '../core/importType'
88
import docsUrl from '../docsUrl'
99

10+
const depFieldCache = new Map()
11+
1012
function hasKeys(obj = {}) {
1113
return Object.keys(obj).length > 0
1214
}
@@ -49,9 +51,14 @@ function getDependencies(context, packageDir) {
4951
if (paths.length > 0) {
5052
// use rule config to find package.json
5153
paths.forEach(dir => {
52-
const _packageContent = extractDepFields(
53-
JSON.parse(fs.readFileSync(path.join(dir, 'package.json'), 'utf8'))
54-
)
54+
const packageJsonPath = path.join(dir, 'package.json')
55+
if (!depFieldCache.has(packageJsonPath)) {
56+
const depFields = extractDepFields(
57+
JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'))
58+
)
59+
depFieldCache.set(packageJsonPath, depFields)
60+
}
61+
const _packageContent = depFieldCache.get(packageJsonPath)
5562
Object.keys(packageContent).forEach(depsKey =>
5663
Object.assign(packageContent[depsKey], _packageContent[depsKey])
5764
)

0 commit comments

Comments
 (0)