8000 fixed corner case with constructor · vuejs/vue-class-component@49e9c90 · GitHub
[go: up one dir, main page]

Skip to content

Commit 49e9c90

Browse files
author
Gleb Ivanov
committed
fixed corner case with constructor
1 parent 07eff38 commit 49e9c90

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

src/component.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,13 @@ export function componentFactory (
2626
): VueClass<Vue> {
2727
const reflectionMap: ReflectionMap = {
2828
instance: {},
29-
static: {}
29+
static: {},
30+
constructor: []
3031
}
3132

33+
if (reflectionIsSupported()) {
34+
reflectionMap.constructor = Reflect.getOwnMetadataKeys(Component)
35+
}
3236
options.name = options.name || (Component as any)._componentTag || (Component as any).name
3337
// prototype props.
3438
const proto = Component.prototype

src/reflect.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export type StringToArrayMap = {
55
}
66

77
export type ReflectionMap = {
8+
constructor: Array<string>,
89
instance: StringToArrayMap,
910
static: StringToArrayMap
1011
}
@@ -20,13 +21,19 @@ export function copyReflectionMetadata(
2021
) {
2122
shallowCopy(from.prototype, to.prototype, reflectionMap.instance)
2223
shallowCopy(from, to, reflectionMap.static)
24+
shallowCopy(from, to, {'constructor': reflectionMap.constructor})
2325
}
2426

2527
function shallowCopy(from: VueConstructor, to: VueConstructor, propertyKeys: StringToArrayMap) {
2628
for (const propertyKey in propertyKeys) {
2729
propertyKeys[propertyKey].forEach((metadataKey) => {
28-
const metadata = Reflect.getOwnMetadata(metadataKey, from, propertyKey)
29-
Reflect.defineMetadata(metadataKey, metadata, to, propertyKey)
30+
if (propertyKey == 'constructor') {
31+
const metadata = Reflect.getOwnMetadata(metadataKey, from)
32+
Reflect.defineMetadata(metadataKey, metadata, to)
33+
} else {
34+
const metadata = Reflect.getOwnMetadata(metadataKey, from, propertyKey)
35+
Reflect.defineMetadata(metadataKey, metadata, to, propertyKey)
36+
}
3037
})
3138
}
3239
}

test/test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,8 +372,14 @@ describe('vue-class-component', () => {
372372
})
373373

374374
it('copies reflection metadata', function () {
375+
const trickyDecorator = (proto: Vue, _: string) =>
376+
Reflect.defineMetadata('worksConstructor', true, proto.constructor);
377+
375378
@Component
376379
class Test extends Vue {
380+
@trickyDecorator
381+
trickyCase: string = 'trickyCase'
382+
377383
@Reflect.metadata('worksStatic', true)
378384
static staticValue: string = 'staticValue'
379385

@@ -385,6 +391,7 @@ describe('vue-class-component', () => {
385391
set testAccessor(value: boolean) { void(value) }
386392
}
387393

394+
expect(Reflect.getOwnMetadata('worksConstructor', Test)).to.equal(true)
388395
expect(Reflect.getOwnMetadata('worksStatic', Test, 'staticValue')).to.equal(true)
389396
expect(Reflect.getOwnMetadata('worksMethod', Test.prototype, 'test')).to.equal(true)
390397
expect(Reflect.getOwnMetadata('worksAccessor', Test.prototype, 'testAccessor')).to.equal(true)

0 commit comments

Comments
 (0)
0