Skip to content

Commit b7ae4de

Browse files
authored
fix modulo case (#80)
* fix modulo case closes #79 * remove
1 parent 38109a2 commit b7ae4de

File tree

10 files changed

+1111
-111
lines changed

10 files changed

+1111
-111
lines changed

src/message/tokenizer.ts

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ export interface TokenizeContext {
6262
lastEndLoc: Position
6363
braceNest: number
6464
inLinked: boolean
65+
text: string
6566
}
6667

6768
export interface Tokenizer {
@@ -95,7 +96,8 @@ export function createTokenizer(
9596
lastStartLoc: _initLoc,
9697
lastEndLoc: _initLoc,
9798
braceNest: 0,
98-
inLinked: false
99+
inLinked: false,
100+
text: ''
99101
}
100102

101103
const context = (): TokenizeContext => _context
@@ -341,32 +343,33 @@ export function createTokenizer(
341343
return ret
342344
}
343345

344-
function isTextStart(scnr: Scanner): boolean {
345-
const fn = (hasSpace = false): boolean => {
346+
function isTextStart(scnr: Scanner, reset = true): boolean {
347+
const fn = (hasSpace = false, prev = '', detectModulo = false): boolean => {
346348
const ch = scnr.currentPeek()
347-
if (
348-
ch === TokenChars.BraceLeft ||
349-
ch === TokenChars.BraceRight ||
350-
ch === TokenChars.Modulo ||
351-
ch === TokenChars.LinkedAlias ||
352-
!ch
353-
) {
354-
return hasSpace
349+
if (ch === TokenChars.BraceLeft) {
350+
return prev === TokenChars.Modulo ? false : hasSpace
351+
} else if (ch === TokenChars.LinkedAlias || !ch) {
352+
return prev === TokenChars.Modulo ? true : hasSpace
353+
} else if (ch === TokenChars.Modulo) {
354+
scnr.peek()
355+
return fn(hasSpace, TokenChars.Modulo, true)
355356
} else if (ch === TokenChars.Pipe) {
356-
return false
357+
return prev === TokenChars.Modulo || detectModulo
358+
? true
359+
: !(prev === SPACE || prev === NEW_LINE)
357360
} else if (ch === SPACE) {
358361
scnr.peek()
359-
return fn(true)
362+
return fn(true, SPACE, detectModulo)
360363
} else if (ch === NEW_LINE) {
361364
scnr.peek()
362-
return fn(true)
365+
return fn(true, NEW_LINE, detectModulo)
363366
} else {
364367
return true
365368
}
366369
}
367370

368371
const ret = fn()
369-
scnr.resetPeek()
372+
reset && scnr.resetPeek()
370373

371374
return ret
372375
}
@@ -439,13 +442,26 @@ export function createTokenizer(
439442
if (
440443
ch === TokenChars.BraceLeft ||
441444
ch === TokenChars.BraceRight ||
442-
ch === TokenChars.Modulo ||
443445
ch === TokenChars.LinkedAlias ||
444-
ch === EOF
446+
!ch
445447
) {
446448
return buf
449+
} else if (ch === TokenChars.Modulo) {
450+
if (isTextStart(scnr)) {
451+
buf += ch
452+
scnr.next()
453+
return fn(buf)
454+
} else {
455+
return buf
456+
}
457+
} else if (ch === TokenChars.Pipe) {
458+
return buf
447459
} else if (ch === SPACE || ch === NEW_LINE) {
448-
if (isPluralStart(scnr)) {
460+
if (isTextStart(scnr)) {
461+
buf += ch
462+
scnr.next()
463+
return fn(buf)
464+
} else if (isPluralStart(scnr)) {
449465
return buf
450466
} else {
451467
buf += ch
@@ -920,10 +936,6 @@ export function createTokenizer(
920936
case TokenChars.LinkedAlias:
921937
return readTokenInLinked(scnr, context) || getEndToken(context)
922938

923-
case TokenChars.Modulo:
924-
scnr.next()
925-
return getToken(context, TokenTypes.Modulo, TokenChars.Modulo)
926-
927939
default:
928940
if (isPluralStart(scnr)) {
929941
token = getToken(context, TokenTypes.Pipe, readPlural(scnr))
@@ -937,6 +949,10 @@ export function createTokenizer(
937949
return getToken(context, TokenTypes.Text, readText(scnr))
938950
}
939951

952+
if (ch === TokenChars.Modulo) {
953+
scnr.next()
954+
return getToken(context, TokenTypes.Modulo, TokenChars.Modulo)
955+
}
940956
break
941957
}
942958

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,39 @@ Object {
5252
}
5353
`;
5454

55+
exports[`edge cases %: code 1`] = `
56+
"function __msg__ (ctx) {
57+
const { normalize: _normalize } = ctx
58+
return _normalize([
59+
\\"%\\"
60+
])
61+
}"
62+
`;
63+
64+
exports[`edge cases hi %s !: code 1`] = `
65+
"function __msg__ (ctx) {
66+
const { normalize: _normalize } = ctx
67+
return _normalize([
68+
\\"hi %s !\\"
69+
])
70+
}"
71+
`;
72+
73+
exports[`edge cases no apples %| one apple % | too much apples : code 1`] = `
74+
"function __msg__ (ctx) {
75+
const { normalize: _normalize, pluralIndex: _pluralIndex, pluralRule: _pluralRule, orgPluralRule: _orgPluralRule } = ctx
76+
return [
77+
_normalize([
78+
\\"no apples %\\"
79+
]), _normalize([
80+
\\"one apple %\\"
81+
]), _normalize([
82+
\\"too much apples \\"
83+
])
84+
][_pluralRule(_pluralIndex, 3, _orgPluralRule)]
85+
}"
86+
`;
87+
5588
exports[`warnHtmlMessage default: code 1`] = `
5689
"function __msg__ (ctx) {
5790
const { normalize: _normalize } = ctx

test/message/__snapshots__/tokenizer.test.ts.snap

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2033,38 +2033,6 @@ world",
20332033
20342034
exports[`token analysis: "hi %name" tokens 1`] = `
20352035
Array [
2036-
Object {
2037-
"loc": Object {
2038-
"end": Object {
2039-
"column": 4,
2040-
"line": 1,
2041-
"offset": 3,
2042-
},
2043-
"start": Object {
2044-
"column": 1,
2045-
"line": 1,
2046-
"offset": 0,
2047-
},
2048-
},
2049-
"type": 0,
2050-
"value": "hi ",
2051-
},
2052-
Object {
2053-
"loc": Object {
2054-
"end": Object {
2055-
"column": 5,
2056-
"line": 1,
2057-
"offset": 4,
2058-
},
2059-
"start": Object {
2060-
"column": 4,
2061-
"line": 1,
2062-
"offset": 3,
2063-
},
2064-
},
2065-
"type": 4,
2066-
"value": "%",
2067-
},
20682036
Object {
20692037
"loc": Object {
20702038
"end": Object {
@@ -2073,13 +2041,13 @@ Array [
20732041
"offset": 8,
20742042
},
20752043
"start": Object {
2076-
"column": 5,
2044+
"column": 1,
20772045
"line": 1,
2078-
"offset": 4,
2046+
"offset": 0,
20792047
},
20802048
},
20812049
"type": 0,
2082-
"value": "name",
2050+
"value": "hi %name",
20832051
},
20842052
Object {
20852053
"loc": Object {

test/message/compiler.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,21 @@ describe('edge cases', () => {
4646
})
4747
expect(code.toString()).toMatchSnapshot('code')
4848
})
49+
50+
test(`hi %s !`, () => {
51+
const code = compile(`hi %s !`)
52+
expect(code.toString()).toMatchSnapshot('code')
53+
})
54+
55+
test(`%`, () => {
56+
const code = compile(`%`)
57+
expect(code.toString()).toMatchSnapshot('code')
58+
})
59+
60+
test(`no apples %| one apple % | too much apples `, () => {
61+
const code = compile(`no apples %| one apple % | too much apples `)
62+
expect(code.toString()).toMatchSnapshot('code')
63+
})
4964
})
5065

5166
/* eslint-enable @typescript-eslint/no-empty-function, no-irregular-whitespace */

0 commit comments

Comments
 (0)