Skip to content

Commit c371d09

Browse files
authored
chore: refactor (#38)
* chore: refactor * Create .changeset/weak-avocados-suffer.md
1 parent f1bfef5 commit c371d09

File tree

2 files changed

+54
-116
lines changed

2 files changed

+54
-116
lines changed

.changeset/weak-avocados-suffer.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"typescript-eslint-parser-for-extra-files": minor
3+
---
4+
5+
refactor

src/ts.ts

Lines changed: 49 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ export class TSService {
6363
private readonly fileWatchCallbacks = new Map<
6464
string,
6565
{
66-
setupTarget: () => void;
67-
resetTarget: () => void;
6866
update: () => void;
6967
}
7068
>();
@@ -76,9 +74,7 @@ export class TSService {
7674
}
7775

7876
public getProgram(code: string, filePath: string): ts.Program {
79-
const normalized = normalizeFileName(
80-
toRealFileName(filePath, this.extraFileExtensions)
81-
);
77+
const normalized = normalizeFileName(filePath);
8278
const lastTarget = this.currTarget;
8379

8480
const dirMap = new Map<string, { name: string; path: string }>();
@@ -101,19 +97,8 @@ export class TSService {
10197
.get(normalizeFileName(this.tsconfigPath))
10298
?.update();
10399
}
100+
this.fileWatchCallbacks.get(normalizeFileName(targetPath))?.update();
104101
}
105-
getRefreshTargetFileNames(
106-
lastTarget.filePath,
107-
this.extraFileExtensions
108-
).forEach((vFilePath) => {
109-
this.fileWatchCallbacks.get(vFilePath)?.resetTarget();
110-
});
111-
getRefreshTargetFileNames(
112-
this.currTarget.filePath,
113-
this.extraFileExtensions
114-
).forEach((vFilePath) => {
115-
this.fileWatchCallbacks.get(vFilePath)?.setupTarget();
116-
});
117102

118103
const program = this.watch.getProgram().getProgram();
119104
// sets parent pointers in source files
@@ -286,22 +271,17 @@ export class TSService {
286271
return distinctArray(...results);
287272
};
288273
watchCompilerHost.readFile = (fileName, ...args) => {
289-
const realFileName = toRealFileName(fileName, extraFileExtensions);
290-
const normalized = normalizeFileName(realFileName);
291-
if (this.currTarget.filePath === normalized) {
274+
const realFileName = getRealFileNameIfExist(fileName);
275+
if (realFileName == null) {
276+
return undefined;
277+
}
278+
if (this.currTarget.filePath === realFileName) {
292279
// It is the file currently being parsed.
293280
return transformExtraFile(this.currTarget.code, {
294-
filePath: normalized,
281+
filePath: realFileName,
295282
current: true,
296283
});
297284
}
298-
if (isVirtualTSX(fileName, extraFileExtensions)) {
299-
const dts = toExtraDtsFileName(normalized, extraFileExtensions);
300-
if (original.fileExists.call(watchCompilerHost, dts)) {
301-
// If the .d.ts file exists, respect it and consider the virtual file not to exist.
302-
return undefined;
303-
}
304-
}
305285

306286
const code = original.readFile.call(
307287
watchCompilerHost,
@@ -312,76 +292,52 @@ export class TSService {
312292
return code;
313293
}
314294
return transformExtraFile(code, {
315-
filePath: normalized,
295+
filePath: realFileName,
316296
current: false,
317297
});
318298
};
319299
// Modify it so that it can be determined that the virtual file actually exists.
320-
watchCompilerHost.fileExists = (fileName, ...args) => {
321-
const normalizedFileName = normalizeFileName(fileName);
300+
watchCompilerHost.fileExists = (fileName) => {
301+
return getRealFileNameIfExist(fileName) != null;
302+
};
322303

304+
const getRealFileNameIfExist = (fileName: string): string | null => {
305+
const normalizedFileName = normalizeFileName(fileName);
323306
// Even if it is actually a file, if it is specified as a directory to the target file,
324307
// it is assumed that it does not exist as a file.
325308
if (this.currTarget.dirMap.has(normalizedFileName)) {
326-
return false;
309+
return null;
327310
}
328-
const normalizedRealFileName = toRealFileName(
329-
normalizedFileName,
330-
extraFileExtensions
331-
);
332-
if (this.currTarget.filePath === normalizedRealFileName) {
311+
if (this.currTarget.filePath === normalizedFileName) {
333312
// It is the file currently being parsed.
334-
return true;
313+
return normalizedFileName;
335314
}
336-
if (
337-
original.fileExists.call(
338-
watchCompilerHost,
339-
normalizedRealFileName,
340-
...args
341-
)
342-
) {
343-
if (isVirtualTSX(fileName, extraFileExtensions)) {
344-
if (
345-
original.fileExists.call(
346-
watchCompilerHost,
347-
toExtraDtsFileName(normalizedRealFileName, extraFileExtensions),
348-
...args
349-
)
350-
) {
315+
const exists = original.fileExists.call(
316+
watchCompilerHost,
317+
normalizedFileName
318+
);
319+
if (exists) {
320+
return normalizedFileName;
321+
}
322+
if (isVirtualTSX(normalizedFileName, extraFileExtensions)) {
323+
const real = normalizedFileName.slice(0, -4);
324+
for (const dts of toExtraDtsFileNames(real, extraFileExtensions)) {
325+
if (original.fileExists.call(watchCompilerHost, dts)) {
351326
// If the d.ts file exists, respect it and consider the virtual file not to exist.
352-
return false;
327+
return null;
353328
}
354329
}
355-
return true;
330+
if (original.fileExists.call(watchCompilerHost, real)) {
331+
return real;
332+
}
356333
}
357-
return false;
334+
return null;
358335
};
359336

360-
// It keeps a callback to mark the parsed file as changed so that it can be reparsed.
337+
// It keeps a callback to mark the parsed file as changed so that it can be re-parsed.
361338
watchCompilerHost.watchFile = (fileName, callback) => {
362339
const normalized = normalizeFileName(fileName);
363340
this.fileWatchCallbacks.set(normalized, {
364-
// The function is called when the file is targeted for parsing.
365-
setupTarget: () => {
366-
if (isExtraDts(fileName, extraFileExtensions)) {
367-
callback(fileName, ts.FileWatcherEventKind.Deleted);
368-
} else if (isVirtualTSX(fileName, extraFileExtensions)) {
369-
callback(fileName, ts.FileWatcherEventKind.Created);
370-
} else {
371-
callback(fileName, ts.FileWatcherEventKind.Changed);
372-
}
373-
},
374-
// The function is called when the file leaves the target of parsing.
375-
resetTarget: () => {
376-
if (isExtraDts(fileName, extraFileExtensions)) {
377-
// If the .d.ts file exists, it will take respect.
378-
callback(fileName, ts.FileWatcherEventKind.Created);
379-
} else if (isVirtualTSX(fileName, extraFileExtensions)) {
380-
callback(fileName, ts.FileWatcherEventKind.Deleted);
381-
} else {
382-
callback(fileName, ts.FileWatcherEventKind.Changed);
383-
}
384-
},
385341
update: () => callback(fileName, ts.FileWatcherEventKind.Changed),
386342
});
387343

@@ -420,44 +376,31 @@ export class TSService {
420376
}
421377
}
422378

423-
/**
424-
* If the given filename is a extra extension file (.vue),
425-
* return a list of filenames containing virtual filename (.vue.tsx) and type def filename (.vue.d.ts).
426-
*/
427-
function getRefreshTargetFileNames(
428-
fileName: string,
429-
extraFileExtensions: string[]
430-
) {
431-
if (isExtra(fileName, extraFileExtensions)) {
432-
return [`${fileName}.tsx`, `${fileName}.d.ts`, fileName];
433-
}
434-
return [fileName];
435-
}
436-
437379
/** If the given filename has extra extensions, returns the d.ts filename. */
438-
function toExtraDtsFileName(fileName: string, extraFileExtensions: string[]) {
439-
if (isExtra(fileName, extraFileExtensions)) {
440-
return `${fileName}.d.ts`;
441-
}
442-
return fileName;
443-
}
444-
445-
/** If the given filename is a virtual filename (.vue.tsx), returns the real filename. */
446-
function toRealFileName(fileName: string, extraFileExtensions: string[]) {
447-
if (isVirtualTSX(fileName, extraFileExtensions)) {
448-
return fileName.slice(0, -4);
380+
function toExtraDtsFileNames(fileName: string, extraFileExtensions: string[]) {
381+
const ext = getExtIfExtra(fileName, extraFileExtensions);
382+
if (ext != null) {
383+
return [`${fileName}.d.ts`, `${fileName.slice(0, -ext.length)}.d${ext}.ts`];
449384
}
450-
return fileName;
385+
return [];
451386
}
452387

453388
/** Checks the given filename has extra extension or not. */
454389
function isExtra(fileName: string, extraFileExtensions: string[]): boolean {
390+
return getExtIfExtra(fileName, extraFileExtensions) != null;
391+
}
392+
393+
/** Gets the file extension if the given file is an extra extension file. */
394+
function getExtIfExtra(
395+
fileName: string,
396+
extraFileExtensions: string[]
397+
): string | null {
455398
for (const extraFileExtension of extraFileExtensions) {
456399
if (fileName.endsWith(extraFileExtension)) {
457-
return true;
400+
return extraFileExtension;
458401
}
459402
}
460-
return false;
403+
return null;
461404
}
462405

463406
/** Checks the given filename is virtual file tsx or not. */
@@ -473,16 +416,6 @@ function isVirtualTSX(
473416
return false;
474417
}
475418

476-
/** Checks the given filename has extra extension with d.ts or not. */
477-
function isExtraDts(fileName: string, extraFileExtensions: string[]): boolean {
478-
for (const extraFileExtension of extraFileExtensions) {
479-
if (fileName.endsWith(`${extraFileExtension}.d.ts`)) {
480-
return true;
481-
}
482-
}
483-
return false;
484-
}
485-
486419
function formatDiagnostics(diagnostics: ts.Diagnostic[]) {
487420
return ts.formatDiagnostics(diagnostics, {
488421
getCanonicalFileName: (f) => f,

0 commit comments

Comments
 (0)