2016-11-22 09:10:23 -08:00
|
|
|
/**
|
|
|
|
|
* @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
|
|
|
|
|
*/
|
|
|
|
|
|
2019-08-15 10:42:00 -07:00
|
|
|
import {NgAnalyzedModules} from '@angular/compiler';
|
|
|
|
|
import {getTemplateExpressionDiagnostics} from '@angular/compiler-cli/src/language_services';
|
2019-08-09 15:52:49 -07:00
|
|
|
import * as ts from 'typescript';
|
|
|
|
|
|
2017-05-09 16:16:50 -07:00
|
|
|
import {AstResult} from './common';
|
2019-08-15 10:42:00 -07:00
|
|
|
import * as ng from './types';
|
2019-08-09 15:52:49 -07:00
|
|
|
import {offsetSpan, spanOf} from './utils';
|
2016-11-22 09:10:23 -08:00
|
|
|
|
2019-08-15 10:42:00 -07:00
|
|
|
/**
|
|
|
|
|
* Return diagnostic information for the parsed AST of the template.
|
|
|
|
|
* @param ast contains HTML and template AST
|
|
|
|
|
*/
|
2019-08-21 14:36:00 -07:00
|
|
|
export function getTemplateDiagnostics(ast: AstResult): ng.Diagnostic[] {
|
2019-08-15 10:42:00 -07:00
|
|
|
const results: ng.Diagnostic[] = [];
|
2019-08-21 14:36:00 -07:00
|
|
|
const {parseErrors, templateAst, htmlAst, template} = ast;
|
|
|
|
|
if (parseErrors) {
|
|
|
|
|
results.push(...parseErrors.map(e => {
|
2019-08-09 15:52:49 -07:00
|
|
|
return {
|
2019-08-15 10:42:00 -07:00
|
|
|
kind: ng.DiagnosticKind.Error,
|
2019-08-09 15:52:49 -07:00
|
|
|
span: offsetSpan(spanOf(e.span), template.span.start),
|
|
|
|
|
message: e.msg,
|
|
|
|
|
};
|
|
|
|
|
}));
|
|
|
|
|
}
|
2019-08-21 14:36:00 -07:00
|
|
|
const expressionDiagnostics = getTemplateExpressionDiagnostics({
|
|
|
|
|
templateAst: templateAst,
|
|
|
|
|
htmlAst: htmlAst,
|
|
|
|
|
offset: template.span.start,
|
|
|
|
|
query: template.query,
|
|
|
|
|
members: template.members,
|
|
|
|
|
});
|
|
|
|
|
results.push(...expressionDiagnostics);
|
2019-08-09 15:52:49 -07:00
|
|
|
return results;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-15 10:42:00 -07:00
|
|
|
/**
|
|
|
|
|
* Generate an error message that indicates a directive is not part of any
|
|
|
|
|
* NgModule.
|
|
|
|
|
* @param name class name
|
|
|
|
|
* @param isComponent true if directive is an Angular Component
|
|
|
|
|
*/
|
|
|
|
|
function missingDirective(name: string, isComponent: boolean) {
|
|
|
|
|
const type = isComponent ? 'Component' : 'Directive';
|
|
|
|
|
return `${type} '${name}' is not included in a module and will not be ` +
|
|
|
|
|
'available inside a template. Consider adding it to a NgModule declaration.';
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-22 09:10:23 -08:00
|
|
|
export function getDeclarationDiagnostics(
|
2019-08-15 10:42:00 -07:00
|
|
|
declarations: ng.Declaration[], modules: NgAnalyzedModules): ng.Diagnostic[] {
|
|
|
|
|
const directives = new Set<ng.StaticSymbol>();
|
|
|
|
|
for (const ngModule of modules.ngModules) {
|
|
|
|
|
for (const directive of ngModule.declaredDirectives) {
|
|
|
|
|
directives.add(directive.reference);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const results: ng.Diagnostic[] = [];
|
2016-11-22 09:10:23 -08:00
|
|
|
|
|
|
|
|
for (const declaration of declarations) {
|
2019-08-15 10:42:00 -07:00
|
|
|
const {errors, metadata, type, declarationSpan} = declaration;
|
|
|
|
|
for (const error of errors) {
|
|
|
|
|
results.push({
|
|
|
|
|
kind: ng.DiagnosticKind.Error,
|
|
|
|
|
message: error.message,
|
|
|
|
|
span: error.span,
|
2016-12-02 14:34:16 -08:00
|
|
|
});
|
2016-11-22 09:10:23 -08:00
|
|
|
}
|
2019-08-15 10:42:00 -07:00
|
|
|
if (!metadata) {
|
|
|
|
|
continue; // declaration is not an Angular directive
|
|
|
|
|
}
|
|
|
|
|
if (metadata.isComponent) {
|
|
|
|
|
if (!modules.ngModuleByPipeOrDirective.has(declaration.type)) {
|
|
|
|
|
results.push({
|
|
|
|
|
kind: ng.DiagnosticKind.Error,
|
|
|
|
|
message: missingDirective(type.name, metadata.isComponent),
|
|
|
|
|
span: declarationSpan,
|
|
|
|
|
});
|
2016-11-22 09:10:23 -08:00
|
|
|
}
|
2019-08-15 10:42:00 -07:00
|
|
|
const {template, templateUrl} = metadata.template !;
|
|
|
|
|
if (template === null && !templateUrl) {
|
|
|
|
|
results.push({
|
|
|
|
|
kind: ng.DiagnosticKind.Error,
|
|
|
|
|
message: `Component '${type.name}' must have a template or templateUrl`,
|
|
|
|
|
span: declarationSpan,
|
|
|
|
|
});
|
|
|
|
|
} else if (template && templateUrl) {
|
|
|
|
|
results.push({
|
|
|
|
|
kind: ng.DiagnosticKind.Error,
|
|
|
|
|
message: `Component '${type.name}' must not have both template and templateUrl`,
|
|
|
|
|
span: declarationSpan,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
} else if (!directives.has(declaration.type)) {
|
|
|
|
|
results.push({
|
|
|
|
|
kind: ng.DiagnosticKind.Error,
|
|
|
|
|
message: missingDirective(type.name, metadata.isComponent),
|
|
|
|
|
span: declarationSpan,
|
|
|
|
|
});
|
2016-11-22 09:10:23 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return results;
|
|
|
|
|
}
|
2019-08-09 15:52:49 -07:00
|
|
|
|
2019-08-15 10:42:00 -07:00
|
|
|
/**
|
|
|
|
|
* Return a recursive data structure that chains diagnostic messages.
|
|
|
|
|
* @param chain
|
|
|
|
|
*/
|
|
|
|
|
function chainDiagnostics(chain: ng.DiagnosticMessageChain): ts.DiagnosticMessageChain {
|
2019-08-09 15:52:49 -07:00
|
|
|
return {
|
|
|
|
|
messageText: chain.message,
|
|
|
|
|
category: ts.DiagnosticCategory.Error,
|
|
|
|
|
code: 0,
|
2019-08-15 10:42:00 -07:00
|
|
|
next: chain.next ? chainDiagnostics(chain.next) : undefined
|
2019-08-09 15:52:49 -07:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-15 10:42:00 -07:00
|
|
|
/**
|
|
|
|
|
* Convert ng.Diagnostic to ts.Diagnostic.
|
|
|
|
|
* @param d diagnostic
|
|
|
|
|
* @param file
|
|
|
|
|
*/
|
2019-08-09 15:52:49 -07:00
|
|
|
export function ngDiagnosticToTsDiagnostic(
|
2019-08-15 10:42:00 -07:00
|
|
|
d: ng.Diagnostic, file: ts.SourceFile | undefined): ts.Diagnostic {
|
2019-08-09 15:52:49 -07:00
|
|
|
return {
|
|
|
|
|
file,
|
|
|
|
|
start: d.span.start,
|
|
|
|
|
length: d.span.end - d.span.start,
|
2019-08-15 10:42:00 -07:00
|
|
|
messageText: typeof d.message === 'string' ? d.message : chainDiagnostics(d.message),
|
2019-08-09 15:52:49 -07:00
|
|
|
category: ts.DiagnosticCategory.Error,
|
|
|
|
|
code: 0,
|
|
|
|
|
source: 'ng',
|
|
|
|
|
};
|
|
|
|
|
}
|
2019-08-12 16:54:36 -07:00
|
|
|
|
2019-08-15 10:42:00 -07:00
|
|
|
/**
|
|
|
|
|
* Return elements filtered by unique span.
|
|
|
|
|
* @param elements
|
|
|
|
|
*/
|
|
|
|
|
export function uniqueBySpan<T extends{span: ng.Span}>(elements: T[]): T[] {
|
2019-08-12 16:54:36 -07:00
|
|
|
const result: T[] = [];
|
|
|
|
|
const map = new Map<number, Set<number>>();
|
|
|
|
|
for (const element of elements) {
|
|
|
|
|
const {span} = element;
|
|
|
|
|
let set = map.get(span.start);
|
|
|
|
|
if (!set) {
|
|
|
|
|
set = new Set();
|
|
|
|
|
map.set(span.start, set);
|
|
|
|
|
}
|
|
|
|
|
if (!set.has(span.end)) {
|
|
|
|
|
set.add(span.end);
|
|
|
|
|
result.push(element);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|