@itrocks/parameter-name 0.1.0 → 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,29 +1,43 @@
1
- import { readFileSync } from 'node:fs';
2
- import ts from 'typescript';
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';
3
6
  export function parameterNamesFromFile(fileName, className, methodName) {
4
7
  const isMethod = (methodName === 'constructor')
5
- ? ts.isConstructorDeclaration
8
+ ? isConstructorDeclaration
6
9
  : function (node) {
7
- return ts.isMethodDeclaration(node) && node.name && ts.isIdentifier(node.name) && (node.name.text === methodName);
10
+ return isMethodDeclaration(node) && isIdentifier(node.name) && (node.name.text === methodName);
8
11
  };
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);
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 [];
18
20
  }
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 : '');
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
+ }
24
35
  }
36
+ searchClass(sourceFile);
37
+ return propertyNames;
38
+ }
39
+ finally {
40
+ api.close();
25
41
  }
26
- searchClass(sourceFile);
27
- return propertyNames;
28
42
  }
29
43
  //# sourceMappingURL=parameter-name.js.map
package/package.json CHANGED
@@ -5,7 +5,7 @@
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": {
@@ -47,7 +47,9 @@
47
47
  "url": "git+https://github.com/itrocks-ts/parameter-name.git"
48
48
  },
49
49
  "scripts": {
50
- "build": "tsc"
50
+ "build": "tsc",
51
+ "pretest": "npm run build",
52
+ "test": "node --test"
51
53
  },
52
- "version": "0.1.0"
54
+ "version": "0.2.0"
53
55
  }