Skip to content

Commit f233f74

Browse files
crisbetoatscott
authored andcommitted
fix(compiler-cli): make required inputs diagnostic less noisy
Currently when a required input is missing, we produce a diagnostic on the entire start tag. This can be really noisy if there are already some attributes on the element. This change switch to only highlighting the tag name instead.
1 parent 615518d commit f233f74

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

packages/compiler-cli/src/ngtsc/typecheck/src/oob.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
TmplAstTextAttribute,
2929
TmplAstVariable,
3030
TmplAstViewportDeferredTrigger,
31+
ParseSourceSpan,
3132
} from '@angular/compiler';
3233
import ts from 'typescript';
3334

@@ -562,11 +563,33 @@ export class OutOfBandDiagnosticRecorderImpl implements OutOfBandDiagnosticRecor
562563
isComponent ? 'component' : 'directive'
563564
} ${directiveName} must be specified.`;
564565

566+
let span: ParseSourceSpan;
567+
let name: string | null;
568+
569+
if (element instanceof TmplAstElement || element instanceof TmplAstDirective) {
570+
name = element.name;
571+
} else if (element instanceof TmplAstComponent) {
572+
name = element.componentName;
573+
} else {
574+
name = null;
575+
}
576+
577+
if (name === null) {
578+
span = element.startSourceSpan;
579+
} else {
580+
// Only highlight the tag name since highlighting the entire start tag can be noisy.
581+
const start = element.startSourceSpan.start.moveBy(1);
582+
const end = element.startSourceSpan.end.moveBy(
583+
start.offset + name.length - element.startSourceSpan.end.offset,
584+
);
585+
span = new ParseSourceSpan(start, end);
586+
}
587+
565588
this._diagnostics.push(
566589
makeTemplateDiagnostic(
567590
id,
568591
this.resolver.getTemplateSourceMapping(id),
569-
element.startSourceSpan,
592+
span,
570593
ts.DiagnosticCategory.Error,
571594
ngErrorCode(ErrorCode.MISSING_REQUIRED_INPUTS),
572595
message,

packages/compiler-cli/src/ngtsc/typecheck/test/diagnostics_spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,7 +1046,7 @@ class TestComponent {
10461046
);
10471047

10481048
expect(messages).toEqual([
1049-
`TestComponent.html(1, 1): Required input 'input' from directive Dir must be specified.`,
1049+
`TestComponent.html(1, 2): Required input 'input' from directive Dir must be specified.`,
10501050
]);
10511051
});
10521052

@@ -1103,8 +1103,8 @@ class TestComponent {
11031103
);
11041104

11051105
expect(messages).toEqual([
1106-
`TestComponent.html(1, 1): Required inputs 'input', 'otherInput' from directive Dir must be specified.`,
1107-
`TestComponent.html(1, 1): Required input 'otherDirInput' from directive OtherDir must be specified.`,
1106+
`TestComponent.html(1, 2): Required inputs 'input', 'otherInput' from directive Dir must be specified.`,
1107+
`TestComponent.html(1, 2): Required input 'otherDirInput' from directive OtherDir must be specified.`,
11081108
]);
11091109
});
11101110

@@ -1136,7 +1136,7 @@ class TestComponent {
11361136
);
11371137

11381138
expect(messages).toEqual([
1139-
`TestComponent.html(1, 1): Required input 'inputAlias' from directive Dir must be specified.`,
1139+
`TestComponent.html(1, 2): Required input 'inputAlias' from directive Dir must be specified.`,
11401140
]);
11411141
});
11421142

@@ -1337,7 +1337,7 @@ class TestComponent {
13371337
);
13381338

13391339
expect(messages).toEqual([
1340-
`TestComponent.html(1, 1): Required input 'customAlias' from directive HostDir must be specified.`,
1340+
`TestComponent.html(1, 2): Required input 'customAlias' from directive HostDir must be specified.`,
13411341
]);
13421342
});
13431343

0 commit comments

Comments
 (0)