Skip to content

Commit

Permalink
feat: support finding translations pipe in KeyedRead nodes
Browse files Browse the repository at this point in the history
seems like KeyedRead was always there, but not detected.

Finds keys in static object and array access, e.g.:

```
{
  foo: 'a' | translate,
  bar: 'b' | translate,
}[ key ];
```

```
[
  'a' | translate,
  'b' | translate,
}[ index ];
```
  • Loading branch information
sod committed May 6, 2024
1 parent 51d2a35 commit 93a5d2c
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 93a5d2c

Please sign in to comment.