Skip to content

feat: use encapsulated api #11220

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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/compiler-sfc/src/compileScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
type SFCScriptBlock,
} from './parse'
import type { ParserPlugin } from '@babel/parser'
import { generateCodeFrame } from '@vue/shared'
import { generateCodeFrame, isString } from '@vue/shared'
import type {
ArrayPattern,
CallExpression,
Expand Down Expand Up @@ -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) {
Expand Down
9 changes: 4 additions & 5 deletions packages/compiler-sfc/src/script/context.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { CallExpression, Node, ObjectPattern, Program } from '@babel/types'
import type { SFCDescriptor } from '../parse'
import { generateCodeFrame, isArray } from '@vue/shared'
import { generateCodeFrame, isArray, isBoolean } from '@vue/shared'
import { type ParserPlugin, parse as babelParse } from '@babel/parser'
import type { ImportBinding, SFCScriptCompileOptions } from '../compileScript'
import type { PropsDestructureBindings } from './defineProps'
Expand Down Expand Up @@ -99,10 +99,9 @@ export class ScriptCompileContext {
const customElement = options.customElement
const filename = this.descriptor.filename
if (customElement) {
this.isCE =
typeof customElement === 'boolean'
? customElement
: customElement(filename)
this.isCE = isBoolean(customElement)
? customElement
: customElement(filename)
}
// resolve parser plugins
const plugins: ParserPlugin[] = resolveParserPlugins(
Expand Down
11 changes: 4 additions & 7 deletions packages/compiler-sfc/src/script/resolveType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import {
} from './utils'
import { type ScriptCompileContext, resolveParserPlugins } from './context'
import type { 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 @@ -244,7 +244,7 @@ function innerResolveTypeElements(
typeParams,
)
} else {
if (typeof typeName === 'string') {
if (isString(typeName)) {
if (typeParameters && typeParameters[typeName]) {
return resolveTypeElements(
ctx,
Expand Down Expand Up @@ -729,7 +729,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 Expand Up @@ -827,10 +827,7 @@ export function registerTS(_loadTS: () => typeof TS) {
try {
return _loadTS()
} catch (err: any) {
if (
typeof err.message === 'string' &&
err.message.includes('Cannot find module')
) {
if (isString(err.message) && err.message.includes('Cannot find module')) {
throw new Error(
'Failed to load TypeScript, which is required for resolving imported types. ' +
'Please make sure "typescript" is installed as a project dependency.',
Expand Down
5 changes: 2 additions & 3 deletions packages/compiler-sfc/src/script/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
Node,
StringLiteral,
} from '@babel/types'
import { isString } from '@vue/shared'
import path from 'path'

export const UNKNOWN_TYPE = 'Unknown'
Expand Down Expand Up @@ -40,9 +41,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
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type { SFCDescriptor } from '../parse'
import { getEscapedCssVarName } from '../script/utils'
import type { 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
3 changes: 2 additions & 1 deletion packages/compiler-ssr/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { ssrTransformModel } from './transforms/ssrVModel'
import { ssrTransformShow } from './transforms/ssrVShow'
import { ssrInjectFallthroughAttrs } from './transforms/ssrInjectFallthroughAttrs'
import { ssrInjectCssVars } from './transforms/ssrInjectCssVars'
import { isString } from '@vue/shared'

export function compile(
source: string | RootNode,
Expand All @@ -45,7 +46,7 @@ export function compile(
hoistStatic: false,
}

const ast = typeof source === 'string' ? baseParse(source, options) : source
const ast = isString(source) ? baseParse(source, options) : source

// Save raw options for AST. This is needed when performing sub-transforms
// on slot vnode branches.
Expand Down
11 changes: 9 additions & 2 deletions packages/compiler-ssr/src/transforms/ssrTransformComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,14 @@ import {
ssrProcessTransitionGroup,
ssrTransformTransitionGroup,
} from './ssrTransformTransitionGroup'
import { extend, isArray, isObject, isPlainObject, isSymbol } from '@vue/shared'
import {
extend,
isArray,
isObject,
isPlainObject,
isString,
isSymbol,
} from '@vue/shared'
import { buildSSRProps } from './ssrTransformElement'
import {
ssrProcessTransition,
Expand Down Expand Up @@ -255,7 +262,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
2 changes: 1 addition & 1 deletion packages/runtime-core/src/compat/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
isArray,
isObject,
isReservedProp,
isString,
normalizeClass,
} from '@vue/shared'
import type { 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
}
10 changes: 8 additions & 2 deletions packages/runtime-core/src/components/Suspense.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ import {
normalizeVNode,
openBlock,
} from '../vnode'
import { ShapeFlags, isArray, isFunction, toNumber } from '@vue/shared'
import {
ShapeFlags,
isArray,
isFunction,
isNumber,
toNumber,
} from '@vue/shared'
import { type ComponentInternalInstance, handleSetupResult } from '../component'
import type { Slots } from '../componentSlots'
import {
Expand Down Expand Up @@ -500,7 +506,7 @@ function createSuspenseBoundary(
hiddenContainer,
deps: 0,
pendingId: suspenseId++,
timeout: typeof timeout === 'number' ? timeout : -1,
timeout: isNumber(timeout) ? timeout : -1,
activeBranch: null,
pendingBranch: null,
isInFallback: !isHydrating,
Expand Down
17 changes: 13 additions & 4 deletions packages/runtime-core/src/customFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,16 @@ import {
isShallow,
toRaw,
} from '@vue/reactivity'
import { EMPTY_OBJ, extend, isArray, isFunction, isObject } from '@vue/shared'
import {
EMPTY_OBJ,
extend,
isArray,
isBoolean,
isFunction,
isNumber,
isObject,
isString,
} from '@vue/shared'
import type { ComponentInternalInstance, ComponentOptions } from './component'
import type { ComponentPublicInstance } from './componentPublicInstance'

Expand Down Expand Up @@ -144,11 +153,11 @@ export function initCustomFormatter() {
}

function formatValue(v: unknown, asRaw = true) {
if (typeof v === 'number') {
if (isNumber(v)) {
return ['span', numberStyle, v]
} else if (typeof v === 'string') {
} else if (isString(v)) {
return ['span', stringStyle, JSON.stringify(v)]
} else if (typeof v === 'boolean') {
} else if (isBoolean(v)) {
return ['span', keywordStyle, v]
} else if (isObject(v)) {
return ['object', { object: asRaw ? toRaw(v) : v }]
Expand Down
4 changes: 2 additions & 2 deletions packages/runtime-core/src/helpers/renderList.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { VNode, VNodeChild } from '../vnode'
import { isArray, isObject, isString } from '@vue/shared'
import { isArray, isNumber, isObject, isString } from '@vue/shared'
import { warn } from '../warning'

/**
Expand Down Expand Up @@ -64,7 +64,7 @@ export function renderList(
for (let i = 0, l = source.length; i < l; i++) {
ret[i] = renderItem(source[i], i, undefined, cached && cached[i])
}
} else if (typeof source === 'number') {
} else if (isNumber(source)) {
if (__DEV__ && !Number.isInteger(source)) {
warn(`The v-for range expect an integer value but got ${source}.`)
}
Expand Down
6 changes: 4 additions & 2 deletions packages/runtime-core/src/vnode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import {
SlotFlags,
extend,
isArray,
isBoolean,
isFunction,
isNumber,
isObject,
isOn,
isString,
Expand Down Expand Up @@ -420,7 +422,7 @@ const normalizeRef = ({
ref_key,
ref_for,
}: VNodeProps): VNodeNormalizedRefAtom | null => {
if (typeof ref === 'number') {
if (isNumber(ref)) {
ref = '' + ref
}
return (
Expand Down Expand Up @@ -759,7 +761,7 @@ export function createCommentVNode(
}

export function normalizeVNode(child: VNodeChild): VNode {
if (child == null || typeof child === 'boolean') {
if (child == null || isBoolean(child)) {
// empty placeholder
return createVNode(Comment)
} else if (isArray(child)) {
Expand Down
8 changes: 2 additions & 6 deletions packages/runtime-core/src/warning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
type Data,
formatComponentName,
} from './component'
import { isFunction, isString } from '@vue/shared'
import { isBoolean, isFunction, isNumber, isString } from '@vue/shared'
import { isRef, pauseTracking, resetTracking, toRaw } from '@vue/reactivity'
import { ErrorCodes, callWithErrorHandling } from './errorHandling'

Expand Down Expand Up @@ -145,11 +145,7 @@ function formatProp(key: string, value: unknown, raw?: boolean): any {
if (isString(value)) {
value = JSON.stringify(value)
return raw ? value : [`${key}=${value}`]
} else if (
typeof value === 'number' ||
typeof value === 'boolean' ||
value == null
) {
} else if (isNumber(value) || isBoolean(value) || value == null) {
return raw ? value : [`${key}=${value}`]
} else if (isRef(value)) {
value = formatProp(key, toRaw(value.value), true)
Expand Down
12 changes: 10 additions & 2 deletions packages/runtime-dom/src/apiCustomElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,15 @@ import {
nextTick,
warn,
} from '@vue/runtime-core'
import { camelize, extend, hyphenate, isArray, toNumber } from '@vue/shared'
import {
camelize,
extend,
hyphenate,
isArray,
isNumber,
isString,
toNumber,
} from '@vue/shared'
import { hydrate, render } from '.'

export type VueElementConstructor<P = {}> = {
Expand Down Expand Up @@ -360,7 +368,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) || isNumber(val)) {
this.setAttribute(hyphenate(key), val + '')
} else if (!val) {
this.removeAttribute(hyphenate(key))
Expand Down
4 changes: 2 additions & 2 deletions packages/server-renderer/src/helpers/ssrRenderList.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isArray, isObject, isString } from '@vue/shared'
import { isArray, isNumber, isObject, isString } from '@vue/shared'
import { warn } from '@vue/runtime-core'

export function ssrRenderList(
Expand All @@ -9,7 +9,7 @@ export function ssrRenderList(
for (let i = 0, l = source.length; i < l; i++) {
renderItem(source[i], i)
}
} else if (typeof source === 'number') {
} else if (isNumber(source)) {
if (__DEV__ && !Number.isInteger(source)) {
warn(`The v-for range expect an integer value but got ${source}.`)
return
Expand Down
3 changes: 3 additions & 0 deletions packages/shared/src/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ export const isRegExp = (val: unknown): val is RegExp =>
toTypeString(val) === '[object RegExp]'
export const isFunction = (val: unknown): val is Function =>
typeof val === 'function'
export const isNumber = (val: unknown): val is number => typeof val === 'number'
export const isBoolean = (val: unknown): val is boolean =>
typeof val === 'boolean'
export const isString = (val: unknown): val is string => typeof val === 'string'
export const isSymbol = (val: unknown): val is symbol => typeof val === 'symbol'
export const isObject = (val: unknown): val is Record<any, any> =>
Expand Down
4 changes: 2 additions & 2 deletions packages/shared/src/normalizeProp.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { hyphenate, isArray, isObject, isString } from './general'
import { hyphenate, isArray, isNumber, isObject, isString } from './general'

export type NormalizedStyle = Record<string, string | number>

Expand Down Expand Up @@ -51,7 +51,7 @@ export function stringifyStyle(
}
for (const key in styles) {
const value = styles[key]
if (isString(value) || typeof value === 'number') {
if (isString(value) || isNumber(value)) {
const normalizedKey = key.startsWith(`--`) ? key : hyphenate(key)
// only render valid values
ret += `${normalizedKey}:${value};`
Expand Down