fix(language-service): Update types for TypeScript nullability support

This commit is contained in:
Miško Hevery
2017-03-24 09:57:32 -07:00
committed by Hans
parent 6f5fccfeb7
commit 540581da3e
28 changed files with 234 additions and 225 deletions

View File

@ -20,18 +20,18 @@ export interface SymbolInfo {
span: Span;
}
export function locateSymbol(info: TemplateInfo): SymbolInfo {
const templatePosition = info.position - info.template.span.start;
export function locateSymbol(info: TemplateInfo): SymbolInfo|undefined {
const templatePosition = info.position ! - info.template.span.start;
const path = new TemplateAstPath(info.templateAst, templatePosition);
if (path.tail) {
let symbol: Symbol = undefined;
let span: Span = undefined;
let symbol: Symbol = undefined !;
let span: Span = undefined !;
const attributeValueSymbol = (ast: AST, inEvent: boolean = false): boolean => {
const attribute = findAttribute(info);
if (attribute) {
if (inSpan(templatePosition, spanOf(attribute.valueSpan))) {
const scope = getExpressionScope(info, path, inEvent);
const expressionOffset = attribute.valueSpan.start.offset + 1;
const expressionOffset = attribute.valueSpan !.start.offset + 1;
const result = getExpressionSymbol(
scope, ast, templatePosition - expressionOffset, info.template.query);
if (result) {
@ -52,28 +52,28 @@ export function locateSymbol(info: TemplateInfo): SymbolInfo {
if (component) {
symbol = info.template.query.getTypeSymbol(component.directive.type.reference);
symbol = symbol && new OverrideKindSymbol(symbol, 'component');
span = spanOf(ast);
span = spanOf(ast) !;
} else {
// Find a directive that matches the element name
const directive =
ast.directives.find(d => d.directive.selector.indexOf(ast.name) >= 0);
ast.directives.find(d => d.directive.selector !.indexOf(ast.name) >= 0);
if (directive) {
symbol = info.template.query.getTypeSymbol(directive.directive.type.reference);
symbol = symbol && new OverrideKindSymbol(symbol, 'directive');
span = spanOf(ast);
span = spanOf(ast) !;
}
}
},
visitReference(ast) {
symbol = info.template.query.getTypeSymbol(tokenReference(ast.value));
span = spanOf(ast);
span = spanOf(ast) !;
},
visitVariable(ast) {},
visitEvent(ast) {
if (!attributeValueSymbol(ast.handler, /* inEvent */ true)) {
symbol = findOutputBinding(info, path, ast);
symbol = findOutputBinding(info, path, ast) !;
symbol = symbol && new OverrideKindSymbol(symbol, 'event');
span = spanOf(ast);
span = spanOf(ast) !;
}
},
visitElementProperty(ast) { attributeValueSymbol(ast.value); },
@ -93,12 +93,12 @@ export function locateSymbol(info: TemplateInfo): SymbolInfo {
visitText(ast) {},
visitDirective(ast) {
symbol = info.template.query.getTypeSymbol(ast.directive.type.reference);
span = spanOf(ast);
span = spanOf(ast) !;
},
visitDirectiveProperty(ast) {
if (!attributeValueSymbol(ast.value)) {
symbol = findInputBinding(info, path, ast);
span = spanOf(ast);
symbol = findInputBinding(info, path, ast) !;
span = spanOf(ast) !;
}
}
},
@ -109,14 +109,15 @@ export function locateSymbol(info: TemplateInfo): SymbolInfo {
}
}
function findAttribute(info: TemplateInfo): Attribute {
const templatePosition = info.position - info.template.span.start;
function findAttribute(info: TemplateInfo): Attribute|undefined {
const templatePosition = info.position ! - info.template.span.start;
const path = new HtmlAstPath(info.htmlAst, templatePosition);
return path.first(Attribute);
}
function findInputBinding(
info: TemplateInfo, path: TemplateAstPath, binding: BoundDirectivePropertyAst): Symbol {
info: TemplateInfo, path: TemplateAstPath, binding: BoundDirectivePropertyAst): Symbol|
undefined {
const element = path.first(ElementAst);
if (element) {
for (const directive of element.directives) {
@ -133,7 +134,7 @@ function findInputBinding(
}
function findOutputBinding(
info: TemplateInfo, path: TemplateAstPath, binding: BoundEventAst): Symbol {
info: TemplateInfo, path: TemplateAstPath, binding: BoundEventAst): Symbol|undefined {
const element = path.first(ElementAst);
if (element) {
for (const directive of element.directives) {