Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: do not fail on JSX spread attribute in Trans component #637

Merged
merged 1 commit into from Oct 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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