Skip to content

Commit a69b041

Browse files
committed
delete entry from the cache when referenced file is removed, added tests
1 parent 6455b8e commit a69b041

7 files changed

Lines changed: 47 additions & 9 deletions

File tree

src/harness/fourslash.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,9 @@ module FourSlash {
285285
case FourSlashTestType.Native:
286286
return new Harness.LanguageService.NativeLanugageServiceAdapter(cancellationToken, compilationOptions);
287287
case FourSlashTestType.Shims:
288-
return new Harness.LanguageService.ShimLanugageServiceAdapter(cancellationToken, compilationOptions);
288+
return new Harness.LanguageService.ShimLanugageServiceAdapter(/*preprocessToResolve*/ false, cancellationToken, compilationOptions);
289+
case FourSlashTestType.ShimsWithPreprocess:
290+
return new Harness.LanguageService.ShimLanugageServiceAdapter(/*preprocessToResolve*/ true, cancellationToken, compilationOptions);
289291
case FourSlashTestType.Server:
290292
return new Harness.LanguageService.ServerLanugageServiceAdapter(cancellationToken, compilationOptions);
291293
default:

src/harness/fourslashRunner.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
const enum FourSlashTestType {
66
Native,
77
Shims,
8+
ShimsWithPreprocess,
89
Server
910
}
1011

@@ -23,6 +24,10 @@ class FourSlashRunner extends RunnerBase {
2324
this.basePath = "tests/cases/fourslash/shims";
2425
this.testSuiteName = "fourslash-shims";
2526
break;
27+
case FourSlashTestType.ShimsWithPreprocess:
28+
this.basePath = 'tests/cases/fourslash/shims-pp';
29+
this.testSuiteName = 'fourslash-shims-pp';
30+
break;
2631
case FourSlashTestType.Server:
2732
this.basePath = "tests/cases/fourslash/server";
2833
this.testSuiteName = "fourslash-server";

src/harness/harnessLanguageService.ts

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,35 @@ module Harness.LanguageService {
203203
/// Shim adapter
204204
class ShimLanguageServiceHost extends LanguageServiceAdapterHost implements ts.LanguageServiceShimHost, ts.CoreServicesShimHost {
205205
private nativeHost: NativeLanguageServiceHost;
206-
constructor(cancellationToken?: ts.HostCancellationToken, options?: ts.CompilerOptions) {
206+
207+
public getModuleResolutionsForFile: (fileName: string)=> string;
208+
209+
constructor(preprocessToResolve: boolean, cancellationToken?: ts.HostCancellationToken, options?: ts.CompilerOptions) {
207210
super(cancellationToken, options);
208211
this.nativeHost = new NativeLanguageServiceHost(cancellationToken, options);
212+
213+
if (preprocessToResolve) {
214+
let compilerOptions = this.nativeHost.getCompilationSettings()
215+
let moduleResolutionHost: ts.ModuleResolutionHost = {
216+
fileExists: fileName => this.getScriptInfo(fileName) !== undefined,
217+
readFile: fileName => {
218+
let scriptInfo = this.getScriptInfo(fileName);
219+
return scriptInfo && scriptInfo.content;
220+
}
221+
};
222+
this.getModuleResolutionsForFile = (fileName) => {
223+
let scriptInfo = this.getScriptInfo(fileName);
224+
let preprocessInfo = ts.preProcessFile(scriptInfo.content, /*readImportFiles*/ true);
225+
let imports: ts.Map<string> = {};
226+
for (let module of preprocessInfo.importedFiles) {
227+
let resolutionInfo = ts.resolveModuleName(module.fileName, fileName, compilerOptions, moduleResolutionHost);
228+
if (resolutionInfo.resolvedFileName) {
229+
imports[module.fileName] = resolutionInfo.resolvedFileName;
230+
}
231+
}
232+
return JSON.stringify(imports);
233+
}
234+
}
209235
}
210236

211237
getFilenames(): string[] { return this.nativeHost.getFilenames(); }
@@ -228,7 +254,7 @@ module Harness.LanguageService {
228254

229255
readDirectory(rootDir: string, extension: string): string {
230256
throw new Error("NYI");
231-
}
257+
}
232258
fileExists(fileName: string) { return this.getScriptInfo(fileName) !== undefined; }
233259
readFile(fileName: string) {
234260
let snapshot = this.nativeHost.getScriptSnapshot(fileName);
@@ -400,8 +426,8 @@ module Harness.LanguageService {
400426
export class ShimLanugageServiceAdapter implements LanguageServiceAdapter {
401427
private host: ShimLanguageServiceHost;
402428
private factory: ts.TypeScriptServicesFactory;
403-
constructor(cancellationToken?: ts.HostCancellationToken, options?: ts.CompilerOptions) {
404-
this.host = new ShimLanguageServiceHost(cancellationToken, options);
429+
constructor(preprocessToResolve: boolean, cancellationToken?: ts.HostCancellationToken, options?: ts.CompilerOptions) {
430+
this.host = new ShimLanguageServiceHost(preprocessToResolve, cancellationToken, options);
405431
this.factory = new TypeScript.Services.TypeScriptServicesFactory();
406432
}
407433
getHost() { return this.host; }

src/harness/runner.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,10 @@ if (testConfigFile !== "") {
6868
case "fourslash-shims":
6969
runners.push(new FourSlashRunner(FourSlashTestType.Shims));
7070
break;
71-
case "fourslash-server":
71+
case 'fourslash-shims-pp':
72+
runners.push(new FourSlashRunner(FourSlashTestType.ShimsWithPreprocess));
73+
break;
74+
case 'fourslash-server':
7275
runners.push(new FourSlashRunner(FourSlashTestType.Server));
7376
break;
7477
case "fourslash-generated":
@@ -98,6 +101,7 @@ if (runners.length === 0) {
98101
// language services
99102
runners.push(new FourSlashRunner(FourSlashTestType.Native));
100103
runners.push(new FourSlashRunner(FourSlashTestType.Shims));
104+
runners.push(new FourSlashRunner(FourSlashTestType.ShimsWithPreprocess));
101105
runners.push(new FourSlashRunner(FourSlashTestType.Server));
102106
// runners.push(new GeneratedFourslashRunner());
103107
}

src/server/editorServices.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ namespace ts.server {
200200
removeReferencedFile(info: ScriptInfo) {
201201
if (!info.isOpen) {
202202
this.filenameToScript[info.fileName] = undefined;
203+
this.resolvedModuleNames.remove(info.fileName);
203204
}
204205
}
205206

src/services/shims.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ namespace ts {
6161
getProjectVersion?(): string;
6262
useCaseSensitiveFileNames?(): boolean;
6363

64-
getModuleResolutionsForFile?(fileName: string): string;
64+
getModuleResolutionsForFile?(fileName: string): string;
6565
}
6666

6767
/** Public interface of the the of a config service shim instance.*/

tests/cases/unittests/services/colorization.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ interface ClassificationEntry {
99

1010
describe('Colorization', function () {
1111
// Use the shim adapter to ensure test coverage of the shim layer for the classifier
12-
var languageServiceAdabtor = new Harness.LanguageService.ShimLanugageServiceAdapter();
13-
var classifier = languageServiceAdabtor.getClassifier();
12+
var languageServiceAdapter = new Harness.LanguageService.ShimLanugageServiceAdapter(/*preprocessToResolve*/ false);
13+
var classifier = languageServiceAdapter.getClassifier();
1414

1515
function getEntryAtPosistion(result: ts.ClassificationResult, position: number) {
1616
var entryPosition = 0;

0 commit comments

Comments
 (0)