Skip to content

Commit

Permalink
Merge pull request #900 from glsignal/patch-non-literal-child-warning
Browse files Browse the repository at this point in the history
fix: suppress warning about non-literal child when key/defaults are specified
  • Loading branch information
karellm authored Oct 1, 2023
2 parents e7977ef + ff3b778 commit 4a372d8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/lexers/jsx-lexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export default class JsxLexer extends JavascriptLexer {
'warning',
`"${attributeName}" prop is not a string literal: ${attribute.initializer.expression.text}`
)

return undefined
}

Expand Down Expand Up @@ -177,7 +178,12 @@ export default class JsxLexer extends JavascriptLexer {
}

nodeToString(node, sourceText) {
const children = this.parseChildren.call(this, node.children, sourceText)
const children = this.parseChildren.call(
this,
node,
node.children,
sourceText
)

const elemsToString = (children) =>
children
Expand Down Expand Up @@ -211,7 +217,7 @@ export default class JsxLexer extends JavascriptLexer {
.replace(/(\n|\r)\s*/g, ' ')
}

parseChildren(children = [], sourceText) {
parseChildren(node, children = [], sourceText) {
return children
.map((child) => {
if (child.kind === ts.SyntaxKind.JsxText) {
Expand All @@ -235,7 +241,7 @@ export default class JsxLexer extends JavascriptLexer {
type: 'tag',
children: hasDynamicChildren
? []
: this.parseChildren(child.children, sourceText),
: this.parseChildren(child, child.children, sourceText),
name,
isBasic,
selfClosing: child.kind === ts.SyntaxKind.JsxSelfClosingElement,
Expand Down Expand Up @@ -331,7 +337,17 @@ export default class JsxLexer extends JavascriptLexer {
child.expression.end
)

this.emit('warning', `Child is not literal: ${slicedExpression}`)
const tagNode = node.openingElement || node
const attrValues = tagNode.attributes.properties
.filter((attr) => [this.attr, 'defaults'].includes(attr.name?.text))
.map(
(attr) =>
attr.initializer.expression?.text ?? attr.initializer.text
)

if (attrValues.some((attr) => !attr)) {
this.emit('warning', `Child is not literal: ${slicedExpression}`)
}

return {
type: 'js',
Expand Down
14 changes: 14 additions & 0 deletions test/lexers/jsx-lexer.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { assert } from 'chai'
import sinon from 'sinon'
import JsxLexer from '../../src/lexers/jsx-lexer.js'

describe('JsxLexer', () => {
Expand Down Expand Up @@ -556,6 +557,19 @@ describe('JsxLexer', () => {
)
done()
})

it('does not emit a warning about non-literal child when defaults and i18nKey are specified', (done) => {
const Lexer = new JsxLexer({
transIdentityFunctionsToIgnore: ['funcCall'],
})
const content =
'<Trans i18nKey="testkey" defaults="test">{anotherFuncCall({ name: "John" })}</Trans>'
const spy = sinon.spy()
Lexer.on('warning', spy)
assert.equal(Lexer.extract(content)[0].defaultValue, 'test')
assert.isFalse(spy.called)
done()
})
})
})
})

0 comments on commit 4a372d8

Please sign in to comment.