Skip to content

Commit

Permalink
Merge ca9300e into 1e6f54e
Browse files Browse the repository at this point in the history
  • Loading branch information
ktutnik committed Jan 15, 2023
2 parents 1e6f54e + ca9300e commit a214a45
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 15 deletions.
11 changes: 10 additions & 1 deletion packages/reflect/src/parser.ts
Expand Up @@ -209,11 +209,20 @@ function parseMethods(owner: Class): MethodReflection[] {
return result
}

function isFunction(owner:Class, member:string) {
const descriptor = Object.getOwnPropertyDescriptor(owner, member)
return typeof descriptor?.value === "function"
}

function parseProperties(owner: Class): PropertyReflection[] {
const result: PropertyReflection[] = []
const members = getClassMembers(owner)
for (const name of members) {
if (typeof (owner as any)[name] === "function" || typeof owner.prototype[name] === "function") continue;
// not static property
if (isFunction(owner, name)) continue;
// not instance property
if (isFunction(owner.prototype, name)) continue;

// static property
const classDes = getMemberTypeDescriptor(owner, name)
if (classDes && !StaticMemberExclude.includes(name)) {
Expand Down
28 changes: 28 additions & 0 deletions tests/behavior/reflect/__snapshots__/class.spec.ts.snap
Expand Up @@ -1386,6 +1386,34 @@ Object {
}
`;

exports[`Class Introspection Should not throw error when inspecting getter/setter which accessing private field 1`] = `
Object {
"ctor": Object {
"kind": "Constructor",
"name": "constructor",
"parameters": Array [],
},
"decorators": Array [],
"kind": "Class",
"methods": Array [],
"name": "MyClass",
"properties": Array [
Object {
"decorators": Array [],
"get": get options,
"kind": "Property",
"name": "options",
"set": undefined,
"type": undefined,
"typeClassification": undefined,
},
],
"super": Object,
"type": MyClass,
"typeClassification": "Class",
}
`;

exports[`Class Introspection Should reflect destructed tuple parameters type 1`] = `
Object {
"decorators": Array [],
Expand Down
41 changes: 27 additions & 14 deletions tests/behavior/reflect/class.spec.ts
Expand Up @@ -244,15 +244,15 @@ describe("Class Introspection", () => {

it("Should inspect static method", () => {
class DummyClass {
static myStaticMethod() {}
static myStaticMethod() { }
}
const meta = reflect(DummyClass)
expect(meta).toMatchSnapshot()
})

it("Should inspect static method's parameters ", () => {
class DummyClass {
static myStaticMethod(par1:number, par2:string) {}
static myStaticMethod(par1: number, par2: string) { }
}
const meta = reflect(DummyClass)
expect(meta).toMatchSnapshot()
Expand All @@ -261,7 +261,7 @@ describe("Class Introspection", () => {
it("Should inspect static method return type with decorator", () => {
class DummyClass {
@noop()
static myStaticMethod(par1:number, par2:string):Number { return 1 }
static myStaticMethod(par1: number, par2: string): Number { return 1 }
}
const meta = reflect(DummyClass)
expect(meta).toMatchSnapshot()
Expand All @@ -270,7 +270,7 @@ describe("Class Introspection", () => {
it("Should inspect static method return type with decorator override", () => {
class DummyClass {
@type([Number])
static myStaticMethod(par1:number, par2:string):Number[] { return [1] }
static myStaticMethod(par1: number, par2: string): Number[] { return [1] }
}
const meta = reflect(DummyClass)
expect(meta).toMatchSnapshot()
Expand All @@ -279,16 +279,16 @@ describe("Class Introspection", () => {
it("Should inspect static method's parameter type with decorator", () => {
class DummyClass {
@noop()
static myStaticMethod(par1:number, par2:string) {}
static myStaticMethod(par1: number, par2: string) { }
}
const meta = reflect(DummyClass)
expect(meta).toMatchSnapshot()
})

it("Should able to distinguish between static method and instance method with the same name", () => {
class DummyClass {
static method() {}
method() {}
static method() { }
method() { }
}
const meta = reflect(DummyClass)
expect(meta).toMatchSnapshot()
Expand All @@ -297,25 +297,25 @@ describe("Class Introspection", () => {
it("Should inspect static property", () => {
class DummyClass {
@noop()
static myProp:string
static myProp: string
}
const meta = reflect(DummyClass)
expect(meta).toMatchSnapshot()
})

it("Should inspect static property type with decorator override", () => {
class DummyClass {
@type([Number])
static myProp:number[]
static myProp: number[]
}
const meta = reflect(DummyClass)
expect(meta).toMatchSnapshot()
})

it("Should inspect static getter and setter", () => {
class DummyClass {
static get myProp():number { return 1 }
static set myProp(value:number) { }
static get myProp(): number { return 1 }
static set myProp(value: number) { }
}
const meta = reflect(DummyClass)
expect(meta).toMatchSnapshot()
Expand All @@ -324,10 +324,23 @@ describe("Class Introspection", () => {
it("Should inspect static getter and setter type", () => {
class DummyClass {
@noop()
static get myProp():number { return 1 }
static set myProp(value:number) { }
static get myProp(): number { return 1 }
static set myProp(value: number) { }
}
const meta = reflect(DummyClass)
expect(meta).toMatchSnapshot()
})

it("Should not throw error when inspecting getter/setter which accessing private field", () => {
class MyClass {
#opt = 1

get options() {
return this.#opt;
}
}

const metadata = reflect(MyClass);
expect(metadata).toMatchSnapshot();
});
})

0 comments on commit a214a45

Please sign in to comment.