@itrocks/parameter-name 0.2.0 → 0.3.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.
@@ -1,43 +1,23 @@
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';
1
+ import { parseAll } from '@itrocks/ast';
2
+ import { readFileSync } from 'node:fs';
6
3
  export function parameterNamesFromFile(fileName, className, methodName) {
7
- const isMethod = (methodName === 'constructor')
8
- ? isConstructorDeclaration
9
- : function (node) {
10
- return isMethodDeclaration(node) && isIdentifier(node.name) && (node.name.text === methodName);
11
- };
12
4
  const declarationFileName = fileName.substring(0, fileName.lastIndexOf('.')) + '.d.ts';
13
- const api = new API();
5
+ let source;
14
6
  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 [];
20
- }
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
- }
35
- }
36
- searchClass(sourceFile);
37
- return propertyNames;
7
+ source = readFileSync(declarationFileName, 'utf8');
38
8
  }
39
- finally {
40
- api.close();
9
+ catch {
10
+ return [];
41
11
  }
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
+ : [];
42
22
  }
43
23
  //# sourceMappingURL=parameter-name.js.map
package/package.json CHANGED
@@ -4,12 +4,12 @@
4
4
  "email": "baptiste@pillot.fr"
5
5
  },
6
6
  "dependencies": {
7
- "@itrocks/class-type": "latest",
8
- "typescript": "^7.0"
7
+ "@itrocks/ast": "latest"
9
8
  },
10
9
  "description": "Runtime parameter name reflection from TypeScript declaration files",
11
10
  "devDependencies": {
12
- "@types/node": "^24.10"
11
+ "@types/node": "^24.10",
12
+ "typescript": "^7.0"
13
13
  },
14
14
  "engines": {
15
15
  "node": ">=24"
@@ -51,5 +51,5 @@
51
51
  "pretest": "npm run build",
52
52
  "test": "node --test"
53
53
  },
54
- "version": "0.2.0"
54
+ "version": "0.3.1"
55
55
  }