@itrocks/parameter-name 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,14 @@
1
+ ISC License
2
+ Copyright (C) 2024, Baptiste Pillot <baptiste@pillot.fr>
3
+
4
+ Permission to use, copy, modify, and/or distribute this software for any
5
+ purpose with or without fee is hereby granted, provided that the above
6
+ copyright notice and this permission notice appear in all copies.
7
+
8
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13
+ OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14
+ CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,9 @@
1
+ [![npm version](https://img.shields.io/npm/v/@itrocks/parameter-name?logo=npm)](https://www.npmjs.org/package/@itrocks/parameter-name)
2
+ [![npm downloads](https://img.shields.io/npm/dm/@itrocks/parameter-name)](https://www.npmjs.org/package/@itrocks/parameter-name)
3
+ [![GitHub](https://img.shields.io/github/last-commit/itrocks-ts/parameter-name?color=2dba4e&label=commit&logo=github)](https://github.com/itrocks-ts/parameter-name)
4
+ [![issues](https://img.shields.io/github/issues/itrocks-ts/parameter-name)](https://github.com/itrocks-ts/parameter-name/issues)
5
+ [![discord](https://img.shields.io/discord/1314141024020467782?color=7289da&label=discord&logo=discord&logoColor=white)](https://25.re/ditr)
6
+
7
+ # parameter-name
8
+
9
+ Runtime parameter name reflection from TypeScript declaration files.
@@ -0,0 +1 @@
1
+ export declare function parameterNamesFromFile(fileName: string, className: string, methodName: string): string[];
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.parameterNamesFromFile = parameterNamesFromFile;
7
+ const node_fs_1 = require("node:fs");
8
+ const typescript_1 = __importDefault(require("typescript"));
9
+ function parameterNamesFromFile(fileName, className, methodName) {
10
+ const isMethod = (methodName === 'constructor')
11
+ ? typescript_1.default.isConstructorDeclaration
12
+ : function (node) {
13
+ return typescript_1.default.isMethodDeclaration(node) && node.name && typescript_1.default.isIdentifier(node.name) && (node.name.text === methodName);
14
+ };
15
+ const content = (0, node_fs_1.readFileSync)(fileName.substring(0, fileName.lastIndexOf('.')) + '.d.ts', 'utf8');
16
+ const sourceFile = typescript_1.default.createSourceFile(fileName, content, typescript_1.default.ScriptTarget.Latest, true);
17
+ let propertyNames = new Array;
18
+ function searchClass(node) {
19
+ if (typescript_1.default.isClassDeclaration(node)
20
+ && node.name
21
+ && typescript_1.default.isIdentifier(node.name)
22
+ && (node.name.text === className)) {
23
+ return typescript_1.default.forEachChild(node, searchMethod);
24
+ }
25
+ typescript_1.default.forEachChild(node, searchClass);
26
+ }
27
+ function searchMethod(node) {
28
+ if (isMethod(node)) {
29
+ propertyNames = node.parameters.map(parameter => typescript_1.default.isIdentifier(parameter.name) ? parameter.name.text : '');
30
+ }
31
+ }
32
+ searchClass(sourceFile);
33
+ return propertyNames;
34
+ }
35
+ //# sourceMappingURL=parameter-name.js.map
@@ -0,0 +1 @@
1
+ export declare function parameterNamesFromFile(fileName: string, className: string, methodName: string): string[];
@@ -0,0 +1,29 @@
1
+ import { readFileSync } from 'node:fs';
2
+ import ts from 'typescript';
3
+ export function parameterNamesFromFile(fileName, className, methodName) {
4
+ const isMethod = (methodName === 'constructor')
5
+ ? ts.isConstructorDeclaration
6
+ : function (node) {
7
+ return ts.isMethodDeclaration(node) && node.name && ts.isIdentifier(node.name) && (node.name.text === methodName);
8
+ };
9
+ const content = readFileSync(fileName.substring(0, fileName.lastIndexOf('.')) + '.d.ts', 'utf8');
10
+ const sourceFile = ts.createSourceFile(fileName, content, ts.ScriptTarget.Latest, true);
11
+ let propertyNames = new Array;
12
+ function searchClass(node) {
13
+ if (ts.isClassDeclaration(node)
14
+ && node.name
15
+ && ts.isIdentifier(node.name)
16
+ && (node.name.text === className)) {
17
+ return ts.forEachChild(node, searchMethod);
18
+ }
19
+ ts.forEachChild(node, searchClass);
20
+ }
21
+ function searchMethod(node) {
22
+ if (isMethod(node)) {
23
+ propertyNames = node.parameters.map(parameter => ts.isIdentifier(parameter.name) ? parameter.name.text : '');
24
+ }
25
+ }
26
+ searchClass(sourceFile);
27
+ return propertyNames;
28
+ }
29
+ //# sourceMappingURL=parameter-name.js.map
package/package.json ADDED
@@ -0,0 +1,56 @@
1
+ {
2
+ "author": {
3
+ "name": "Baptiste Pillot",
4
+ "email": "baptiste@pillot.fr"
5
+ },
6
+ "dependencies": {
7
+ "@itrocks/class-type": "latest",
8
+ "typescript": "~5.6"
9
+ },
10
+ "description": "Runtime parameter name reflection from TypeScript declaration files",
11
+ "devDependencies": {
12
+ "@types/node": "^22.10"
13
+ },
14
+ "engines": {
15
+ "node": ">=18"
16
+ },
17
+ "exports": {
18
+ "import": "./esm/parameter-name.js",
19
+ "require": "./cjs/parameter-name.js"
20
+ },
21
+ "files": [
22
+ "LICENSE",
23
+ "README.md",
24
+ "cjs/*",
25
+ "esm/*",
26
+ "!*.map"
27
+ ],
28
+ "homepage": "https://it.rocks",
29
+ "keywords": [
30
+ "backend",
31
+ "constructor",
32
+ "function",
33
+ "it.rocks",
34
+ "method",
35
+ "name",
36
+ "parameter",
37
+ "parameters",
38
+ "parse",
39
+ "parsing",
40
+ "reflection",
41
+ "utility"
42
+ ],
43
+ "license": "ISC",
44
+ "name": "@itrocks/parameter-name",
45
+ "repository": {
46
+ "type": "git",
47
+ "url": "git+https://github.com/itrocks-ts/parameter-name.git"
48
+ },
49
+ "scripts": {
50
+ "build": "npm run build:cjs && npm run build:esm",
51
+ "build:cjs": "tsc -p tsconfig.cjs.json",
52
+ "build:esm": "tsc -p tsconfig.esm.json"
53
+ },
54
+ "types": "./esm/parameter-name.d.ts",
55
+ "version": "0.0.1"
56
+ }