Skip to content

Commit d0874ef

Browse files
authored
Merge pull request #817 from 2Pacalypse-/master
Add support for string concatenation in default values
2 parents 8936ab2 + 8ddc0a0 commit d0874ef

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/lexers/javascript-lexer.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,14 +256,28 @@ export default class JavascriptLexer extends BaseLexer {
256256

257257
let optionsArgument = node.arguments.shift()
258258

259-
// Second argument could be a string default value
259+
// Second argument could be a (concatenated) string default value
260260
if (
261261
optionsArgument &&
262262
(optionsArgument.kind === ts.SyntaxKind.StringLiteral ||
263263
optionsArgument.kind === ts.SyntaxKind.NoSubstitutionTemplateLiteral)
264264
) {
265265
entry.defaultValue = optionsArgument.text
266266
optionsArgument = node.arguments.shift()
267+
} else if (
268+
optionsArgument &&
269+
optionsArgument.kind === ts.SyntaxKind.BinaryExpression
270+
) {
271+
const concatenatedString = this.concatenateString(optionsArgument)
272+
if (!concatenatedString) {
273+
this.emit(
274+
'warning',
275+
`Default value is not a string literal: ${optionsArgument.text}`
276+
)
277+
return null
278+
}
279+
entry.defaultValue = concatenatedString
280+
optionsArgument = node.arguments.shift()
267281
}
268282

269283
if (

test/lexers/javascript-lexer.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ describe('JavascriptLexer', () => {
2727
done()
2828
})
2929

30+
it('extracts the second argument string concatenation as defaultValue', (done) => {
31+
const Lexer = new JavascriptLexer()
32+
const content = 'i18n.t("first", "bla" + "bla" + "bla")'
33+
assert.deepEqual(Lexer.extract(content), [
34+
{ key: 'first', defaultValue: 'blablabla' },
35+
])
36+
done()
37+
})
38+
3039
it('extracts the defaultValue/context options', (done) => {
3140
const Lexer = new JavascriptLexer()
3241
const content = 'i18n.t("first", {defaultValue: "foo", context: \'bar\'})'

0 commit comments

Comments
 (0)