Skip to content

Commit

Permalink
feat: Support finding translations pipe in KeyedRead nodes (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
sod committed May 14, 2024
1 parent 51d2a35 commit 75b697b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/parsers/pipe.parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import {
TmplAstSwitchBlock,
TmplAstDeferredBlock,
TmplAstForLoopBlock,
TmplAstElement
TmplAstElement,
KeyedRead
} from '@angular/compiler';

import { ParserInterface } from './parser.interface.js';
Expand Down Expand Up @@ -195,6 +196,19 @@ export class PipeParser implements ParserInterface {
return this.getTranslatablesFromAsts(ast.args);
}

// immediately accessed static object or array - the angular parser bundles this as "KeyedRead", where:
// { 'a': 1, 'b': 2 }[ 'a' ];
// ^^^ <- keyedRead.key
// ^^^^^^^^^^^^^^^^^^ <- keyedRead.receiver
//
// html examples:
// - { key1: 'value1' | translate, key2: 'value2' | translate }[key]
// - [ 'value1' | translate, 'value2' | translate ][key]
// - [ 'foo', 'bar' ][ 'key' | translate ]
if (ast instanceof KeyedRead) {
return this.getTranslatablesFromAsts([ast.receiver, ast.key]);
}

return [];
}

Expand Down
28 changes: 28 additions & 0 deletions tests/parsers/pipe.parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,34 @@ describe('PipeParser', () => {
expect(keys).to.deep.equal(['Hello', 'World']);
});

it('should extract strings from object map', () => {
const contents = `{{ {
choice1: 'Hello' | ${translatePipeName},
choice2: 'World' | ${translatePipeName},
}[choice] }}`;
const keys = parser.extract(contents, templateFilename).keys();
expect(keys).to.deep.equal(['Hello', 'World']);
});

it('should extract strings from object map inside attribute', () => {
const contents = `
<span [attr]="{
choice1: 'Hello' | ${translatePipeName},
choice2: 'World' | ${translatePipeName}
}[choice]"></span>`;
const keys = parser.extract(contents, templateFilename).keys();
expect(keys).to.deep.equal(['Hello', 'World']);
});

it('should extract strings from KeyedRead.key', () => {
const contents = `{{ {
choice1: 'Foo',
choice2: 'Bar',
}[ 'choice' | ${translatePipeName} ] }}`;
const keys = parser.extract(contents, templateFilename).keys();
expect(keys).to.deep.equal(['choice']);
});

it('should extract strings from object', () => {
const contents = `{{ { foo: 'Hello' | ${translatePipeName}, bar: ['World' | ${translatePipeName}], deep: { nested: { baz: 'Yes' | ${translatePipeName} } } } | json }}`;
const keys = parser.extract(contents, templateFilename).keys();
Expand Down

0 comments on commit 75b697b

Please sign in to comment.