Skip to content

Commit

Permalink
feat(cli): Use non-anonymous functions in module export
Browse files Browse the repository at this point in the history
  • Loading branch information
eemeli committed May 18, 2019
1 parent cac8cbf commit 5316a1a
Show file tree
Hide file tree
Showing 4 changed files with 694 additions and 722 deletions.
30 changes: 23 additions & 7 deletions packages/cli/src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/

import { source } from 'common-tags'
import { property } from 'safe-identifier'
import { identifier, property } from 'safe-identifier'
import * as common from './common'

var argv = require('minimist')(process.argv.slice(2), {
Expand Down Expand Up @@ -45,7 +45,7 @@ function write(str, end) {

const es6module = value => source`
export default {
${value}
${value}
}
`

Expand Down Expand Up @@ -73,14 +73,30 @@ function printPluralsModule(es6) {
const fn = mpc.compile().toString()
mpc.test()
const i = cp.indexOf(fn)
return [lc, i === -1 ? fn : `_${i}`]
return [
lc,
i === -1
? fn.replace(/^function\b/, `function ${identifier(lc)}`)
: `_${i}`
]
})
const pm = plurals.map(([lc, fn]) => `${property(null, lc)}: ${fn}`)
for (let i = 0; i < cp.length; ++i) {
write(cp[i].replace(/^function\b/, `function _${i}`), '\n\n')
}
if (es6) {
write(cp.map((p, i) => `const _${i} = ${p};`).join('\n'), '\n\n')
write(es6module(pm.join(',\n\n')), '\n')
const exp = []
for (const [lc, fn] of plurals) {
const prop = property(null, lc)
if (fn[0] === '_') exp.push(`${prop}: ${fn}`)
else {
write(fn, '\n\n')
const id = identifier(lc)
exp.push(id === prop ? id : `${prop}: ${id}`)
}
}
write(es6module(exp.join(',\n')), '\n')
} else {
write(cp.map((p, i) => `var _${i} = ${p};`).join('\n'), '\n\n')
const pm = plurals.map(([lc, fn]) => `${property(null, lc)}: ${fn}`)
write(umd('plurals', pm.join(',\n\n')), '\n')
}
}
Expand Down
Loading

0 comments on commit 5316a1a

Please sign in to comment.