Skip to content
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

fix(reflect): Fix issue when reflect async function inside object #1033

Merged
merged 1 commit into from
Dec 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 9 additions & 6 deletions packages/reflect/src/parser.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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