Skip to content

Commit

Permalink
chore: Add package.json cache to no-extraneous-dependencies rule
Browse files Browse the repository at this point in the history
  • Loading branch information
fa93hws committed Nov 18, 2020
1 parent dd4e416 commit b8362da
Showing 1 changed file with 10 additions and 3 deletions.
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 b8362da

Please sign in to comment.