diff --git a/packages/reflect/src/parser.ts b/packages/reflect/src/parser.ts index 53b9a903..4d77ea1d 100644 --- a/packages/reflect/src/parser.ts +++ b/packages/reflect/src/parser.ts @@ -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]", "") } @@ -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)) { @@ -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 }) diff --git a/tests/behavior/reflect/__snapshots__/function.spec.ts.snap b/tests/behavior/reflect/__snapshots__/function.spec.ts.snap index 041ad3de..1ea38a18 100644 --- a/tests/behavior/reflect/__snapshots__/function.spec.ts.snap +++ b/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", diff --git a/tests/behavior/reflect/function.spec.ts b/tests/behavior/reflect/function.spec.ts index 82eff236..c697cba1 100644 --- a/tests/behavior/reflect/function.spec.ts +++ b/tests/behavior/reflect/function.spec.ts @@ -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", () => {