Skip to content

Commit 8677737

Browse files
committed
Optimize
1 parent 0074696 commit 8677737

3 files changed

Lines changed: 42 additions & 33 deletions

File tree

src/analyzer.ts

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,19 @@ export class ComponentLensAnalyzer {
135135
}
136136

137137
private async getFileComponentKind(filePath: string): Promise<ComponentKind> {
138-
const [sourceText, signature] = await Promise.all([
139-
this.host.readFileAsync
140-
? this.host.readFileAsync(filePath)
141-
: this.host.readFile(filePath),
142-
this.host.getSignatureAsync
143-
? this.host.getSignatureAsync(filePath)
144-
: this.host.getSignature(filePath),
145-
])
138+
let sourceText: string | undefined
139+
let signature: string | undefined
140+
141+
if (this.host.readFileAsync) {
142+
;[sourceText, signature] = await Promise.all([
143+
this.host.readFileAsync(filePath),
144+
this.host.getSignatureAsync!(filePath),
145+
])
146+
} else {
147+
sourceText = this.host.readFile(filePath)
148+
signature = this.host.getSignature(filePath)
149+
}
150+
146151
if (sourceText === undefined || signature === undefined) {
147152
return 'unknown'
148153
}
@@ -205,14 +210,15 @@ function collectImportBindings(
205210
return
206211
}
207212

208-
const source = statement.moduleSpecifier.text
209213
const importClause = statement.importClause
210214
if (!importClause) {
211215
return
212216
}
213217

218+
const binding: ImportBinding = { source: statement.moduleSpecifier.text }
219+
214220
if (importClause.name) {
215-
imports.set(importClause.name.text, { source })
221+
imports.set(importClause.name.text, binding)
216222
}
217223

218224
const namedBindings = importClause.namedBindings
@@ -221,12 +227,12 @@ function collectImportBindings(
221227
}
222228

223229
if (ts.isNamespaceImport(namedBindings)) {
224-
imports.set(namedBindings.name.text, { source })
230+
imports.set(namedBindings.name.text, binding)
225231
return
226232
}
227233

228234
for (const element of namedBindings.elements) {
229-
imports.set(element.name.text, { source })
235+
imports.set(element.name.text, binding)
230236
}
231237
}
232238

@@ -332,22 +338,22 @@ function isComponentInitializer(
332338
)
333339
}
334340

335-
function collectJsxTags(root: ts.Node): JsxTagReference[] {
341+
function collectJsxTags(sourceFile: ts.SourceFile): JsxTagReference[] {
336342
const jsxTags: JsxTagReference[] = []
337343
const visit = (node: ts.Node): void => {
338344
if (
339345
ts.isJsxOpeningElement(node) ||
340346
ts.isJsxSelfClosingElement(node) ||
341347
ts.isJsxClosingElement(node)
342348
) {
343-
const jsxTag = createJsxTagReference(node, node.getSourceFile())
349+
const jsxTag = createJsxTagReference(node, sourceFile)
344350
if (jsxTag) {
345351
jsxTags.push(jsxTag)
346352
}
347353
}
348354
ts.forEachChild(node, visit)
349355
}
350-
ts.forEachChild(root, visit)
356+
ts.forEachChild(sourceFile, visit)
351357
return jsxTags
352358
}
353359

src/extension.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export function activate(context: vscode.ExtensionContext): void {
3232
}
3333

3434
const refreshVisibleEditors = async (): Promise<void> => {
35+
sourceHost.invalidateDocumentCache()
3536
await Promise.all(
3637
vscode.window.visibleTextEditors.map((editor) => refreshEditor(editor)),
3738
)
@@ -255,11 +256,20 @@ class WorkspaceSourceHost implements SourceHost {
255256
}
256257
}
257258

259+
public invalidateDocumentCache(): void {
260+
this.documentCache = undefined
261+
}
262+
263+
private documentCache: Map<string, vscode.TextDocument> | undefined
264+
258265
private getOpenDocument(filePath: string): vscode.TextDocument | undefined {
259-
const normalizedPath = path.normalize(filePath)
260-
return vscode.workspace.textDocuments.find(
261-
(document) => path.normalize(document.fileName) === normalizedPath,
262-
)
266+
if (!this.documentCache) {
267+
this.documentCache = new Map()
268+
for (const document of vscode.workspace.textDocuments) {
269+
this.documentCache.set(path.normalize(document.fileName), document)
270+
}
271+
}
272+
return this.documentCache.get(path.normalize(filePath))
263273
}
264274
}
265275

src/resolver.ts

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export class ImportResolver {
5656
fromFilePath: string,
5757
specifier: string,
5858
): string | undefined {
59-
const normalizedFromFilePath = normalizePath(fromFilePath)
59+
const normalizedFromFilePath = path.normalize(fromFilePath)
6060
const cacheKey = `${normalizedFromFilePath}::${specifier}`
6161
const cached = this.resolutionCache.get(cacheKey)
6262
if (cached !== undefined || this.resolutionCache.has(cacheKey)) {
@@ -86,7 +86,7 @@ export class ImportResolver {
8686
return undefined
8787
}
8888

89-
const resolvedFilePath = normalizePath(result.resolvedFileName)
89+
const resolvedFilePath = path.normalize(result.resolvedFileName)
9090
this.resolutionCache.set(cacheKey, resolvedFilePath)
9191
return resolvedFilePath
9292
}
@@ -137,19 +137,17 @@ export class ImportResolver {
137137
}
138138

139139
function findNearestConfigFile(startDirectory: string): string | undefined {
140-
let currentDirectory = normalizePath(startDirectory)
140+
let currentDirectory = path.normalize(startDirectory)
141141

142142
for (;;) {
143143
for (const configFileName of CONFIG_FILE_NAMES) {
144-
const candidatePath = normalizePath(
145-
path.join(currentDirectory, configFileName),
146-
)
144+
const candidatePath = path.join(currentDirectory, configFileName)
147145
if (ts.sys.fileExists(candidatePath)) {
148146
return candidatePath
149147
}
150148
}
151149

152-
const parentDirectory = normalizePath(path.dirname(currentDirectory))
150+
const parentDirectory = path.dirname(currentDirectory)
153151
if (parentDirectory === currentDirectory) {
154152
return undefined
155153
}
@@ -159,14 +157,9 @@ function findNearestConfigFile(startDirectory: string): string | undefined {
159157
}
160158

161159
function isSupportedSourceFile(filePath: string): boolean {
162-
const normalizedFilePath = normalizePath(filePath)
163-
if (normalizedFilePath.endsWith('.d.ts')) {
160+
if (filePath.endsWith('.d.ts')) {
164161
return false
165162
}
166163

167-
return SOURCE_EXTENSIONS.has(path.extname(normalizedFilePath).toLowerCase())
168-
}
169-
170-
function normalizePath(filePath: string): string {
171-
return path.normalize(filePath)
164+
return SOURCE_EXTENSIONS.has(path.extname(filePath).toLowerCase())
172165
}

0 commit comments

Comments
 (0)