Skip to content

Commit

Permalink
BREAKING CHANGE: rename createElement to h (#400)
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu authored Jun 23, 2020
1 parent a1792de commit 23e90f5
Show file tree
Hide file tree
Showing 14 changed files with 29 additions and 27 deletions.
4 changes: 2 additions & 2 deletions src/apis/computed.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getCurrentVue, getCurrentVM } from '../runtimeContext'
import { getCurrentVue, getCurrentInstance } from '../runtimeContext'
import { createRef, Ref } from '../reactivity'
import { defineComponentInstance } from '../helper'
import { warn } from '../utils'
Expand All @@ -22,7 +22,7 @@ export function computed<T>(options: Option<T>): WritableComputedRef<T>
export function computed<T>(
options: Option<T>['get'] | Option<T>
): ComputedRef<T> | WritableComputedRef<T> {
const vm = getCurrentVM()
const vm = getCurrentInstance()
let get: Option<T>['get'], set: Option<T>['set'] | undefined
if (typeof options === 'function') {
get = options
Expand Down
4 changes: 2 additions & 2 deletions src/apis/inject.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ComponentInstance } from '../component'
import { currentVMInFn } from '../helper'
import { hasOwn, warn } from '../utils'
import { getCurrentVM } from '../runtimeContext'
import { getCurrentInstance } from '../runtimeContext'

const NOT_FOUND = {}
export interface InjectionKey<T> extends Symbol {}
Expand Down Expand Up @@ -48,7 +48,7 @@ export function inject(
return defaultValue
}

const vm = getCurrentVM()
const vm = getCurrentInstance()
if (vm) {
const val = resolveInject(key, vm)
if (val !== NOT_FOUND) {
Expand Down
8 changes: 6 additions & 2 deletions src/apis/lifecycle.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { VueConstructor } from 'vue'
import { ComponentInstance } from '../component'
import { getCurrentVue, setCurrentVM, getCurrentVM } from '../runtimeContext'
import {
getCurrentVue,
setCurrentVM,
getCurrentInstance,
} from '../runtimeContext'
import { currentVMInFn } from '../helper'

const genName = (name: string) => `on${name[0].toUpperCase() + name.slice(1)}`
Expand All @@ -26,7 +30,7 @@ function injectHookOption(

function wrapHookCall(vm: ComponentInstance, fn: Function) {
return (...args: any) => {
let preVm = getCurrentVM()
let preVm = getCurrentInstance()
setCurrentVM(vm)
try {
return fn(...args)
Expand Down
4 changes: 2 additions & 2 deletions src/apis/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ComponentInstance } from '../component'
import { Ref, isRef, isReactive } from '../reactivity'
import { assert, logError, noopFn, warn, isFunction } from '../utils'
import { defineComponentInstance } from '../helper'
import { getCurrentVM, getCurrentVue } from '../runtimeContext'
import { getCurrentInstance, getCurrentVue } from '../runtimeContext'
import { WatcherPreFlushQueueKey, WatcherPostFlushQueueKey } from '../symbols'
import { ComputedRef } from './computed'

Expand Down Expand Up @@ -95,7 +95,7 @@ function getWatchEffectOption(options?: Partial<WatchOptions>): WatchOptions {
}

function getWatcherVM() {
let vm = getCurrentVM()
let vm = getCurrentInstance()
if (!vm) {
if (!fallbackVM) {
fallbackVM = defineComponentInstance(getCurrentVue())
Expand Down
6 changes: 3 additions & 3 deletions src/createElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ type CreateElement = Vue['$createElement']

let fallbackCreateElement: CreateElement

const createElement: CreateElement = function createElement(...args: any) {
export const createElement: CreateElement = function createElement(
...args: any
) {
if (!currentVM) {
warn('`createElement()` has been called outside of render function.')
if (!fallbackCreateElement) {
Expand All @@ -20,5 +22,3 @@ const createElement: CreateElement = function createElement(...args: any) {

return currentVM.$createElement.apply(currentVM, args)
} as any

export default createElement
4 changes: 2 additions & 2 deletions src/helper.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import Vue, { VNode, ComponentOptions, VueConstructor } from 'vue'
import { ComponentInstance } from './component'
import { currentVue, getCurrentVM } from './runtimeContext'
import { currentVue, getCurrentInstance } from './runtimeContext'
import { warn } from './utils'

export function currentVMInFn(hook: string): ComponentInstance | null {
const vm = getCurrentVM()
const vm = getCurrentInstance()
if (__DEV__ && !vm) {
warn(
`${hook} is called when there is no active component instance to be ` +
Expand Down
10 changes: 4 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Vue, { VueConstructor } from 'vue'
import { Data, SetupFunction, SetupContext } from './component'
import { Data, SetupFunction } from './component'
import { currentVue } from './runtimeContext'
import { install } from './install'
import { mixin } from './setup'
Expand All @@ -21,17 +21,15 @@ if (currentVue && typeof window !== 'undefined' && window.Vue) {
}

export default plugin
export { default as createElement } from './createElement'
export { SetupContext }
export { createElement as h } from './createElement'
export { getCurrentInstance } from './runtimeContext'
export {
defineComponent,
ComponentRenderProxy,
PropType,
PropOptions,
SetupContext,
} from './component'
// For getting a hold of the interal instance in setup() - useful for advanced
// plugins
export { getCurrentVM as getCurrentInstance } from './runtimeContext'

export * from './apis/state'
export * from './apis/lifecycle'
Expand Down
2 changes: 1 addition & 1 deletion src/runtimeContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function setCurrentVue(vue: VueConstructor) {
currentVue = vue
}

export function getCurrentVM(): ComponentInstance | null {
export function getCurrentInstance(): ComponentInstance | null {
return currentVM
}

Expand Down
4 changes: 2 additions & 2 deletions src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
Data,
} from './component'
import { Ref, isRef, isReactive, markRaw } from './reactivity'
import { getCurrentVM, setCurrentVM } from './runtimeContext'
import { getCurrentInstance, setCurrentVM } from './runtimeContext'
import { resolveSlots, createSlotProxy } from './helper'
import { hasOwn, isPlainObject, assert, proxy, warn, isFunction } from './utils'
import { ref } from './apis/state'
Expand Down Expand Up @@ -112,7 +112,7 @@ function activateCurrentInstance(
fn: (vm_: ComponentInstance) => any,
onError?: (err: Error) => void
) {
let preVm = getCurrentVM()
let preVm = getCurrentInstance()
setCurrentVM(vm)
try {
return fn(vm)
Expand Down
2 changes: 1 addition & 1 deletion test/setup.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const Vue = require('vue/dist/vue.common.js')
const {
ref,
computed,
createElement: h,
h,
provide,
inject,
reactive,
Expand Down
2 changes: 1 addition & 1 deletion test/setupContext.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Vue = require('vue/dist/vue.common.js')
const { ref, watch, createElement: h } = require('../src')
const { h } = require('../src')

describe('setupContext', () => {
it('should have proper properties', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/templateRefs.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Vue = require('vue/dist/vue.common.js')
const { ref, watchEffect, watch, createElement: h } = require('../src')
const { ref, watchEffect } = require('../src')

describe('ref', () => {
it('should work', (done) => {
Expand Down
2 changes: 1 addition & 1 deletion test/types/defineComponent.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
defineComponent,
createElement as h,
h,
ref,
SetupContext,
PropType,
Expand Down
2 changes: 1 addition & 1 deletion test/v3/runtime-core/apiLifecycle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
onBeforeMount,
onMounted,
ref,
createElement as h,
h,
onBeforeUpdate,
onUpdated,
onBeforeUnmount,
Expand Down

0 comments on commit 23e90f5

Please sign in to comment.