Skip to content

Commit

Permalink
Merge pull request #637 from dimazollo/hotfix/jsx-spread-operator
Browse files Browse the repository at this point in the history
Fix: do not fail on JSX spread attribute in Trans component
  • Loading branch information
karellm committed Oct 5, 2022
2 parents 67cf845 + 63fcf74 commit b222944
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/lexers/jsx-lexer.js
Expand Up @@ -64,9 +64,9 @@ export default class JsxLexer extends JavascriptLexer {
jsxExtractor(node, sourceText) {
const tagNode = node.openingElement || node

const getPropValue = (node, tagName) => {
const getPropValue = (node, attributeName) => {
const attribute = node.attributes.properties.find(
(attr) => attr.name.text === tagName
(attr) => attr.name !== undefined && attr.name.text === attributeName
)
if (!attribute) {
return undefined
Expand Down Expand Up @@ -109,6 +109,14 @@ export default class JsxLexer extends JavascriptLexer {
}

tagNode.attributes.properties.forEach((property) => {
if (property.kind === ts.SyntaxKind.JsxSpreadAttribute) {
this.emit(
'warning',
`Component attribute is a JSX spread attribute : ${property.expression.text}`
)
return
}

if (this.omitAttributes.includes(property.name.text)) {
return
}
Expand Down
13 changes: 13 additions & 0 deletions test/lexers/jsx-lexer.test.js
Expand Up @@ -221,6 +221,19 @@ describe('JsxLexer', () => {
])
done()
})

it('emits a `warning` event if the component attribute is a JSX spread attribute', (done) => {
const Lexer = new JsxLexer()
const content = '<Trans defaults="bar" {...spread} />'
Lexer.on('warning', (message) => {
assert.equal(
message,
'Component attribute is a JSX spread attribute : spread'
)
done()
})
assert.deepEqual(Lexer.extract(content), [{ defaultValue: 'bar' }])
})
})

describe('supports TypeScript', () => {
Expand Down

0 comments on commit b222944

Please sign in to comment.