Skip to content

Commit 0074696

Browse files
committed
Optimize
1 parent 88ba9c9 commit 0074696

2 files changed

Lines changed: 43 additions & 24 deletions

File tree

src/analyzer.ts

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,7 @@ function parseFileAnalysis(filePath: string, sourceText: string): FileAnalysis {
184184
collectLocalComponentName(statement, localComponentNames)
185185
}
186186

187-
const jsxTags: JsxTagReference[] = []
188-
visitNode(sourceFile, jsxTags)
187+
const jsxTags = collectJsxTags(sourceFile)
189188

190189
return {
191190
imports,
@@ -321,7 +320,8 @@ function isComponentInitializer(
321320
return false
322321
}
323322

324-
const calleeName = initializer.expression.getText()
323+
const callee = initializer.expression
324+
const calleeName = ts.isIdentifier(callee) ? callee.text : callee.getText()
325325
if (!COMPONENT_WRAPPER_NAMES.has(calleeName)) {
326326
return false
327327
}
@@ -332,19 +332,23 @@ function isComponentInitializer(
332332
)
333333
}
334334

335-
function visitNode(node: ts.Node, jsxTags: JsxTagReference[]): void {
336-
if (
337-
ts.isJsxOpeningElement(node) ||
338-
ts.isJsxSelfClosingElement(node) ||
339-
ts.isJsxClosingElement(node)
340-
) {
341-
const jsxTag = createJsxTagReference(node, node.getSourceFile())
342-
if (jsxTag) {
343-
jsxTags.push(jsxTag)
335+
function collectJsxTags(root: ts.Node): JsxTagReference[] {
336+
const jsxTags: JsxTagReference[] = []
337+
const visit = (node: ts.Node): void => {
338+
if (
339+
ts.isJsxOpeningElement(node) ||
340+
ts.isJsxSelfClosingElement(node) ||
341+
ts.isJsxClosingElement(node)
342+
) {
343+
const jsxTag = createJsxTagReference(node, node.getSourceFile())
344+
if (jsxTag) {
345+
jsxTags.push(jsxTag)
346+
}
344347
}
348+
ts.forEachChild(node, visit)
345349
}
346-
347-
ts.forEachChild(node, (childNode) => visitNode(childNode, jsxTags))
350+
ts.forEachChild(root, visit)
351+
return jsxTags
348352
}
349353

350354
function createJsxTagReference(

src/resolver.ts

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,24 @@ export class ImportResolver {
3131
string,
3232
CachedCompilerOptions
3333
>()
34+
private readonly configPathCache = new Map<string, string | undefined>()
3435
private readonly resolutionCache = new Map<string, string | undefined>()
35-
36-
public constructor(private readonly host: SourceHost) {}
36+
private readonly hostDirectoryExists: (directoryPath: string) => boolean
37+
private readonly hostFileExists: (filePath: string) => boolean
38+
private readonly hostReadFile: (filePath: string) => string | undefined
39+
40+
public constructor(private readonly host: SourceHost) {
41+
this.hostDirectoryExists = (directoryPath) =>
42+
host.fileExists(directoryPath) || ts.sys.directoryExists(directoryPath)
43+
this.hostFileExists = (filePath) =>
44+
host.fileExists(filePath) || ts.sys.fileExists(filePath)
45+
this.hostReadFile = (filePath) =>
46+
host.readFile(filePath) ?? ts.sys.readFile(filePath)
47+
}
3748

3849
public clear(): void {
3950
this.compilerOptionsCache.clear()
51+
this.configPathCache.clear()
4052
this.resolutionCache.clear()
4153
}
4254

@@ -53,15 +65,11 @@ export class ImportResolver {
5365

5466
const compilerOptions = this.getCompilerOptions(normalizedFromFilePath)
5567
const resolutionHost: ts.ModuleResolutionHost = {
56-
directoryExists: (directoryPath) =>
57-
this.host.fileExists(directoryPath) ||
58-
ts.sys.directoryExists(directoryPath),
59-
fileExists: (filePath) =>
60-
this.host.fileExists(filePath) || ts.sys.fileExists(filePath),
68+
directoryExists: this.hostDirectoryExists,
69+
fileExists: this.hostFileExists,
6170
getCurrentDirectory: () => path.dirname(normalizedFromFilePath),
6271
getDirectories: ts.sys.getDirectories,
63-
readFile: (filePath) =>
64-
this.host.readFile(filePath) ?? ts.sys.readFile(filePath),
72+
readFile: this.hostReadFile,
6573
realpath: ts.sys.realpath,
6674
useCaseSensitiveFileNames: ts.sys.useCaseSensitiveFileNames,
6775
}
@@ -84,7 +92,14 @@ export class ImportResolver {
8492
}
8593

8694
private getCompilerOptions(filePath: string): ts.CompilerOptions {
87-
const configPath = findNearestConfigFile(path.dirname(filePath))
95+
const directory = path.dirname(filePath)
96+
let configPath: string | undefined
97+
if (this.configPathCache.has(directory)) {
98+
configPath = this.configPathCache.get(directory)
99+
} else {
100+
configPath = findNearestConfigFile(directory)
101+
this.configPathCache.set(directory, configPath)
102+
}
88103
if (!configPath) {
89104
return DEFAULT_COMPILER_OPTIONS
90105
}

0 commit comments

Comments
 (0)