-
-
Notifications
You must be signed in to change notification settings - Fork 185
Expand file tree
/
Copy pathexport.ts
More file actions
163 lines (139 loc) · 5.51 KB
/
export.ts
File metadata and controls
163 lines (139 loc) · 5.51 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import * as ts from "typescript";
import * as lua from "../../LuaAST";
import { TransformationContext } from "../context";
import { createModuleLocalName } from "../visitors/namespace";
import { createExportsIdentifier } from "./lua-ast";
import { getSymbolInfo } from "./symbols";
import { findFirstNodeAbove } from "./typescript";
export function hasDefaultExportModifier(node: ts.Node): boolean {
return (
ts.canHaveModifiers(node) &&
node.modifiers?.some(modifier => modifier.kind === ts.SyntaxKind.DefaultKeyword) === true
);
}
export function hasExportModifier(node: ts.Node): boolean {
return (
ts.canHaveModifiers(node) &&
node.modifiers?.some(modifier => modifier.kind === ts.SyntaxKind.ExportKeyword) === true
);
}
export function shouldBeExported(node: ts.Node): boolean {
if (hasExportModifier(node)) {
// Don't export if we're inside a namespace (module declaration)
return ts.findAncestor(node, ts.isModuleDeclaration) === undefined;
}
return false;
}
export const createDefaultExportStringLiteral = (original?: ts.Node): lua.StringLiteral =>
lua.createStringLiteral("default", original);
export function getExportedSymbolDeclaration(symbol: ts.Symbol): ts.Declaration | undefined {
const declarations = symbol.getDeclarations();
if (declarations) {
return declarations.find(d => (ts.getCombinedModifierFlags(d) & ts.ModifierFlags.Export) !== 0);
}
}
export function getSymbolFromIdentifier(
context: TransformationContext,
identifier: lua.Identifier
): ts.Symbol | undefined {
if (identifier.symbolId !== undefined) {
const symbolInfo = getSymbolInfo(context, identifier.symbolId);
if (symbolInfo !== undefined) {
return symbolInfo.symbol;
}
}
}
export function getIdentifierExportScope(
context: TransformationContext,
identifier: lua.Identifier
): ts.SourceFile | ts.ModuleDeclaration | undefined {
const symbol = getSymbolFromIdentifier(context, identifier);
if (!symbol) {
return undefined;
}
return getSymbolExportScope(context, symbol);
}
function isGlobalAugmentation(module: ts.ModuleDeclaration): boolean {
return (module.flags & ts.NodeFlags.GlobalAugmentation) !== 0;
}
export function getSymbolExportScope(
context: TransformationContext,
symbol: ts.Symbol
): ts.SourceFile | ts.ModuleDeclaration | undefined {
const exportedDeclaration = getExportedSymbolDeclaration(symbol);
if (!exportedDeclaration) {
return undefined;
}
const scope = findFirstNodeAbove(
exportedDeclaration,
(n): n is ts.SourceFile | ts.ModuleDeclaration => ts.isSourceFile(n) || ts.isModuleDeclaration(n)
);
if (!scope) {
return undefined;
}
if (ts.isModuleDeclaration(scope) && isGlobalAugmentation(scope)) {
return undefined;
}
if (!isSymbolExportedFromScope(context, symbol, scope)) {
return undefined;
}
return scope;
}
export function getExportedSymbolsFromScope(
context: TransformationContext,
scope: ts.SourceFile | ts.ModuleDeclaration
): ts.Symbol[] {
const scopeSymbol = context.checker.getSymbolAtLocation(ts.isSourceFile(scope) ? scope : scope.name);
if (scopeSymbol?.exports === undefined) {
return [];
}
// ts.Iterator is not a ES6-compatible iterator, because TypeScript targets ES5
const it: Iterable<ts.Symbol> = { [Symbol.iterator]: () => scopeSymbol.exports!.values() };
return [...it];
}
export function getDependenciesOfSymbol(context: TransformationContext, originalSymbol: ts.Symbol): ts.Symbol[] {
return getExportedSymbolsFromScope(context, context.sourceFile).filter(exportSymbol =>
exportSymbol.declarations
?.filter(ts.isExportSpecifier)
.map(context.checker.getExportSpecifierLocalTargetSymbol)
.includes(originalSymbol)
);
}
export function isSymbolExported(context: TransformationContext, symbol: ts.Symbol): boolean {
return (
getExportedSymbolDeclaration(symbol) !== undefined ||
// Symbol may have been exported separately (e.g. 'const foo = "bar"; export { foo }')
isSymbolExportedFromScope(context, symbol, context.sourceFile)
);
}
export function isSymbolExportedFromScope(
context: TransformationContext,
symbol: ts.Symbol,
scope: ts.SourceFile | ts.ModuleDeclaration
): boolean {
return getExportedSymbolsFromScope(context, scope).includes(symbol);
}
export function addExportToIdentifier(
context: TransformationContext,
identifier: lua.Identifier
): lua.AssignmentLeftHandSideExpression {
const exportScope = getIdentifierExportScope(context, identifier);
return exportScope ? createExportedIdentifier(context, identifier, exportScope) : identifier;
}
export function createExportedIdentifier(
context: TransformationContext,
identifier: lua.Identifier,
exportScope?: ts.SourceFile | ts.ModuleDeclaration
): lua.AssignmentLeftHandSideExpression {
if (!identifier.exportable) {
return identifier;
}
const exportTable =
exportScope && ts.isModuleDeclaration(exportScope)
? createModuleLocalName(context, exportScope)
: createExportsIdentifier();
return lua.createTableIndexExpression(exportTable, lua.createStringLiteral(identifier.text));
}
export function createDefaultExportExpression(node: ts.Node): lua.AssignmentLeftHandSideExpression {
return lua.createTableIndexExpression(createExportsIdentifier(), createDefaultExportStringLiteral(node), node);
}