From 25249fa14b715cd9ce329442dacfd0cdd0e71ff3 Mon Sep 17 00:00:00 2001 From: Ketut Sandiarsa Date: Sun, 1 Dec 2019 14:12:05 +0800 Subject: [PATCH] v2.2.8 --- package.json | 6 +-- src/index.ts | 26 ++++++----- test/__snapshots__/class.spec.js.snap | 45 +++++++++++++++++++ test/__snapshots__/get-parameter.spec.js.snap | 6 ++- test/class.spec.ts | 9 ++++ test/get-parameter.spec.ts | 37 ++++++++++++--- tsconfig.build.json | 1 + tsconfig.json | 2 +- 8 files changed, 110 insertions(+), 22 deletions(-) diff --git a/package.json b/package.json index 9839f18..b1e455e 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/src/index.ts b/src/index.ts index 2db29a8..bc61dc1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -66,10 +66,9 @@ 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 { @@ -77,6 +76,13 @@ export function getParameterNames(fn: Function) { } } +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) @@ -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 { @@ -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) diff --git a/test/__snapshots__/class.spec.js.snap b/test/__snapshots__/class.spec.js.snap index a7ab7a3..5b75d28 100644 --- a/test/__snapshots__/class.spec.js.snap +++ b/test/__snapshots__/class.spec.js.snap @@ -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 { diff --git a/test/__snapshots__/get-parameter.spec.js.snap b/test/__snapshots__/get-parameter.spec.js.snap index f2849de..2d0389c 100644 --- a/test/__snapshots__/get-parameter.spec.js.snap +++ b/test/__snapshots__/get-parameter.spec.js.snap @@ -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 [ diff --git a/test/class.spec.ts b/test/class.spec.ts index b6058b7..772f3bd 100644 --- a/test/class.spec.ts +++ b/test/class.spec.ts @@ -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 { return 1 } + } + const meta = reflect(DummyClass) + expect(meta).toMatchSnapshot() + }) + it("Should inspect destructed parameters type with method decorator", () => { class Domain { constructor( diff --git a/test/get-parameter.spec.ts b/test/get-parameter.spec.ts index 95afb17..d4aa750 100644 --- a/test/get-parameter.spec.ts +++ b/test/get-parameter.spec.ts @@ -1,4 +1,4 @@ -import { decorate, getConstructorParameters, getParameterNames } from "../src" +import { decorate, getConstructorParameters, getParameterNames, getMethodParameters } from "../src" function globalFunction(a: any, b: any) { @@ -88,7 +88,7 @@ describe("Method Parameters", () => { globalFunction(par1, par2) } } - const result = getParameterNames(DummyClass.prototype["myMethod"]) + const result = getMethodParameters(DummyClass, "myMethod") expect(result).toMatchSnapshot() }) @@ -106,7 +106,7 @@ describe("Method Parameters", () => { globalFunction(par1, par2) } } - const result = getParameterNames(DummyClass.prototype["myMethod"]) + const result = getMethodParameters(DummyClass, "myMethod") expect(result).toMatchSnapshot() }) @@ -121,7 +121,7 @@ describe("Method Parameters", () => { globalFunction(par1, par2) } } - const result = getParameterNames(DummyClass.prototype["myMethod"]) + const result = getMethodParameters(DummyClass, "myMethod") expect(result).toMatchSnapshot() }) }) @@ -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]" @@ -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) @@ -190,7 +213,7 @@ describe("Durability", () => { class MyClass { myMethod() { } } - const result = getParameterNames(MyClass.prototype["myMethod"]) + const result = getMethodParameters(MyClass, "myMethod") expect(result).toMatchSnapshot() }) @@ -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() }) }) \ No newline at end of file diff --git a/tsconfig.build.json b/tsconfig.build.json index db17b73..7f35a66 100644 --- a/tsconfig.build.json +++ b/tsconfig.build.json @@ -1,6 +1,7 @@ { "extends": "./tsconfig.json", "compilerOptions": { + "target": "es6", "outDir": "lib", "declaration": true, "inlineSourceMap": false diff --git a/tsconfig.json b/tsconfig.json index 7938049..da123d2 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,6 +1,6 @@ { "compilerOptions": { - "target": "es6", + "target": "es2017", "module": "commonjs", "strict": true, "inlineSourceMap": true,