@simplysm/sd-cli 12.5.17 → 12.5.19

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,70 +0,0 @@
1
- import ts from 'typescript';
2
- import path from 'path';
3
-
4
- const createArrayExpression = ts.factory.createArrayLiteralExpression;
5
- const createStringLiteral = ts.factory.createStringLiteral;
6
-
7
- export function keysTransformer(program: ts.Program): ts.TransformerFactory<ts.SourceFile> {
8
- return (context: ts.TransformationContext) => (file: ts.SourceFile) => visitNodeAndChildren(file, program, context);
9
- }
10
-
11
- function visitNodeAndChildren(node: ts.SourceFile, program: ts.Program, context: ts.TransformationContext): ts.SourceFile;
12
- function visitNodeAndChildren(node: ts.Node, program: ts.Program, context: ts.TransformationContext): ts.Node | undefined;
13
- function visitNodeAndChildren(node: ts.Node, program: ts.Program, context: ts.TransformationContext): ts.Node | undefined {
14
- return ts.visitEachChild(visitNode(node, program), childNode => visitNodeAndChildren(childNode, program, context), context);
15
- }
16
-
17
- function visitNode(node: ts.SourceFile, program: ts.Program): ts.SourceFile;
18
- function visitNode(node: ts.Node, program: ts.Program): ts.Node | undefined;
19
- function visitNode(node: ts.Node, program: ts.Program): ts.Node | undefined {
20
- const typeChecker = program.getTypeChecker();
21
- if (isKeysImportExpression(node)) {
22
- return;
23
- }
24
- if (!isKeysCallExpression(node, typeChecker)) {
25
- return node;
26
- }
27
- if (!node.typeArguments) {
28
- return createArrayExpression([]);
29
- }
30
- const type = typeChecker.getTypeFromTypeNode(node.typeArguments[0]);
31
- const properties = typeChecker.getPropertiesOfType(type);
32
- return createArrayExpression(properties.map(property => createStringLiteral(property.name)));
33
- }
34
-
35
- const indexJs = path.join(__dirname, 'index.js');
36
- function isKeysImportExpression(node: ts.Node): node is ts.ImportDeclaration {
37
- if (!ts.isImportDeclaration(node)) {
38
- return false;
39
- }
40
- const module = (node.moduleSpecifier as ts.StringLiteral).text;
41
- try {
42
- return indexJs === (
43
- module.startsWith('.')
44
- ? require.resolve(path.resolve(path.dirname(node.getSourceFile().fileName), module))
45
- : require.resolve(module)
46
- );
47
- } catch {
48
- return false;
49
- }
50
- }
51
-
52
- const indexTs = path.join(__dirname, 'index.d.ts');
53
- function isKeysCallExpression(node: ts.Node, typeChecker: ts.TypeChecker): node is ts.CallExpression {
54
- if (!ts.isCallExpression(node)) {
55
- return false;
56
- }
57
- const declaration = typeChecker.getResolvedSignature(node)?.declaration;
58
- if (!declaration || ts.isJSDocSignature(declaration) || declaration.name?.getText() !== 'keys') {
59
- return false;
60
- }
61
- try {
62
- // require.resolve is required to resolve symlink.
63
- // https://github.com/kimamula/ts-transformer-keys/issues/4#issuecomment-643734716
64
- return require.resolve(declaration.getSourceFile().fileName) === indexTs;
65
- } catch {
66
- // declaration.getSourceFile().fileName may not be in Node.js require stack and require.resolve may result in an error.
67
- // https://github.com/kimamula/ts-transformer-keys/issues/47
68
- return false;
69
- }
70
- }