Skip to content

Commit

Permalink
Merge 32de477 into 719d23a
Browse files Browse the repository at this point in the history
  • Loading branch information
ktutnik committed Dec 28, 2022
2 parents 719d23a + 32de477 commit e560773
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
15 changes: 9 additions & 6 deletions packages/reflect/src/parser.ts
Expand Up @@ -63,20 +63,23 @@ function refineCode(fn: Class | Function, functionOnly = false) {
const code = fn.toString()

// for class created dynamically using reflect.create()
if(code.search(/^class(\s*)extends\s*BaseClass\s*{\s*}/gm) > -1)
if (code.search(/^class(\s*)extends\s*BaseClass\s*{\s*}/gm) > -1)
return "class DynamicClass extends Parent {}"

// for class created using reflect.class() but without base class
if(code.search(/^class(\s*){\s*}/gm) > -1)
if (code.search(/^class(\s*){\s*}/gm) > -1)
return "class DynamicClass {}"

// in case function inside object, it will cause error
// example
// const obj = { fn(par1) {} }
// reflect(obj.fn)
if(functionOnly && code.search(/^([A-z0-9]+)\s*\((?:[^)(]+|\((?:[^)(]+|\([^)(]*\))*\))*\)/gm) > -1)
if (functionOnly && code.search(/^([A-z0-9]+)\s*\((?:[^)(]+|\((?:[^)(]+|\([^)(]*\))*\))*\)/gm) > -1)
return `function ${code}`;


if (functionOnly && code.search(/^async\s*([A-z0-9]+)\s*\((?:[^)(]+|\((?:[^)(]+|\([^)(]*\))*\))*\)/gm) > -1)
return code.replace("async", "async function")

// for the rest code, sometime its contain [native code], just remove it
return code.replace("[native code]", "")
}
Expand Down Expand Up @@ -201,7 +204,7 @@ 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;
if (typeof (owner as any)[name] === "function" || typeof owner.prototype[name] === "function") continue;
// static property
const classDes = getMemberTypeDescriptor(owner, name)
if (classDes && !StaticMemberExclude.includes(name)) {
Expand All @@ -218,7 +221,7 @@ function parseProperties(owner: Class): PropertyReflection[] {
// instead we need to apply decorator on it.
const metadata = getMetadata(owner, name)
const isStatic = metadata.some(meta => {
const opt:DecoratorOption = meta[DecoratorOptionId]
const opt: DecoratorOption = meta[DecoratorOptionId]
return !!opt.isStatic
})
result.push({ kind: isStatic ? "StaticProperty" : "Property", name, decorators: [], get: undefined, set: undefined })
Expand Down
19 changes: 19 additions & 0 deletions tests/behavior/reflect/__snapshots__/function.spec.ts.snap
@@ -1,5 +1,24 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Reflect function Should able to reflect async function inside object 1`] = `
Object {
"kind": "Function",
"name": "fn",
"parameters": Array [
Object {
"decorators": Array [],
"fields": "par1",
"index": 0,
"kind": "Parameter",
"name": "par1",
"type": undefined,
"typeClassification": undefined,
},
],
"returnType": undefined,
}
`;

exports[`Reflect function Should able to reflect destructured parameter 1`] = `
Object {
"kind": "Function",
Expand Down
8 changes: 8 additions & 0 deletions tests/behavior/reflect/function.spec.ts
Expand Up @@ -34,6 +34,14 @@ describe("Reflect function", () => {
const meta = reflect(obj.fn)
expect(meta).toMatchSnapshot()
})

it("Should able to reflect async function inside object", () => {
const obj = {
async fn(par1:string) {}
}
const meta = reflect(obj.fn)
expect(meta).toMatchSnapshot()
})
})

describe("Reflect lambda function", () => {
Expand Down

0 comments on commit e560773

Please sign in to comment.