Skip to content

Make static properties/methods accessible through the runtime object built from the class #156

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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 names on the underlying Vue object.`);
Copy link
Author

Choose a reason for hiding this comment

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

Suggested change
console.warn(`Property/method ${property} of ${classObject.name} is not supported for static access, as it conflicts with names on the underlying Vue object.`);
console.warn(`Property/method ${property} of ${classObject.name} is not supported for static access, as it conflicts with 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);
}
}