Skip to content

Commit

Permalink
Support TypeScript typecasts
Browse files Browse the repository at this point in the history
Fixes #603
  • Loading branch information
joshkel committed Jun 14, 2022
1 parent 67cf845 commit dbe5156
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/lexers/jsx-lexer.js
Expand Up @@ -203,7 +203,14 @@ export default class JsxLexer extends JavascriptLexer {
type: 'text',
content: '',
}
} else if (child.expression.kind === ts.SyntaxKind.StringLiteral) {
}

// simplify trivial expressions, like TypeScript typecasts
if (child.expression.kind === ts.SyntaxKind.AsExpression) {
child = child.expression
}

if (child.expression.kind === ts.SyntaxKind.StringLiteral) {
return {
type: 'text',
content: child.expression.text,
Expand Down
9 changes: 9 additions & 0 deletions test/lexers/jsx-lexer.test.js
Expand Up @@ -341,6 +341,15 @@ describe('JsxLexer', () => {
done()
})

it('erases typecasts', (done) => {
const Lexer = new JsxLexer()
const content = '<Trans>{{ key: property } as any}</Trans>'
assert.deepEqual(Lexer.extract(content), [
{ key: '{{key}}', defaultValue: '{{key}}' },
])
done()
})

it('keeps self-closing tags untouched when transSupportBasicHtmlNodes is true', (done) => {
const Lexer = new JsxLexer({ transSupportBasicHtmlNodes: true })
const content = '<Trans>a<br />b</Trans>'
Expand Down

0 comments on commit dbe5156

Please sign in to comment.