Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: replace typeof with type guard #8505

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -905,7 +905,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 @@ -210,7 +210,7 @@ function innerResolveTypeElements(
typeParams
)
} else {
if (typeof typeName === 'string') {
if (isString(typeName)) {
if (typeParameters && typeParameters[typeName]) {
return resolveTypeElements(
ctx,
Expand Down Expand Up @@ -688,7 +688,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 { getEscapedCssVarName } 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 @@ -176,9 +177,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'
import {
ssrProcessTransition,
Expand Down Expand Up @@ -175,7 +175,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 @@ -254,7 +254,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 {
SlotsType,
DefineComponent
} 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