Skip to content

Commit

Permalink
chore(lint): apply eslint fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Teages committed Feb 15, 2024
1 parent 6000b21 commit 1f903b7
Show file tree
Hide file tree
Showing 12 changed files with 90 additions and 86 deletions.
74 changes: 37 additions & 37 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,45 @@
"i18n-ally.namespace": true,
"i18n-ally.sourceLanguage": "en",

// Enable the ESlint flat config support
"eslint.experimental.useFlatConfig": true,
// Enable the ESlint flat config support
"eslint.experimental.useFlatConfig": true,

// Disable the default formatter, use eslint instead
"prettier.enable": false,
"editor.formatOnSave": false,
// Disable the default formatter, use eslint instead
"prettier.enable": false,
"editor.formatOnSave": false,

// Auto fix
"editor.codeActionsOnSave": {
"source.fixAll.eslint": "explicit",
"source.organizeImports": "never"
},
// Auto fix
"editor.codeActionsOnSave": {
"source.fixAll.eslint": "explicit",
"source.organizeImports": "never"
},

// Silent the stylistic rules in you IDE, but still auto fix them
"eslint.rules.customizations": [
{ "rule": "style/*", "severity": "off" },
{ "rule": "format/*", "severity": "off" },
{ "rule": "*-indent", "severity": "off" },
{ "rule": "*-spacing", "severity": "off" },
{ "rule": "*-spaces", "severity": "off" },
{ "rule": "*-order", "severity": "off" },
{ "rule": "*-dangle", "severity": "off" },
{ "rule": "*-newline", "severity": "off" },
{ "rule": "*quotes", "severity": "off" },
{ "rule": "*semi", "severity": "off" }
],
// Silent the stylistic rules in you IDE, but still auto fix them
"eslint.rules.customizations": [
{ "rule": "style/*", "severity": "off" },
{ "rule": "format/*", "severity": "off" },
{ "rule": "*-indent", "severity": "off" },
{ "rule": "*-spacing", "severity": "off" },
{ "rule": "*-spaces", "severity": "off" },
{ "rule": "*-order", "severity": "off" },
{ "rule": "*-dangle", "severity": "off" },
{ "rule": "*-newline", "severity": "off" },
{ "rule": "*quotes", "severity": "off" },
{ "rule": "*semi", "severity": "off" }
],

// Enable eslint for all supported languages
"eslint.validate": [
"javascript",
"javascriptreact",
"typescript",
"typescriptreact",
"vue",
"html",
"markdown",
"json",
"jsonc",
"yaml",
"toml"
],
// Enable eslint for all supported languages
"eslint.validate": [
"javascript",
"javascriptreact",
"typescript",
"typescriptreact",
"vue",
"html",
"markdown",
"json",
"jsonc",
"yaml",
"toml"
]
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Add these config to your `settings.json`:
"i18n-ally.keystyle": "nested",
"i18n-ally.pathMatcher": "{locale}/{namespaces}.json",
"i18n-ally.namespace": true,
"i18n-ally.sourceLanguage": "en-US", // or your default locale
"i18n-ally.sourceLanguage": "en-US" // or your default locale
}
```

Expand Down
2 changes: 1 addition & 1 deletion playground/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ export default defineNuxtConfig({
'ru-RU': { name: 'русский' },
},
},
devtools: { enabled: true }
devtools: { enabled: true },
})
2 changes: 1 addition & 1 deletion playground/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"private": true,
"name": "playground",
"type": "module",
"private": true,
"scripts": {
"dev": "nuxi dev",
"build": "nuxi build",
Expand Down
20 changes: 10 additions & 10 deletions src/gen.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { globSync } from "glob"
import { genDynamicImport } from "knitwork"
import { relative } from "pathe"
import { globSync } from 'glob'
import { genDynamicImport } from 'knitwork'
import { relative } from 'pathe'

export function genAvailableLocalesCode(
available: Array<{ code: string, name: string }>,
defaultLang: string,
) {
const code = [
`export const available = ${JSON.stringify(available)}`,
`export const defaultLang = '${defaultLang}'`
`export const defaultLang = '${defaultLang}'`,
].join('\n')

const type = [
`export declare const available: { code: ${ available.map(o => `'${o.code}'`).join(' | ') }, name: string }[]`,
`export declare const defaultLang: '${defaultLang}'`
`export declare const available: { code: ${available.map(o => `'${o.code}'`).join(' | ')}, name: string }[]`,
`export declare const defaultLang: '${defaultLang}'`,
].join('\n')

return { code, type }
Expand All @@ -37,21 +37,21 @@ export function genLazyImportLangCode(
.replace(/\//g, '.')
return ` ${name}: (await (${genDynamicImport(p)})()).default`
}).join(',\n'),
` })`
` })`,
].join('\n')
}),
`}`,
`export function lazyImportLang(locale) {`,
` return locales[locale]()`,
`}`
`}`,
].join('\n')

const type = [
`interface LocaleRecord {`,
` [key: string]: string | LocaleRecord;`,
`}`,
`type LocaleCode = ${Object.keys(lang).map((code) => `'${code}'`).join(' | ')};`,
`export function lazyImportLang(locale: LocaleCode): Promise<LocaleRecord>`
`type LocaleCode = ${Object.keys(lang).map(code => `'${code}'`).join(' | ')};`,
`export function lazyImportLang(locale: LocaleCode): Promise<LocaleRecord>`,
].join('\n')

return { code, type }
Expand Down
23 changes: 11 additions & 12 deletions src/module.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { defineNuxtModule, addPlugin, createResolver, addTemplate, addTypeTemplate, addImports, useLogger } from '@nuxt/kit'
import { addImports, addPlugin, addTemplate, addTypeTemplate, createResolver, defineNuxtModule, useLogger } from '@nuxt/kit'
import type { LocaleOptions } from './options'
import { genAvailableLocalesCode, genLazyImportLangCode } from './gen'

export default defineNuxtModule<LocaleOptions>({
meta: {
name: '@teages/nuxt-i18n-lite',
configKey: 'locale'
configKey: 'locale',
},
// Default configuration options of the Nuxt module
defaults: {
langDir: 'locales',
lang: {}
lang: {},
},
setup(options, nuxt) {
const resolver = createResolver(import.meta.url)
const srcResolver = createResolver(nuxt.options.srcDir)
const localesResolver = createResolver(srcResolver.resolve(
options.langDir
options.langDir,
))

const logger = useLogger('nuxt-i18n-lite')
Expand All @@ -31,37 +31,36 @@ export default defineNuxtModule<LocaleOptions>({
const available = Object.entries(options.lang).map(([code, lang]) => {
return {
code,
name: lang.name
name: lang.name,
}
})
const defaultLang = options.defaultLocale ?? available[0].code

const availableCodes = genAvailableLocalesCode(available, defaultLang)
addTemplate({
filename: 'locales/available.mjs',
getContents: () => availableCodes.code
getContents: () => availableCodes.code,
})
addTypeTemplate({
filename: 'locales/available.d.ts',
getContents: () => availableCodes.type
getContents: () => availableCodes.type,
})

const lazyImportLangCode = genLazyImportLangCode(options.lang, localesResolver.resolve)
addTemplate({
filename: 'locales/lazy-import.mjs',
getContents: () => lazyImportLangCode.code
getContents: () => lazyImportLangCode.code,
})
addTypeTemplate({
filename: 'locales/lazy-import.d.ts',
getContents: () => lazyImportLangCode.type
getContents: () => lazyImportLangCode.type,
})

addPlugin(resolver.resolve('./runtime/plugins/locale'))
addImports({
name: 'useLocales',
as: 'useLocales',
from: resolver.resolve('./runtime/composables/locale')
from: resolver.resolve('./runtime/composables/locale'),
})
}
},
})

13 changes: 8 additions & 5 deletions src/runtime/composables/internal.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { LocaleCode } from "#build/locales/lazy-import"
import destr from 'destr'
import type { LocaleCode } from '#build/locales/lazy-import'
import { available } from '#build/locales/available'
import { computed, useCookie, useState } from "#imports"
import destr from "destr"
import { computed, useCookie, useState } from '#imports'

export function useLocalesInternal() {
const _locales: Array<{ name: string, code: LocaleCode }> = available as any
Expand All @@ -26,7 +26,7 @@ export function useLocalesInternal() {
})
const userPrefer = useCookie<LocaleCode | null>('locale', {
maxAge: 35_600 * 24 * 60 * 60, // forever
decode: val => {
decode: (val) => {
const parsed = destr<string>(decodeURIComponent(val))
if (_locales.find(l => l.code === parsed)) {
return parsed as LocaleCode
Expand All @@ -38,6 +38,9 @@ export function useLocalesInternal() {
const locales = computed(() => _locales)

return {
locales, browserPrefer, browserMatch, userPrefer,
locales,
browserPrefer,
browserMatch,
userPrefer,
}
}
8 changes: 4 additions & 4 deletions src/runtime/composables/locale.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { useI18n } from 'vue-i18n'
import { lazyImportLang, type LocaleCode } from '#build/locales/lazy-import'
import { computed, useHead } from '#imports'
import { useLocalesInternal } from './internal'
import type { WritableComputedRef } from 'vue'
import { useLocalesInternal } from './internal'
import { type LocaleCode, lazyImportLang } from '#build/locales/lazy-import'
import { computed, useHead } from '#imports'

export function useLocales(): {
t: ReturnType<typeof useI18n>['t']
Expand Down Expand Up @@ -50,6 +50,6 @@ export function useLocales(): {
setLocale,
userPrefer,
browserPrefer,
browserMatch
browserMatch,
}
}
15 changes: 8 additions & 7 deletions src/runtime/plugins/locale.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { defineNuxtPlugin, useHead, useRequestHeaders } from '#imports'
import { createI18n } from 'vue-i18n'
import { decodeBrowserAcceptLang, lazyImportLang } from '../utils'
import { useLocalesInternal } from '../composables/internal'
import { defineNuxtPlugin, useHead, useRequestHeaders } from '#imports'
import { defaultLang } from '#build/locales/available'


export default defineNuxtPlugin(async (nuxtApp) => {
const { browserPrefer, browserMatch, userPrefer } = useLocalesInternal()

if (process.server) {
if (import.meta.server) {
const rawAcceptLang = useRequestHeaders(['accept-language'])['accept-language'] ?? ''
browserPrefer.value = [...decodeBrowserAcceptLang(rawAcceptLang)]
}
Expand All @@ -23,14 +22,16 @@ export default defineNuxtPlugin(async (nuxtApp) => {
fallbackLocale: defaultLang,
messages: {
[defaultLang]: await lazyImportLang(defaultLang),
...(locale !== defaultLang ? {
[locale]: await lazyImportLang(locale),
} : {}),
...(locale !== defaultLang
? {
[locale]: await lazyImportLang(locale),
}
: {}),
},
})
vueApp.use(i18n)

if (process.server) {
if (import.meta.server) {
useHead({
htmlAttrs: {
lang: locale,
Expand Down
3 changes: 2 additions & 1 deletion src/runtime/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export function decodeBrowserAcceptLang(rawAcceptLang: string): string[] {
langSet.delete(langCode[0])
}
langSet.add(langCode[0])
} else {
}
else {
keepSet.add(lang.code)
}
}
Expand Down
10 changes: 5 additions & 5 deletions test/basic.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { describe, it, expect } from 'vitest'
import { fileURLToPath } from 'node:url'
import { setup, $fetch } from '@nuxt/test-utils/e2e'
import { describe, expect, it } from 'vitest'
import { $fetch, setup } from '@nuxt/test-utils/e2e'

describe('Basic', async () => {
describe('basic', async () => {
await setup({
rootDir: fileURLToPath(new URL('./fixtures/basic', import.meta.url)),
})
Expand Down Expand Up @@ -35,7 +35,7 @@ describe('Basic', async () => {
const html = await $fetch('/', {
headers: {
'Accept-Language': 'zh-CN',
Cookie: 'locale=ja-JP',
'Cookie': 'locale=ja-JP',
},
})
check(html, 'ja-JP')
Expand All @@ -45,7 +45,7 @@ describe('Basic', async () => {
const html = await $fetch('/', {
headers: {
'Accept-Language': 'zh-CN',
Cookie: 'locale=ha-CK',
'Cookie': 'locale=ha-CK',
},
})
check(html, 'zh-CN')
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/basic/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"private": true,
"name": "basic",
"type": "module"
"type": "module",
"private": true
}

0 comments on commit 1f903b7

Please sign in to comment.