Skip to content

Commit

Permalink
Merge pull request #604 from joshkel/jsx-as
Browse files Browse the repository at this point in the history
Support TypeScript typecasts
  • Loading branch information
karellm committed Oct 5, 2022
2 parents 2f68c19 + dbe5156 commit dfaf626
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 @@ -211,7 +211,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 @@ -354,6 +354,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 dfaf626

Please sign in to comment.