From 617a979cabba93b8858e6a53c4cb02ee1f14cd9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=B6=E8=BF=9C=E6=96=B9?= Date: Mon, 5 Jun 2023 21:50:51 +0800 Subject: [PATCH] chore: replace typeof with type guard --- packages/compiler-core/src/compat/compatConfig.ts | 3 ++- packages/compiler-sfc/src/compileScript.ts | 4 ++-- packages/compiler-sfc/src/script/resolveType.ts | 6 +++--- packages/compiler-sfc/src/script/utils.ts | 5 ++--- packages/compiler-sfc/src/style/cssVars.ts | 5 ++--- .../src/transforms/ssrTransformComponent.ts | 6 +++--- .../src/transforms/ssrTransformElement.ts | 3 ++- .../reactivity-transform/src/reactivityTransform.ts | 2 +- packages/runtime-core/src/compat/compatConfig.ts | 6 +++--- packages/runtime-core/src/compat/global.ts | 2 +- packages/runtime-core/src/compat/renderFn.ts | 2 +- packages/runtime-core/src/compat/renderHelpers.ts | 5 +++-- packages/runtime-core/src/customFormatter.ts | 11 +++++++++-- packages/runtime-core/src/devtools.ts | 3 ++- packages/runtime-dom/src/apiCustomElement.ts | 11 +++++++++-- 15 files changed, 45 insertions(+), 29 deletions(-) diff --git a/packages/compiler-core/src/compat/compatConfig.ts b/packages/compiler-core/src/compat/compatConfig.ts index dcb304263b4..2886f95cb6c 100644 --- a/packages/compiler-core/src/compat/compatConfig.ts +++ b/packages/compiler-core/src/compat/compatConfig.ts @@ -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 @@ -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 diff --git a/packages/compiler-sfc/src/compileScript.ts b/packages/compiler-sfc/src/compileScript.ts index 6f096ff3e04..0e783260166 100644 --- a/packages/compiler-sfc/src/compileScript.ts +++ b/packages/compiler-sfc/src/compileScript.ts @@ -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, @@ -886,7 +886,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) { diff --git a/packages/compiler-sfc/src/script/resolveType.ts b/packages/compiler-sfc/src/script/resolveType.ts index d30150a82d5..0da1b1272a2 100644 --- a/packages/compiler-sfc/src/script/resolveType.ts +++ b/packages/compiler-sfc/src/script/resolveType.ts @@ -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' @@ -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) @@ -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 { diff --git a/packages/compiler-sfc/src/script/utils.ts b/packages/compiler-sfc/src/script/utils.ts index 42c4718e3a8..63234fb5e65 100644 --- a/packages/compiler-sfc/src/script/utils.ts +++ b/packages/compiler-sfc/src/script/utils.ts @@ -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' @@ -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)) ) } diff --git a/packages/compiler-sfc/src/style/cssVars.ts b/packages/compiler-sfc/src/style/cssVars.ts index c86dbf2a5b9..d33b014eeb0 100644 --- a/packages/compiler-sfc/src/style/cssVars.ts +++ b/packages/compiler-sfc/src/style/cssVars.ts @@ -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` @@ -167,9 +168,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('') diff --git a/packages/compiler-ssr/src/transforms/ssrTransformComponent.ts b/packages/compiler-ssr/src/transforms/ssrTransformComponent.ts index dc8c6a4ae4f..4da96055d23 100644 --- a/packages/compiler-ssr/src/transforms/ssrTransformComponent.ts +++ b/packages/compiler-ssr/src/transforms/ssrTransformComponent.ts @@ -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 @@ -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`. @@ -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]) diff --git a/packages/compiler-ssr/src/transforms/ssrTransformElement.ts b/packages/compiler-ssr/src/transforms/ssrTransformElement.ts index 2c42a83ba37..62794b3868d 100644 --- a/packages/compiler-ssr/src/transforms/ssrTransformElement.ts +++ b/packages/compiler-ssr/src/transforms/ssrTransformElement.ts @@ -36,6 +36,7 @@ import { isBooleanAttr, isBuiltInDirective, isSSRSafeAttrName, + isString, NO, propsToAttrMap } from '@vue/shared' @@ -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) diff --git a/packages/reactivity-transform/src/reactivityTransform.ts b/packages/reactivity-transform/src/reactivityTransform.ts index c0e2d1b6897..b29a01ddf04 100644 --- a/packages/reactivity-transform/src/reactivityTransform.ts +++ b/packages/reactivity-transform/src/reactivityTransform.ts @@ -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) diff --git a/packages/runtime-core/src/compat/compatConfig.ts b/packages/runtime-core/src/compat/compatConfig.ts index 88b58fdfab0..24398b47a04 100644 --- a/packages/runtime-core/src/compat/compatConfig.ts +++ b/packages/runtime-core/src/compat/compatConfig.ts @@ -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( diff --git a/packages/runtime-core/src/compat/global.ts b/packages/runtime-core/src/compat/global.ts index 9f1a6d1cd12..703a2e091bd 100644 --- a/packages/runtime-core/src/compat/global.ts +++ b/packages/runtime-core/src/compat/global.ts @@ -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) { diff --git a/packages/runtime-core/src/compat/renderFn.ts b/packages/runtime-core/src/compat/renderFn.ts index 4cf2fc226cf..f4b0bed6e6d 100644 --- a/packages/runtime-core/src/compat/renderFn.ts +++ b/packages/runtime-core/src/compat/renderFn.ts @@ -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, diff --git a/packages/runtime-core/src/compat/renderHelpers.ts b/packages/runtime-core/src/compat/renderHelpers.ts index aa156c11a40..0db7273a219 100644 --- a/packages/runtime-core/src/compat/renderHelpers.ts +++ b/packages/runtime-core/src/compat/renderHelpers.ts @@ -5,6 +5,7 @@ import { isArray, isObject, isReservedProp, + isString, normalizeClass } from '@vue/shared' import { ComponentInternalInstance } from '../component' @@ -170,7 +171,7 @@ 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] } } @@ -178,5 +179,5 @@ export function legacyBindDynamicKeys(props: any, values: any[]) { } export function legacyPrependModifier(value: any, symbol: string) { - return typeof value === 'string' ? symbol + value : value + return isString(value) ? symbol + value : value } diff --git a/packages/runtime-core/src/customFormatter.ts b/packages/runtime-core/src/customFormatter.ts index 768240feb62..a95323c26be 100644 --- a/packages/runtime-core/src/customFormatter.ts +++ b/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' @@ -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] diff --git a/packages/runtime-core/src/devtools.ts b/packages/runtime-core/src/devtools.ts index f05128d47aa..4b37a9f4acf 100644 --- a/packages/runtime-core/src/devtools.ts +++ b/packages/runtime-core/src/devtools.ts @@ -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 @@ -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) ) { diff --git a/packages/runtime-dom/src/apiCustomElement.ts b/packages/runtime-dom/src/apiCustomElement.ts index 1e551cc05da..6612ff697c6 100644 --- a/packages/runtime-dom/src/apiCustomElement.ts +++ b/packages/runtime-dom/src/apiCustomElement.ts @@ -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

= { @@ -338,7 +345,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))