Skip to content

Commit

Permalink
Merge aaec9bd into b197345
Browse files Browse the repository at this point in the history
  • Loading branch information
ktutnik committed Jan 7, 2023
2 parents b197345 + aaec9bd commit b02b067
Show file tree
Hide file tree
Showing 6 changed files with 28 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
7 changes: 7 additions & 0 deletions tests/behavior/reflect/primitve.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,37 @@ THIS TEST REQUIRED NODE.JS 10
describe("Primitive Class", () => {
it("Should inspect Boolean", () => {
const meta = reflect(Boolean)
expect(meta.kind).toBe("Class")
expect(meta.name).toBe("Boolean")
})
it("Should inspect String", () => {
const meta = reflect(String)
expect(meta.kind).toBe("Class")
expect(meta.name).toBe("String")
})
it("Should inspect Number", () => {
const meta = reflect(Number)
expect(meta.kind).toBe("Class")
expect(meta.name).toBe("Number")
})
it("Should inspect Date", () => {
const meta = reflect(Date)
expect(meta.kind).toBe("Class")
expect(meta.name).toBe("Date")
})
it("Should inspect Array", () => {
const meta = reflect(Array)
expect(meta.kind).toBe("Class")
expect(meta.name).toBe("Array")
})
it("Should inspect Promise", () => {
const meta = reflect(Promise)
expect(meta.kind).toBe("Class")
expect(meta.name).toBe("Promise")
})
it("Should inspect Object", () => {
const meta = reflect(Object)
expect(meta.kind).toBe("Class")
expect(meta.name).toBe("Object")
})
})

0 comments on commit b02b067

Please sign in to comment.