fix(compiler): report errors for missing binding names (#34595)
Currently, would-be binding attributes that are missing binding names are not parsed as bindings, and fall through as regular attributes. In some cases, this can lead to a runtime error; trying to assign `#` as a DOM attribute in an element like in `<div #></div>` fails because `#` is not a valid attribute name. Attributes composed of binding prefixes but not defining a binding should be considered invalid, as this almost certainly indicates an unintentional elision of a binding by the developer. This commit introduces error reporting for attributes with a binding name prefix but no actual binding name. Closes https://github.com/angular/vscode-ng-language-service/issues/293. PR Close #34595
This commit is contained in:
@ -21,7 +21,7 @@ import * as t from './r3_ast';
|
||||
import {I18N_ICU_VAR_PREFIX, isI18nRootNode} from './view/i18n/util';
|
||||
|
||||
const BIND_NAME_REGEXP =
|
||||
/^(?:(?:(?:(bind-)|(let-)|(ref-|#)|(on-)|(bindon-)|(@))(.+))|\[\(([^\)]+)\)\]|\[([^\]]+)\]|\(([^\)]+)\))$/;
|
||||
/^(?:(?:(?:(bind-)|(let-)|(ref-|#)|(on-)|(bindon-)|(@))(.*))|\[\(([^\)]+)\)\]|\[([^\]]+)\]|\(([^\)]+)\))$/;
|
||||
|
||||
// Group 1 = "bind-"
|
||||
const KW_BIND_IDX = 1;
|
||||
@ -399,7 +399,10 @@ class HtmlAstToIvyAst implements html.Visitor {
|
||||
valueSpan: ParseSourceSpan|undefined, variables: t.Variable[]) {
|
||||
if (identifier.indexOf('-') > -1) {
|
||||
this.reportError(`"-" is not allowed in variable names`, sourceSpan);
|
||||
} else if (identifier.length === 0) {
|
||||
this.reportError(`Variable does not have a name`, sourceSpan);
|
||||
}
|
||||
|
||||
variables.push(new t.Variable(identifier, value, sourceSpan, valueSpan));
|
||||
}
|
||||
|
||||
@ -408,6 +411,8 @@ class HtmlAstToIvyAst implements html.Visitor {
|
||||
valueSpan: ParseSourceSpan|undefined, references: t.Reference[]) {
|
||||
if (identifier.indexOf('-') > -1) {
|
||||
this.reportError(`"-" is not allowed in reference names`, sourceSpan);
|
||||
} else if (identifier.length === 0) {
|
||||
this.reportError(`Reference does not have a name`, sourceSpan);
|
||||
}
|
||||
|
||||
references.push(new t.Reference(identifier, value, sourceSpan, valueSpan));
|
||||
|
Reference in New Issue
Block a user