@@ -20,7 +20,7 @@ import type {
20
20
} from '../types'
21
21
22
22
import { getValue } from './get-value'
23
- import { hashObject } from './hash'
23
+ import { hashObject , hashify } from './hash'
24
24
import { hasValidExtension , ignore } from './ignore'
25
25
import { parse } from './parse'
26
26
import { relative , resolve } from './resolve'
@@ -1078,11 +1078,6 @@ export function recursivePatternCapture(
1078
1078
}
1079
1079
}
1080
1080
1081
- let parserOptionsHash = ''
1082
- let prevParserOptions = ''
1083
- let settingsHash = ''
1084
- let prevSettings = ''
1085
-
1086
1081
/**
1087
1082
* don't hold full context object in memory, just grab what we need.
1088
1083
* also calculate a cacheKey, where parts of the cacheKey hash are memoized
@@ -1093,19 +1088,8 @@ function childContext(
1093
1088
) : ChildContext {
1094
1089
const { settings, parserOptions, parserPath, languageOptions } = context
1095
1090
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
-
1106
1091
return {
1107
- cacheKey :
1108
- String ( parserPath ) + parserOptionsHash + settingsHash + String ( path ) ,
1092
+ cacheKey : makeContextCacheKey ( context ) + String ( path ) ,
1109
1093
settings,
1110
1094
parserOptions,
1111
1095
parserPath,
@@ -1118,6 +1102,49 @@ function childContext(
1118
1102
}
1119
1103
}
1120
1104
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
+
1121
1148
/**
1122
1149
* sometimes legacy support isn't _that_ hard... right?
1123
1150
*/
0 commit comments