unsplash / intlc

Compile ICU messages into code. Supports TypeScript and JSX. No runtime.
MIT License
56 stars 2 forks source link

Allow plural lone wildcard #158

Closed samhh closed 2 years ago

samhh commented 2 years ago

Closes #108. #136 will follow in another PR.

Given:

{
  "f": { "message": "{n, plural, other {}}" },
  "g": { "message": "{n, selectordinal, other {}}" },
}

Previously:

$ intlc compile -l foo ./bar.json
./bar.json:2:34:
  |
2 |   "f": { "message": "{n, plural, other {}}" },
  |                                  ^^^^
unexpected "othe"
expecting "few", "many", "one", "two", "zero", '=', or white space

./bar.json:3:41:
  |
3 |   "g": { "message": "{n, selectordinal, other {}}" },
  |                                         ^^^^
unexpected "othe"
expecting "few", "many", "one", "two", "zero", '=', or white space

Now:

$ intlc compile -l foo ./bar.json
export const f: (x: { n: number }) => string = x => `${(() => { switch (x.n as typeof x.n) {  default: return ``; } })()}`
export const g: (x: { n: number }) => string = x => `${(() => { switch (new Intl.PluralRules('xyz', { type: 'ordinal' }).select(x.n)) {  default: return ``; } })()}`

Most of the diff is due to changes to the shape of the AST. Most attention should be paid to those AST changes themselves, and then the drastically simplified parsing (:smiley:). The tests should have caught any potential regressions... in theory!

Looking at the output there's a lot of space for optimisation, but I'm avoiding anything like that for now in pursuit of a simple compiler, as with how we solved #111.