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:

committed by
Andrew Kushnir

parent
8bc5fb2ab6
commit
3f257e96c6
63
packages/language-service/src/global_symbols.ts
Normal file
63
packages/language-service/src/global_symbols.ts
Normal file
@ -0,0 +1,63 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import * as ng from '../src/types';
|
||||
|
||||
export const EMPTY_SYMBOL_TABLE: Readonly<ng.SymbolTable> = {
|
||||
size: 0,
|
||||
get: () => undefined,
|
||||
has: () => false,
|
||||
values: () => [],
|
||||
};
|
||||
|
||||
/**
|
||||
* A factory function that returns a symbol table that contains all global symbols
|
||||
* available in an interpolation scope in a template.
|
||||
* This function creates the table the first time it is called, and return a cached
|
||||
* value for all subsequent calls.
|
||||
*/
|
||||
export const createGlobalSymbolTable: (query: ng.SymbolQuery) => ng.SymbolTable = (function() {
|
||||
let GLOBAL_SYMBOL_TABLE: ng.SymbolTable|undefined;
|
||||
return function(query: ng.SymbolQuery) {
|
||||
if (GLOBAL_SYMBOL_TABLE) {
|
||||
return GLOBAL_SYMBOL_TABLE;
|
||||
}
|
||||
GLOBAL_SYMBOL_TABLE = query.createSymbolTable([
|
||||
// The `$any()` method casts the type of an expression to `any`.
|
||||
// https://angular.io/guide/template-syntax#the-any-type-cast-function
|
||||
{
|
||||
name: '$any',
|
||||
kind: 'method',
|
||||
type: {
|
||||
name: '$any',
|
||||
kind: 'method',
|
||||
type: undefined,
|
||||
language: 'typescript',
|
||||
container: undefined,
|
||||
public: true,
|
||||
callable: true,
|
||||
definition: undefined,
|
||||
nullable: false,
|
||||
members: () => EMPTY_SYMBOL_TABLE,
|
||||
signatures: () => [],
|
||||
selectSignature(args: ng.Symbol[]) {
|
||||
if (args.length !== 1) {
|
||||
return;
|
||||
}
|
||||
return {
|
||||
arguments: EMPTY_SYMBOL_TABLE, // not used
|
||||
result: query.getBuiltinType(ng.BuiltinType.Any),
|
||||
};
|
||||
},
|
||||
indexed: () => undefined,
|
||||
},
|
||||
},
|
||||
]);
|
||||
return GLOBAL_SYMBOL_TABLE;
|
||||
};
|
||||
})();
|
Reference in New Issue
Block a user