fix(language-service): Add global symbol for $any() (#33245)

This commit introduces a "global symbol table" in the language service for symbols that are available in the top level scope,
and add `$any()` to it.

See https://angular.io/guide/template-syntax#the-any-type-cast-function

PR closes https://github.com/angular/vscode-ng-language-service/issues/242

PR Close #33245
This commit is contained in:
Keen Yee Liau
2019-10-17 18:42:27 -07:00
committed by Andrew Kushnir
parent 8bc5fb2ab6
commit 3f257e96c6
7 changed files with 139 additions and 3 deletions

View File

@ -26,6 +26,7 @@ import {MockTypescriptHost} from './test_utils';
const EXPRESSION_CASES = '/app/expression-cases.ts';
const NG_FOR_CASES = '/app/ng-for-cases.ts';
const NG_IF_CASES = '/app/ng-if-cases.ts';
const TEST_TEMPLATE = '/app/test.ng';
describe('diagnostics', () => {
const mockHost = new MockTypescriptHost(['/app/main.ts', '/app/parsing-cases.ts']);
@ -55,6 +56,26 @@ describe('diagnostics', () => {
}
});
// https://github.com/angular/vscode-ng-language-service/issues/242
it('should support $any() type cast function', () => {
mockHost.override(TEST_TEMPLATE, `<div>{{$any(title).xyz}}</div>`);
const diags = ngLS.getDiagnostics(TEST_TEMPLATE);
expect(diags).toEqual([]);
});
it('should report error for $any() with incorrect number of arguments', () => {
const templates = [
'<div>{{$any().xyz}}</div>', // no argument
'<div>{{$any(title, title).xyz}}</div>', // two arguments
];
for (const template of templates) {
mockHost.override(TEST_TEMPLATE, template);
const diags = ngLS.getDiagnostics(TEST_TEMPLATE);
expect(diags.length).toBe(1);
expect(diags[0].messageText).toBe('Unable to resolve signature for call of method $any');
}
});
describe('in expression-cases.ts', () => {
it('should report access to an unknown field', () => {
const diags = ngLS.getDiagnostics(EXPRESSION_CASES).map(d => d.messageText);