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: cannot resolve empty string on plural #1985

Merged
merged 1 commit into from
Oct 6, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/core-base/src/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function formatMessageParts<Message = string>(
node: MessageNode
): MessageFunctionReturn<Message> {
const _static = node.s || node.static
if (_static) {
if (_static != null) {
return ctx.type === 'text'
? (_static as MessageFunctionReturn<Message>)
: ctx.normalize([_static] as MessageType<Message>[])
Expand Down
13 changes: 13 additions & 0 deletions packages/core-base/test/format.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,16 @@ describe('features', () => {
})
})
})

describe('edge cases', () => {
test('empty string in interpolation', () => {
const { ast } = compile(`{''} | {n} test | {n} tests`, {
jit: true
})
const msg = format(ast)
const ctx = context({
pluralIndex: 0
})
expect(msg(ctx)).toBe('')
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,15 @@ exports[`edge cases > {_field} with the same value already exists. > code 1`] =
}"
`;

exports[`edge cases > empty literal string in interpolation > code 1`] = `
"function __msg__ (ctx) {
const { normalize: _normalize } = ctx
return _normalize([
""
])
}"
`;

exports[`edge cases > hi %s ! > code 1`] = `
"function __msg__ (ctx) {
const { normalize: _normalize } = ctx
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`empty literal string in interpolation 1`] = `
{
"body": {
"items": [
{
"type": 9,
},
],
"static": "",
"type": 2,
},
"type": 0,
}
`;

exports[`full text items: foo{'@'}domain.com 1`] = `
{
"body": {
Expand Down
5 changes: 5 additions & 0 deletions packages/message-compiler/test/compiler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,9 @@ describe('edge cases', () => {
const { code } = compile(`%{nickname} %{action} issue %{code}`)
expect(code).toMatchSnapshot('code')
})

test('empty literal string in interpolation', () => {
const { code } = compile(`{''}`)
expect(code).toMatchSnapshot('code')
})
})
9 changes: 9 additions & 0 deletions packages/message-compiler/test/optimizer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,12 @@ test(`incldue dynamic node in pluarl: no apples | {0} apple | {n} apples`, () =>
.filter(Boolean)
expect(messages).toEqual(['no apples'])
})

test('empty literal string in interpolation', () => {
const parser = createParser({ location: false })
const msg = `{''}`
const ast = optimize(parser.parse(msg))

expect(ast).toMatchSnapshot()
expect((ast.body as MessageNode).static).toBe('')
})
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,64 @@ exports[`emoji > hi, {'😺'} ! 1`] = `
}
`;

exports[`empty string 1`] = `
{
"body": {
"end": 4,
"items": [
{
"end": 4,
"loc": {
"end": {
"column": 5,
"line": 1,
"offset": 4,
},
"start": {
"column": 1,
"line": 1,
"offset": 0,
},
},
"start": 0,
"type": 9,
"value": "",
},
],
"loc": {
"end": {
"column": 5,
"line": 1,
"offset": 4,
},
"start": {
"column": 1,
"line": 1,
"offset": 0,
},
},
"start": 0,
"type": 2,
},
"end": 4,
"loc": {
"end": {
"column": 5,
"line": 1,
"offset": 4,
},
"source": "{''}",
"start": {
"column": 1,
"line": 1,
"offset": 0,
},
},
"start": 0,
"type": 0,
}
`;

exports[`errors > include new line: hi { 'foo\\n' } 1`] = `
{
"body": {
Expand Down
19 changes: 19 additions & 0 deletions packages/message-compiler/test/parser/literal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,25 @@ describe('unicode', () => {
})
})

test('empty string', () => {
const text = `{''}`
const parser = createParser({ onError: spy })
const ast = parser.parse(text)

expect(ast).toMatchSnapshot()
expect(spy).not.toHaveBeenCalled()
expect(ast.type).toEqual(NodeTypes.Resource)
expect(ast.body.type).toEqual(NodeTypes.Message)
const message = ast.body as MessageNode
expect(message.items).toHaveLength(1)
expect(message.items).toMatchObject([
{
type: NodeTypes.Literal,
value: ''
}
])
})

describe('intlify message syntax special characters', () => {
const items = ['{', '}', '@', '|', '%']
for (const ch of items) {
Expand Down
35 changes: 35 additions & 0 deletions packages/message-compiler/test/tokenizer/literal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,41 @@ describe('escapes', () => {
}
})
})

test('empty string', () => {
const tokenizer = createTokenizer(`{''}`)
expect(tokenizer.nextToken()).toEqual({
type: TokenTypes.BraceLeft,
value: '{',
loc: {
start: { line: 1, column: 1, offset: 0 },
end: { line: 1, column: 2, offset: 1 }
}
})
expect(tokenizer.nextToken()).toEqual({
type: TokenTypes.Literal,
value: '',
loc: {
start: { line: 1, column: 2, offset: 1 },
end: { line: 1, column: 4, offset: 3 }
}
})
expect(tokenizer.nextToken()).toEqual({
type: TokenTypes.BraceRight,
value: '}',
loc: {
start: { line: 1, column: 4, offset: 3 },
end: { line: 1, column: 5, offset: 4 }
}
})
expect(tokenizer.nextToken()).toEqual({
type: TokenTypes.EOF,
loc: {
start: { line: 1, column: 5, offset: 4 },
end: { line: 1, column: 5, offset: 4 }
}
})
})
})

describe('errors', () => {
Expand Down
13 changes: 13 additions & 0 deletions packages/vue-i18n-core/test/issues.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1405,3 +1405,16 @@ test('#1912', async () => {

expect(el?.innerHTML).include(`No apples found`)
})

test('#1972', async () => {
const i18n = createI18n({
legacy: false,
locale: 'en',
messages: {
en: {
test: "{''} | {n} test | {n} tests"
}
}
})
expect(i18n.global.t('test', 0)).toEqual('')
})