New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(gen): consider named functions (#1616) #1617
Conversation
✅ Deploy Preview for nuxt-i18n-v8 ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
@ineshbose |
My bad! Didn't see the pipeline result for that. Should be fixed. |
LGTM! Thank you so much! |
closes #1616 |
@ineshbose @kazupon const createAnynomousFunction = () => (() => 1)
code = createAnynomousFunction()
console.log(`(${code.toString().replace(new RegExp(`^${code.name}`), 'function ')})`)
// Logs: "(function () => 1)" instead of "(() => 1)" Real use case, implementing CLDR pluralization rules: const createPluralizationRule = (formatterFn) => {
return (value, choicesList) => {
const stringValue = String(value)
const decimalPart = !stringValue.includes('.') ? '' : stringValue.split('.')[1]
const decimalLength = decimalPart.length
const numericValue = +value
const integerPart = +(stringValue.split('.')[0])
const truncatedDecimal = decimalPart.length === 0 ? 0 : +decimalPart.replace(/0+$/, '')
const choice = formatterFn(numericValue, integerPart, decimalLength, +decimalPart, truncatedDecimal)
return choice < choicesLength ? choice : choicesLength - 1
}
}
export default {
pluralizationRules: {
de: createPluralizationRule((_n, i, v) => i === 1 && v === 0 ? 0 : 1)
}
} |
@Aareksio thanks for the feedback. I tried it with anonymous functions (not quite the same way you have however using a higher order function), but in those cases, @kazupon I think as mentioned before, ideally it would be best to rewrite the script using an AST walker? |
export const resolveNuxtI18nOptions = async (context) => {
const nuxtI18nOptions = Object({})
const vueI18nOptionsLoader = async (context) => Object({"pluralizationRules":Object({"de":(function (value, choicesLength) => {
const stringValue = String(value);
const decimalPart = !stringValue.includes('.') ? '' : stringValue.split('.')[1];
const decimalLength = decimalPart.length;
const numericValue = +value;
const integerPart = +stringValue.split('.')[0];
const truncatedDecimal = decimalPart.length === 0 ? 0 : +decimalPart.replace(/0+$/, '');
const choice = formatterFn(numericValue, integerPart, decimalLength, +decimalPart, truncatedDecimal);
return choice < choicesLength ? choice : choicesLength - 1;
})}),})
nuxtI18nOptions.vueI18n = await vueI18nOptionsLoader(context)
// ...
return nuxtI18nOptions
} This approach doesn't work anyway, as That said, the input is valid javascript, while the output has invalid syntax. Probably easiest dirty fix would be to do something like the below, to avoid |
/ping @ineshbose |
@kazupon already on it! 😉 @Aareksio I'm able to understand and replicate your issue with all versions of What I'm analysing is that in export default {
pluralizationRules: {
de: createPluralizationRule((_n, i, v) => i === 1 && v === 0 ? 0 : 1)
}
} The function export default {
pluralizationRules: {
de: createPluralizationRule
}
} would be perfectly valid with Meanwhile it would be great to open a separate issue for this (titled like "Function context undefined due to code.toString()" or "gen.ts does not support higher order functions"). 🙂 |
Exactly, I pointed this out in the reply above. The What is important, is that a function with a name const createFunctionWithNoName= () => (() => 1)
code = createFunctionWithNoName()
console.log(`(${code.toString().replace(new RegExp(`^${code.name}`), 'function ')})`)
// Logs: "(function () => 1)" instead of "(() => 1)" |
Thanks for the reproduction. I see your point and also agree the dirty fix would work. I've taken a note of this and trying to rework the file to consider all possibilities (such as returning functions, using contexts, etc, as you raised). Meanwhile please use the alternative solution where possible. 🙏🏻 |
…1617) * fix(gen): consider named functions (nuxt-modules#1616) * fix(gen): added tests for function to code * fix: linted the new tests for gen
resolve #1616
Along with function name typo fix.