refactor(compiler): attribute ast records span of the value (#12132)
This commit is contained in:
parent
16cfb88c00
commit
709a6dea06
|
@ -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); }
|
||||
}
|
||||
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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('<div bar></div>', '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('<div bar="12"></div>', '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', () => {
|
||||
|
|
Loading…
Reference in New Issue