Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
path,
filename:
'physicalFilename' in context
Expand All @@ -1117,6 +1102,49 @@ function childContext(
}
}

const optionsHashesCache: Record<
string,
{ value: string; hash: string } | undefined
> = {}

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