Skip to content

Commit cb53163

Browse files
committed
change(export-map): include languageOptions in context cache key
1 parent 622cf86 commit cb53163

File tree

1 file changed

+45
-18
lines changed

1 file changed

+45
-18
lines changed

src/utils/export-map.ts

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import type {
2020
} from '../types'
2121

2222
import { getValue } from './get-value'
23-
import { hashObject } from './hash'
23+
import { hashObject, hashify } from './hash'
2424
import { hasValidExtension, ignore } from './ignore'
2525
import { parse } from './parse'
2626
import { relative, resolve } from './resolve'
@@ -1078,11 +1078,6 @@ export function recursivePatternCapture(
10781078
}
10791079
}
10801080

1081-
let parserOptionsHash = ''
1082-
let prevParserOptions = ''
1083-
let settingsHash = ''
1084-
let prevSettings = ''
1085-
10861081
/**
10871082
* don't hold full context object in memory, just grab what we need.
10881083
* also calculate a cacheKey, where parts of the cacheKey hash are memoized
@@ -1093,19 +1088,8 @@ function childContext(
10931088
): ChildContext {
10941089
const { settings, parserOptions, parserPath, languageOptions } = context
10951090

1096-
if (JSON.stringify(settings) !== prevSettings) {
1097-
settingsHash = hashObject({ settings }).digest('hex')
1098-
prevSettings = JSON.stringify(settings)
1099-
}
1100-
1101-
if (JSON.stringify(parserOptions) !== prevParserOptions) {
1102-
parserOptionsHash = hashObject({ parserOptions }).digest('hex')
1103-
prevParserOptions = JSON.stringify(parserOptions)
1104-
}
1105-
11061091
return {
1107-
cacheKey:
1108-
String(parserPath) + parserOptionsHash + settingsHash + String(path),
1092+
cacheKey: makeContextCacheKey(context) + String(path),
11091093
settings,
11101094
parserOptions,
11111095
parserPath,
@@ -1118,6 +1102,49 @@ function childContext(
11181102
}
11191103
}
11201104

1105+
const optionsHashesCache: Record<
1106+
string,
1107+
{ value: string; hash: string } | undefined
1108+
> = {}
1109+
1110+
function getOptionsHash(key: string, value: unknown) {
1111+
const entry = optionsHashesCache[key]
1112+
const stringifiedValue = JSON.stringify(value)
1113+
1114+
if (stringifiedValue === entry?.value) {
1115+
return entry.hash
1116+
}
1117+
1118+
const hash = hashify(value).digest('hex')
1119+
optionsHashesCache[key] = { value: stringifiedValue, hash }
1120+
1121+
return hash
1122+
}
1123+
1124+
function makeContextCacheKey(context: RuleContext | ChildContext) {
1125+
const { settings, parserPath, parserOptions, languageOptions } = context
1126+
1127+
let hash = getOptionsHash('settings', settings)
1128+
1129+
const usedParserOptions = languageOptions
1130+
? languageOptions.parserOptions
1131+
: parserOptions
1132+
1133+
hash += getOptionsHash('parserOptions', usedParserOptions)
1134+
1135+
if (languageOptions) {
1136+
const { parser: { meta } = {}, ecmaVersion, sourceType } = languageOptions
1137+
hash +=
1138+
getOptionsHash('parserMeta', meta) +
1139+
String(ecmaVersion) +
1140+
String(sourceType)
1141+
} else {
1142+
hash += String(parserPath)
1143+
}
1144+
1145+
return hash
1146+
}
1147+
11211148
/**
11221149
* sometimes legacy support isn't _that_ hard... right?
11231150
*/

0 commit comments

Comments
 (0)