Skip to content

Commit

Permalink
refactor(export-map): use versioning approach instead of hashes for o…
Browse files Browse the repository at this point in the history
…ptions
  • Loading branch information
kosmotema committed Jun 29, 2024
1 parent f62f89e commit 75f7705
Showing 1 changed file with 17 additions and 29 deletions.
46 changes: 17 additions & 29 deletions src/utils/export-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import type {
} from '../types'

import { getValue } from './get-value'
import { hashObject, hashify } from './hash'
import { hashObject } from './hash'
import { hasValidExtension, ignore } from './ignore'
import { parse } from './parse'
import { relative, resolve } from './resolve'
Expand Down Expand Up @@ -1103,55 +1103,43 @@ function childContext(
}
}

type OptionsHashesCache = Record<
'settings' | 'parserOptions' | 'parserMeta',
{ value: unknown; hash: string }
type OptionsVersionsCache = Record<
'settings' | 'parserOptions' | 'parser',
{ value: unknown; version: number }
>

const optionsHashesCache: OptionsHashesCache = {
settings: { value: null, hash: '' },
parserOptions: { value: null, hash: '' },
parserMeta: { value: null, hash: '' },
const optionsVersionsCache: OptionsVersionsCache = {
settings: { value: null, version: 0 },
parserOptions: { value: null, version: 0 },
parser: { value: null, version: 0 },
}

function getOptionsHash(key: keyof OptionsHashesCache, value: unknown) {
const entry = optionsHashesCache[key]
function getOptionsVersion(key: keyof OptionsVersionsCache, value: unknown) {
const entry = optionsVersionsCache[key]

if (dequal(value, entry.value)) {
return entry.hash
if (!dequal(value, entry.value)) {
entry.value = value
entry.version += 1
}

const hash = hashify(value).digest('hex')

optionsHashesCache[key].value = value
optionsHashesCache[key].hash = hash

return hash
return String(entry.version)
}

function makeContextCacheKey(context: RuleContext | ChildContext) {
const { settings, parserPath, parserOptions, languageOptions } = context

let hash = getOptionsHash('settings', settings)
let hash = getOptionsVersion('settings', settings)

const usedParserOptions = languageOptions?.parserOptions ?? parserOptions

hash += getOptionsHash('parserOptions', usedParserOptions)
hash += getOptionsVersion('parserOptions', usedParserOptions)

if (languageOptions) {
const { ecmaVersion, sourceType } = languageOptions
hash += String(ecmaVersion) + String(sourceType)
}

if (parserPath) {
hash += parserPath
} else {
const { meta } = languageOptions?.parser ?? {}

if (meta) {
hash += getOptionsHash('parserMeta', meta)
}
}
hash += getOptionsVersion('parser', parserPath || languageOptions?.parser)

return hash
}
Expand Down

0 comments on commit 75f7705

Please sign in to comment.