test(language-service): Remove MockTypescriptHost.readFileContent() (#32782)

readFileContent() has the exact same functionality as readFile(), but it
is not actually part of ts.LanguageServiceHost interface.
It's not actually needed, so replace it with readFile() instead.

PR Close #32782
This commit is contained in:
Keen Yee Liau 2019-09-19 15:32:40 -07:00 committed by atscott
parent 45c893d0e1
commit 53b32f17b3
5 changed files with 16 additions and 16 deletions

View File

@ -88,12 +88,12 @@ describe('completions', () => {
} catch (e) {
// Emit enough diagnostic information to reproduce the error.
console.error(
`Position: ${position}\nContent: "${mockHost.getFileContent(fileName)}"\nStack:\n${e.stack}`);
`Position: ${position}\nContent: "${mockHost.readFile(fileName)}"\nStack:\n${e.stack}`);
throw e;
}
}
const originalContent = mockHost.getFileContent(fileName) !;
const originalContent = mockHost.readFile(fileName) !;
// For each character in the file, add it to the file and request a completion after it.
for (let index = 0, len = originalContent.length; index < len; index++) {

View File

@ -155,7 +155,7 @@ describe('definitions', () => {
expect(def.fileName).toBe(refFileName);
expect(def.name).toBe('TestComponent');
expect(def.kind).toBe('component');
const content = mockHost.getFileContent(refFileName) !;
const content = mockHost.readFile(refFileName) !;
const begin = '/*BeginTestComponent*/ ';
const start = content.indexOf(begin) + begin.length;
const end = content.indexOf(' /*EndTestComponent*/');
@ -192,7 +192,7 @@ describe('definitions', () => {
expect(def.fileName).toBe(refFileName);
expect(def.name).toBe('testEvent');
expect(def.kind).toBe('event');
const content = mockHost.getFileContent(refFileName) !;
const content = mockHost.readFile(refFileName) !;
const ref = `@Output('test') testEvent = new EventEmitter();`;
expect(def.textSpan).toEqual({
start: content.indexOf(ref),
@ -230,7 +230,7 @@ describe('definitions', () => {
expect(def.fileName).toBe(refFileName);
expect(def.name).toBe('name');
expect(def.kind).toBe('property');
const content = mockHost.getFileContent(refFileName) !;
const content = mockHost.readFile(refFileName) !;
const ref = `@Input('tcName') name = 'test';`;
expect(def.textSpan).toEqual({
start: content.indexOf(ref),

View File

@ -109,7 +109,7 @@ describe('diagnostics', () => {
expect(messageText)
.toBe(
`Component 'MyComponent' is not included in a module and will not be available inside a template. Consider adding it to a NgModule declaration.`);
const content = mockHost.getFileContent(fileName) !;
const content = mockHost.readFile(fileName) !;
const keyword = '@Component';
expect(start).toBe(content.lastIndexOf(keyword) + 1); // exclude leading '@'
expect(length).toBe(keyword.length - 1); // exclude leading '@'

View File

@ -149,7 +149,7 @@ export class MockTypescriptHost implements ts.LanguageServiceHost {
}
getScriptSnapshot(fileName: string): ts.IScriptSnapshot|undefined {
const content = this.getFileContent(fileName);
const content = this.readFile(fileName);
if (content) return ts.ScriptSnapshot.fromString(content);
return undefined;
}
@ -172,7 +172,12 @@ export class MockTypescriptHost implements ts.LanguageServiceHost {
fileExists(fileName: string): boolean { return this.getRawFileContent(fileName) != null; }
readFile(path: string): string|undefined { return this.getRawFileContent(path); }
readFile(fileName: string): string|undefined {
const content = this.getRawFileContent(fileName);
if (content) {
return removeReferenceMarkers(removeLocationMarkers(content));
}
}
getMarkerLocations(fileName: string): {[name: string]: number}|undefined {
let content = this.getRawFileContent(fileName);
@ -188,11 +193,6 @@ export class MockTypescriptHost implements ts.LanguageServiceHost {
}
}
getFileContent(fileName: string): string|undefined {
const content = this.getRawFileContent(fileName);
if (content) return removeReferenceMarkers(removeLocationMarkers(content));
}
/**
* Reset the project to its original state, effectively removing all overrides.
*/
@ -269,7 +269,7 @@ export class MockTypescriptHost implements ts.LanguageServiceHost {
*/
addCode(code: string) {
const fileName = '/app/app.component.ts';
const originalContent = this.getFileContent(fileName);
const originalContent = this.readFile(fileName);
const newContent = originalContent + code;
this.override(fileName, newContent);
return fileName;

View File

@ -62,8 +62,8 @@ describe('TypeScriptServiceHost', () => {
expect(ngLSHost.getAnalyzedModules().ngModules).toEqual([]);
// Now add a script, this would change the program
const fileName = '/app/main.ts';
const content = (tsLSHost as MockTypescriptHost).getFileContent(fileName) !;
(tsLSHost as MockTypescriptHost).addScript(fileName, content);
const content = tsLSHost.readFile(fileName) !;
tsLSHost.addScript(fileName, content);
// If the caches are not cleared, we would get back an empty array.
// But if the caches are cleared then the analyzed modules will be non-empty.
expect(ngLSHost.getAnalyzedModules().ngModules.length).not.toEqual(0);