@itrocks/parameter-name 0.1.0 → 0.3.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,29 +1,23 @@
1
+ import { parseAll } from '@itrocks/ast';
1
2
  import { readFileSync } from 'node:fs';
2
- import ts from 'typescript';
3
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);
4
+ const declarationFileName = fileName.substring(0, fileName.lastIndexOf('.')) + '.d.ts';
5
+ let source;
6
+ try {
7
+ source = readFileSync(declarationFileName, 'utf8');
20
8
  }
21
- function searchMethod(node) {
22
- if (isMethod(node)) {
23
- propertyNames = node.parameters.map(parameter => ts.isIdentifier(parameter.name) ? parameter.name.text : '');
24
- }
9
+ catch {
10
+ return [];
25
11
  }
26
- searchClass(sourceFile);
27
- return propertyNames;
12
+ const declaration = parseAll(source, declarationFileName).declarations.find(declaration => (declaration.kind === 'class') && (declaration.name === className));
13
+ if (!declaration || (declaration.kind !== 'class'))
14
+ return [];
15
+ const method = declaration.members.find(member => (methodName === 'constructor')
16
+ ? member.kind === 'constructor'
17
+ : (member.kind === 'method') && (member.name === methodName)
18
+ && new RegExp('(?:^|[\\s;{}])' + methodName.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') + '\\s*\\(').test(source));
19
+ return method && ((method.kind === 'constructor') || (method.kind === 'method'))
20
+ ? method.parameters.map(parameter => parameter.name ?? '')
21
+ : [];
28
22
  }
29
23
  //# sourceMappingURL=parameter-name.js.map
package/package.json CHANGED
@@ -4,12 +4,13 @@
4
4
  "email": "baptiste@pillot.fr"
5
5
  },
6
6
  "dependencies": {
7
- "@itrocks/class-type": "latest",
8
- "typescript": "^6.0"
7
+ "@itrocks/ast": "latest",
8
+ "@itrocks/class-type": "latest"
9
9
  },
10
10
  "description": "Runtime parameter name reflection from TypeScript declaration files",
11
11
  "devDependencies": {
12
- "@types/node": "^24.10"
12
+ "@types/node": "^24.10",
13
+ "typescript": "^7.0"
13
14
  },
14
15
  "engines": {
15
16
  "node": ">=24"
@@ -47,7 +48,9 @@
47
48
  "url": "git+https://github.com/itrocks-ts/parameter-name.git"
48
49
  },
49
50
  "scripts": {
50
- "build": "tsc"
51
+ "build": "tsc",
52
+ "pretest": "npm run build",
53
+ "test": "node --test"
51
54
  },
52
- "version": "0.1.0"
55
+ "version": "0.3.0"
53
56
  }