Skip to content
This repository has been archived by the owner on Dec 12, 2020. It is now read-only.

Commit

Permalink
v2.2.8
Browse files Browse the repository at this point in the history
  • Loading branch information
ktutnik committed Dec 2, 2019
1 parent 986f8e7 commit 25249fa
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 22 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tinspector",
"version": "2.2.7",
"version": "2.2.8",
"description": "TypeScript type inspector",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand All @@ -18,8 +18,8 @@
"license": "MIT",
"dependencies": {
"@types/acorn": "4.0.3",
"reflect-metadata": "^0.1.13",
"acorn": "7.1.0"
"acorn": "7.1.0",
"reflect-metadata": "^0.1.13"
},
"devDependencies": {
"@types/jest": "^24.0.23",
Expand Down
26 changes: 16 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,23 @@ function getNamesFromAst(nodes: any[]) {
}

export function getParameterNames(fn: Function) {
const body = fn.toString()
const src = !body.startsWith("function") ? "function " + body : body
try {
const ast = parse(src)
const body = fn.toString()
const ast = parse(body)
return getNamesFromAst((ast as any).body[0].params)
}
catch {
return []
}
}

export function getMethodParameters(fn: Class, method: string) {
const body = fn.toString()
const ast = parse(body)
const ctor = getNode(ast, x => x.type === "MethodDefinition" && x.kind === "method" && x.key.name === method)
return getNamesFromAst(ctor ? (ctor as any).value.params : [])
}

export function getConstructorParameters(fn: Class) {
const body = fn.toString()
const ast = parse(body)
Expand Down Expand Up @@ -320,14 +326,14 @@ function reflectFunction(fn: Function): FunctionReflection {
return { kind: "Function", name: fn.name, parameters, returnType: undefined }
}

function reflectMethod(clazz: Class, method: Function, iterator: DecoratorIterator): MethodReflection {
const parType: any[] = Reflect.getOwnMetadata(DESIGN_PARAMETER_TYPE, clazz.prototype, method.name) || []
const rawReturnType: any = Reflect.getOwnMetadata(DESIGN_RETURN_TYPE, clazz.prototype, method.name)
const parameters = getParameterNames(method).map((x, i) => reflectParameter(x, parType[i], iterator("Parameter", method.name, i)))
const decorators = iterator("Method", method.name)
function reflectMethod(clazz: Class, method: string, iterator: DecoratorIterator): MethodReflection {
const parType: any[] = Reflect.getOwnMetadata(DESIGN_PARAMETER_TYPE, clazz.prototype, method) || []
const rawReturnType: any = Reflect.getOwnMetadata(DESIGN_RETURN_TYPE, clazz.prototype, method)
const parameters = getMethodParameters(clazz, method).map((x, i) => reflectParameter(x, parType[i], iterator("Parameter", method, i)))
const decorators = iterator("Method", method)
const returnType = getReflectionType(decorators, rawReturnType)
const typeClassification = getTypeClassification(returnType)
return { kind: "Method", name: method.name, parameters, decorators, returnType, typeClassification }
return { kind: "Method", name: method, parameters, decorators, returnType, typeClassification }
}

function reflectProperty(name: string, typeAnnotation: Class, des: PropertyDescriptor | undefined, iterator: DecoratorIterator): PropertyReflection {
Expand All @@ -344,7 +350,7 @@ function reflectMember(clazz: Class, name: string, iterator: DecoratorIterator)
const type: any = Reflect.getOwnMetadata(DESIGN_TYPE, clazz.prototype, name)
const des = Reflect.getOwnPropertyDescriptor(clazz.prototype, name)
if (des && typeof des.value === "function" && !des.get && !des.set) {
return reflectMethod(clazz, clazz.prototype[name], iterator)
return reflectMethod(clazz, name, iterator)
}
else {
return reflectProperty(name, type, des, iterator)
Expand Down
45 changes: 45 additions & 0 deletions test/__snapshots__/class.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,51 @@ Object {
}
`;

exports[`Class Introspection Should inspect async method with parameters 1`] = `
Object {
"ctor": Object {
"kind": "Constructor",
"name": "constructor",
"parameters": Array [],
},
"decorators": Array [],
"kind": "Class",
"methods": Array [
Object {
"decorators": Array [
Object {},
],
"kind": "Method",
"name": "dummyMethod",
"parameters": Array [
Object {
"decorators": Array [],
"kind": "Parameter",
"name": "dummy",
"properties": Object {},
"type": String,
"typeClassification": "Primitive",
},
Object {
"decorators": Array [],
"kind": "Parameter",
"name": "other",
"properties": Object {},
"type": Object,
"typeClassification": "Primitive",
},
],
"returnType": Promise,
"typeClassification": "Class",
},
],
"name": "DummyClass",
"properties": Array [],
"type": DummyClass,
"typeClassification": "Class",
}
`;

exports[`Class Introspection Should inspect class properly 1`] = `
Object {
"ctor": Object {
Expand Down
6 changes: 5 additions & 1 deletion test/__snapshots__/get-parameter.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ exports[`Durability Should not error when provided method first than constructor

exports[`Durability Should not error when provided method without parameter 1`] = `Array []`;

exports[`Durability Should not error when provided proxy 1`] = `Array []`;
exports[`Durability getConstructorParameters should not error when provided proxy 1`] = `Array []`;

exports[`Durability getMethodParameters should not error when provided proxy 1`] = `Array []`;

exports[`Durability getParameterNames should not error when provided proxy 1`] = `Array []`;

exports[`Function Parameters Should get function parameter name 1`] = `
Array [
Expand Down
9 changes: 9 additions & 0 deletions test/class.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ describe("Class Introspection", () => {
expect(meta).toMatchSnapshot()
})

it("Should inspect async method with parameters", () => {
class DummyClass {
@decorateMethod({})
async dummyMethod(dummy: string, other: any): Promise<number> { return 1 }
}
const meta = reflect(DummyClass)
expect(meta).toMatchSnapshot()
})

it("Should inspect destructed parameters type with method decorator", () => {
class Domain {
constructor(
Expand Down
37 changes: 30 additions & 7 deletions test/get-parameter.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { decorate, getConstructorParameters, getParameterNames } from "../src"
import { decorate, getConstructorParameters, getParameterNames, getMethodParameters } from "../src"

function globalFunction(a: any, b: any) {

Expand Down Expand Up @@ -88,7 +88,7 @@ describe("Method Parameters", () => {
globalFunction(par1, par2)
}
}
const result = getParameterNames(DummyClass.prototype["myMethod"])
const result = getMethodParameters(DummyClass, "myMethod")
expect(result).toMatchSnapshot()
})

Expand All @@ -106,7 +106,7 @@ describe("Method Parameters", () => {
globalFunction(par1, par2)
}
}
const result = getParameterNames(DummyClass.prototype["myMethod"])
const result = getMethodParameters(DummyClass, "myMethod")
expect(result).toMatchSnapshot()
})

Expand All @@ -121,7 +121,7 @@ describe("Method Parameters", () => {
globalFunction(par1, par2)
}
}
const result = getParameterNames(DummyClass.prototype["myMethod"])
const result = getMethodParameters(DummyClass, "myMethod")
expect(result).toMatchSnapshot()
})
})
Expand Down Expand Up @@ -171,7 +171,7 @@ describe("Function Parameters", () => {
})

describe("Durability", () => {
it("Should not error when provided proxy", () => {
it("getParameterNames should not error when provided proxy", () => {
function MyFunction() { }
MyFunction.toString = () => {
return "[Function]"
Expand All @@ -180,6 +180,29 @@ describe("Durability", () => {
expect(result).toMatchSnapshot()
})

it("getConstructorParameters should not error when provided proxy", () => {
class MyFunction {
constructor(){}
}
MyFunction.toString = () => {
return "[Function]"
}
const result = getConstructorParameters(MyFunction)
expect(result).toMatchSnapshot()
})

it("getMethodParameters should not error when provided proxy", () => {
class MyFunction {
constructor(){}
myMethod(){}
}
MyFunction.toString = () => {
return "[Function]"
}
const result = getMethodParameters(MyFunction, "myMethod")
expect(result).toMatchSnapshot()
})

it("Should not error when provided function without parameter", () => {
function myFun() { }
const result = getParameterNames(myFun)
Expand All @@ -190,7 +213,7 @@ describe("Durability", () => {
class MyClass {
myMethod() { }
}
const result = getParameterNames(MyClass.prototype["myMethod"])
const result = getMethodParameters(MyClass, "myMethod")
expect(result).toMatchSnapshot()
})

Expand Down Expand Up @@ -275,7 +298,7 @@ describe("Parameter Destructuring", () => {
class MyClass {
myMethod({ date: tanggal, num }: MyModel) { }
}
const result = getParameterNames(MyClass.prototype["myMethod"])
const result = getMethodParameters(MyClass, "myMethod")
expect(result).toMatchSnapshot()
})
})
1 change: 1 addition & 0 deletions tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"target": "es6",
"outDir": "lib",
"declaration": true,
"inlineSourceMap": false
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "es6",
"target": "es2017",
"module": "commonjs",
"strict": true,
"inlineSourceMap": true,
Expand Down

0 comments on commit 25249fa

Please sign in to comment.