Skip to content

Commit

Permalink
feat!: Use const and => notation for function stringification
Browse files Browse the repository at this point in the history
BREAKING CHANGE: While using ES6 syntax for functions does not change
their behaviour in any way, it will need to be transpiled if the target
environment (e.g. IE 11) does not support it.
  • Loading branch information
eemeli committed Oct 19, 2020
1 parent ba55599 commit b9da90a
Show file tree
Hide file tree
Showing 14 changed files with 1,206 additions and 1,315 deletions.
12 changes: 4 additions & 8 deletions packages/cli/src/print-plurals.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,11 @@ export default function printPluralsModule(args) {
const plurals = []
for (const [fn, locales] of Object.entries(localesByFn)) {
if (locales.length > maxRepeat && commonId <= 'z') {
str += fn.replace(/^function\b/, `function ${commonId}`) + '\n'
str += `const ${commonId} = ${fn};\n`
for (const lc of locales) plurals.push({ lc, id: commonId })
commonId = String.fromCharCode(commonId.charCodeAt(0) + 1)
} else {
for (const lc of locales) {
plurals.push({ lc, fn: fn.replace(/^function\b/, `function ${lc}`) })
}
for (const lc of locales) plurals.push({ lc, fn })
}
}
plurals.sort((a, b) => (a.lc < b.lc ? -1 : 1))
Expand All @@ -42,10 +40,8 @@ export default function printPluralsModule(args) {
const pm = plurals.map(({ lc, id, fn }) => `${lc}: ${id || fn}`)
str += printUMD('plurals', pm.join(',\n\n')) + '\n'
} else {
for (const { lc, id, fn } of plurals) {
if (id) str += `export const ${lc} = ${id};\n`
else str += `export ${fn}\n`
}
for (const { lc, id, fn } of plurals)
str += `export const ${lc} = ${id || fn};\n`
}
return str
}
2 changes: 1 addition & 1 deletion packages/cli/src/print-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default function printPluralTypes(args) {
mpc.test()
const id = identifier(lc)
const cat = stringifyCategories(args, mpc.categories)
str += `export function ${id}(${fnArgs}): ${cat};\n`
str += `export const ${id}: (${fnArgs}) => ${cat};\n`
}
return str
}
16 changes: 8 additions & 8 deletions packages/compiler/src/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,16 @@ export default class Compiler {
compile() {
if (!this.fn) {
this.fn = this.buildFunction()
this.fn.toString = name => {
const str = Function.prototype.toString.call(this.fn)
const func = name ? `function ${name}` : 'function'
this.fn.toString = () =>
// The /*``*/ is present in Node 8 output, due to
// https://bugs.chromium.org/p/v8/issues/detail?id=2470
return str.replace(
/^function(?: \w+)\(([^)]+)\)/,
(_, args) => `${func}(${args.replace('/*``*/', '').trim()})`
)
}
Function.prototype.toString
.call(this.fn)
.replace(
/^function(?: \w+)\(([^)]+)\)/,
(_, args) => `(${args.replace('/*``*/', '').trim()}) =>`
)
.replace(/{\s*return\s+([^{}]*);\s*}$/, '$1')
this.test = () => this.tests.testAll(this.fn)
}
return this.fn
Expand Down
2 changes: 1 addition & 1 deletion packages/compiler/src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,6 @@ export default class Parser {
}
}
if (!vars.length) return ''
return 'var ' + ["s = String(n).split('.')", ...vars].join(', ')
return 'const ' + ["s = String(n).split('.')", ...vars].join(', ')
}
}
426 changes: 213 additions & 213 deletions packages/plurals/cardinals.d.ts

Large diffs are not rendered by default.

Loading

0 comments on commit b9da90a

Please sign in to comment.