Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(language-service): clean up exports and consolidate types #36533

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 0 additions & 4 deletions goldens/packages-circular-deps.json
Expand Up @@ -1956,10 +1956,6 @@
"packages/forms/src/directives/validators.ts",
"packages/forms/src/validators.ts"
],
[
"packages/language-service/src/common.ts",
"packages/language-service/src/types.ts"
],
[
"packages/language-service/src/completions.ts",
"packages/language-service/src/template.ts",
Expand Down
3 changes: 1 addition & 2 deletions packages/language-service/language-service.ts
Expand Up @@ -15,6 +15,5 @@
*/
export {createLanguageService} from './src/language_service';
export * from './src/ts_plugin';
export {Completion, Completions, Declaration, Declarations, Definition, Diagnostic, Diagnostics, Hover, HoverTextSection, LanguageService, LanguageServiceHost, Location, Span, TemplateSource, TemplateSources} from './src/types';
export {Declaration, Definition, Diagnostic, LanguageService, LanguageServiceHost, Span, TemplateSource} from './src/types';
export {TypeScriptServiceHost, createLanguageServiceFromTypescript} from './src/typescript_host';
export {VERSION} from './src/version';
27 changes: 0 additions & 27 deletions packages/language-service/src/common.ts

This file was deleted.

26 changes: 13 additions & 13 deletions packages/language-service/src/completions.ts
Expand Up @@ -10,13 +10,12 @@ import {AbsoluteSourceSpan, AST, AstPath, AttrAst, Attribute, BoundDirectiveProp
import {$$, $_, isAsciiLetter, isDigit} from '@angular/compiler/src/chars';

import {ATTR, getBindingDescriptor} from './binding_utils';
import {AstResult} from './common';
import {diagnosticInfoFromTemplateInfo, getExpressionScope} from './expression_diagnostics';
import {getExpressionScope} from './expression_diagnostics';
import {getExpressionCompletions} from './expressions';
import {attributeNames, elementNames, eventNames, propertyNames} from './html_info';
import {InlineTemplate} from './template';
import * as ng from './types';
import {findTemplateAstAt, getPathToNodeAtPosition, getSelectors, inSpan, isStructuralDirective, spanOf} from './utils';
import {diagnosticInfoFromTemplateInfo, findTemplateAstAt, getPathToNodeAtPosition, getSelectors, inSpan, isStructuralDirective, spanOf} from './utils';

const HIDDEN_HTML_ELEMENTS: ReadonlySet<string> =
new Set(['html', 'script', 'noscript', 'base', 'body', 'title', 'head', 'link']);
Expand Down Expand Up @@ -56,7 +55,7 @@ function isIdentifierPart(code: number) {
* `position`, nothing is returned.
*/
function getBoundedWordSpan(
templateInfo: AstResult, position: number, ast: HtmlAst|undefined): ts.TextSpan|undefined {
templateInfo: ng.AstResult, position: number, ast: HtmlAst|undefined): ts.TextSpan|undefined {
const {template} = templateInfo;
const templateSrc = template.source;

Expand Down Expand Up @@ -127,7 +126,7 @@ function getBoundedWordSpan(
}

export function getTemplateCompletions(
templateInfo: AstResult, position: number): ng.CompletionEntry[] {
templateInfo: ng.AstResult, position: number): ng.CompletionEntry[] {
let result: ng.CompletionEntry[] = [];
const {htmlAst, template} = templateInfo;
// The templateNode starts at the delimiter character so we add 1 to skip it.
Expand Down Expand Up @@ -204,7 +203,7 @@ export function getTemplateCompletions(
});
}

function attributeCompletions(info: AstResult, path: AstPath<HtmlAst>): ng.CompletionEntry[] {
function attributeCompletions(info: ng.AstResult, path: AstPath<HtmlAst>): ng.CompletionEntry[] {
const attr = path.tail;
const elem = path.parentOf(attr);
if (!(attr instanceof Attribute) || !(elem instanceof Element)) {
Expand Down Expand Up @@ -258,7 +257,7 @@ function attributeCompletions(info: AstResult, path: AstPath<HtmlAst>): ng.Compl
}

function attributeCompletionsForElement(
info: AstResult, elementName: string): ng.CompletionEntry[] {
info: ng.AstResult, elementName: string): ng.CompletionEntry[] {
const results: ng.CompletionEntry[] = [];

if (info.template instanceof InlineTemplate) {
Expand Down Expand Up @@ -292,7 +291,8 @@ function attributeCompletionsForElement(
* @param info Object that contains the template AST
* @param htmlPath Path to the HTML node
*/
function attributeValueCompletions(info: AstResult, htmlPath: HtmlAstPath): ng.CompletionEntry[] {
function attributeValueCompletions(
info: ng.AstResult, htmlPath: HtmlAstPath): ng.CompletionEntry[] {
// Find the corresponding Template AST path.
const templatePath = findTemplateAstAt(info.templateAst, htmlPath.position);
const visitor = new ExpressionVisitor(info, htmlPath.position, () => {
Expand Down Expand Up @@ -334,7 +334,7 @@ function attributeValueCompletions(info: AstResult, htmlPath: HtmlAstPath): ng.C
return visitor.results;
}

function elementCompletions(info: AstResult): ng.CompletionEntry[] {
function elementCompletions(info: ng.AstResult): ng.CompletionEntry[] {
const results: ng.CompletionEntry[] = [...ANGULAR_ELEMENTS];

if (info.template instanceof InlineTemplate) {
Expand Down Expand Up @@ -380,7 +380,7 @@ function entityCompletions(value: string, position: number): ng.CompletionEntry[
return result;
}

function interpolationCompletions(info: AstResult, position: number): ng.CompletionEntry[] {
function interpolationCompletions(info: ng.AstResult, position: number): ng.CompletionEntry[] {
// Look for an interpolation in at the position.
const templatePath = findTemplateAstAt(info.templateAst, position);
if (!templatePath.tail) {
Expand All @@ -399,7 +399,7 @@ function interpolationCompletions(info: AstResult, position: number): ng.Complet
// code checks for this case and returns element completions if it is detected or undefined
// if it is not.
function voidElementAttributeCompletions(
info: AstResult, path: AstPath<HtmlAst>): ng.CompletionEntry[] {
info: ng.AstResult, path: AstPath<HtmlAst>): ng.CompletionEntry[] {
const tail = path.tail;
if (tail instanceof Text) {
const match = tail.value.match(/<(\w(\w|\d|-)*:)?(\w(\w|\d|-)*)\s/);
Expand All @@ -417,7 +417,7 @@ class ExpressionVisitor extends NullTemplateVisitor {
private readonly completions = new Map<string, ng.CompletionEntry>();

constructor(
private readonly info: AstResult, private readonly position: number,
private readonly info: ng.AstResult, private readonly position: number,
private readonly getExpressionScope: () => ng.SymbolTable) {
super();
}
Expand Down Expand Up @@ -619,7 +619,7 @@ interface AngularAttributes {
* @param info
* @param elementName
*/
function angularAttributes(info: AstResult, elementName: string): AngularAttributes {
function angularAttributes(info: ng.AstResult, elementName: string): AngularAttributes {
const {selectors, map: selectorMap} = getSelectors(info);
const templateRefs = new Set<string>();
const inputs = new Set<string>();
Expand Down
7 changes: 3 additions & 4 deletions packages/language-service/src/definitions.ts
Expand Up @@ -8,11 +8,10 @@

import * as path from 'path';
import * as ts from 'typescript'; // used as value and is provided at runtime
import {AstResult} from './common';

import {locateSymbols} from './locate_symbol';
import {getPropertyAssignmentFromValue, isClassDecoratorProperty} from './template';
import {Span} from './types';
import {findTightestNode} from './utils';
import {AstResult, Span} from './types';
import {findTightestNode, getPropertyAssignmentFromValue, isClassDecoratorProperty} from './utils';

/**
* Convert Angular Span to TypeScript TextSpan. Angular Span has 'start' and
Expand Down
5 changes: 1 addition & 4 deletions packages/language-service/src/diagnostics.ts
Expand Up @@ -10,20 +10,17 @@ import {NgAnalyzedModules} from '@angular/compiler';
import * as path from 'path';
import * as ts from 'typescript';

import {AstResult} from './common';
import {createDiagnostic, Diagnostic} from './diagnostic_messages';
import {getTemplateExpressionDiagnostics} from './expression_diagnostics';
import * as ng from './types';
import {TypeScriptServiceHost} from './typescript_host';
import {findPropertyValueOfType, findTightestNode, offsetSpan, spanOf} from './utils';



/**
* Return diagnostic information for the parsed AST of the template.
* @param ast contains HTML and template AST
*/
export function getTemplateDiagnostics(ast: AstResult): ng.Diagnostic[] {
export function getTemplateDiagnostics(ast: ng.AstResult): ng.Diagnostic[] {
const {parseErrors, templateAst, htmlAst, template} = ast;
if (parseErrors && parseErrors.length) {
return parseErrors.map(e => {
Expand Down
42 changes: 9 additions & 33 deletions packages/language-service/src/expression_diagnostics.ts
Expand Up @@ -6,33 +6,22 @@
* found in the LICENSE file at https://angular.io/license
*/

import {AST, AstPath, Attribute, BoundDirectivePropertyAst, BoundElementPropertyAst, BoundEventAst, BoundTextAst, CompileDirectiveSummary, CompileTypeMetadata, DirectiveAst, ElementAst, EmbeddedTemplateAst, identifierName, Node, ParseSourceSpan, RecursiveTemplateAstVisitor, ReferenceAst, TemplateAst, TemplateAstPath, templateVisitAll, tokenReference, VariableAst} from '@angular/compiler';
import {AST, AstPath, Attribute, BoundDirectivePropertyAst, BoundElementPropertyAst, BoundEventAst, BoundTextAst, CompileDirectiveSummary, CompileTypeMetadata, DirectiveAst, ElementAst, EmbeddedTemplateAst, identifierName, ParseSourceSpan, RecursiveTemplateAstVisitor, ReferenceAst, TemplateAst, TemplateAstPath, templateVisitAll, tokenReference, VariableAst} from '@angular/compiler';

import {AstResult} from './common';
import {createDiagnostic, Diagnostic} from './diagnostic_messages';
import {AstType} from './expression_type';
import {BuiltinType, Definition, Span, Symbol, SymbolDeclaration, SymbolQuery, SymbolTable} from './symbols';
import * as ng from './types';
import {findOutputBinding, getPathToNodeAtPosition} from './utils';

export interface DiagnosticTemplateInfo {
fileName?: string;
offset: number;
query: SymbolQuery;
members: SymbolTable;
htmlAst: Node[];
templateAst: TemplateAst[];
source: string;
}

export function getTemplateExpressionDiagnostics(info: DiagnosticTemplateInfo): ng.Diagnostic[] {
export function getTemplateExpressionDiagnostics(info: ng.DiagnosticTemplateInfo): ng.Diagnostic[] {
const visitor = new ExpressionDiagnosticsVisitor(
info, (path: TemplateAstPath) => getExpressionScope(info, path));
templateVisitAll(visitor, info.templateAst);
return visitor.diagnostics;
}

function getReferences(info: DiagnosticTemplateInfo): SymbolDeclaration[] {
function getReferences(info: ng.DiagnosticTemplateInfo): SymbolDeclaration[] {
const result: SymbolDeclaration[] = [];

function processReferences(references: ReferenceAst[]) {
Expand Down Expand Up @@ -68,7 +57,7 @@ function getReferences(info: DiagnosticTemplateInfo): SymbolDeclaration[] {
return result;
}

function getDefinitionOf(info: DiagnosticTemplateInfo, ast: TemplateAst): Definition|undefined {
function getDefinitionOf(info: ng.DiagnosticTemplateInfo, ast: TemplateAst): Definition|undefined {
if (info.fileName) {
const templateOffset = info.offset;
return [{
Expand All @@ -88,7 +77,7 @@ function getDefinitionOf(info: DiagnosticTemplateInfo, ast: TemplateAst): Defini
* @param path template AST path
*/
function getVarDeclarations(
info: DiagnosticTemplateInfo, path: TemplateAstPath): SymbolDeclaration[] {
info: ng.DiagnosticTemplateInfo, path: TemplateAstPath): SymbolDeclaration[] {
const results: SymbolDeclaration[] = [];
for (let current = path.head; current; current = path.childOf(current)) {
if (!(current instanceof EmbeddedTemplateAst)) {
Expand Down Expand Up @@ -154,7 +143,7 @@ function getVariableTypeFromDirectiveContext(
* @param templateElement
*/
function refinedVariableType(
value: string, mergedTable: SymbolTable, info: DiagnosticTemplateInfo,
value: string, mergedTable: SymbolTable, info: ng.DiagnosticTemplateInfo,
templateElement: EmbeddedTemplateAst): Symbol {
if (value === '$implicit') {
// Special case: ngFor directive
Expand Down Expand Up @@ -206,7 +195,7 @@ function refinedVariableType(
}

function getEventDeclaration(
info: DiagnosticTemplateInfo, path: TemplateAstPath): SymbolDeclaration|undefined {
info: ng.DiagnosticTemplateInfo, path: TemplateAstPath): SymbolDeclaration|undefined {
const event = path.tail;
if (!(event instanceof BoundEventAst)) {
// No event available in this context.
Expand Down Expand Up @@ -241,7 +230,7 @@ function getEventDeclaration(
* derived for.
*/
export function getExpressionScope(
info: DiagnosticTemplateInfo, path: TemplateAstPath): SymbolTable {
info: ng.DiagnosticTemplateInfo, path: TemplateAstPath): SymbolTable {
let result = info.members;
const references = getReferences(info);
const variables = getVarDeclarations(info, path);
Expand All @@ -262,7 +251,7 @@ class ExpressionDiagnosticsVisitor extends RecursiveTemplateAstVisitor {
diagnostics: ng.Diagnostic[] = [];

constructor(
private info: DiagnosticTemplateInfo,
private info: ng.DiagnosticTemplateInfo,
private getExpressionScope: (path: TemplateAstPath, includeEvent: boolean) => SymbolTable) {
super();
this.path = new AstPath<TemplateAst>([]);
Expand Down Expand Up @@ -386,16 +375,3 @@ function hasTemplateReference(type: CompileTypeMetadata): boolean {
function spanOf(sourceSpan: ParseSourceSpan): Span {
return {start: sourceSpan.start.offset, end: sourceSpan.end.offset};
}


export function diagnosticInfoFromTemplateInfo(info: AstResult): DiagnosticTemplateInfo {
return {
fileName: info.template.fileName,
offset: info.template.span.start,
query: info.template.query,
members: info.template.members,
htmlAst: info.htmlAst,
templateAst: info.templateAst,
source: info.template.source,
};
}
10 changes: 5 additions & 5 deletions packages/language-service/src/expression_type.ts
Expand Up @@ -12,7 +12,7 @@ import {createDiagnostic, Diagnostic} from './diagnostic_messages';
import {BuiltinType, Signature, Symbol, SymbolQuery, SymbolTable} from './symbols';
import * as ng from './types';

export interface ExpressionDiagnosticsContext {
interface ExpressionDiagnosticsContext {
inEvent?: boolean;
}

Expand Down Expand Up @@ -225,7 +225,7 @@ export class AstType implements AstVisitor {
return this.anyType;
}

visitImplicitReceiver(ast: ImplicitReceiver): Symbol {
visitImplicitReceiver(_ast: ImplicitReceiver): Symbol {
const _this = this;
// Return a pseudo-symbol for the implicit receiver.
// The members of the implicit receiver are what is defined by the
Expand All @@ -247,11 +247,11 @@ export class AstType implements AstVisitor {
signatures(): Signature[] {
return [];
},
selectSignature(types): Signature |
selectSignature(_types): Signature |
undefined {
return undefined;
},
indexed(argument): Symbol |
indexed(_argument): Symbol |
undefined {
return undefined;
},
Expand Down Expand Up @@ -366,7 +366,7 @@ export class AstType implements AstVisitor {
return this.getType(ast.value);
}

visitQuote(ast: Quote) {
visitQuote(_ast: Quote) {
// The type of a quoted expression is any.
return this.query.getBuiltinType(BuiltinType.Any);
}
Expand Down