Skip to content

Commit

Permalink
fix(gen): consider named functions (#1616) (#1617)
Browse files Browse the repository at this point in the history
* fix(gen): consider named functions (#1616)

* fix(gen): added tests for function to code

* fix: linted the new tests for gen
  • Loading branch information
ineshbose committed Nov 13, 2022
1 parent 6d1344c commit e928b2f
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/gen.ts
Expand Up @@ -120,7 +120,7 @@ export function generateLoaderOptions(
codes += `}\n`
return codes
} else if (rootKey === 'additionalMessages') {
return `export const ${rootKey} = ${generaeteAdditionalMessages(rootValue, dev)}\n`
return `export const ${rootKey} = ${generateAdditionalMessages(rootValue, dev)}\n`
} else {
return `export const ${rootKey} = ${toCode(rootValue)}\n`
}
Expand Down Expand Up @@ -157,7 +157,7 @@ function generateVueI18nOptions(options: Record<string, any>, dev: boolean): str
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function generaeteAdditionalMessages(value: Record<string, any>, dev: boolean): string {
function generateAdditionalMessages(value: Record<string, any>, dev: boolean): string {
let genCode = 'Object({'
for (const [locale, messages] of Object.entries(value)) {
genCode += `${JSON.stringify(locale)}:[`
Expand Down Expand Up @@ -198,7 +198,7 @@ export function toCode(code: any): string {
}

if (isFunction(code) && code.toString) {
return `(${code.toString()})`
return `(${code.toString().replace(new RegExp(`^${code.name}`), 'function ')})`
}

if (isArray(code)) {
Expand Down
54 changes: 54 additions & 0 deletions test/__snapshots__/gen.test.ts.snap
Expand Up @@ -55,6 +55,60 @@ export const NUXT_I18N_MODULE_ID = \\"@nuxtjs/i18n\\"
"
`;

exports[`toCode: function (arrow) 1`] = `
"export const localeCodes = [\\"en\\",\\"ja\\",\\"fr\\"]
export const additionalMessages = Object({})
export const resolveNuxtI18nOptions = async (context) => {
const nuxtI18nOptions = Object({})
nuxtI18nOptions.defaultLocale = \\"en\\"
const vueI18nOptionsLoader = async (context) => Object({\\"locale\\":\\"en\\",\\"fallbackLocale\\":\\"en\\",\\"messages\\": Object({\\"en\\":{
\\"hello\\": (()=>{const fn=(ctx) => {const { normalize: _normalize } = ctx;return _normalize([\\"Hello!\\"])};fn.source=\\"Hello!\\";return fn;})()
},}),})
nuxtI18nOptions.vueI18n = await vueI18nOptionsLoader(context)
nuxtI18nOptions.locales = [Object({\\"code\\":\\"en\\",\\"file\\":\\"en.json\\",\\"path\\":\\"/path/to/en.json\\",\\"testFunc\\":((prop) => {
return \`Hello \${prop}\`;
})}),Object({\\"code\\":\\"ja\\",\\"file\\":\\"ja.json\\",\\"path\\":\\"/path/to/ja.json\\",\\"testFunc\\":((prop) => {
return \`Hello \${prop}\`;
})})]
return nuxtI18nOptions
}
export const nuxtI18nOptionsDefault = Object({vueI18n: undefined,locales: [],defaultLocale: \\"\\",defaultDirection: \\"ltr\\",routesNameSeparator: \\"___\\",trailingSlash: false,defaultLocaleRouteNameSuffix: \\"default\\",strategy: \\"prefix_except_default\\",lazy: false,langDir: null,rootRedirect: null,detectBrowserLanguage: Object({\\"alwaysRedirect\\":false,\\"cookieCrossOrigin\\":false,\\"cookieDomain\\":null,\\"cookieKey\\":\\"i18n_redirected\\",\\"cookieSecure\\":false,\\"fallbackLocale\\":\\"\\",\\"redirectOn\\":\\"root\\",\\"useCookie\\":true}),differentDomains: false,baseUrl: \\"\\",dynamicRouteParams: false,parsePages: true,pages: Object({}),skipSettingLocaleOnNavigate: false,onBeforeLanguageSwitch: (() => \\"\\"),onLanguageSwitched: (() => null),types: undefined,debug: false})
export const nuxtI18nInternalOptions = Object({__normalizedLocales: [Object({\\"code\\":\\"en\\"})]})
export const NUXT_I18N_MODULE_ID = \\"@nuxtjs/i18n\\"
"
`;
exports[`toCode: function (named) 1`] = `
"export const localeCodes = [\\"en\\",\\"ja\\",\\"fr\\"]
export const additionalMessages = Object({})
export const resolveNuxtI18nOptions = async (context) => {
const nuxtI18nOptions = Object({})
nuxtI18nOptions.defaultLocale = \\"en\\"
const vueI18nOptionsLoader = async (context) => Object({\\"locale\\":\\"en\\",\\"fallbackLocale\\":\\"en\\",\\"messages\\": Object({\\"en\\":{
\\"hello\\": (()=>{const fn=(ctx) => {const { normalize: _normalize } = ctx;return _normalize([\\"Hello!\\"])};fn.source=\\"Hello!\\";return fn;})()
},}),})
nuxtI18nOptions.vueI18n = await vueI18nOptionsLoader(context)
nuxtI18nOptions.locales = [Object({\\"code\\":\\"en\\",\\"file\\":\\"en.json\\",\\"path\\":\\"/path/to/en.json\\",\\"testFunc\\":(function (prop) {
return \`Hello \${prop}\`;
})}),Object({\\"code\\":\\"ja\\",\\"file\\":\\"ja.json\\",\\"path\\":\\"/path/to/ja.json\\",\\"testFunc\\":(function (prop) {
return \`Hello \${prop}\`;
})})]
return nuxtI18nOptions
}
export const nuxtI18nOptionsDefault = Object({vueI18n: undefined,locales: [],defaultLocale: \\"\\",defaultDirection: \\"ltr\\",routesNameSeparator: \\"___\\",trailingSlash: false,defaultLocaleRouteNameSuffix: \\"default\\",strategy: \\"prefix_except_default\\",lazy: false,langDir: null,rootRedirect: null,detectBrowserLanguage: Object({\\"alwaysRedirect\\":false,\\"cookieCrossOrigin\\":false,\\"cookieDomain\\":null,\\"cookieKey\\":\\"i18n_redirected\\",\\"cookieSecure\\":false,\\"fallbackLocale\\":\\"\\",\\"redirectOn\\":\\"root\\",\\"useCookie\\":true}),differentDomains: false,baseUrl: \\"\\",dynamicRouteParams: false,parsePages: true,pages: Object({}),skipSettingLocaleOnNavigate: false,onBeforeLanguageSwitch: (() => \\"\\"),onLanguageSwitched: (() => null),types: undefined,debug: false})
export const nuxtI18nInternalOptions = Object({__normalizedLocales: [Object({\\"code\\":\\"en\\"})]})
export const NUXT_I18N_MODULE_ID = \\"@nuxtjs/i18n\\"
"
`;
exports[`vueI18n: path 1`] = `
"import locale_en from \\"../locales/en.json\\" assert { type: \\"json\\" };
import locale_ja from \\"../locales/ja.json\\" assert { type: \\"json\\" };
Expand Down
40 changes: 40 additions & 0 deletions test/gen.test.ts
Expand Up @@ -101,3 +101,43 @@ it('vueI18n: path', () => {
expect(validateSyntax(code)).toBe(true)
expect(code).toMatchSnapshot()
})

it('toCode: function (arrow)', () => {
const code = generateLoaderOptions(false, 'locales', '..', {
localeCodes: LOCALE_CODES,
additionalMessages: {},
nuxtI18nOptions: {
...NUXT_I18N_OPTIONS,
locales: LOCALE_INFO.map(locale => ({
...locale,
testFunc: (prop: unknown) => {
return `Hello ${prop}`
}
}))
},
nuxtI18nOptionsDefault: DEFAULT_OPTIONS,
nuxtI18nInternalOptions: NUXT_I18N_INTERNAL_OPTIONS
})
expect(validateSyntax(code)).toBe(true)
expect(code).toMatchSnapshot()
})

it('toCode: function (named)', () => {
const code = generateLoaderOptions(false, 'locales', '..', {
localeCodes: LOCALE_CODES,
additionalMessages: {},
nuxtI18nOptions: {
...NUXT_I18N_OPTIONS,
locales: LOCALE_INFO.map(locale => ({
...locale,
testFunc(prop: unknown) {
return `Hello ${prop}`
}
}))
},
nuxtI18nOptionsDefault: DEFAULT_OPTIONS,
nuxtI18nInternalOptions: NUXT_I18N_INTERNAL_OPTIONS
})
expect(validateSyntax(code)).toBe(true)
expect(code).toMatchSnapshot()
})

0 comments on commit e928b2f

Please sign in to comment.