Add support for extracting array of strings in ServiceParser + tests

This commit is contained in:
Kim Biesbjerg 2016-12-08 15:24:23 +01:00
parent 3e84dd2f72
commit fdf26d6af5
2 changed files with 55 additions and 7 deletions

View File

@ -10,19 +10,41 @@ export class ServiceParser implements ParserInterface {
return results;
}
const methodPattern: string = '(?:get|instant)\\s*\\\(\\s*([\'"`])([^\\1\\r\\n]+)\\1';
const regExp: RegExp = new RegExp(`${translateServiceVar}\.${methodPattern}`, 'g');
const methodRegExp: RegExp = new RegExp(/(?:get|instant)\s*\(\s*(\[?([\'"`])([^\1\r\n]+)\2\]?)/);
const regExp: RegExp = new RegExp(`${translateServiceVar}\.${methodRegExp.source}`, 'g');
let matches;
while (matches = regExp.exec(contents)) {
results.push(matches[2]);
if (this._stringContainsArray(matches[1])) {
results.push(...this._stringToArray(matches[1]));
} else {
results.push(matches[3]);
}
}
return results;
}
/**
* Extract name of TranslateService variable for use in patterns
* Checks if string contains an array
*/
protected _stringContainsArray(input: string): boolean {
return input.startsWith('[') && input.endsWith(']');
}
/**
* Converts string to array
*/
protected _stringToArray(input: string): string[] {
if (this._stringContainsArray(input)) {
return eval(input);
}
return [];
}
/**
* Extracts name of TranslateService variable for use in patterns
*/
protected _extractTranslateServiceVar(contents: string): string {
const matches = contents.match(/([a-z0-9_]+)\s*:\s*TranslateService/i);

View File

@ -34,7 +34,7 @@ describe('ServiceParser', () => {
expect(messages).to.equal('_translateService');
});
it('should extract string passed to translateService.get()', () => {
it('should extract strings in TranslateService\'s get() method', () => {
const contents = `
@Component({ })
export class AppComponent {
@ -47,7 +47,7 @@ describe('ServiceParser', () => {
expect(messages).to.deep.equal(['Hello World']);
});
it('should extract string passed to translateService.instant()', () => {
it('should extract strings in TranslateService\'s instant() method', () => {
const contents = `
@Component({ })
export class AppComponent {
@ -60,7 +60,33 @@ describe('ServiceParser', () => {
expect(messages).to.deep.equal(['Hello World']);
});
it('should not extract string passed to get() or instant() methods of other services', () => {
it('should extract array of strings in TranslateService\'s get() method', () => {
const contents = `
@Component({ })
export class AppComponent {
public constructor(protected _translateService: TranslateService) { }
public test() {
this._translateService.get(['Hello', 'World']);
}
`;
const messages = parser.process(componentFilename, contents);
expect(messages).to.deep.equal(['Hello', 'World']);
});
it('should extract array of strings in TranslateService\'s instant() method', () => {
const contents = `
@Component({ })
export class AppComponent {
public constructor(protected _translateService: TranslateService) { }
public test() {
this._translateService.instant(['Hello', 'World']);
}
`;
const messages = parser.process(componentFilename, contents);
expect(messages).to.deep.equal(['Hello', 'World']);
});
it('should not extract strings in get()/instant() methods of other services', () => {
const contents = `
@Component({ })
export class AppComponent {