Skip to content

Commit

Permalink
fix(reflect): Fix issue where primitive data type inspected as Functi…
Browse files Browse the repository at this point in the history
…on instead of Class
  • Loading branch information
ktutnik committed Jan 7, 2023
1 parent b197345 commit 5255d3b
Show file tree
Hide file tree
Showing 7 changed files with 1,650 additions and 4 deletions.
16 changes: 16 additions & 0 deletions packages/reflect/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,28 @@ namespace reflection {
case Number:
case Object:
case Date:
case Promise:
return false
default:
return true
}
}

export function isPrimitive(type:Function) {
switch (type) {
case Boolean:
case String:
case Array:
case Number:
case Object:
case Date:
case Promise:
return true
default:
return false
}
}

export function getMethods(meta: ClassReflection) {
return meta.methods.map(x => ({
name: x.name,
Expand Down
4 changes: 2 additions & 2 deletions packages/reflect/src/reflect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function traverseObject(fn: any, name: string, ctx: TraverseContext): Reflection
if (ctx.path.some(x => x === fn)) return
return reflectObject(fn, name, { path: ctx.path.concat(fn) })
}
if (typeof fn === "function" && reflection.isConstructor(fn))
if ((typeof fn === "function" && reflection.isConstructor(fn)))
return reflectClass(fn)
if (typeof fn === "function")
return parseFunction(fn)
Expand All @@ -69,7 +69,7 @@ function reflectModuleOrClass(opt: string | Class | Function) {
if (typeof opt === "string") {
return reflectObject(require(opt), "module", { path: [] })
}
if (reflection.isConstructor(opt))
if (reflection.isConstructor(opt) || reflection.isPrimitive(opt))
return reflectClass(opt as Class)
if (typeof opt === "function")
return parseFunction(opt)
Expand Down
1 change: 1 addition & 0 deletions tests/behavior/export/__snapshots__/export.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ Object {
"isConstructor": isConstructor,
"isCustomClass": isCustomClass,
"isParameterProperties": isParameterProperties,
"isPrimitive": isPrimitive,
},
"type": type,
"useCache": useCache,
Expand Down
2 changes: 1 addition & 1 deletion tests/behavior/reflect/__snapshots__/class.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ Object {
},
],
"returnType": Promise,
"typeClassification": "Class",
"typeClassification": "Primitive",
},
],
"name": "DummyClass",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2880,7 +2880,7 @@ Object {
"name": "myMethod",
"parameters": Array [],
"returnType": Promise,
"typeClassification": "Class",
"typeClassification": "Primitive",
},
],
"name": "MyClass",
Expand Down

0 comments on commit 5255d3b

Please sign in to comment.