Skip to content

Commit

Permalink
perf(utils): faster ImportDeclaration detect (#163)
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW authored Sep 29, 2024
1 parent 38d0081 commit 3811c79
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
5 changes: 4 additions & 1 deletion src/rules/namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,10 @@ export = createRule<[Options], MessageId>({

// same as above, but does not add names to local map
ExportNamespaceSpecifier(namespace) {
const declaration = importDeclaration(context, namespace)
const declaration = importDeclaration(
context,
namespace as TSESTree.ImportDefaultSpecifier,
)

const imports = ExportMap.get(declaration.source.value, context)
if (imports == null) {
Expand Down
6 changes: 5 additions & 1 deletion src/utils/import-declaration.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { AST_NODE_TYPES } from '@typescript-eslint/utils'
import type { TSESTree } from '@typescript-eslint/utils'

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

export const importDeclaration = (
context: RuleContext,
node: TSESTree.Node,
node: TSESTree.ImportDefaultSpecifier,
) => {
if (node.parent && node.parent.type === AST_NODE_TYPES.ImportDeclaration) {
return node.parent
}
const ancestors = context.sourceCode.getAncestors(node)
return ancestors[ancestors.length - 1] as TSESTree.ImportDeclaration
}
6 changes: 1 addition & 5 deletions src/utils/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,6 @@ function fullResolve(
return { found: true, path: cachedPath }
}

function cache(resolvedPath: string | null) {
fileExistsCache.set(cacheKey, resolvedPath)
}

function withResolver(resolver: Resolver, config: unknown) {
if (resolver.interfaceVersion === 2) {
return resolver.resolve(modulePath, sourceFile, config)
Expand Down Expand Up @@ -170,7 +166,7 @@ function fullResolve(
}

// else, counts
cache(resolved.path as string | null)
fileExistsCache.set(cacheKey, resolved.path as string | null)
return resolved
}

Expand Down

0 comments on commit 3811c79

Please sign in to comment.