Skip to content

Commit

Permalink
refactor: replace dequal w/ stable-hash (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW authored Jul 1, 2024
1 parent ded3e80 commit 0f07c36
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 33 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@
"dependencies": {
"@typescript-eslint/utils": "^7.4.0",
"debug": "^4.3.4",
"dequal": "^2.0.3",
"doctrine": "^3.0.0",
"eslint-import-resolver-node": "^0.3.9",
"get-tsconfig": "^4.7.3",
"is-glob": "^4.0.3",
"minimatch": "^9.0.3",
"semver": "^7.6.0",
"stable-hash": "^0.0.4",
"tslib": "^2.6.2"
},
"devDependencies": {
Expand Down
39 changes: 7 additions & 32 deletions src/utils/export-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import path from 'node:path'

import type { TSESLint, TSESTree } from '@typescript-eslint/utils'
import debug from 'debug'
import { dequal } from 'dequal'
import type { Annotation } from 'doctrine'
import doctrine from 'doctrine'
import type { AST } from 'eslint'
import { SourceCode } from 'eslint'
import type { TsConfigJsonResolved } from 'get-tsconfig'
import { getTsconfig } from 'get-tsconfig'
import stableHash from 'stable-hash'

import type {
ChildContext,
Expand Down Expand Up @@ -1103,44 +1103,19 @@ function childContext(
}
}

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

const optionsVersionsCache: OptionsVersionsCache = {
settings: { value: null, version: 0 },
parserOptions: { value: null, version: 0 },
parser: { value: null, version: 0 },
}

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

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

return String(entry.version)
}

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

let hash = getOptionsVersion('settings', settings)

const usedParserOptions = languageOptions?.parserOptions ?? parserOptions

hash += getOptionsVersion('parserOptions', usedParserOptions)
let hash =
stableHash(settings) +
stableHash(languageOptions?.parserOptions ?? parserOptions)

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

hash += getOptionsVersion(
'parser',
hash += stableHash(
parserPath ?? languageOptions?.parser?.meta ?? languageOptions?.parser,
)

Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6156,6 +6156,11 @@ sprintf-js@~1.0.2:
resolved "https://registry.yarnpkg.com/sprintf-js/-/sprintf-js-1.0.3.tgz#04e6926f662895354f3dd015203633b857297e2c"
integrity sha512-D9cPgkvLlV3t3IzL0D0YLvGA9Ahk4PcvVwUbN0dSGr1aP0Nrt4AEnTUbuGvquEC0mA64Gqt1fzirlRs5ibXx8g==

stable-hash@^0.0.4:
version "0.0.4"
resolved "https://registry.yarnpkg.com/stable-hash/-/stable-hash-0.0.4.tgz#55ae7dadc13e4b3faed13601587cec41859b42f7"
integrity sha512-LjdcbuBeLcdETCrPn9i8AYAZ1eCtu4ECAWtP7UleOiZ9LzVxRzzUZEoZ8zB24nhkQnDWyET0I+3sWokSDS3E7g==

stack-utils@^2.0.3:
version "2.0.6"
resolved "https://registry.yarnpkg.com/stack-utils/-/stack-utils-2.0.6.tgz#aaf0748169c02fc33c8232abccf933f54a1cc34f"
Expand Down

0 comments on commit 0f07c36

Please sign in to comment.