Skip to content
Open
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
7 changes: 4 additions & 3 deletions src/component.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { defineComponent, type ComponentCustomOptions, type MethodOptions } from 'vue';
import { obtainSlot } from './slot'
import { getSuperSlot, getProviderFunction, optionNullableClassDecorator } from './utils'
import { getSuperSlot, getProviderFunction, optionNullableClassDecorator, assignStaticClassProperties } from './utils'
import { build as optionSetup } from './option/setup'
import { build as optionComputed } from './option/computed'
import { build as optionData } from './option/data'
Expand Down Expand Up @@ -31,7 +31,7 @@ function componentOptionFactory(cons: VueCons, extend?: any) {
optionRef(cons, optionBuilder)//after Computed
optionAccessor(cons, optionBuilder)
optionMethodsAndHooks(cons, optionBuilder)//the last one
const raw = {
const raw: any = {
name: cons.name,
setup: optionBuilder.setup,
data() {
Expand All @@ -51,7 +51,8 @@ function componentOptionFactory(cons: VueCons, extend?: any) {
...optionBuilder.hooks,
extends: extend
}
return raw as any
assignStaticClassProperties(cons, raw);
return raw
}

type ComponentOption = {
Expand Down
29 changes: 29 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,32 @@ export function optionNullableClassDecorator<T>(handler: { (cons: VueCons, optio
return decorator
}

export function assignStaticClassProperties<T extends VueCons = any>(source: T, target: any) {
// keep track of things we've assigned (e.g. overridden variables in child class)
const previouslyAssigned: Record<string, true> = {};

while (source !== Base) {
const classObject = source;
for (const property of Object.getOwnPropertyNames(classObject)) {
if (property === 'prototype' || property === 'name' || property === 'length') {
continue;
}
if ((property in target) && !(property in previouslyAssigned)) {
console.warn(`Property/method ${property} of ${classObject.name} is not supported for static access, as it conflicts with property names in the underlying Vue object.`);
continue;
}
previouslyAssigned[property] = true;
if (typeof (classObject as any)[property] === 'function') {
target[property] = (...args: unknown[]): unknown => {
return (classObject as any)[property].apply(classObject, args);
}
} else {
target[property] = new Proxy(classObject, {
get(target: any, prop) { return target[prop] },
set(target: any, prop, value) { target[prop] = value; return true; },
});
}
}
source = Object.getPrototypeOf(classObject);
}
}