Skip to content

Commit

Permalink
Merge pull request #87 from tall1on/master
Browse files Browse the repository at this point in the history
+ correctly expose `methods` option to recover compatibility to `vue-class-component`
  • Loading branch information
ruojianll committed Sep 21, 2023
2 parents bd2ade0 + 891ad52 commit a41aa9b
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 33 deletions.
22 changes: 12 additions & 10 deletions src/component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineComponent, type ComponentCustomOptions } from 'vue';
import { defineComponent, type ComponentCustomOptions, type MethodOptions } from 'vue';
import { obtainSlot, getSuperSlot, getProviderFunction } from './utils'
import { build as optionSetup } from './option/setup'
import { build as optionComputed } from './option/computed'
Expand Down Expand Up @@ -70,6 +70,7 @@ type ComponentOption = {
template?: string
mixins?: any[]
setup?: ComponentSetupFunction
methods?: MethodOptions
}

type ComponentConsOption = Cons | ComponentOption
Expand All @@ -78,7 +79,7 @@ function buildComponent(cons: Cons, arg: ComponentOption, extend?: any): any {
const option = ComponentOption(cons, extend)
const slot = obtainSlot(cons.prototype)
Object.keys(arg).reduce<Record<string, any>>((option, name: string) => {
if (['options', 'modifier', 'emits', 'setup', 'provide'].includes(name)) {
if (['options', 'modifier', 'methods', 'emits', 'setup', 'provide'].includes(name)) {
return option
}
option[name] = arg[name as keyof ComponentOption]
Expand All @@ -92,11 +93,16 @@ function buildComponent(cons: Cons, arg: ComponentOption, extend?: any): any {
}
option.emits = emits

//merge methods
if ('object' === typeof arg.methods && !Array.isArray(arg.methods) && arg.methods !== null) {
option.methods??={}
Object.assign(option.methods, arg.methods);
}

//merge setup function
if (!option.setup) {
option.setup = arg.setup
} else {

const oldSetup: OptionSetupFunction = option.setup
const newSetup: ComponentSetupFunction = arg.setup ?? function () { return {} }

Expand All @@ -105,16 +111,13 @@ function buildComponent(cons: Cons, arg: ComponentOption, extend?: any): any {
const oldRet = oldSetup(props, ctx)
if (oldRet instanceof Promise || newRet instanceof Promise) {
return Promise.all([newRet, oldRet]).then((arr) => {
const ret = Object.assign({}, arr[0], arr[1])
return ret
return Object.assign({}, arr[0], arr[1])
})
} else {

const ret = Object.assign({}, newRet, oldRet)
return ret
return Object.assign({}, newRet, oldRet)
}

}

option.setup = setup
}

Expand All @@ -130,7 +133,6 @@ function buildComponent(cons: Cons, arg: ComponentOption, extend?: any): any {
if (map && map.size > 0) {
map.forEach((v) => {
v.forEach(ite=>ite.creator.apply({}, [option, ite.key]))

})
}

Expand Down
22 changes: 4 additions & 18 deletions src/option/methodsAndHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Cons } from '../component'
import type { OptionBuilder } from '../optionBuilder'
import { obtainSlot, toComponentReverse, excludeNames, getValidNames, optionNullableMemberDecorator } from '../utils'

export const HookNames = [
export const HookNames: ReadonlyArray<string> = [
"beforeCreate",
"created",
"beforeMount",
Expand All @@ -29,7 +29,6 @@ export const decorator = optionNullableMemberDecorator(function (proto: any, nam
map.set(name, null)
})


export function build(cons: Cons, optionBuilder: OptionBuilder) {
const slot = obtainSlot(cons.prototype)
const protoArr = toComponentReverse(cons.prototype)
Expand All @@ -41,15 +40,7 @@ export function build(cons: Cons, optionBuilder: OptionBuilder) {
const MethodFunctions: Record<string, Function> = {}
protoArr.forEach(proto => {
let names = getValidNames(proto, (des, name) => {

if (name === 'constructor') {
return false
}
if (typeof des.value === 'function') {

return true
}
return false
return typeof des.value === 'function' && name !== 'constructor'
})
names = excludeNames(names, slot, (mapName) => {
//include these names:
Expand All @@ -59,16 +50,12 @@ export function build(cons: Cons, optionBuilder: OptionBuilder) {
return !['watch', 'hooks', 'emits', 'provide'].includes(mapName)
});
names.forEach(name => {
if (HookNames.includes(name as any) || map.has(name)) {

if (HookNames.includes(name) || map.has(name)) {
HookFunctions[name] = proto[name]
}
else {
} else {
MethodFunctions[name] = proto[name]
}
})


})

Object.assign(optionBuilder.methods, MethodFunctions)
Expand All @@ -83,5 +70,4 @@ export function build(cons: Cons, optionBuilder: OptionBuilder) {
}
}
Object.assign(optionBuilder.hooks, HookFunctions)

}
2 changes: 1 addition & 1 deletion test/custom/custom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ describe('create decorator',
})
}
)
export default {}
export default {}
2 changes: 1 addition & 1 deletion test/option/emit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,4 @@ describe('decorator Emit',
})
}
)
export default {}
export default {}
11 changes: 8 additions & 3 deletions test/option/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ import { expect } from 'chai';
import 'mocha';
import { Component, Base, toNative } from '../../dist'

@Component
@Component({
methods: {
optionTest: () => 'option value'
}
})
class Comp extends Base {
method() {
return 'method value'
}

}
const CompContext = toNative(Comp) as any

Expand All @@ -17,7 +20,9 @@ describe('option methods',
it('default', () => {
expect('function').to.equal(typeof CompContext?.methods?.method)
expect('method value').to.equal(CompContext.methods.method())
expect('function').to.equal(typeof CompContext?.methods?.optionTest)
expect('option value').to.equal(CompContext.methods.optionTest())
})
}
)
export default {}
export default {}

2 comments on commit a41aa9b

@fb-sean
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please release this fix its really needed.

@ruojianll
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fb-sean v3.0.3 released

Please sign in to comment.