Skip to content

Commit 617a979

Browse files
chore: replace typeof with type guard
1 parent 020851e commit 617a979

File tree

15 files changed

+45
-29
lines changed

15 files changed

+45
-29
lines changed

Diff for: packages/compiler-core/src/compat/compatConfig.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { SourceLocation } from '../ast'
22
import { CompilerError } from '../errors'
33
import { ParserContext } from '../parse'
44
import { TransformContext } from '../transform'
5+
import { isFunction } from '@vue/shared'
56

67
export type CompilerCompatConfig = Partial<
78
Record<CompilerDeprecationTypes, boolean | 'suppress-warning'>
@@ -149,7 +150,7 @@ export function warnDeprecation(
149150
}
150151
const { message, link } = deprecationData[key]
151152
const msg = `(deprecation ${key}) ${
152-
typeof message === 'function' ? message(...args) : message
153+
isFunction(message) ? message(...args) : message
153154
}${link ? `\n Details: ${link}` : ``}`
154155

155156
const err = new SyntaxError(msg) as CompilerError

Diff for: packages/compiler-sfc/src/compileScript.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
} from '@vue/compiler-dom'
77
import { DEFAULT_FILENAME, SFCDescriptor, SFCScriptBlock } from './parse'
88
import { ParserPlugin } from '@babel/parser'
9-
import { generateCodeFrame } from '@vue/shared'
9+
import { generateCodeFrame, isString } from '@vue/shared'
1010
import {
1111
Node,
1212
Declaration,
@@ -886,7 +886,7 @@ export function compileScript(
886886
tips.forEach(warnOnce)
887887
}
888888
const err = errors[0]
889-
if (typeof err === 'string') {
889+
if (isString(err)) {
890890
throw new Error(err)
891891
} else if (err) {
892892
if (err.loc) {

Diff for: packages/compiler-sfc/src/script/resolveType.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import {
3434
} from './utils'
3535
import { ScriptCompileContext, resolveParserPlugins } from './context'
3636
import { ImportBinding, SFCScriptCompileOptions } from '../compileScript'
37-
import { capitalize, hasOwn } from '@vue/shared'
37+
import { capitalize, hasOwn, isString } from '@vue/shared'
3838
import { parse as babelParse } from '@babel/parser'
3939
import { parse } from '../parse'
4040
import { createCache } from '../cache'
@@ -178,7 +178,7 @@ function innerResolveTypeElements(
178178
if (resolved) {
179179
return resolveTypeElements(ctx, resolved, resolved._ownerScope)
180180
} else {
181-
if (typeof typeName === 'string') {
181+
if (isString(typeName)) {
182182
if (
183183
// @ts-ignore
184184
SupportedBuiltinsSet.has(typeName)
@@ -621,7 +621,7 @@ function innerResolveTypeReference(
621621
node: ReferenceTypes,
622622
onlyExported: boolean
623623
): ScopeTypeNode | undefined {
624-
if (typeof name === 'string') {
624+
if (isString(name)) {
625625
if (scope.imports[name]) {
626626
return resolveTypeFromImport(ctx, node, name, scope)
627627
} else {

Diff for: packages/compiler-sfc/src/script/utils.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
} from '@babel/types'
1111
import path from 'path'
1212
import { TS_NODE_TYPES } from '@vue/compiler-dom'
13+
import { isString } from '@vue/shared'
1314

1415
export const UNKNOWN_TYPE = 'Unknown'
1516

@@ -49,9 +50,7 @@ export function isCallOf(
4950
test &&
5051
node.type === 'CallExpression' &&
5152
node.callee.type === 'Identifier' &&
52-
(typeof test === 'string'
53-
? node.callee.name === test
54-
: test(node.callee.name))
53+
(isString(test) ? node.callee.name === test : test(node.callee.name))
5554
)
5655
}
5756

Diff for: packages/compiler-sfc/src/style/cssVars.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { SFCDescriptor } from '../parse'
1111
import { escapeSymbolsRE } from '../script/utils'
1212
import { PluginCreator } from 'postcss'
1313
import hash from 'hash-sum'
14+
import { isString } from '@vue/shared'
1415

1516
export const CSS_VARS_HELPER = `useCssVars`
1617

@@ -167,9 +168,7 @@ export function genCssVarsCode(
167168
? transformed.content
168169
: transformed.children
169170
.map(c => {
170-
return typeof c === 'string'
171-
? c
172-
: (c as SimpleExpressionNode).content
171+
return isString(c) ? c : (c as SimpleExpressionNode).content
173172
})
174173
.join('')
175174

Diff for: packages/compiler-ssr/src/transforms/ssrTransformComponent.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ import {
5454
ssrProcessTransitionGroup,
5555
ssrTransformTransitionGroup
5656
} from './ssrTransformTransitionGroup'
57-
import { isSymbol, isObject, isArray } from '@vue/shared'
57+
import { isSymbol, isObject, isArray, isString } from '@vue/shared'
5858
import { buildSSRProps } from './ssrTransformElement'
5959

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

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

251-
if (typeof component === 'string') {
251+
if (isString(component)) {
252252
// static component
253253
context.pushStatement(
254254
createCallExpression(`_push`, [node.ssrCodegenNode])

Diff for: packages/compiler-ssr/src/transforms/ssrTransformElement.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import {
3636
isBooleanAttr,
3737
isBuiltInDirective,
3838
isSSRSafeAttrName,
39+
isString,
3940
NO,
4041
propsToAttrMap
4142
} from '@vue/shared'
@@ -393,7 +394,7 @@ function removeStaticBinding(
393394
) {
394395
const regExp = new RegExp(`^ ${binding}=".+"$`)
395396

396-
const i = tag.findIndex(e => typeof e === 'string' && regExp.test(e))
397+
const i = tag.findIndex(e => isString(e) && regExp.test(e))
397398

398399
if (i > -1) {
399400
tag.splice(i, 1)

Diff for: packages/reactivity-transform/src/reactivityTransform.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ export function transformAST(
537537
function segToString(seg: PathSegmentAtom): string {
538538
if (typeof seg === 'number') {
539539
return `[${seg}]`
540-
} else if (typeof seg === 'string') {
540+
} else if (isString(seg)) {
541541
return `.${seg}`
542542
} else {
543543
return snip(seg)

Diff for: packages/runtime-core/src/compat/compatConfig.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -480,9 +480,9 @@ export function warnDeprecation(
480480

481481
const { message, link } = deprecationData[key]
482482
warn(
483-
`(deprecation ${key}) ${
484-
typeof message === 'function' ? message(...args) : message
485-
}${link ? `\n Details: ${link}` : ``}`
483+
`(deprecation ${key}) ${isFunction(message) ? message(...args) : message}${
484+
link ? `\n Details: ${link}` : ``
485+
}`
486486
)
487487
if (!isCompatEnabled(key, instance, true)) {
488488
console.error(

Diff for: packages/runtime-core/src/compat/global.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ function installCompatMount(
487487
}
488488

489489
let container: Element
490-
if (typeof selectorOrEl === 'string') {
490+
if (isString(selectorOrEl)) {
491491
// eslint-disable-next-line
492492
const result = document.querySelector(selectorOrEl)
493493
if (!result) {

Diff for: packages/runtime-core/src/compat/renderFn.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ export function compatH(
127127
}
128128

129129
// to support v2 string component name look!up
130-
if (typeof type === 'string') {
130+
if (isString(type)) {
131131
const t = hyphenate(type)
132132
if (t === 'transition' || t === 'transition-group' || t === 'keep-alive') {
133133
// since transition and transition-group are runtime-dom-specific,

Diff for: packages/runtime-core/src/compat/renderHelpers.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
isArray,
66
isObject,
77
isReservedProp,
8+
isString,
89
normalizeClass
910
} from '@vue/shared'
1011
import { ComponentInternalInstance } from '../component'
@@ -170,13 +171,13 @@ export function legacyMarkOnce(tree: VNode) {
170171
export function legacyBindDynamicKeys(props: any, values: any[]) {
171172
for (let i = 0; i < values.length; i += 2) {
172173
const key = values[i]
173-
if (typeof key === 'string' && key) {
174+
if (isString(key) && key) {
174175
props[values[i]] = values[i + 1]
175176
}
176177
}
177178
return props
178179
}
179180

180181
export function legacyPrependModifier(value: any, symbol: string) {
181-
return typeof value === 'string' ? symbol + value : value
182+
return isString(value) ? symbol + value : value
182183
}

Diff for: packages/runtime-core/src/customFormatter.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import { isReactive, isReadonly, isRef, Ref, toRaw } from '@vue/reactivity'
2-
import { EMPTY_OBJ, extend, isArray, isFunction, isObject } from '@vue/shared'
2+
import {
3+
EMPTY_OBJ,
4+
extend,
5+
isArray,
6+
isFunction,
7+
isObject,
8+
isString
9+
} from '@vue/shared'
310
import { isShallow } from '../../reactivity/src/reactive'
411
import { ComponentInternalInstance, ComponentOptions } from './component'
512
import { ComponentPublicInstance } from './componentPublicInstance'
@@ -140,7 +147,7 @@ export function initCustomFormatter() {
140147
function formatValue(v: unknown, asRaw = true) {
141148
if (typeof v === 'number') {
142149
return ['span', numberStyle, v]
143-
} else if (typeof v === 'string') {
150+
} else if (isString(v)) {
144151
return ['span', stringStyle, JSON.stringify(v)]
145152
} else if (typeof v === 'boolean') {
146153
return ['span', keywordStyle, v]

Diff for: packages/runtime-core/src/devtools.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import { App } from './apiCreateApp'
33
import { Fragment, Text, Comment, Static } from './vnode'
44
import { ComponentInternalInstance } from './component'
5+
import { isFunction } from '@vue/shared'
56

67
interface AppRecord {
78
id: number
@@ -115,7 +116,7 @@ export const devtoolsComponentRemoved = (
115116
) => {
116117
if (
117118
devtools &&
118-
typeof devtools.cleanupBuffer === 'function' &&
119+
isFunction(devtools.cleanupBuffer) &&
119120
// remove the component if it wasn't buffered
120121
!devtools.cleanupBuffer(component)
121122
) {

Diff for: packages/runtime-dom/src/apiCustomElement.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,14 @@ import {
2323
ComponentInjectOptions,
2424
SlotsType
2525
} from '@vue/runtime-core'
26-
import { camelize, extend, hyphenate, isArray, toNumber } from '@vue/shared'
26+
import {
27+
camelize,
28+
extend,
29+
hyphenate,
30+
isArray,
31+
isString,
32+
toNumber
33+
} from '@vue/shared'
2734
import { hydrate, render } from '.'
2835

2936
export type VueElementConstructor<P = {}> = {
@@ -338,7 +345,7 @@ export class VueElement extends BaseClass {
338345
if (shouldReflect) {
339346
if (val === true) {
340347
this.setAttribute(hyphenate(key), '')
341-
} else if (typeof val === 'string' || typeof val === 'number') {
348+
} else if (isString(val) || typeof val === 'number') {
342349
this.setAttribute(hyphenate(key), val + '')
343350
} else if (!val) {
344351
this.removeAttribute(hyphenate(key))

0 commit comments

Comments
 (0)