Skip to content

Commit 2f8da46

Browse files
committed
fixup! feat(compiler): Add extended diagnostic to warn when text attributes are intended to be bindings
1 parent 9bc2bdc commit 2f8da46

2 files changed

Lines changed: 12 additions & 11 deletions

File tree

‎packages/compiler-cli/src/ngtsc/typecheck/extended/checks/text_attribute_not_binding/index.ts‎

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,27 @@ class TextAttributeNotBindingSpec extends
3232
if (!(node instanceof TmplAstTextAttribute)) return [];
3333

3434
const name = node.name;
35-
if ((!name.startsWith('attr.') && !name.startsWith('style.') && !name.startsWith('class.')) ||
36-
!node.value) {
35+
if ((!name.startsWith('attr.') && !name.startsWith('style.') && !name.startsWith('class.'))) {
3736
return [];
3837
}
3938

40-
const boundSyntax = node.sourceSpan.toString();
4139
let errorString: string;
4240
if (name.startsWith('attr.')) {
4341
const staticAttr = name.replace('attr.', '');
44-
errorString = `Static attributes should be written without the 'attr.' prefix: ${
45-
staticAttr}="${node.value}".`;
46-
42+
errorString = `Static attributes should be written without the 'attr.' prefix.`;
43+
if (node.value) {
44+
errorString += ` For example, ${staticAttr}="${node.value}".`;
45+
}
4746
} else {
4847
const expectedKey = `[${name}]`;
4948
const expectedValue =
5049
// true/false are special cases because we don't want to convert them to strings but
5150
// rather maintain the logical true/false when bound.
5251
(node.value === 'true' || node.value === 'false') ? node.value : `'${node.value}'`;
53-
errorString = boundSyntax.replace(
54-
name,
55-
`Attribute, style, and class bindings should be enclosed with square braces: '${
56-
expectedKey}="${expectedValue}"'.`);
52+
errorString = 'Attribute, style, and class bindings should be enclosed with square braces.';
53+
if (node.value) {
54+
errorString += ` For example, '${expectedKey}="${expectedValue}"'.`;
55+
}
5756
}
5857
const diagnostic = ctx.makeTemplateDiagnostic(node.sourceSpan, errorString);
5958
return [diagnostic];

‎packages/compiler-cli/src/ngtsc/typecheck/extended/test/checks/text_attribute_not_binding/text_attribute_not_binding_spec.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@ runInEachFileSystem(() => {
103103
templateTypeChecker, program.getTypeChecker(), [textAttributeNotBindingFactory],
104104
{} /* options */);
105105
const diags = extendedTemplateChecker.getDiagnosticsForComponent(component);
106-
expect(diags.length).toBe(0);
106+
expect(diags.length).toBe(1);
107+
expect(diags[0].code).toBe(ngErrorCode(ErrorCode.TEXT_ATTRIBUTE_NOT_BINDING));
108+
expect(getSourceCodeForDiagnostic(diags[0])).toBe(`attr.readonly`);
107109
});
108110
});
109111
});

0 commit comments

Comments
 (0)