From 2f542070413fc89f0d5716469fe6c97a01e5851d Mon Sep 17 00:00:00 2001 From: Rizumu Ayaka Date: Tue, 20 Feb 2024 15:20:05 +0700 Subject: [PATCH] style: update code lint --- README.md | 16 +++++++++------- README.zh-Hans.md | 10 +++++----- env.d.ts | 1 + example/netlify.toml | 18 +++++++++--------- example/shims.d.ts | 3 ++- example/test/basic.test.ts | 2 +- example/test/component.test.ts | 2 +- example/tsconfig.json | 26 +++++++++++++------------- example/vite.config.ts | 4 ++-- packages/.test/mount.ts | 2 +- packages/resolvers/yup.test.ts | 4 ++-- packages/resolvers/yup.ts | 7 ++----- src/getters.test.ts | 8 ++++---- src/index.test.ts | 10 +++++----- src/index.ts | 2 +- src/submitter.test.ts | 14 +++++++------- src/type/form.ts | 8 +++----- src/type/util.ts | 14 +++++++------- src/util/deepEqual.ts | 2 +- src/util/invoke.test.ts | 4 ++-- src/util/invoke.ts | 5 +++-- src/util/is.test.ts | 4 ++-- src/util/is.ts | 20 +++++++++++--------- src/util/watchIgnorable.test.ts | 4 ++-- src/util/watchIgnorable.ts | 19 ++++++++----------- test/basic.test.ts | 4 ++-- tsconfig.json | 12 ++++++------ 27 files changed, 112 insertions(+), 113 deletions(-) diff --git a/README.md b/README.md index 1468413..54a4336 100644 --- a/README.md +++ b/README.md @@ -97,7 +97,9 @@ const { form, status, reset, dirtyFields } = useForm({ - + ``` @@ -178,9 +180,8 @@ Use `rule` to define the validation rules for form fields. The verification proc ```ts // formRules.ts function isRequired(value) { - if (value && value.trim()) { + if (value && value.trim()) return true - } return t('required') // i18n support } @@ -352,9 +353,8 @@ const { form, rule } = useForm({ const text = 'abcd' const isValid = rule.userName.validate(text) // false -if (isValid) { +if (isValid) form.userName = text -} ``` You can also get access to the error message by indicating `fullResult: true` in the second options argument, in which case an object containing the message will be returned. @@ -474,10 +474,12 @@ Some suggestions: v-model="form.age" type="text" :class="status.age.isError && '!border-red'" - /> + >

{{ status.age.message }}

- + ``` diff --git a/README.zh-Hans.md b/README.zh-Hans.md index 345e023..cf5b4d1 100644 --- a/README.zh-Hans.md +++ b/README.zh-Hans.md @@ -96,7 +96,9 @@ const { form, status, reset, dirtyFields } = useForm({ - + ``` @@ -176,9 +178,8 @@ dirtyFields.value /* value: {} */ ```ts // formRules.ts export function isRequired(value) { - if (value && value.trim()) { + if (value && value.trim()) return true - } return t('required') // i18n 支持 } @@ -350,9 +351,8 @@ const { form, rule } = useForm({ const text = 'abcd' const isValid = rule.userName.validate(text) // false -if (isValid) { +if (isValid) form.userName = text -} ``` 您还可以获取到该字段的错误信息,方法是在第二个选项参数里传入 `fullResult: true`。此时该方法会返回一个带有错误信息的对象。 diff --git a/env.d.ts b/env.d.ts index 9745e27..f39e848 100644 --- a/env.d.ts +++ b/env.d.ts @@ -2,6 +2,7 @@ declare module '*.vue' { import type { ComponentOptions } from 'vue' + const componentOptions: ComponentOptions export default componentOptions } diff --git a/example/netlify.toml b/example/netlify.toml index bd06a07..55a2f3d 100755 --- a/example/netlify.toml +++ b/example/netlify.toml @@ -1,12 +1,12 @@ -[build.environment] - NPM_FLAGS = "--version" - NODE_VERSION = "16" - [build] - publish = "dist" - command = "npx pnpm i --store=node_modules/.pnpm-store && npx pnpm run build" +publish = "dist" +command = "npx pnpm i --store=node_modules/.pnpm-store && npx pnpm run build" + +[build.environment] +NPM_FLAGS = "--version" +NODE_VERSION = "16" [[redirects]] - from = "/*" - to = "/index.html" - status = 200 +from = "/*" +to = "/index.html" +status = 200 diff --git a/example/shims.d.ts b/example/shims.d.ts index 2b97bd9..fe00a85 100644 --- a/example/shims.d.ts +++ b/example/shims.d.ts @@ -1,5 +1,6 @@ declare module '*.vue' { import type { DefineComponent } from 'vue' - const component: DefineComponent<{}, {}, any> + + const component: DefineComponent export default component } diff --git a/example/test/basic.test.ts b/example/test/basic.test.ts index ed8d435..90b716f 100644 --- a/example/test/basic.test.ts +++ b/example/test/basic.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from 'vitest' -describe('Hi', () => { +describe('hi', () => { it('should works', () => { expect(1 + 1).toEqual(2) }) diff --git a/example/test/component.test.ts b/example/test/component.test.ts index b4b23e0..8cc00f1 100644 --- a/example/test/component.test.ts +++ b/example/test/component.test.ts @@ -2,7 +2,7 @@ import { mount } from '@vue/test-utils' import { describe, expect, it } from 'vitest' import Counter from '../src/components/Counter.vue' -describe('Counter.vue', () => { +describe('counter.vue', () => { it('should render', () => { const wrapper = mount(Counter, { props: { initial: 10 } }) expect(wrapper.text()).toContain('10') diff --git a/example/tsconfig.json b/example/tsconfig.json index ee3de9c..ebab319 100644 --- a/example/tsconfig.json +++ b/example/tsconfig.json @@ -1,26 +1,26 @@ { "compilerOptions": { - "baseUrl": ".", - "module": "ESNext", "target": "es2016", - "lib": ["DOM", "ESNext"], - "strict": true, "jsx": "preserve", - "esModuleInterop": true, - "skipLibCheck": true, + "lib": ["DOM", "ESNext"], + "baseUrl": ".", + "module": "ESNext", "moduleResolution": "node", + "paths": { + "~/*": ["src/*"], + "root/*": ["./../*"] + }, "resolveJsonModule": true, - "noUnusedLocals": true, - "strictNullChecks": true, - "forceConsistentCasingInFileNames": true, "types": [ "vite/client", "vite-plugin-pages/client" ], - "paths": { - "~/*": ["src/*"], - "root/*": ["./../*"] - } + "strict": true, + "strictNullChecks": true, + "noUnusedLocals": true, + "esModuleInterop": true, + "forceConsistentCasingInFileNames": true, + "skipLibCheck": true }, "exclude": ["dist", "node_modules"] } diff --git a/example/vite.config.ts b/example/vite.config.ts index 560f8c1..29d7e4c 100644 --- a/example/vite.config.ts +++ b/example/vite.config.ts @@ -1,7 +1,7 @@ /// -import path from 'path' -import { fileURLToPath } from 'url' +import path from 'node:path' +import { fileURLToPath } from 'node:url' import Vue from '@vitejs/plugin-vue' import Unocss from 'unocss/vite' import AutoImport from 'unplugin-auto-import/vite' diff --git a/packages/.test/mount.ts b/packages/.test/mount.ts index 31cacaa..535c79b 100644 --- a/packages/.test/mount.ts +++ b/packages/.test/mount.ts @@ -2,7 +2,7 @@ import type { Component, InjectionKey, Ref } from 'vue' import { createApp, defineComponent, h, provide, ref } from 'vue' type InstanceType = V extends { new (...arg: any[]): infer X } ? X : never -type VM = InstanceType & { unmount(): void } +type VM = InstanceType & { unmount: () => void } export function mount(Comp: V) { const el = document.createElement('div') diff --git a/packages/resolvers/yup.test.ts b/packages/resolvers/yup.test.ts index 8840054..ca249f0 100644 --- a/packages/resolvers/yup.test.ts +++ b/packages/resolvers/yup.test.ts @@ -1,11 +1,11 @@ -import { expect, test } from 'vitest' +import { expect, it } from 'vitest' import { nextTick, ref } from 'vue' import * as yup from 'yup' import { useForm } from '../../src' import { useSetup } from '../.test' import { yupFieldRule } from './yup' -test('yupFieldRule', async () => { +it('yupFieldRule', async () => { const wr = useSetup(() => { const local = ref('en') /** mock i18n `t` function */ diff --git a/packages/resolvers/yup.ts b/packages/resolvers/yup.ts index 9932a8d..8d77ff9 100644 --- a/packages/resolvers/yup.ts +++ b/packages/resolvers/yup.ts @@ -1,14 +1,11 @@ -import type { Schema, ValidationError, AnyObject, ValidateOptions } from 'yup' +import type { AnyObject, Schema, ValidateOptions, ValidationError } from 'yup' export interface ResolverOptions { model?: 'validateSync' | 'validate' } /** yup field rule resolver */ -export const yupFieldRule = ( - fieldSchema: SchemaT, - schemaOptions: ValidateOptions = {}, -) => { +export function yupFieldRule(fieldSchema: SchemaT, schemaOptions: ValidateOptions = {}) { return (val: unknown) => { try { fieldSchema.validateSync( diff --git a/src/getters.test.ts b/src/getters.test.ts index 63b05a8..22bad67 100644 --- a/src/getters.test.ts +++ b/src/getters.test.ts @@ -1,4 +1,4 @@ -import { describe, expect, it, test } from 'vitest' +import { describe, expect, it } from 'vitest' import { nextTick, reactive, watchEffect } from 'vue' import { useSetup } from '../packages/.test' import { useDirtyFields, useIsError } from './getters' @@ -80,7 +80,7 @@ describe('useDirtyFields', () => { }) }) -test('useForm dirtyFields', () => { +it('useForm dirtyFields', () => { const wr = useSetup(() => { const { form, status, reset, dirtyFields, isDirty } = useForm({ form: () => ({ @@ -117,7 +117,7 @@ test('useForm dirtyFields', () => { // 确保重制后的修改检查依然是正确的 // Ensure that the modification checks are still correct after the remake -test('useForm reset dirtyFields', async () => { +it('useForm reset dirtyFields', async () => { const wr = useSetup(() => { const { form, status, reset, dirtyFields, isDirty } = useForm({ form: () => ({ @@ -147,7 +147,7 @@ test('useForm reset dirtyFields', async () => { expect(wr.isDirty).true }) -test('useIsError', () => { +it('useIsError', () => { expect(useIsError).toBeDefined() const status = reactive({ diff --git a/src/index.test.ts b/src/index.test.ts index ea57efb..c71da30 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -64,7 +64,7 @@ describe('useForm', () => { }) // 规则验证在第一次值更改后开始工作 - it('Rule validation starts working after the first value change', async () => { + it('rule validation starts working after the first value change', async () => { const wrapper = useSetup(() => { const { form, status, isError } = useForm({ form: () => ({ @@ -125,7 +125,7 @@ describe('useForm', () => { }) // 响应规则更改 - it('Responding to rule changes', async () => { + it('responding to rule changes', async () => { const useI18n = () => { const local = ref('en') return { @@ -176,7 +176,7 @@ describe('useForm', () => { }) // 使用数组定义多个规则 - it('Define multiple rules using an array', async () => { + it('define multiple rules using an array', async () => { const wrapper = useSetup(() => { const { form, status } = useForm({ form: () => ({ @@ -208,7 +208,7 @@ describe('useForm', () => { }) // 手动触发验证和清除错误 - it('Manually trigger validation and clearErrors', async () => { + it('manually trigger validation and clearErrors', async () => { const wrapper = useSetup(() => { const { form, status, clearErrors, verify } = useForm({ form: () => ({ @@ -364,7 +364,7 @@ describe('useForm', () => { }) // 自定义表单校验信息占位内容 - it('Customize the placeholder content of the validation message', async () => { + it('customize the placeholder content of the validation message', async () => { const wrapper = useSetup(() => { const { form, status } = useForm({ form: () => ({ diff --git a/src/index.ts b/src/index.ts index 88ffba0..27c75c2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,7 +8,7 @@ import { useDirtyFields, useIsError, useIsFormDirty } from './getters' import { createSubmitter } from './submitter' import { initRule } from './rule' -const defaultParam: Required<{ defaultMessage: UseFormDefaultMessage; lazy: UseFormLazy }> = { +const defaultParam: Required<{ defaultMessage: UseFormDefaultMessage, lazy: UseFormLazy }> = { defaultMessage: '', lazy: false, } diff --git a/src/submitter.test.ts b/src/submitter.test.ts index 659711c..7a471a5 100644 --- a/src/submitter.test.ts +++ b/src/submitter.test.ts @@ -1,4 +1,4 @@ -import { describe, expect, test, vi } from 'vitest' +import { describe, expect, it, vi } from 'vitest' import { nextTick } from 'vue' import { createSubmit } from './submitter' import { useForm } from '.' @@ -14,7 +14,7 @@ describe('createSubmit', () => { }) const { form } = formData - test('this should work', () => { + it('this should work', () => { const submitFn = vi.fn(() => 0) const { submit } = createSubmit(formData, submitFn) @@ -28,7 +28,7 @@ describe('createSubmit', () => { expect(submitFn).toHaveBeenCalledTimes(1) }) - test('skip rule validation', () => { + it('skip rule validation', () => { form.field = 1 const submitFn = vi.fn(() => 0) @@ -41,7 +41,7 @@ describe('createSubmit', () => { expect(submitFn).toHaveBeenCalledTimes(1) }) - test('async submit', async () => { + it('async submit', async () => { form.field = 0 const submitFn = vi.fn(async () => { @@ -62,7 +62,7 @@ describe('createSubmit', () => { expect(submitFn).toHaveBeenCalledOnce() }) - test('close `submitting` after async error', async () => { + it('close `submitting` after async error', async () => { form.field = 0 const submitFn = vi.fn(async () => { @@ -85,7 +85,7 @@ describe('createSubmit', () => { }) describe('submitter in useForm', () => { - test('this should work', async () => { + it('this should work', async () => { const { form, submitter } = useForm({ form: () => ({ field: 0, @@ -110,7 +110,7 @@ describe('submitter in useForm', () => { expect(submitting.value).false }) - test('contains various parameters', async () => { + it('contains various parameters', async () => { const { form, submitter } = useForm({ form: () => ({ field: 0, diff --git a/src/type/form.ts b/src/type/form.ts index 818b640..9109698 100644 --- a/src/type/form.ts +++ b/src/type/form.ts @@ -24,17 +24,15 @@ export interface UseFormParam { * Unless `verify()` or `status[fieldName].verify()` is called manually to validate the rule. * * @default false - * */ + */ lazy?: UseFormLazy } export interface ValidateOptions { fullResult?: FullResult } -export interface ValidateResult { valid: boolean; message: string } +export interface ValidateResult { valid: boolean, message: string } export interface UseFormReturnRuleItem { - validate(value: any): boolean - validate(value: any, options: ValidateOptions): boolean - validate(value: any, options: ValidateOptions): ValidateResult + validate: ((value: any) => boolean) & ((value: any, options: ValidateOptions) => boolean) & ((value: any, options: ValidateOptions) => ValidateResult) } export type UseFormReturnRule = Record, UseFormReturnRuleItem> diff --git a/src/type/util.ts b/src/type/util.ts index 85a4a0d..447bc67 100644 --- a/src/type/util.ts +++ b/src/type/util.ts @@ -1,11 +1,11 @@ export type BaseType = -| null -| undefined -| string -| number -| boolean -| symbol -| bigint + | null + | undefined + | string + | number + | boolean + | symbol + | bigint export type AnyObject = Record export type UnknownObject = Record diff --git a/src/util/deepEqual.ts b/src/util/deepEqual.ts index 42785e9..55095ba 100644 --- a/src/util/deepEqual.ts +++ b/src/util/deepEqual.ts @@ -1,6 +1,6 @@ import { isBaseType, isDateObject } from './is' -export interface DeepEqualReturn { equal: boolean; pointersEqual: boolean } +export interface DeepEqualReturn { equal: boolean, pointersEqual: boolean } export function deepEqual(value1: any, value2: any): DeepEqualReturn { return deepEqualRecursion(value1, value2) diff --git a/src/util/invoke.test.ts b/src/util/invoke.test.ts index c33d31c..00c2496 100644 --- a/src/util/invoke.test.ts +++ b/src/util/invoke.test.ts @@ -1,7 +1,7 @@ -import { expect, test } from 'vitest' +import { expect, it } from 'vitest' import { invoke } from './invoke' -test('invoke', async () => { +it('invoke', async () => { expect(invoke(() => 'hello')).toBe('hello') expect(await invoke(async () => 'wait')).toBe('wait') diff --git a/src/util/invoke.ts b/src/util/invoke.ts index 64a3e3b..590897d 100644 --- a/src/util/invoke.ts +++ b/src/util/invoke.ts @@ -4,5 +4,6 @@ * @param args arguments * @returns result */ -export const invoke = - (func: (...ages: ArgsT) => RetT, ...args: ArgsT) => func(...args) +export function invoke(func: (...ages: ArgsT) => RetT, ...args: ArgsT) { + return func(...args) +} diff --git a/src/util/is.test.ts b/src/util/is.test.ts index 8b3c896..110c9cc 100644 --- a/src/util/is.test.ts +++ b/src/util/is.test.ts @@ -1,7 +1,7 @@ -import { expect, test } from 'vitest' +import { expect, it } from 'vitest' import { isPromise } from './is' -test('isPromise', () => { +it('isPromise', () => { const promise = new Promise(() => 1) expect(isPromise(promise)).toBe(true) diff --git a/src/util/is.ts b/src/util/is.ts index 68acfe1..d12f01a 100644 --- a/src/util/is.ts +++ b/src/util/is.ts @@ -14,15 +14,17 @@ export const isDateObject = (value: unknown): value is Date => value instanceof export const isObjectType = (value: unknown): value is T => typeof value === 'object' -export const isObject = (value: any): value is T => - isObjectType(value) - && !isNullOrUndefined(value) - && !isArray(value) - && !isDateObject(value) - -export const isBaseType = (value: unknown): value is BaseType => - isNullOrUndefined(value) - || !isObjectType(value) +export function isObject(value: any): value is T { + return isObjectType(value) + && !isNullOrUndefined(value) + && !isArray(value) + && !isDateObject(value) +} + +export function isBaseType(value: unknown): value is BaseType { + return isNullOrUndefined(value) + || !isObjectType(value) +} export const isPromise = (obj: unknown) => Promise.resolve(obj) === obj diff --git a/src/util/watchIgnorable.test.ts b/src/util/watchIgnorable.test.ts index 3ddea91..2ca94ee 100644 --- a/src/util/watchIgnorable.test.ts +++ b/src/util/watchIgnorable.test.ts @@ -1,8 +1,8 @@ -import { expect, test } from 'vitest' +import { expect, it } from 'vitest' import { nextTick, ref } from 'vue' import { watchIgnorable } from './watchIgnorable' -test('ignore async updates', async () => { +it('ignore async updates', async () => { const source = ref(0) const target = ref(0) const { ignoreUpdates } = watchIgnorable(source, value => target.value = value) diff --git a/src/util/watchIgnorable.ts b/src/util/watchIgnorable.ts index 166e96c..5ffa0b6 100644 --- a/src/util/watchIgnorable.ts +++ b/src/util/watchIgnorable.ts @@ -21,19 +21,16 @@ export function watchIgnorable = false>( }, { ...options, flush: 'sync' }, ) - const stop = watch(source, - (...args) => { - const ignore = ignoreCount.value > 0 - && ignoreCount.value === syncCount.value + const stop = watch(source, (...args) => { + const ignore = ignoreCount.value > 0 + && ignoreCount.value === syncCount.value - ignoreCount.value = 0 - syncCount.value = 0 + ignoreCount.value = 0 + syncCount.value = 0 - if (!ignore) - cb(...args) - }, - options, - ) + if (!ignore) + cb(...args) + }, options) const ignoreUpdates: IgnoredUpdater = (updater) => { const prev = syncCount.value updater() diff --git a/test/basic.test.ts b/test/basic.test.ts index 0d124d6..7a15f90 100644 --- a/test/basic.test.ts +++ b/test/basic.test.ts @@ -1,6 +1,6 @@ -import { expect, test } from 'vitest' +import { expect, it } from 'vitest' import { message } from '../src/constants' -test('message contains \'Hello\'', () => { +it('message contains \'Hello\'', () => { expect(message).toContain('Hello') }) diff --git a/tsconfig.json b/tsconfig.json index 5a10db6..b99f3a5 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -3,16 +3,16 @@ "target": "es6", "module": "esnext", "moduleResolution": "node", + "paths": { + "~/*": ["./example/src/*"] + }, "strict": true, - "esModuleInterop": true, - "skipLibCheck": true, + "noEmit": true, "allowSyntheticDefaultImports": true, + "esModuleInterop": true, "forceConsistentCasingInFileNames": true, - "noEmit": true, "isolatedModules": true, - "paths": { - "~/*": ["./example/src/*"], - } + "skipLibCheck": true }, "include": ["src/**/*", "test/**/*", "packages/**/*"], "exclude": ["node_modules", "dist"]