Skip to content

Commit

Permalink
Suppress warning when node specifies key & defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
glsignal committed Aug 31, 2023
1 parent dde6686 commit c97b2c4
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 48 deletions.
60 changes: 37 additions & 23 deletions src/lexers/jsx-lexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,37 +65,40 @@ export default class JsxLexer extends JavascriptLexer {
return keysWithPrefixes
}

jsxExtractor(node, sourceText) {
const tagNode = node.openingElement || node

const getPropValue = (node, attributeName) => {
const attribute = node.attributes.properties.find(
(attr) => attr.name !== undefined && attr.name.text === attributeName
)
if (!attribute) {
return undefined
}
getPropValue(node, attributeName, warn = true) {
const attribute = node.attributes.properties.find(
(attr) => attr.name !== undefined && attr.name.text === attributeName
)
if (!attribute) {
return undefined
}

if (attribute.initializer.expression?.kind === ts.SyntaxKind.Identifier) {
if (attribute.initializer.expression?.kind === ts.SyntaxKind.Identifier) {
if (warn) {
this.emit(
'warning',
`"${attributeName}" prop is not a string literal: ${attribute.initializer.expression.text}`
)
return undefined
}

return attribute.initializer.expression
? attribute.initializer.expression.text
: attribute.initializer.text
return undefined
}

const getKey = (node) => getPropValue(node, this.attr)
return attribute.initializer.expression
? attribute.initializer.expression.text
: attribute.initializer.text
}

jsxExtractor(node, sourceText) {
const tagNode = node.openingElement || node

const getKey = (node) => this.getPropValue(node, this.attr)

if (this.componentFunctions.includes(tagNode.tagName.text)) {
const entry = {}
entry.key = getKey(tagNode)

const namespace = getPropValue(tagNode, 'ns')
const namespace = this.getPropValue(tagNode, 'ns')
if (namespace) {
entry.namespace = namespace
}
Expand Down Expand Up @@ -142,7 +145,7 @@ export default class JsxLexer extends JavascriptLexer {
})

const nodeAsString = this.nodeToString.call(this, node, sourceText)
const defaultsProp = getPropValue(tagNode, 'defaults')
const defaultsProp = this.getPropValue(tagNode, 'defaults')
let defaultValue = defaultsProp || nodeAsString

// If `shouldUnescape` is not true, it means the value cannot contain HTML entities,
Expand All @@ -167,15 +170,20 @@ export default class JsxLexer extends JavascriptLexer {
entry.key = getKey(tagNode)
return entry.key ? entry : null
} else if (tagNode.tagName.text === 'Translation') {
const namespace = getPropValue(tagNode, 'ns')
const namespace = this.getPropValue(tagNode, 'ns')
if (namespace) {
this.defaultNamespace = namespace
}
}
}

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 @@ -209,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 @@ -231,7 +239,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 @@ -327,7 +335,13 @@ export default class JsxLexer extends JavascriptLexer {
child.expression.end
)

this.emit('warning', `Child is not literal: ${slicedExpression}`)
const tagNode = node.openingElement || node
if (
!this.getPropValue(tagNode, this.attr, false) ||
!this.getPropValue(tagNode, 'defaults', false)
) {
this.emit('warning', `Child is not literal: ${slicedExpression}`)
}

return {
type: 'js',
Expand Down
35 changes: 10 additions & 25 deletions test/lexers/jsx-lexer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -537,32 +537,17 @@ describe('JsxLexer', () => {
done()
})

describe('when i18nKey is given', () => {
it('does not emit a warning when given a non-literal child', (done) => {
const Lexer = new JsxLexer({
transIdentityFunctionsToIgnore: ['funcCall'],
})
const content =
'<Trans i18nKey="testkey">Hi, {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()
})

it('does not emit a warning when defaults are specified and given a non-literal child', (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()
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()
})
})
})
Expand Down

0 comments on commit c97b2c4

Please sign in to comment.