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

perf(utils): faster ImportDeclaration detect #163

Merged
merged 3 commits into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from all 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: 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 @@ -153,10 +153,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 @@ -195,7 +191,7 @@ function fullResolve(
}

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

Expand Down
Loading