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:
parent
45c893d0e1
commit
53b32f17b3
@ -88,12 +88,12 @@ describe('completions', () => {
|
|||||||
} catch (e) {
|
} catch (e) {
|
||||||
// Emit enough diagnostic information to reproduce the error.
|
// Emit enough diagnostic information to reproduce the error.
|
||||||
console.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;
|
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 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++) {
|
for (let index = 0, len = originalContent.length; index < len; index++) {
|
||||||
|
@ -155,7 +155,7 @@ describe('definitions', () => {
|
|||||||
expect(def.fileName).toBe(refFileName);
|
expect(def.fileName).toBe(refFileName);
|
||||||
expect(def.name).toBe('TestComponent');
|
expect(def.name).toBe('TestComponent');
|
||||||
expect(def.kind).toBe('component');
|
expect(def.kind).toBe('component');
|
||||||
const content = mockHost.getFileContent(refFileName) !;
|
const content = mockHost.readFile(refFileName) !;
|
||||||
const begin = '/*BeginTestComponent*/ ';
|
const begin = '/*BeginTestComponent*/ ';
|
||||||
const start = content.indexOf(begin) + begin.length;
|
const start = content.indexOf(begin) + begin.length;
|
||||||
const end = content.indexOf(' /*EndTestComponent*/');
|
const end = content.indexOf(' /*EndTestComponent*/');
|
||||||
@ -192,7 +192,7 @@ describe('definitions', () => {
|
|||||||
expect(def.fileName).toBe(refFileName);
|
expect(def.fileName).toBe(refFileName);
|
||||||
expect(def.name).toBe('testEvent');
|
expect(def.name).toBe('testEvent');
|
||||||
expect(def.kind).toBe('event');
|
expect(def.kind).toBe('event');
|
||||||
const content = mockHost.getFileContent(refFileName) !;
|
const content = mockHost.readFile(refFileName) !;
|
||||||
const ref = `@Output('test') testEvent = new EventEmitter();`;
|
const ref = `@Output('test') testEvent = new EventEmitter();`;
|
||||||
expect(def.textSpan).toEqual({
|
expect(def.textSpan).toEqual({
|
||||||
start: content.indexOf(ref),
|
start: content.indexOf(ref),
|
||||||
@ -230,7 +230,7 @@ describe('definitions', () => {
|
|||||||
expect(def.fileName).toBe(refFileName);
|
expect(def.fileName).toBe(refFileName);
|
||||||
expect(def.name).toBe('name');
|
expect(def.name).toBe('name');
|
||||||
expect(def.kind).toBe('property');
|
expect(def.kind).toBe('property');
|
||||||
const content = mockHost.getFileContent(refFileName) !;
|
const content = mockHost.readFile(refFileName) !;
|
||||||
const ref = `@Input('tcName') name = 'test';`;
|
const ref = `@Input('tcName') name = 'test';`;
|
||||||
expect(def.textSpan).toEqual({
|
expect(def.textSpan).toEqual({
|
||||||
start: content.indexOf(ref),
|
start: content.indexOf(ref),
|
||||||
|
@ -109,7 +109,7 @@ describe('diagnostics', () => {
|
|||||||
expect(messageText)
|
expect(messageText)
|
||||||
.toBe(
|
.toBe(
|
||||||
`Component 'MyComponent' is not included in a module and will not be available inside a template. Consider adding it to a NgModule declaration.`);
|
`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';
|
const keyword = '@Component';
|
||||||
expect(start).toBe(content.lastIndexOf(keyword) + 1); // exclude leading '@'
|
expect(start).toBe(content.lastIndexOf(keyword) + 1); // exclude leading '@'
|
||||||
expect(length).toBe(keyword.length - 1); // exclude leading '@'
|
expect(length).toBe(keyword.length - 1); // exclude leading '@'
|
||||||
|
@ -149,7 +149,7 @@ export class MockTypescriptHost implements ts.LanguageServiceHost {
|
|||||||
}
|
}
|
||||||
|
|
||||||
getScriptSnapshot(fileName: string): ts.IScriptSnapshot|undefined {
|
getScriptSnapshot(fileName: string): ts.IScriptSnapshot|undefined {
|
||||||
const content = this.getFileContent(fileName);
|
const content = this.readFile(fileName);
|
||||||
if (content) return ts.ScriptSnapshot.fromString(content);
|
if (content) return ts.ScriptSnapshot.fromString(content);
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
@ -172,7 +172,12 @@ export class MockTypescriptHost implements ts.LanguageServiceHost {
|
|||||||
|
|
||||||
fileExists(fileName: string): boolean { return this.getRawFileContent(fileName) != null; }
|
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 {
|
getMarkerLocations(fileName: string): {[name: string]: number}|undefined {
|
||||||
let content = this.getRawFileContent(fileName);
|
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.
|
* Reset the project to its original state, effectively removing all overrides.
|
||||||
*/
|
*/
|
||||||
@ -269,7 +269,7 @@ export class MockTypescriptHost implements ts.LanguageServiceHost {
|
|||||||
*/
|
*/
|
||||||
addCode(code: string) {
|
addCode(code: string) {
|
||||||
const fileName = '/app/app.component.ts';
|
const fileName = '/app/app.component.ts';
|
||||||
const originalContent = this.getFileContent(fileName);
|
const originalContent = this.readFile(fileName);
|
||||||
const newContent = originalContent + code;
|
const newContent = originalContent + code;
|
||||||
this.override(fileName, newContent);
|
this.override(fileName, newContent);
|
||||||
return fileName;
|
return fileName;
|
||||||
|
@ -62,8 +62,8 @@ describe('TypeScriptServiceHost', () => {
|
|||||||
expect(ngLSHost.getAnalyzedModules().ngModules).toEqual([]);
|
expect(ngLSHost.getAnalyzedModules().ngModules).toEqual([]);
|
||||||
// Now add a script, this would change the program
|
// Now add a script, this would change the program
|
||||||
const fileName = '/app/main.ts';
|
const fileName = '/app/main.ts';
|
||||||
const content = (tsLSHost as MockTypescriptHost).getFileContent(fileName) !;
|
const content = tsLSHost.readFile(fileName) !;
|
||||||
(tsLSHost as MockTypescriptHost).addScript(fileName, content);
|
tsLSHost.addScript(fileName, content);
|
||||||
// If the caches are not cleared, we would get back an empty array.
|
// 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.
|
// But if the caches are cleared then the analyzed modules will be non-empty.
|
||||||
expect(ngLSHost.getAnalyzedModules().ngModules.length).not.toEqual(0);
|
expect(ngLSHost.getAnalyzedModules().ngModules.length).not.toEqual(0);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user