refactor(compiler): associate accurate source spans with ICU expressions (#39072)

Prior to this change, expressions within ICUs would have a source span
corresponding with the whole ICU. This commit narrows down the source
spans of these expressions to the exact location in the source file, as
a prerequisite for reporting type check errors within these expressions.

PR Close #39072
This commit is contained in:
JoostK
2020-10-01 00:17:21 +02:00
committed by atscott
parent 42a164f522
commit 4b06ef75aa
11 changed files with 169 additions and 27 deletions

View File

@ -89,7 +89,13 @@ class R3AstSourceSpans implements t.Visitor<void> {
}
visitIcu(icu: t.Icu) {
return null;
this.result.push(['Icu', humanizeSpan(icu.sourceSpan)]);
for (const key of Object.keys(icu.vars)) {
this.result.push(['Icu:Var', humanizeSpan(icu.vars[key].sourceSpan)]);
}
for (const key of Object.keys(icu.placeholders)) {
this.result.push(['Icu:Placeholder', humanizeSpan(icu.placeholders[key].sourceSpan)]);
}
}
private visitAll(nodes: t.Node[][]) {
@ -420,4 +426,40 @@ describe('R3 AST source spans', () => {
]);
});
});
describe('ICU expressions', () => {
it('is correct for variables and placeholders', () => {
expectFromHtml('<span i18n>{item.var, plural, other { {{item.placeholder}} items } }</span>')
.toEqual([
[
'Element',
'<span i18n>{item.var, plural, other { {{item.placeholder}} items } }</span>',
'<span i18n>', '</span>'
],
['Icu', '{item.var, plural, other { {{item.placeholder}} items } }'],
['Icu:Var', 'item.var'],
['Icu:Placeholder', '{{item.placeholder}}'],
]);
});
it('is correct for nested ICUs', () => {
expectFromHtml(
'<span i18n>{item.var, plural, other { {{item.placeholder}} {nestedVar, plural, other { {{nestedPlaceholder}} }}} }</span>')
.toEqual([
[
'Element',
'<span i18n>{item.var, plural, other { {{item.placeholder}} {nestedVar, plural, other { {{nestedPlaceholder}} }}} }</span>',
'<span i18n>', '</span>'
],
[
'Icu',
'{item.var, plural, other { {{item.placeholder}} {nestedVar, plural, other { {{nestedPlaceholder}} }}} }'
],
['Icu:Var', 'nestedVar'],
['Icu:Var', 'item.var'],
['Icu:Placeholder', '{{item.placeholder}}'],
['Icu:Placeholder', '{{nestedPlaceholder}}'],
]);
});
});
});