feat(language-service): provide diagnostic for invalid templateUrls (#32586)

`templateUrls` that do not point to actual files are now diagnosed as such
by the Language Service. Support for `styleUrls` will come in a next PR.

This introduces a utility method `getPropertyValueOfType` that scans
TypeScript ASTs until a property assignment whose initializer of a
certain type is found. This PR also notices a couple of things that
could be improved in the language-service implementation, such as
enumerating directive properties and unifying common logic, that will be
fixed in future PRs.

Part of #32564.

PR Close #32586
This commit is contained in:
ayazhafiz
2019-09-10 10:55:12 -05:00
committed by Kara Erickson
parent 88c28ce208
commit adeee0fa7f
4 changed files with 145 additions and 9 deletions

View File

@ -475,6 +475,40 @@ describe('diagnostics', () => {
`Module '"../node_modules/@angular/core/core"' has no exported member 'OpaqueToken'.`);
});
describe('URL diagnostics', () => {
it('should report errors for invalid templateUrls', () => {
const fileName = mockHost.addCode(`
@Component({
templateUrl: '«notAFile»',
})
export class MyComponent {}`);
const marker = mockHost.getReferenceMarkerFor(fileName, 'notAFile');
const diagnostics = ngLS.getDiagnostics(fileName) !;
const urlDiagnostic =
diagnostics.find(d => d.messageText === 'URL does not point to a valid file');
expect(urlDiagnostic).toBeDefined();
const {start, length} = urlDiagnostic !;
expect(start).toBe(marker.start);
expect(length).toBe(marker.length);
});
it('should not report errors for valid templateUrls', () => {
const fileName = mockHost.addCode(`
@Component({
templateUrl: './test.ng',
})
export class MyComponent {}`);
const diagnostics = ngLS.getDiagnostics(fileName) !;
const urlDiagnostic =
diagnostics.find(d => d.messageText === 'URL does not point to a valid file');
expect(urlDiagnostic).toBeUndefined();
});
});
// https://github.com/angular/vscode-ng-language-service/issues/235
// There is no easy fix for this issue currently due to the way template
// tokenization is done. In the example below, the whole string