refactor(MessageExtractor): pass the interpolationConfig around

This commit is contained in:
Victor Berchet 2016-06-20 11:27:57 -07:00
parent 99587ea4ed
commit e38e04c1c2
1 changed files with 17 additions and 18 deletions

View File

@ -91,7 +91,6 @@ export function removeDuplicates(messages: Message[]): Message[] {
export class MessageExtractor { export class MessageExtractor {
private _messages: Message[]; private _messages: Message[];
private _errors: ParseError[]; private _errors: ParseError[];
private _interpolationConfig: InterpolationConfig;
constructor( constructor(
private _htmlParser: HtmlParser, private _parser: Parser, private _implicitTags: string[], private _htmlParser: HtmlParser, private _parser: Parser, private _implicitTags: string[],
@ -102,47 +101,48 @@ export class MessageExtractor {
interpolationConfig: InterpolationConfig = DEFAULT_INTERPOLATION_CONFIG): ExtractionResult { interpolationConfig: InterpolationConfig = DEFAULT_INTERPOLATION_CONFIG): ExtractionResult {
this._messages = []; this._messages = [];
this._errors = []; this._errors = [];
this._interpolationConfig = interpolationConfig;
const res = this._htmlParser.parse(template, sourceUrl, true); const res = this._htmlParser.parse(template, sourceUrl, true);
if (res.errors.length == 0) { if (res.errors.length == 0) {
this._recurse(res.rootNodes); this._recurse(res.rootNodes, interpolationConfig);
} }
return new ExtractionResult(this._messages, this._errors.concat(res.errors)); return new ExtractionResult(this._messages, this._errors.concat(res.errors));
} }
private _extractMessagesFromPart(part: Part): void { private _extractMessagesFromPart(part: Part, interpolationConfig: InterpolationConfig): void {
if (part.hasI18n) { if (part.hasI18n) {
this._messages.push(part.createMessage(this._parser, this._interpolationConfig)); this._messages.push(part.createMessage(this._parser, interpolationConfig));
this._recurseToExtractMessagesFromAttributes(part.children); this._recurseToExtractMessagesFromAttributes(part.children, interpolationConfig);
} else { } else {
this._recurse(part.children); this._recurse(part.children, interpolationConfig);
} }
if (isPresent(part.rootElement)) { if (isPresent(part.rootElement)) {
this._extractMessagesFromAttributes(part.rootElement); this._extractMessagesFromAttributes(part.rootElement, interpolationConfig);
} }
} }
private _recurse(nodes: HtmlAst[]): void { private _recurse(nodes: HtmlAst[], interpolationConfig: InterpolationConfig): void {
if (isPresent(nodes)) { if (isPresent(nodes)) {
let parts = partition(nodes, this._errors, this._implicitTags); let parts = partition(nodes, this._errors, this._implicitTags);
parts.forEach(part => this._extractMessagesFromPart(part)); parts.forEach(part => this._extractMessagesFromPart(part, interpolationConfig));
} }
} }
private _recurseToExtractMessagesFromAttributes(nodes: HtmlAst[]): void { private _recurseToExtractMessagesFromAttributes(
nodes: HtmlAst[], interpolationConfig: InterpolationConfig): void {
nodes.forEach(n => { nodes.forEach(n => {
if (n instanceof HtmlElementAst) { if (n instanceof HtmlElementAst) {
this._extractMessagesFromAttributes(n); this._extractMessagesFromAttributes(n, interpolationConfig);
this._recurseToExtractMessagesFromAttributes(n.children); this._recurseToExtractMessagesFromAttributes(n.children, interpolationConfig);
} }
}); });
} }
private _extractMessagesFromAttributes(p: HtmlElementAst): void { private _extractMessagesFromAttributes(
p: HtmlElementAst, interpolationConfig: InterpolationConfig): void {
let transAttrs: string[] = let transAttrs: string[] =
isPresent(this._implicitAttrs[p.name]) ? this._implicitAttrs[p.name] : []; isPresent(this._implicitAttrs[p.name]) ? this._implicitAttrs[p.name] : [];
let explicitAttrs: string[] = []; let explicitAttrs: string[] = [];
@ -151,8 +151,7 @@ export class MessageExtractor {
p.attrs.filter(attr => attr.name.startsWith(I18N_ATTR_PREFIX)).forEach(attr => { p.attrs.filter(attr => attr.name.startsWith(I18N_ATTR_PREFIX)).forEach(attr => {
try { try {
explicitAttrs.push(attr.name.substring(I18N_ATTR_PREFIX.length)); explicitAttrs.push(attr.name.substring(I18N_ATTR_PREFIX.length));
this._messages.push( this._messages.push(messageFromI18nAttribute(this._parser, interpolationConfig, p, attr));
messageFromI18nAttribute(this._parser, this._interpolationConfig, p, attr));
} catch (e) { } catch (e) {
if (e instanceof I18nError) { if (e instanceof I18nError) {
this._errors.push(e); this._errors.push(e);
@ -167,7 +166,7 @@ export class MessageExtractor {
.filter(attr => explicitAttrs.indexOf(attr.name) == -1) .filter(attr => explicitAttrs.indexOf(attr.name) == -1)
.filter(attr => transAttrs.indexOf(attr.name) > -1) .filter(attr => transAttrs.indexOf(attr.name) > -1)
.forEach( .forEach(
attr => this._messages.push( attr =>
messageFromAttribute(this._parser, this._interpolationConfig, attr))); this._messages.push(messageFromAttribute(this._parser, interpolationConfig, attr)));
} }
} }