tinspector
Advanced tools
Comparing version 2.2.7 to 2.2.8
@@ -20,2 +20,5 @@ import "reflect-metadata"; | ||
kind: "Parameter"; | ||
properties: string | { | ||
[key: string]: string[]; | ||
}; | ||
decorators: any[]; | ||
@@ -78,4 +81,8 @@ type?: any; | ||
export declare const DESIGN_RETURN_TYPE = "design:returntype"; | ||
export declare function getParameterNames(fn: Function): string[]; | ||
export declare function getConstructorParameters(fn: Class): string[]; | ||
export declare function getParameterNames(fn: Function): (string | { | ||
[key: string]: string[]; | ||
})[]; | ||
export declare function getConstructorParameters(fn: Class): (string | { | ||
[key: string]: string[]; | ||
})[]; | ||
export declare function getMembers(fun: Function): string[]; | ||
@@ -82,0 +89,0 @@ export declare function useCache<K, P extends any[], R>(cache: Map<K, R>, fn: (...args: P) => R, getKey: (...args: P) => K): (...args: P) => R; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
require("reflect-metadata"); | ||
const acorn_1 = require("acorn"); | ||
exports.DECORATOR_KEY = "plumier.key:DECORATOR"; | ||
@@ -12,30 +13,56 @@ exports.DESIGN_TYPE = "design:type"; | ||
/* ---------------------------------------------------------------- */ | ||
//logic from https://github.com/goatslacker/get-parameter-names | ||
function cleanUp(fn) { | ||
return fn | ||
//strive comments | ||
.replace(/((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg, '') | ||
//strive rest parameter | ||
.replace(/\.{3}/mg, '') | ||
//strive lambda | ||
.replace(/=>.*$/mg, '') | ||
//strive default params | ||
.replace(/=[^,]+/mg, ''); | ||
function getNode(node, criteria) { | ||
if (criteria(node)) | ||
return node; | ||
if (!node.body) | ||
return; | ||
if (Array.isArray(node.body)) { | ||
for (const child of node.body) { | ||
const result = getNode(child, criteria); | ||
if (result) | ||
return result; | ||
} | ||
} | ||
return getNode(node.body, criteria); | ||
} | ||
function getNamesFromAst(nodes) { | ||
const getName = (node) => { | ||
if (node.type === "Identifier") | ||
return node.name; | ||
if (node.type === "AssignmentPattern") | ||
return node.left.name; | ||
if (node.type === "RestElement") | ||
return node.argument.name; | ||
if (node.type === "Property") { | ||
if (node.value.type === "Identifier") | ||
return node.value.name; | ||
else { | ||
const result = {}; | ||
result[node.key.name] = getName(node.value); | ||
return result; | ||
} | ||
} | ||
//if (node.type === "ObjectPattern") { | ||
return node.properties.map((x) => getName(x)); | ||
//} | ||
}; | ||
return nodes.map(x => getName(x)).filter((x) => !!x); | ||
} | ||
function getParameterNames(fn) { | ||
const regex = /\(\s*([^]*?)\)\s*\{/mg; | ||
const result = regex.exec(fn.toString()); | ||
if (!result) | ||
const body = fn.toString(); | ||
const src = !body.startsWith("function") ? "function " + body : body; | ||
try { | ||
const ast = acorn_1.parse(src); | ||
return getNamesFromAst(ast.body[0].params); | ||
} | ||
catch (_a) { | ||
return []; | ||
const match = cleanUp(result[1]); | ||
return match.split(",").map(x => x.trim()).filter(x => !!x); | ||
} | ||
} | ||
exports.getParameterNames = getParameterNames; | ||
function getConstructorParameters(fn) { | ||
const regex = /constructor\s*\(\s*([^]*?)\)\s*\{/mg; | ||
const result = regex.exec(fn.toString()); | ||
if (!result) | ||
return []; | ||
const match = cleanUp(result[1]); | ||
return match.split(",").map(x => x.trim()).filter(x => !!x); | ||
const body = fn.toString(); | ||
const ast = acorn_1.parse(body); | ||
const ctor = getNode(ast, x => x.type === "MethodDefinition" && x.kind === "constructor"); | ||
return getNamesFromAst(ctor ? ctor.value.params : []); | ||
} | ||
@@ -242,3 +269,12 @@ exports.getConstructorParameters = getConstructorParameters; | ||
const typeClassification = getTypeClassification(type); | ||
return { kind: "Parameter", name, type, decorators, typeClassification }; | ||
let parName; | ||
let properties = {}; | ||
if (typeof name === "object") { | ||
parName = "__destruct__"; | ||
properties = name; | ||
} | ||
else { | ||
parName = name; | ||
} | ||
return { kind: "Parameter", name: parName, type, decorators, typeClassification, properties }; | ||
} | ||
@@ -245,0 +281,0 @@ function reflectFunction(fn) { |
{ | ||
"name": "tinspector", | ||
"version": "2.2.7", | ||
"version": "2.2.8", | ||
"description": "TypeScript type inspector", | ||
@@ -20,12 +20,14 @@ "main": "lib/index.js", | ||
"dependencies": { | ||
"@types/acorn": "4.0.3", | ||
"acorn": "7.1.0", | ||
"reflect-metadata": "^0.1.13" | ||
}, | ||
"devDependencies": { | ||
"@types/jest": "^24.0.18", | ||
"@types/node": "^12.7.11", | ||
"coveralls": "^3.0.6", | ||
"@types/jest": "^24.0.23", | ||
"@types/node": "^12.12.14", | ||
"coveralls": "^3.0.9", | ||
"jest": "^24.9.0", | ||
"tslib": "^1.10.0", | ||
"typescript": "^3.6.3" | ||
"typescript": "^3.7.2" | ||
} | ||
} |
29804
553
3
+ Added@types/acorn@4.0.3
+ Addedacorn@7.1.0
+ Added@types/acorn@4.0.3(transitive)
+ Added@types/estree@1.0.6(transitive)
+ Addedacorn@7.1.0(transitive)