Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add languageOptions to ChildContext #85

Merged
merged 7 commits into from
Jun 30, 2024
5 changes: 5 additions & 0 deletions .changeset/short-bees-sell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-import-x": patch
---

add languageOptions to ChildContext
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export type ChildContext = {
settings: PluginSettings
parserPath?: string | null
parserOptions?: TSESLint.ParserOptions
languageOptions?: TSESLint.FlatConfig.LanguageOptions
path: string
filename?: string
}
Expand Down
66 changes: 47 additions & 19 deletions src/utils/export-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import type {
} from '../types'

import { getValue } from './get-value'
import { hashObject } from './hash'
import { hashObject, hashify } from './hash'
import { hasValidExtension, ignore } from './ignore'
import { parse } from './parse'
import { relative, resolve } from './resolve'
Expand Down Expand Up @@ -1078,11 +1078,6 @@ export function recursivePatternCapture(
}
}

let parserOptionsHash = ''
let prevParserOptions = ''
let settingsHash = ''
let prevSettings = ''

/**
* don't hold full context object in memory, just grab what we need.
* also calculate a cacheKey, where parts of the cacheKey hash are memoized
Expand All @@ -1091,24 +1086,14 @@ function childContext(
path: string,
context: RuleContext | ChildContext,
): ChildContext {
const { settings, parserOptions, parserPath } = context

if (JSON.stringify(settings) !== prevSettings) {
settingsHash = hashObject({ settings }).digest('hex')
prevSettings = JSON.stringify(settings)
}

if (JSON.stringify(parserOptions) !== prevParserOptions) {
parserOptionsHash = hashObject({ parserOptions }).digest('hex')
prevParserOptions = JSON.stringify(parserOptions)
}
const { settings, parserOptions, parserPath, languageOptions } = context

return {
cacheKey:
String(parserPath) + parserOptionsHash + settingsHash + String(path),
cacheKey: makeContextCacheKey(context) + String(path),
settings,
parserOptions,
parserPath,
languageOptions,
kosmotema marked this conversation as resolved.
Show resolved Hide resolved
path,
filename:
'physicalFilename' in context
Expand All @@ -1117,6 +1102,49 @@ function childContext(
}
}

const optionsHashesCache: Record<
string,
{ value: string; hash: string } | undefined
> = {}
kosmotema marked this conversation as resolved.
Show resolved Hide resolved

function getOptionsHash(key: string, value: unknown) {
const entry = optionsHashesCache[key]
const stringifiedValue = JSON.stringify(value)

if (stringifiedValue === entry?.value) {
return entry.hash
}

const hash = hashify(value).digest('hex')
optionsHashesCache[key] = { value: stringifiedValue, hash }

return hash
}

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

let hash = getOptionsHash('settings', settings)

const usedParserOptions = languageOptions
? languageOptions.parserOptions
: parserOptions

hash += getOptionsHash('parserOptions', usedParserOptions)

if (languageOptions) {
const { parser: { meta } = {}, ecmaVersion, sourceType } = languageOptions
hash +=
getOptionsHash('parserMeta', meta) +
String(ecmaVersion) +
String(sourceType)
} else {
hash += String(parserPath)
}

return hash
}

/**
* sometimes legacy support isn't _that_ hard... right?
*/
Expand Down
Loading