Skip to content

Commit 43241a5

Browse files
Keen Yee Liaumatsko
authored andcommitted
fix(language-service): Increase project/script version in MockHost.reset() (#33200)
PR Close #33200
1 parent becd62d commit 43241a5

File tree

2 files changed

+34
-17
lines changed

2 files changed

+34
-17
lines changed

packages/language-service/test/hover_spec.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,22 @@ import {TypeScriptServiceHost} from '../src/typescript_host';
1414

1515
import {MockTypescriptHost} from './test_utils';
1616

17-
describe('hover', () => {
18-
let mockHost: MockTypescriptHost;
19-
let tsLS: ts.LanguageService;
20-
let ngLSHost: TypeScriptServiceHost;
21-
let ngLS: LanguageService;
17+
fdescribe('hover', () => {
18+
// const mockHost: MockTypescriptHost;
19+
// const tsLS: ts.LanguageService;
20+
// const ngLSHost: TypeScriptServiceHost;
21+
// const ngLS: LanguageService;
22+
const mockHost = new MockTypescriptHost(['/app/main.ts']);
23+
const tsLS = ts.createLanguageService(mockHost);
24+
const ngLSHost = new TypeScriptServiceHost(mockHost, tsLS);
25+
const ngLS = createLanguageService(ngLSHost);
2226

2327
beforeEach(() => {
24-
mockHost = new MockTypescriptHost(['/app/main.ts', '/app/parsing-cases.ts']);
25-
tsLS = ts.createLanguageService(mockHost);
26-
ngLSHost = new TypeScriptServiceHost(mockHost, tsLS);
27-
ngLS = createLanguageService(ngLSHost);
28+
// mockHost = new MockTypescriptHost(['/app/main.ts', '/app/parsing-cases.ts']);
29+
// tsLS = ts.createLanguageService(mockHost);
30+
// ngLSHost = new TypeScriptServiceHost(mockHost, tsLS);
31+
// ngLS = createLanguageService(ngLSHost);
32+
mockHost.reset();
2833
});
2934

3035
it('should be able to find field in an interpolation', () => {
@@ -183,20 +188,20 @@ describe('hover', () => {
183188
});
184189

185190
it('should be able to find the NgModule of a directive', () => {
186-
const fileName = '/app/parsing-cases.ts';
191+
const fileName = '/app/app.component.ts';
187192
mockHost.override(fileName, `
188193
import {Directive} from '@angular/core';
189194
190195
@Directive({
191196
selector: '[string-model]',
192197
})
193-
export class «StringModel» {}`);
194-
const marker = mockHost.getReferenceMarkerFor(fileName, 'StringModel');
198+
export class «AppComponent» {}`);
199+
const marker = mockHost.getReferenceMarkerFor(fileName, 'AppComponent');
195200
const quickInfo = ngLS.getHoverAt(fileName, marker.start);
196201
expect(quickInfo).toBeTruthy();
197202
const {textSpan, displayParts} = quickInfo !;
198203
expect(textSpan).toEqual(marker);
199-
expect(toText(displayParts)).toBe('(directive) AppModule.StringModel: class');
204+
expect(toText(displayParts)).toBe('(directive) AppModule.AppComponent: class');
200205
});
201206
});
202207

packages/language-service/test/test_utils.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ const COMPILER_OPTIONS: Readonly<ts.CompilerOptions> = {
9292
};
9393

9494
export class MockTypescriptHost implements ts.LanguageServiceHost {
95-
private angularPath?: string;
95+
private readonly angularPath: string;
9696
private readonly nodeModulesPath: string;
9797
private readonly scriptVersion = new Map<string, number>();
9898
private readonly overrides = new Map<string, string>();
@@ -127,14 +127,16 @@ export class MockTypescriptHost implements ts.LanguageServiceHost {
127127
}
128128

129129
addScript(fileName: string, content: string) {
130+
if (this.scriptVersion.has(fileName)) {
131+
throw new Error(`${fileName} is already in the root files.`);
132+
}
133+
this.scriptVersion.set(fileName, 0);
130134
this.projectVersion++;
131135
this.overrides.set(fileName, content);
132136
this.overrideDirectory.add(path.dirname(fileName));
133137
this.scriptNames.push(fileName);
134138
}
135139

136-
forgetAngular() { this.angularPath = undefined; }
137-
138140
overrideOptions(options: Partial<ts.CompilerOptions>) {
139141
this.options = {...this.options, ...options};
140142
this.projectVersion++;
@@ -185,6 +187,16 @@ export class MockTypescriptHost implements ts.LanguageServiceHost {
185187
* Reset the project to its original state, effectively removing all overrides.
186188
*/
187189
reset() {
190+
// project version and script version must be monotonically increasing,
191+
// they must not be reset to zero.
192+
this.projectVersion++;
193+
for (const fileName of this.overrides.keys()) {
194+
const version = this.scriptVersion.get(fileName);
195+
if (version === undefined) {
196+
throw new Error(`No prior version found for ${fileName}`);
197+
}
198+
this.scriptVersion.set(fileName, version + 1);
199+
}
188200
// Remove overrides from scriptNames
189201
let length = 0;
190202
for (let i = 0; i < this.scriptNames.length; ++i) {
@@ -251,7 +263,7 @@ export class MockTypescriptHost implements ts.LanguageServiceHost {
251263
return result;
252264
}
253265
}
254-
if (this.angularPath && name.startsWith('/' + node_modules + at_angular)) {
266+
if (name.startsWith('/' + node_modules + at_angular)) {
255267
return this.myPath.posix.join(
256268
this.angularPath, name.substr(node_modules.length + at_angular.length + 1));
257269
}

0 commit comments

Comments
 (0)