@itrocks/parameter-name 0.0.5 → 0.2.0

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.
@@ -1,35 +1,43 @@
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) {
1
+ import { isClassDeclaration } from 'typescript/unstable/ast';
2
+ import { isConstructorDeclaration } from 'typescript/unstable/ast';
3
+ import { isIdentifier } from 'typescript/unstable/ast';
4
+ import { isMethodDeclaration } from 'typescript/unstable/ast';
5
+ import { API } from 'typescript/unstable/sync';
6
+ export function parameterNamesFromFile(fileName, className, methodName) {
10
7
  const isMethod = (methodName === 'constructor')
11
- ? typescript_1.default.isConstructorDeclaration
8
+ ? isConstructorDeclaration
12
9
  : function (node) {
13
- return typescript_1.default.isMethodDeclaration(node) && node.name && typescript_1.default.isIdentifier(node.name) && (node.name.text === methodName);
10
+ return isMethodDeclaration(node) && isIdentifier(node.name) && (node.name.text === methodName);
14
11
  };
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);
12
+ const declarationFileName = fileName.substring(0, fileName.lastIndexOf('.')) + '.d.ts';
13
+ const api = new API();
14
+ try {
15
+ const snapshot = api.updateSnapshot({ openFiles: [declarationFileName] });
16
+ const project = snapshot.getDefaultProjectForFile(declarationFileName);
17
+ const sourceFile = project?.program.getSourceFile(declarationFileName);
18
+ if (!sourceFile) {
19
+ return [];
24
20
  }
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 : '');
21
+ let propertyNames = new Array;
22
+ function searchClass(node) {
23
+ if (isClassDeclaration(node)
24
+ && node.name
25
+ && isIdentifier(node.name)
26
+ && (node.name.text === className)) {
27
+ return node.forEachChild(searchMethod);
28
+ }
29
+ node.forEachChild(searchClass);
30
+ }
31
+ function searchMethod(node) {
32
+ if (isMethod(node)) {
33
+ propertyNames = node.parameters.map(parameter => isIdentifier(parameter.name) ? parameter.name.text : '');
34
+ }
30
35
  }
36
+ searchClass(sourceFile);
37
+ return propertyNames;
38
+ }
39
+ finally {
40
+ api.close();
31
41
  }
32
- searchClass(sourceFile);
33
- return propertyNames;
34
42
  }
35
43
  //# sourceMappingURL=parameter-name.js.map
package/package.json CHANGED
@@ -5,24 +5,20 @@
5
5
  },
6
6
  "dependencies": {
7
7
  "@itrocks/class-type": "latest",
8
- "typescript": "~6.0"
8
+ "typescript": "^7.0"
9
9
  },
10
10
  "description": "Runtime parameter name reflection from TypeScript declaration files",
11
11
  "devDependencies": {
12
12
  "@types/node": "^24.10"
13
13
  },
14
14
  "engines": {
15
- "node": ">=18"
16
- },
17
- "exports": {
18
- "import": "./esm/parameter-name.js",
19
- "require": "./cjs/parameter-name.js"
15
+ "node": ">=24"
20
16
  },
17
+ "exports": "./cjs/parameter-name.js",
21
18
  "files": [
22
19
  "LICENSE",
23
20
  "README.md",
24
- "cjs/*",
25
- "esm/*",
21
+ "cjs",
26
22
  "!*.map"
27
23
  ],
28
24
  "homepage": "https://it.rocks",
@@ -51,10 +47,9 @@
51
47
  "url": "git+https://github.com/itrocks-ts/parameter-name.git"
52
48
  },
53
49
  "scripts": {
54
- "build": "npm run build:cjs && npm run build:esm",
55
- "build:cjs": "tsc -p tsconfig.cjs.json",
56
- "build:esm": "tsc -p tsconfig.esm.json"
50
+ "build": "tsc",
51
+ "pretest": "npm run build",
52
+ "test": "node --test"
57
53
  },
58
- "types": "./esm/parameter-name.d.ts",
59
- "version": "0.0.5"
54
+ "version": "0.2.0"
60
55
  }
@@ -1 +0,0 @@
1
- export declare function parameterNamesFromFile(fileName: string, className: string, methodName: string): string[];
@@ -1,29 +0,0 @@
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