From 709a6dea060b388efde641a0fab597f91410b2ea Mon Sep 17 00:00:00 2001 From: Chuck Jazdzewski Date: Fri, 7 Oct 2016 13:53:29 -0700 Subject: [PATCH] refactor(compiler): attribute ast records span of the value (#12132) --- modules/@angular/compiler/src/ml_parser/ast.ts | 4 +++- modules/@angular/compiler/src/ml_parser/parser.ts | 5 ++++- .../compiler/test/ml_parser/html_parser_spec.ts | 12 ++++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/modules/@angular/compiler/src/ml_parser/ast.ts b/modules/@angular/compiler/src/ml_parser/ast.ts index e99274e988..ddd342f5ef 100644 --- a/modules/@angular/compiler/src/ml_parser/ast.ts +++ b/modules/@angular/compiler/src/ml_parser/ast.ts @@ -34,7 +34,9 @@ export class ExpansionCase implements Node { } export class Attribute implements Node { - constructor(public name: string, public value: string, public sourceSpan: ParseSourceSpan) {} + constructor( + public name: string, public value: string, public sourceSpan: ParseSourceSpan, + public valueSpan?: ParseSourceSpan) {} visit(visitor: Visitor, context: any): any { return visitor.visitAttribute(this, context); } } diff --git a/modules/@angular/compiler/src/ml_parser/parser.ts b/modules/@angular/compiler/src/ml_parser/parser.ts index 47e7c6272e..d736014619 100644 --- a/modules/@angular/compiler/src/ml_parser/parser.ts +++ b/modules/@angular/compiler/src/ml_parser/parser.ts @@ -331,12 +331,15 @@ class _TreeBuilder { const fullName = mergeNsAndName(attrName.parts[0], attrName.parts[1]); let end = attrName.sourceSpan.end; let value = ''; + let valueSpan: ParseSourceSpan; if (this._peek.type === lex.TokenType.ATTR_VALUE) { const valueToken = this._advance(); value = valueToken.parts[0]; end = valueToken.sourceSpan.end; + valueSpan = valueToken.sourceSpan; } - return new html.Attribute(fullName, value, new ParseSourceSpan(attrName.sourceSpan.start, end)); + return new html.Attribute( + fullName, value, new ParseSourceSpan(attrName.sourceSpan.start, end), valueSpan); } private _getParentElement(): html.Element { diff --git a/modules/@angular/compiler/test/ml_parser/html_parser_spec.ts b/modules/@angular/compiler/test/ml_parser/html_parser_spec.ts index f185c179df..d510a892bf 100644 --- a/modules/@angular/compiler/test/ml_parser/html_parser_spec.ts +++ b/modules/@angular/compiler/test/ml_parser/html_parser_spec.ts @@ -376,6 +376,18 @@ export function main() { [html.ExpansionCase, '=0', 2, '=0 {msg}'], ]); }); + + it('should not report a value span for an attribute without a value', () => { + const ast = parser.parse('
', 'TestComp'); + expect((ast.rootNodes[0] as html.Element).attrs[0].valueSpan).toBeUndefined(); + }); + + it('should report a value span for an attibute with a value', () => { + const ast = parser.parse('
', 'TestComp'); + const attr = (ast.rootNodes[0] as html.Element).attrs[0]; + expect(attr.valueSpan.start.offset).toEqual(9); + expect(attr.valueSpan.end.offset).toEqual(13); + }); }); describe('errors', () => {