Skip to content

Commit

Permalink
refactor: improve assert and logger
Browse files Browse the repository at this point in the history
  • Loading branch information
haoziqaq committed Dec 7, 2024
1 parent 66862b5 commit eb2813b
Show file tree
Hide file tree
Showing 19 changed files with 73 additions and 85 deletions.
2 changes: 1 addition & 1 deletion packages/varlet-shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"build": "tsup src/index.ts --format esm,cjs --out-dir=lib --dts --clean"
},
"dependencies": {
"rattail": "1.0.14"
"rattail": "1.0.17"
},
"devDependencies": {
"@types/node": "^18.7.18",
Expand Down
23 changes: 22 additions & 1 deletion packages/varlet-shared/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,23 @@
export * from './is'
import { assert as _assert } from 'rattail'

export * from 'rattail'

export function assert(condition: boolean, source: string, message: string): asserts condition {
return _assert(condition, `Varlet [${source}]: ${message}`)
}

export function warn(source: string, message: string): void {
console.warn(`Varlet [${source}]: ${message}`)
}

export function error(source: string, message: string): void {
console.error(`Varlet [${source}]: ${message}`)
}

export const isURL = (val: string | undefined | null) => {
if (!val) {
return false
}

return /^(http)|(\.*\/)/.test(val)
}
7 changes: 0 additions & 7 deletions packages/varlet-shared/src/is.ts

This file was deleted.

6 changes: 2 additions & 4 deletions packages/varlet-ui/src/bottom-navigation-item/provide.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type ComputedRef } from 'vue'
import { useParent } from '@varlet/use'
import { error } from '../utils/logger'
import { assert } from '@varlet/shared'
import {
BOTTOM_NAVIGATION_BIND_BOTTOM_NAVIGATION_ITEM_KEY,
type BottomNavigationProvider,
Expand All @@ -16,9 +16,7 @@ export function useBottomNavigation() {
BOTTOM_NAVIGATION_BIND_BOTTOM_NAVIGATION_ITEM_KEY
)

if (!bindParent) {
error('BottomNavigationItem', '<var-bottom-navigation-item/> must in <var-bottom-navigation/>')
}
assert(!!bindParent, 'BottomNavigationItem', '<var-bottom-navigation-item/> must in <var-bottom-navigation/>')

return {
index,
Expand Down
6 changes: 2 additions & 4 deletions packages/varlet-ui/src/breadcrumb/provide.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { useParent } from '@varlet/use'
import { error } from '../utils/logger'
import { assert } from '@varlet/shared'
import { BREADCRUMBS_BIND_BREADCRUMB_ITEM_KEY, type BreadcrumbsProvider } from '../breadcrumbs/provide'

export function useBreadcrumb() {
const { parentProvider, bindParent, index } = useParent<BreadcrumbsProvider, null>(
BREADCRUMBS_BIND_BREADCRUMB_ITEM_KEY
)

if (!bindParent) {
error('Breadcrumb', '<var-breadcrumb/> must in <var-breadcrumbs/>')
}
assert(!!bindParent, 'Breadcrumb', '<var-breadcrumb/> must in <var-breadcrumbs/>')

return {
index,
Expand Down
6 changes: 2 additions & 4 deletions packages/varlet-ui/src/collapse-item/provide.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useParent } from '@varlet/use'
import { error } from '../utils/logger'
import { assert } from '@varlet/shared'
import { COLLAPSE_BIND_COLLAPSE_ITEM_KEY, CollapseProvider } from '../collapse/provide'
import { type ComputedRef } from 'vue'

Expand All @@ -15,9 +15,7 @@ export function useCollapse() {
COLLAPSE_BIND_COLLAPSE_ITEM_KEY
)

if (!bindParent) {
error('Collapse', '<var-collapse-item/> must in <var-collapse>')
}
assert(!!bindParent, 'Collapse', '<var-collapse-item/> must in <var-collapse>')

return {
index,
Expand Down
14 changes: 9 additions & 5 deletions packages/varlet-ui/src/date-picker/DatePicker.vue
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ import {
type TouchDirection,
type Week,
} from './props'
import { isArray, toNumber, doubleRaf, call } from '@varlet/shared'
import { isArray, toNumber, doubleRaf, call, error } from '@varlet/shared'
import { createNamespace, formatElevation } from '../utils/components'
import { padStart } from '../utils/shared'
import { t } from '../locale'
Expand Down Expand Up @@ -466,21 +466,25 @@ export default defineComponent({
function checkValue() {
if ((props.multiple || props.range) && !isArray(props.modelValue)) {
console.error('[Varlet] DatePicker: type of prop "modelValue" should be an Array')
error('DatePicker', 'type of prop "modelValue" should be an Array')
return false
}
if (!props.multiple && !props.range && isArray(props.modelValue)) {
console.error('[Varlet] DatePicker: type of prop "modelValue" should be a String')
error('DatePicker', 'type of prop "modelValue" should be a String')
return false
}
return true
}
function invalidFormatDate(date: string | Array<string> | undefined): boolean {
if (isArray(date)) return false
if (isArray(date)) {
return false
}
if (date === 'Invalid Date') {
console.error('[Varlet] DatePicker: "modelValue" is an Invalid Date')
error('DatePicker', '"modelValue" is an Invalid Date')
return true
}
Expand Down
6 changes: 2 additions & 4 deletions packages/varlet-ui/src/index-anchor/provide.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ComputedRef } from 'vue'
import { useParent } from '@varlet/use'
import { error } from '../utils/logger'
import { assert } from '@varlet/shared'
import { INDEX_BAR_BIND_INDEX_ANCHOR_KEY, type IndexBarProvider } from '../index-bar/provide'

export interface IndexAnchorProvider {
Expand All @@ -15,9 +15,7 @@ export function useIndexBar() {
INDEX_BAR_BIND_INDEX_ANCHOR_KEY
)

if (!bindParent) {
error('IndexAnchor', 'You should use this component in "IndexBar"')
}
assert(!!bindParent, 'IndexAnchor', 'You should use this component in "IndexBar"')

return {
index,
Expand Down
6 changes: 2 additions & 4 deletions packages/varlet-ui/src/menu-option/provide.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useParent } from '@varlet/use'
import { error } from '../utils/logger'
import { assert } from '@varlet/shared'
import { MENU_SELECT_BIND_MENU_OPTION_KEY, type MenuSelectProvider } from '../menu-select/provide'
import { type OptionProvider } from '../option/provide'

Expand All @@ -10,9 +10,7 @@ export function useMenuSelect() {
MENU_SELECT_BIND_MENU_OPTION_KEY
)

if (!bindParent) {
error('MenuOption', '<var-menu-option/> must in <var-menu-select/>')
}
assert(!!bindParent, 'MenuOption', '<var-menu-option/> must in <var-menu-select/>')

return {
index,
Expand Down
6 changes: 2 additions & 4 deletions packages/varlet-ui/src/option/provide.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type ComputedRef } from 'vue'
import { useParent } from '@varlet/use'
import { error } from '../utils/logger'
import { assert } from '@varlet/shared'
import { SELECT_BIND_OPTION_KEY, type SelectProvider } from '../select/provide'

export interface OptionProvider {
Expand All @@ -13,9 +13,7 @@ export interface OptionProvider {
export function useSelect() {
const { index, parentProvider, bindParent } = useParent<SelectProvider, OptionProvider>(SELECT_BIND_OPTION_KEY)

if (!bindParent) {
error('Option', '<var-option/> must in <var-select/>')
}
assert(!!bindParent, 'Option', '<var-option/> must in <var-select/>')

return {
index,
Expand Down
12 changes: 6 additions & 6 deletions packages/varlet-ui/src/select/Select.vue
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,12 @@ import VarOption from '../option'
import VarFieldDecorator from '../field-decorator'
import VarFormDetails from '../form-details'
import { computed, defineComponent, ref, watch, nextTick } from 'vue'
import { isArray, isEmpty, call, preventDefault, doubleRaf, isFunction } from '@varlet/shared'
import { isArray, isEmpty, call, preventDefault, doubleRaf, isFunction, assert } from '@varlet/shared'
import { props, type SelectValidateTrigger } from './props'
import { useValidation, createNamespace, MaybeVNode } from '../utils/components'
import { useOptions, type SelectProvider } from './provide'
import { useForm } from '../form/provide'
import { focusChildElementByKey, toPxNum } from '../utils/elements'
import { error } from '../utils/logger'
import { useSelectController } from './useSelectController'
import { type OptionProvider } from '../option/provide'
import { useEventListener } from '@varlet/use'
Expand Down Expand Up @@ -222,10 +221,11 @@ export default defineComponent({
watch(
() => props.multiple,
() => {
const { multiple, modelValue } = props
if (multiple && !isArray(modelValue)) {
error('Select', 'The modelValue must be an array when multiple is true')
}
assert(
props.multiple && isArray(props.modelValue),
'Select',
'The modelValue must be an array when multiple is true'
)
}
)
Expand Down
11 changes: 5 additions & 6 deletions packages/varlet-ui/src/slider/Slider.vue
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ import { defineComponent, ref, onBeforeUnmount, computed, reactive, nextTick, wa
import { useValidation, createNamespace } from '../utils/components'
import { useForm } from '../form/provide'
import { getLeft, toSizeUnit } from '../utils/elements'
import { warn } from '../utils/logger'
import { isArray, isNumber, toNumber, getRect, preventDefault, call, hasOwn, clamp } from '@varlet/shared'
import { isArray, isNumber, toNumber, getRect, preventDefault, call, hasOwn, clamp, error, warn } from '@varlet/shared'
import { props, Thumbs, type ThumbProps, type ThumbsProps, type ThumbsListProps } from './props'
import { onSmartMounted, onWindowResize, useEventListener } from '@varlet/use'
import { type SliderProvider } from './provide'
Expand Down Expand Up @@ -449,7 +448,7 @@ export default defineComponent({
function stepValidator() {
if (toNumber(props.step) <= 0) {
warn('[Varlet] Slider', '"step" should be > 0')
warn('Slider', '"step" should be > 0')
return false
}
Expand All @@ -460,17 +459,17 @@ export default defineComponent({
const { range, modelValue } = props
if (range && !isArray(modelValue)) {
console.error('[Varlet] Slider: "modelValue" should be an Array')
error('Slider', '"modelValue" should be an Array')
return false
}
if (!range && isArray(modelValue)) {
console.error('[Varlet] Slider: "modelValue" should be a Number')
error('Slider', '"modelValue" should be a Number')
return false
}
if (range && isArray(modelValue) && modelValue.length < 2) {
console.error('[Varlet] Slider: "modelValue" should have two value')
error('Slider', '"modelValue" should have two value')
return false
}
Expand Down
6 changes: 2 additions & 4 deletions packages/varlet-ui/src/step/provide.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type ComputedRef } from 'vue'
import { useParent } from '@varlet/use'
import { error } from '../utils/logger'
import { assert } from '@varlet/shared'
import { type StepsProvider, STEPS_BIND_STEP_KEY } from '../steps/provide'

export interface StepProvider {
Expand All @@ -10,9 +10,7 @@ export interface StepProvider {
export function useSteps() {
const { parentProvider, index, bindParent } = useParent<StepsProvider, StepProvider>(STEPS_BIND_STEP_KEY)

if (!bindParent) {
error('Steps', '<step/> must in <steps>')
}
assert(!!bindParent, 'Steps', '<step/> must in <steps>')

return {
index,
Expand Down
8 changes: 3 additions & 5 deletions packages/varlet-ui/src/swipe-item/provide.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type ComputedRef } from 'vue'
import { useParent } from '@varlet/use'
import { error } from '../utils/logger'
import { assert } from '@varlet/shared'
import { type ComputedRef } from 'vue'
import { type SwipeProvider, SWIPE_BIND_SWIPE_ITEM_KEY } from '../swipe/provide'

export interface SwipeItemProvider {
Expand All @@ -12,9 +12,7 @@ export interface SwipeItemProvider {
export function useSwipe() {
const { bindParent, index, parentProvider } = useParent<SwipeProvider, SwipeItemProvider>(SWIPE_BIND_SWIPE_ITEM_KEY)

if (!bindParent) {
error('SwipeItem', '<var-swipe-item/> must in <var-swipe/>')
}
assert(!!bindParent, 'SwipeItem', '<var-swipe-item/> must in <var-swipe/>')

return {
index,
Expand Down
6 changes: 2 additions & 4 deletions packages/varlet-ui/src/tab-item/provide.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useChildren, useParent } from '@varlet/use'
import { error } from '../utils/logger'
import { assert } from '@varlet/shared'
import { TABS_ITEMS_BIND_TAB_ITEM_KEY, type TabsItemsProvider } from '../tabs-items/provide'
import { type ComputedRef } from 'vue'
import { type ListProvider } from '../list/provide'
Expand All @@ -18,9 +18,7 @@ export function useTabsItems() {
TABS_ITEMS_BIND_TAB_ITEM_KEY
)

if (!bindParent) {
error('TabItem', '<var-tab-item/> must in <var-tabs-items/>')
}
assert(!!bindParent, 'TabItem', '<var-tab-item/> must in <var-tabs-items/>')

return {
index,
Expand Down
6 changes: 2 additions & 4 deletions packages/varlet-ui/src/tab/provide.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useParent } from '@varlet/use'
import { error } from '../utils/logger'
import { assert } from '@varlet/shared'
import { TABS_BIND_TAB_KEY, type TabsProvider } from '../tabs/provide'
import type { ComputedRef } from 'vue'

Expand All @@ -13,9 +13,7 @@ export interface TabProvider {
export function useTabs() {
const { parentProvider, bindParent, index } = useParent<TabsProvider, TabProvider>(TABS_BIND_TAB_KEY)

if (!bindParent) {
error('Tab', '<var-tab/> must in <var-tabs/>')
}
assert(!!bindParent, 'Tab', '<var-tab/> must in <var-tabs/>')

return {
index,
Expand Down
10 changes: 4 additions & 6 deletions packages/varlet-ui/src/utils/elements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
getScrollLeft,
isNumeric,
} from '@varlet/shared'
import { error } from './logger'
import { assert } from '@varlet/shared'
import { type StyleVars } from '../style-provider'

export function getLeft(element: HTMLElement): number {
Expand Down Expand Up @@ -73,16 +73,14 @@ export function getTarget(target: string | HTMLElement, componentName: string) {
if (isString(target)) {
const el = document.querySelector(target)

if (!el) {
error(componentName, 'target element cannot found')
}
assert(!!el, componentName, 'target element cannot found')

return el as HTMLElement
}

if (isObject(target)) return target
assert(isObject(target), componentName, 'type of prop "target" should be an element object')

error(componentName, 'type of prop "target" should be a selector or an element object')
return target
}

export function getViewportSize() {
Expand Down
7 changes: 0 additions & 7 deletions packages/varlet-ui/src/utils/logger.ts

This file was deleted.

Loading

0 comments on commit eb2813b

Please sign in to comment.