-
Notifications
You must be signed in to change notification settings - Fork 0
/
transformer.ts
93 lines (84 loc) · 3.08 KB
/
transformer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import ts from 'typescript';
import path from 'path';
import { DynamodbRecordTransformer } from './lib/dynamodb_record_transformer';
import { FromDynamodbRecordTransformer } from './lib/from_dynamodb_record_transformer';
export default function transformer(program: ts.Program): ts.TransformerFactory<ts.SourceFile> {
return (context: ts.TransformationContext) => (file: ts.SourceFile) => visitNodeAndChildren(file, program, context);
}
function visitNodeAndChildren(
node: ts.SourceFile,
program: ts.Program,
context: ts.TransformationContext,
): ts.SourceFile;
function visitNodeAndChildren(
node: ts.Node,
program: ts.Program,
context: ts.TransformationContext,
): ts.Node | undefined;
function visitNodeAndChildren(
node: ts.Node,
program: ts.Program,
context: ts.TransformationContext,
): ts.Node | undefined {
return ts.visitEachChild(
visitNode(node, program),
childNode => visitNodeAndChildren(childNode, program, context),
context,
);
}
function visitNode(node: ts.SourceFile, program: ts.Program): ts.SourceFile;
function visitNode(node: ts.Node, program: ts.Program): ts.Node | undefined;
function visitNode(node: ts.Node, program: ts.Program): ts.Node | undefined {
const typeChecker = program.getTypeChecker();
if (isDynamodbRecordImportExpression(node)) {
// remove import expression from transformed code
return undefined;
}
if (isFunctionCallExpression(DynamodbRecordTransformer.funcName, node, typeChecker)) {
return DynamodbRecordTransformer.visitNode(node, typeChecker);
}
if (isFunctionCallExpression(FromDynamodbRecordTransformer.funcName, node, typeChecker)) {
return FromDynamodbRecordTransformer.visitNode(node, typeChecker);
}
return node;
}
const indexJs = path.join(__dirname, 'index.js');
function isDynamodbRecordImportExpression(node: ts.Node): node is ts.ImportDeclaration {
if (!ts.isImportDeclaration(node)) {
return false;
}
const module = (node.moduleSpecifier as ts.StringLiteral).text;
try {
return (
indexJs ===
(module.startsWith('.')
? require.resolve(path.resolve(path.dirname(node.getSourceFile().fileName), module))
: require.resolve(module))
);
} catch (e) {
return false;
}
}
const indexTs = path.join(__dirname, 'index.d.ts');
function isFunctionCallExpression(
functionName: string,
node: ts.Node,
typeChecker: ts.TypeChecker,
): node is ts.CallExpression {
if (!ts.isCallExpression(node)) {
return false;
}
const declaration = typeChecker.getResolvedSignature(node)?.declaration;
if (!declaration || ts.isJSDocSignature(declaration) || declaration.name?.getText() !== functionName) {
return false;
}
try {
// require.resolve is required to resolve symlink.
// https://github.com/kimamula/ts-transformer-keys/issues/4#issuecomment-643734716
return require.resolve(declaration.getSourceFile().fileName) === indexTs;
} catch {
// declaration.getSourceFile().fileName may not be in Node.js require stack and require.resolve may result in an error.
// https://github.com/kimamula/ts-transformer-keys/issues/47
return false;
}
}