Skip to content

Commit 44b8d43

Browse files
authored
fix: plural compiler bug (#28)
1 parent fe7cd02 commit 44b8d43

File tree

5 files changed

+91
-12
lines changed

5 files changed

+91
-12
lines changed

src/message/context.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ const DEFAULT_MODIFIER = (str: unknown): unknown => str
5858
const DEFAULT_MESSAGE = (ctx: MessageContext): unknown => '' // eslint-disable-line
5959
export const DEFAULT_MESSAGE_DATA_TYPE = 'text'
6060
const DEFAULT_NORMALIZE = (values: unknown[]): unknown =>
61-
values.length === 0 ? values[0] : values.join('')
61+
values.length === 0 ? '' : values.join('')
6262
const DEFAULT_INTERPOLATE = toDisplayString
6363

6464
function pluralDefault(choice: number, choicesLength: number): number {

test/message/__snapshots__/compiler.test.ts.snap

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`compile: code 1`] = `
3+
exports[`@.caml:{'no apples'} | {0} apple | {n} apples: code 1`] = `
44
"function __msg__ (ctx) {
55
return [
66
ctx.normalize([
@@ -13,3 +13,39 @@ exports[`compile: code 1`] = `
1313
][ctx.pluralRule(ctx.pluralIndex, 3, ctx.orgPluralRule)]
1414
}"
1515
`;
16+
17+
exports[`edge cases | | | : code 1`] = `
18+
"function __msg__ (ctx) {
19+
return [
20+
ctx.normalize([
21+
22+
]), ctx.normalize([
23+
24+
]), ctx.normalize([
25+
26+
]), ctx.normalize([
27+
28+
])
29+
][ctx.pluralRule(ctx.pluralIndex, 4, ctx.orgPluralRule)]
30+
}"
31+
`;
32+
33+
exports[`edge cases | | | : error 1`] = `
34+
Object {
35+
"code": 10,
36+
"domain": "parser",
37+
"location": Object {
38+
"end": Object {
39+
"column": 8,
40+
"line": 1,
41+
"offset": 7,
42+
},
43+
"start": Object {
44+
"column": 1,
45+
"line": 1,
46+
"offset": 0,
47+
},
48+
},
49+
"message": "Plural must have messages",
50+
}
51+
`;

test/message/__snapshots__/context.test.ts.snap

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,23 @@ Array [
6363
},
6464
]
6565
`;
66+
67+
exports[`edge cases plural: ' | foo | ': error 1`] = `
68+
Object {
69+
"code": 10,
70+
"domain": "parser",
71+
"location": Object {
72+
"end": Object {
73+
"column": 10,
74+
"line": 1,
75+
"offset": 9,
76+
},
77+
"start": Object {
78+
"column": 1,
79+
"line": 1,
80+
"offset": 0,
81+
},
82+
},
83+
"message": "Plural must have messages",
84+
}
85+
`;

test/message/compiler.test.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
import { compile } from '../../src/message/compiler'
22

3-
test('compile', () => {
4-
const code = compile(
5-
`@.caml:{'no apples'} | {0} apple | {n} apples` // eslint-disable-line no-irregular-whitespace
6-
)
3+
/* eslint-disable no-irregular-whitespace */
4+
test(`@.caml:{'no apples'} | {0} apple | {n} apples`, () => {
5+
const code = compile(`@.caml:{'no apples'} | {0} apple | {n} apples`)
76
expect(code.toString()).toMatchSnapshot('code')
87
})
8+
/* eslint-enable no-irregular-whitespace */
9+
10+
describe('edge cases', () => {
11+
test(` | | | `, () => {
12+
const code = compile(` | | | `, {
13+
onError(error) {
14+
expect({ ...error, message: error.message }).toMatchSnapshot('error')
15+
}
16+
})
17+
expect(code.toString()).toMatchSnapshot('code')
18+
})
19+
})

test/message/context.test.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,6 @@ describe('text', () => {
1414
const ctx = createMessageContext()
1515
expect(JSON.stringify(msg(ctx))).toMatch(`hello\\n world`)
1616
})
17-
18-
test(' ', () => {
19-
const msg = compile(' ')
20-
const ctx = createMessageContext()
21-
expect(msg(ctx)).toMatch(` `)
22-
})
2317
})
2418

2519
describe('list', () => {
@@ -348,3 +342,21 @@ describe('custom process', () => {
348342
expect(msg(ctx)).toMatchSnapshot()
349343
})
350344
})
345+
346+
describe('edge cases', () => {
347+
test(`text: ' '`, () => {
348+
const msg = compile(' ')
349+
const ctx = createMessageContext()
350+
expect(msg(ctx)).toMatch(` `)
351+
})
352+
353+
test(`plural: ' | foo | '`, () => {
354+
const msg = compile(` | foo | `, {
355+
onError(error) {
356+
expect({ ...error, message: error.message }).toMatchSnapshot('error')
357+
}
358+
})
359+
const ctx = createMessageContext()
360+
expect(msg(ctx)).toMatch('')
361+
})
362+
})

0 commit comments

Comments
 (0)