Skip to content

Commit 9322e6a

Browse files
alxhubzarend
authored andcommitted
fix(language-service): don't show external template diagnostics in ts files (#41070)
The compiler considers template diagnostics to "belong" to the source file of the component using the template. This means that when diagnostics for a source file are reported, it returns diagnostics of TS structures in the actual source file, diagnostics for any inline templates, and diagnostics of any external templates. The Language Service uses a different model, and wants to show template diagnostics in the actual .html file. Thus, it's not necessary (and in fact incorrect) to include such diagnostics for the actual .ts file as well. Doing this currently causes a bug where external diagnostics appear in the TS file with "random" source spans. This commit changes the Language Service to filter the set of diagnostics returned by the compiler and only include those diagnostics with spans actually within the .ts file itself. Fixes #41032 PR Close #41070
1 parent 6dd5497 commit 9322e6a

File tree

3 files changed

+73
-8
lines changed

3 files changed

+73
-8
lines changed

packages/language-service/ivy/language_service.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,28 @@ export class LanguageService {
6868
const program = compiler.getNextProgram();
6969
const sourceFile = program.getSourceFile(fileName);
7070
if (sourceFile) {
71-
diagnostics.push(...compiler.getDiagnosticsForFile(sourceFile, OptimizeFor.SingleFile));
71+
const ngDiagnostics = compiler.getDiagnosticsForFile(sourceFile, OptimizeFor.SingleFile);
72+
// There are several kinds of diagnostics returned by `NgCompiler` for a source file:
73+
//
74+
// 1. Angular-related non-template diagnostics from decorated classes within that file.
75+
// 2. Template diagnostics for components with direct inline templates (a string literal).
76+
// 3. Template diagnostics for components with indirect inline templates (templates computed
77+
// by expression).
78+
// 4. Template diagnostics for components with external templates.
79+
//
80+
// When showing diagnostics for a TS source file, we want to only include kinds 1 and 2 -
81+
// those diagnostics which are reported at a location within the TS file itself. Diagnostics
82+
// for external templates will be shown when editing that template file (the `else` block)
83+
// below.
84+
//
85+
// Currently, indirect inline template diagnostics (kind 3) are not shown at all by the
86+
// Language Service, because there is no sensible location in the user's code for them. Such
87+
// templates are an edge case, though, and should not be common.
88+
//
89+
// TODO(alxhub): figure out a good user experience for indirect template diagnostics and
90+
// show them from within the Language Service.
91+
diagnostics.push(...ngDiagnostics.filter(
92+
diag => diag.file !== undefined && diag.file.fileName === sourceFile.fileName));
7293
}
7394
} else {
7495
const components = compiler.getComponentsWithTemplateFile(fileName);

packages/language-service/ivy/test/compiler_spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ describe('language-service/compiler integration', () => {
3030
'test.html': `<other-cmp>Test</other-cmp>`
3131
});
3232

33-
expect(project.getDiagnosticsForFile('test.ts').length).toBeGreaterThan(0);
33+
expect(project.getDiagnosticsForFile('test.html').length).toBeGreaterThan(0);
3434

3535
const tmplFile = project.openFile('test.html');
3636
tmplFile.contents = '<div>Test</div>';
37-
expect(project.getDiagnosticsForFile('test.ts').length).toEqual(0);
37+
expect(project.getDiagnosticsForFile('test.html').length).toEqual(0);
3838
});
3939

4040
it('should not produce errors from inline test declarations mixing with those of the app', () => {

packages/language-service/ivy/test/diagnostic_spec.ts

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,44 @@ describe('getSemanticDiagnostics', () => {
7575
expect(diags).toEqual([]);
7676
});
7777

78+
it('should not report external template diagnostics on the TS file', () => {
79+
const files = {
80+
'app.ts': `
81+
import {Component, NgModule} from '@angular/core';
82+
83+
@Component({
84+
templateUrl: './app.html'
85+
})
86+
export class AppComponent {}
87+
`,
88+
'app.html': '{{nope}}'
89+
};
90+
91+
const project = createModuleAndProjectWithDeclarations(env, 'test', files);
92+
const diags = project.getDiagnosticsForFile('app.ts');
93+
expect(diags).toEqual([]);
94+
});
95+
96+
it('should report diagnostics in inline templates', () => {
97+
const files = {
98+
'app.ts': `
99+
import {Component, NgModule} from '@angular/core';
100+
101+
@Component({
102+
template: '{{nope}}',
103+
})
104+
export class AppComponent {}
105+
`
106+
};
107+
const project = createModuleAndProjectWithDeclarations(env, 'test', files);
108+
const diags = project.getDiagnosticsForFile('app.ts');
109+
expect(diags.length).toBe(1);
110+
const {category, file, messageText} = diags[0];
111+
expect(category).toBe(ts.DiagnosticCategory.Error);
112+
expect(file?.fileName).toBe('/test/app.ts');
113+
expect(messageText).toBe(`Property 'nope' does not exist on type 'AppComponent'.`);
114+
});
115+
78116
it('should report member does not exist in external template', () => {
79117
const files = {
80118
'app.ts': `
@@ -179,11 +217,17 @@ describe('getSemanticDiagnostics', () => {
179217
};
180218

181219
const project = env.addProject('test', files);
182-
const diags = project.getDiagnosticsForFile('app.ts');
183-
expect(diags.map(x => x.messageText).sort()).toEqual([
184-
'Parser Error: Bindings cannot contain assignments at column 8 in [{{nope = false}}] in /test/app1.html@0:0',
185-
'Parser Error: Bindings cannot contain assignments at column 8 in [{{nope = true}}] in /test/app2.html@0:0'
186-
]);
220+
const diags1 = project.getDiagnosticsForFile('app1.html');
221+
expect(diags1.length).toBe(1);
222+
expect(diags1[0].messageText)
223+
.toBe(
224+
'Parser Error: Bindings cannot contain assignments at column 8 in [{{nope = false}}] in /test/app1.html@0:0');
225+
226+
const diags2 = project.getDiagnosticsForFile('app2.html');
227+
expect(diags2.length).toBe(1);
228+
expect(diags2[0].messageText)
229+
.toBe(
230+
'Parser Error: Bindings cannot contain assignments at column 8 in [{{nope = true}}] in /test/app2.html@0:0');
187231
});
188232

189233
it('reports a diagnostic for a component without a template', () => {

0 commit comments

Comments
 (0)