@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.
- package/cjs/parameter-name.js +34 -20
- package/package.json +5 -3
package/cjs/parameter-name.js
CHANGED
|
@@ -1,29 +1,43 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
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
|
-
?
|
|
8
|
+
? isConstructorDeclaration
|
|
6
9
|
: function (node) {
|
|
7
|
-
return
|
|
10
|
+
return isMethodDeclaration(node) && isIdentifier(node.name) && (node.name.text === methodName);
|
|
8
11
|
};
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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": "^
|
|
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.
|
|
54
|
+
"version": "0.2.0"
|
|
53
55
|
}
|