Skip to content

Commit aa61d4c

Browse files
authored
fix(gen): consider named functions (nuxt-modules#1616) (nuxt-modules#1617)
* fix(gen): consider named functions (nuxt-modules#1616) * fix(gen): added tests for function to code * fix: linted the new tests for gen
1 parent 47a3466 commit aa61d4c

File tree

3 files changed

+97
-3
lines changed

3 files changed

+97
-3
lines changed

src/gen.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ export function generateLoaderOptions(
120120
codes += `}\n`
121121
return codes
122122
} else if (rootKey === 'additionalMessages') {
123-
return `export const ${rootKey} = ${generaeteAdditionalMessages(rootValue, dev)}\n`
123+
return `export const ${rootKey} = ${generateAdditionalMessages(rootValue, dev)}\n`
124124
} else {
125125
return `export const ${rootKey} = ${toCode(rootValue)}\n`
126126
}
@@ -157,7 +157,7 @@ function generateVueI18nOptions(options: Record<string, any>, dev: boolean): str
157157
}
158158

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

200200
if (isFunction(code) && code.toString) {
201-
return `(${code.toString()})`
201+
return `(${code.toString().replace(new RegExp(`^${code.name}`), 'function ')})`
202202
}
203203

204204
if (isArray(code)) {

test/__snapshots__/gen.test.ts.snap

+54
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,60 @@ export const NUXT_I18N_MODULE_ID = \\"@nuxtjs/i18n\\"
5555
"
5656
`;
5757

58+
exports[`toCode: function (arrow) 1`] = `
59+
"export const localeCodes = [\\"en\\",\\"ja\\",\\"fr\\"]
60+
61+
export const additionalMessages = Object({})
62+
63+
export const resolveNuxtI18nOptions = async (context) => {
64+
const nuxtI18nOptions = Object({})
65+
nuxtI18nOptions.defaultLocale = \\"en\\"
66+
const vueI18nOptionsLoader = async (context) => Object({\\"locale\\":\\"en\\",\\"fallbackLocale\\":\\"en\\",\\"messages\\": Object({\\"en\\":{
67+
\\"hello\\": (()=>{const fn=(ctx) => {const { normalize: _normalize } = ctx;return _normalize([\\"Hello!\\"])};fn.source=\\"Hello!\\";return fn;})()
68+
},}),})
69+
nuxtI18nOptions.vueI18n = await vueI18nOptionsLoader(context)
70+
nuxtI18nOptions.locales = [Object({\\"code\\":\\"en\\",\\"file\\":\\"en.json\\",\\"path\\":\\"/path/to/en.json\\",\\"testFunc\\":((prop) => {
71+
return \`Hello \${prop}\`;
72+
})}),Object({\\"code\\":\\"ja\\",\\"file\\":\\"ja.json\\",\\"path\\":\\"/path/to/ja.json\\",\\"testFunc\\":((prop) => {
73+
return \`Hello \${prop}\`;
74+
})})]
75+
return nuxtI18nOptions
76+
}
77+
78+
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})
79+
80+
export const nuxtI18nInternalOptions = Object({__normalizedLocales: [Object({\\"code\\":\\"en\\"})]})
81+
export const NUXT_I18N_MODULE_ID = \\"@nuxtjs/i18n\\"
82+
"
83+
`;
84+
85+
exports[`toCode: function (named) 1`] = `
86+
"export const localeCodes = [\\"en\\",\\"ja\\",\\"fr\\"]
87+
88+
export const additionalMessages = Object({})
89+
90+
export const resolveNuxtI18nOptions = async (context) => {
91+
const nuxtI18nOptions = Object({})
92+
nuxtI18nOptions.defaultLocale = \\"en\\"
93+
const vueI18nOptionsLoader = async (context) => Object({\\"locale\\":\\"en\\",\\"fallbackLocale\\":\\"en\\",\\"messages\\": Object({\\"en\\":{
94+
\\"hello\\": (()=>{const fn=(ctx) => {const { normalize: _normalize } = ctx;return _normalize([\\"Hello!\\"])};fn.source=\\"Hello!\\";return fn;})()
95+
},}),})
96+
nuxtI18nOptions.vueI18n = await vueI18nOptionsLoader(context)
97+
nuxtI18nOptions.locales = [Object({\\"code\\":\\"en\\",\\"file\\":\\"en.json\\",\\"path\\":\\"/path/to/en.json\\",\\"testFunc\\":(function (prop) {
98+
return \`Hello \${prop}\`;
99+
})}),Object({\\"code\\":\\"ja\\",\\"file\\":\\"ja.json\\",\\"path\\":\\"/path/to/ja.json\\",\\"testFunc\\":(function (prop) {
100+
return \`Hello \${prop}\`;
101+
})})]
102+
return nuxtI18nOptions
103+
}
104+
105+
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})
106+
107+
export const nuxtI18nInternalOptions = Object({__normalizedLocales: [Object({\\"code\\":\\"en\\"})]})
108+
export const NUXT_I18N_MODULE_ID = \\"@nuxtjs/i18n\\"
109+
"
110+
`;
111+
58112
exports[`vueI18n: path 1`] = `
59113
"import locale_en from \\"../locales/en.json\\" assert { type: \\"json\\" };
60114
import locale_ja from \\"../locales/ja.json\\" assert { type: \\"json\\" };

test/gen.test.ts

+40
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,43 @@ it('vueI18n: path', () => {
101101
expect(validateSyntax(code)).toBe(true)
102102
expect(code).toMatchSnapshot()
103103
})
104+
105+
it('toCode: function (arrow)', () => {
106+
const code = generateLoaderOptions(false, 'locales', '..', {
107+
localeCodes: LOCALE_CODES,
108+
additionalMessages: {},
109+
nuxtI18nOptions: {
110+
...NUXT_I18N_OPTIONS,
111+
locales: LOCALE_INFO.map(locale => ({
112+
...locale,
113+
testFunc: (prop: unknown) => {
114+
return `Hello ${prop}`
115+
}
116+
}))
117+
},
118+
nuxtI18nOptionsDefault: DEFAULT_OPTIONS,
119+
nuxtI18nInternalOptions: NUXT_I18N_INTERNAL_OPTIONS
120+
})
121+
expect(validateSyntax(code)).toBe(true)
122+
expect(code).toMatchSnapshot()
123+
})
124+
125+
it('toCode: function (named)', () => {
126+
const code = generateLoaderOptions(false, 'locales', '..', {
127+
localeCodes: LOCALE_CODES,
128+
additionalMessages: {},
129+
nuxtI18nOptions: {
130+
...NUXT_I18N_OPTIONS,
131+
locales: LOCALE_INFO.map(locale => ({
132+
...locale,
133+
testFunc(prop: unknown) {
134+
return `Hello ${prop}`
135+
}
136+
}))
137+
},
138+
nuxtI18nOptionsDefault: DEFAULT_OPTIONS,
139+
nuxtI18nInternalOptions: NUXT_I18N_INTERNAL_OPTIONS
140+
})
141+
expect(validateSyntax(code)).toBe(true)
142+
expect(code).toMatchSnapshot()
143+
})

0 commit comments

Comments
 (0)