Skip to content

Commit 3811c79

Browse files
authored
perf(utils): faster ImportDeclaration detect (#163)
1 parent 38d0081 commit 3811c79

File tree

3 files changed

+10
-7
lines changed

3 files changed

+10
-7
lines changed

src/rules/namespace.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,10 @@ export = createRule<[Options], MessageId>({
151151

152152
// same as above, but does not add names to local map
153153
ExportNamespaceSpecifier(namespace) {
154-
const declaration = importDeclaration(context, namespace)
154+
const declaration = importDeclaration(
155+
context,
156+
namespace as TSESTree.ImportDefaultSpecifier,
157+
)
155158

156159
const imports = ExportMap.get(declaration.source.value, context)
157160
if (imports == null) {

src/utils/import-declaration.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1+
import { AST_NODE_TYPES } from '@typescript-eslint/utils'
12
import type { TSESTree } from '@typescript-eslint/utils'
23

34
import type { RuleContext } from '../types'
45

56
export const importDeclaration = (
67
context: RuleContext,
7-
node: TSESTree.Node,
8+
node: TSESTree.ImportDefaultSpecifier,
89
) => {
10+
if (node.parent && node.parent.type === AST_NODE_TYPES.ImportDeclaration) {
11+
return node.parent
12+
}
913
const ancestors = context.sourceCode.getAncestors(node)
1014
return ancestors[ancestors.length - 1] as TSESTree.ImportDeclaration
1115
}

src/utils/resolve.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,6 @@ function fullResolve(
125125
return { found: true, path: cachedPath }
126126
}
127127

128-
function cache(resolvedPath: string | null) {
129-
fileExistsCache.set(cacheKey, resolvedPath)
130-
}
131-
132128
function withResolver(resolver: Resolver, config: unknown) {
133129
if (resolver.interfaceVersion === 2) {
134130
return resolver.resolve(modulePath, sourceFile, config)
@@ -170,7 +166,7 @@ function fullResolve(
170166
}
171167

172168
// else, counts
173-
cache(resolved.path as string | null)
169+
fileExistsCache.set(cacheKey, resolved.path as string | null)
174170
return resolved
175171
}
176172

0 commit comments

Comments
 (0)