Skip to content

Commit

Permalink
chore: replace typeof with type guard
Browse files Browse the repository at this point in the history
  • Loading branch information
Alfred-Skyblue authored and sxzz committed Aug 21, 2023
1 parent e7d5a41 commit 71c43e8
Show file tree
Hide file tree
Showing 15 changed files with 45 additions and 29 deletions.
3 changes: 2 additions & 1 deletion packages/compiler-core/src/compat/compatConfig.ts
Expand Up @@ -2,6 +2,7 @@ import { SourceLocation } from '../ast'
import { CompilerError } from '../errors'
import { ParserContext } from '../parse'
import { TransformContext } from '../transform'
import { isFunction } from '@vue/shared'

export type CompilerCompatConfig = Partial<
Record<CompilerDeprecationTypes, boolean | 'suppress-warning'>
Expand Down Expand Up @@ -149,7 +150,7 @@ export function warnDeprecation(
}
const { message, link } = deprecationData[key]
const msg = `(deprecation ${key}) ${
typeof message === 'function' ? message(...args) : message
isFunction(message) ? message(...args) : message
}${link ? `\n Details: ${link}` : ``}`

const err = new SyntaxError(msg) as CompilerError
Expand Down
4 changes: 2 additions & 2 deletions packages/compiler-sfc/src/compileScript.ts
Expand Up @@ -6,7 +6,7 @@ import {
} from '@vue/compiler-dom'
import { DEFAULT_FILENAME, SFCDescriptor, SFCScriptBlock } from './parse'
import { ParserPlugin } from '@babel/parser'
import { generateCodeFrame } from '@vue/shared'
import { generateCodeFrame, isString } from '@vue/shared'
import {
Node,
Declaration,
Expand Down Expand Up @@ -901,7 +901,7 @@ export function compileScript(
tips.forEach(warnOnce)
}
const err = errors[0]
if (typeof err === 'string') {
if (isString(err)) {
throw new Error(err)
} else if (err) {
if (err.loc) {
Expand Down
6 changes: 3 additions & 3 deletions packages/compiler-sfc/src/script/resolveType.ts
Expand Up @@ -34,7 +34,7 @@ import {
} from './utils'
import { ScriptCompileContext, resolveParserPlugins } from './context'
import { ImportBinding, SFCScriptCompileOptions } from '../compileScript'
import { capitalize, hasOwn } from '@vue/shared'
import { capitalize, hasOwn, isString } from '@vue/shared'
import { parse as babelParse } from '@babel/parser'
import { parse } from '../parse'
import { createCache } from '../cache'
Expand Down Expand Up @@ -178,7 +178,7 @@ function innerResolveTypeElements(
if (resolved) {
return resolveTypeElements(ctx, resolved, resolved._ownerScope)
} else {
if (typeof typeName === 'string') {
if (isString(typeName)) {
if (
// @ts-ignore
SupportedBuiltinsSet.has(typeName)
Expand Down Expand Up @@ -621,7 +621,7 @@ function innerResolveTypeReference(
node: ReferenceTypes,
onlyExported: boolean
): ScopeTypeNode | undefined {
if (typeof name === 'string') {
if (isString(name)) {
if (scope.imports[name]) {
return resolveTypeFromImport(ctx, node, name, scope)
} else {
Expand Down
5 changes: 2 additions & 3 deletions packages/compiler-sfc/src/script/utils.ts
Expand Up @@ -10,6 +10,7 @@ import {
} from '@babel/types'
import path from 'path'
import { TS_NODE_TYPES } from '@vue/compiler-dom'
import { isString } from '@vue/shared'

export const UNKNOWN_TYPE = 'Unknown'

Expand Down Expand Up @@ -49,9 +50,7 @@ export function isCallOf(
test &&
node.type === 'CallExpression' &&
node.callee.type === 'Identifier' &&
(typeof test === 'string'
? node.callee.name === test
: test(node.callee.name))
(isString(test) ? node.callee.name === test : test(node.callee.name))
)
}

Expand Down
5 changes: 2 additions & 3 deletions packages/compiler-sfc/src/style/cssVars.ts
Expand Up @@ -11,6 +11,7 @@ import { SFCDescriptor } from '../parse'
import { escapeSymbolsRE } from '../script/utils'
import { PluginCreator } from 'postcss'
import hash from 'hash-sum'
import { isString } from '@vue/shared'

export const CSS_VARS_HELPER = `useCssVars`

Expand Down Expand Up @@ -168,9 +169,7 @@ export function genCssVarsCode(
? transformed.content
: transformed.children
.map(c => {
return typeof c === 'string'
? c
: (c as SimpleExpressionNode).content
return isString(c) ? c : (c as SimpleExpressionNode).content
})
.join('')

Expand Down
6 changes: 3 additions & 3 deletions packages/compiler-ssr/src/transforms/ssrTransformComponent.ts
Expand Up @@ -54,7 +54,7 @@ import {
ssrProcessTransitionGroup,
ssrTransformTransitionGroup
} from './ssrTransformTransitionGroup'
import { isSymbol, isObject, isArray } from '@vue/shared'
import { isSymbol, isObject, isArray, isString } from '@vue/shared'
import { buildSSRProps } from './ssrTransformElement'

// We need to construct the slot functions in the 1st pass to ensure proper
Expand Down Expand Up @@ -168,7 +168,7 @@ export const ssrTransformComponent: NodeTransform = (node, context) => {
? buildSlots(node, context, buildSSRSlotFn).slots
: `null`

if (typeof component !== 'string') {
if (!isString(component)) {
// dynamic component that resolved to a `resolveDynamicComponent` call
// expression - since the resolved result may be a plain element (string)
// or a VNode, handle it with `renderVNode`.
Expand Down Expand Up @@ -248,7 +248,7 @@ export function ssrProcessComponent(
node.ssrCodegenNode.arguments.push(`_scopeId`)
}

if (typeof component === 'string') {
if (isString(component)) {
// static component
context.pushStatement(
createCallExpression(`_push`, [node.ssrCodegenNode])
Expand Down
3 changes: 2 additions & 1 deletion packages/compiler-ssr/src/transforms/ssrTransformElement.ts
Expand Up @@ -36,6 +36,7 @@ import {
isBooleanAttr,
isBuiltInDirective,
isSSRSafeAttrName,
isString,
NO,
propsToAttrMap
} from '@vue/shared'
Expand Down Expand Up @@ -393,7 +394,7 @@ function removeStaticBinding(
) {
const regExp = new RegExp(`^ ${binding}=".+"$`)

const i = tag.findIndex(e => typeof e === 'string' && regExp.test(e))
const i = tag.findIndex(e => isString(e) && regExp.test(e))

if (i > -1) {
tag.splice(i, 1)
Expand Down
2 changes: 1 addition & 1 deletion packages/reactivity-transform/src/reactivityTransform.ts
Expand Up @@ -537,7 +537,7 @@ export function transformAST(
function segToString(seg: PathSegmentAtom): string {
if (typeof seg === 'number') {
return `[${seg}]`
} else if (typeof seg === 'string') {
} else if (isString(seg)) {
return `.${seg}`
} else {
return snip(seg)
Expand Down
6 changes: 3 additions & 3 deletions packages/runtime-core/src/compat/compatConfig.ts
Expand Up @@ -480,9 +480,9 @@ export function warnDeprecation(

const { message, link } = deprecationData[key]
warn(
`(deprecation ${key}) ${
typeof message === 'function' ? message(...args) : message
}${link ? `\n Details: ${link}` : ``}`
`(deprecation ${key}) ${isFunction(message) ? message(...args) : message}${
link ? `\n Details: ${link}` : ``
}`
)
if (!isCompatEnabled(key, instance, true)) {
console.error(
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime-core/src/compat/global.ts
Expand Up @@ -487,7 +487,7 @@ function installCompatMount(
}

let container: Element
if (typeof selectorOrEl === 'string') {
if (isString(selectorOrEl)) {
// eslint-disable-next-line
const result = document.querySelector(selectorOrEl)
if (!result) {
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime-core/src/compat/renderFn.ts
Expand Up @@ -127,7 +127,7 @@ export function compatH(
}

// to support v2 string component name look!up
if (typeof type === 'string') {
if (isString(type)) {
const t = hyphenate(type)
if (t === 'transition' || t === 'transition-group' || t === 'keep-alive') {
// since transition and transition-group are runtime-dom-specific,
Expand Down
5 changes: 3 additions & 2 deletions packages/runtime-core/src/compat/renderHelpers.ts
Expand Up @@ -5,6 +5,7 @@ import {
isArray,
isObject,
isReservedProp,
isString,
normalizeClass
} from '@vue/shared'
import { ComponentInternalInstance } from '../component'
Expand Down Expand Up @@ -170,13 +171,13 @@ export function legacyMarkOnce(tree: VNode) {
export function legacyBindDynamicKeys(props: any, values: any[]) {
for (let i = 0; i < values.length; i += 2) {
const key = values[i]
if (typeof key === 'string' && key) {
if (isString(key) && key) {
props[values[i]] = values[i + 1]
}
}
return props
}

export function legacyPrependModifier(value: any, symbol: string) {
return typeof value === 'string' ? symbol + value : value
return isString(value) ? symbol + value : value
}
11 changes: 9 additions & 2 deletions packages/runtime-core/src/customFormatter.ts
@@ -1,5 +1,12 @@
import { isReactive, isReadonly, isRef, Ref, toRaw } from '@vue/reactivity'
import { EMPTY_OBJ, extend, isArray, isFunction, isObject } from '@vue/shared'
import {
EMPTY_OBJ,
extend,
isArray,
isFunction,
isObject,
isString
} from '@vue/shared'
import { isShallow } from '../../reactivity/src/reactive'
import { ComponentInternalInstance, ComponentOptions } from './component'
import { ComponentPublicInstance } from './componentPublicInstance'
Expand Down Expand Up @@ -140,7 +147,7 @@ export function initCustomFormatter() {
function formatValue(v: unknown, asRaw = true) {
if (typeof v === 'number') {
return ['span', numberStyle, v]
} else if (typeof v === 'string') {
} else if (isString(v)) {
return ['span', stringStyle, JSON.stringify(v)]
} else if (typeof v === 'boolean') {
return ['span', keywordStyle, v]
Expand Down
3 changes: 2 additions & 1 deletion packages/runtime-core/src/devtools.ts
Expand Up @@ -2,6 +2,7 @@
import { App } from './apiCreateApp'
import { Fragment, Text, Comment, Static } from './vnode'
import { ComponentInternalInstance } from './component'
import { isFunction } from '@vue/shared'

interface AppRecord {
id: number
Expand Down Expand Up @@ -115,7 +116,7 @@ export const devtoolsComponentRemoved = (
) => {
if (
devtools &&
typeof devtools.cleanupBuffer === 'function' &&
isFunction(devtools.cleanupBuffer) &&
// remove the component if it wasn't buffered
!devtools.cleanupBuffer(component)
) {
Expand Down
11 changes: 9 additions & 2 deletions packages/runtime-dom/src/apiCustomElement.ts
Expand Up @@ -23,7 +23,14 @@ import {
ComponentInjectOptions,
SlotsType
} from '@vue/runtime-core'
import { camelize, extend, hyphenate, isArray, toNumber } from '@vue/shared'
import {
camelize,
extend,
hyphenate,
isArray,
isString,
toNumber
} from '@vue/shared'
import { hydrate, render } from '.'

export type VueElementConstructor<P = {}> = {
Expand Down Expand Up @@ -346,7 +353,7 @@ export class VueElement extends BaseClass {
if (shouldReflect) {
if (val === true) {
this.setAttribute(hyphenate(key), '')
} else if (typeof val === 'string' || typeof val === 'number') {
} else if (isString(val) || typeof val === 'number') {
this.setAttribute(hyphenate(key), val + '')
} else if (!val) {
this.removeAttribute(hyphenate(key))
Expand Down

0 comments on commit 71c43e8

Please sign in to comment.