-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.ts
199 lines (181 loc) · 7.37 KB
/
index.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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import { NodePath, PluginObj, PluginPass } from '@babel/core';
import debugFactory from 'debug';
import * as util from '@babel/types';
import template from '@babel/template';
const pkgName = 'babel-plugin-explicit-exports-references';
const debug = debugFactory(`${pkgName}:index`);
let globalScope: NodePath['scope'];
function updateExportReferences(
path: NodePath<util.Identifier>,
mode: 'named' | 'default',
transformAssignExpr: boolean
): void;
function updateExportReferences(
path: { from: NodePath<util.Identifier>; to: string },
mode: 'named' | 'default',
transformAssignExpr: boolean
): void;
function updateExportReferences(
path: NodePath<util.Identifier> | { from: NodePath<util.Identifier>; to: string },
mode: 'named' | 'default',
transformAssignExpr: boolean
): void {
// @ts-expect-error: need to discriminate between input types
const idPath = (path.isIdentifier?.() ? path : path.from) as NodePath<util.Identifier>;
const localName = idPath.node.name;
// @ts-expect-error: need to discriminate between input types
const exportedName = (path.to as string) || localName;
const globalBinding = globalScope.getBinding(localName);
const referencePaths = [
...(globalBinding?.referencePaths || []),
...(globalBinding?.constantViolations || [])
];
const numberReferences = referencePaths?.length || 0;
const dbg = debug.extend(`mode-${mode}:updating`);
if (numberReferences) {
dbg(
`potentially updating ${numberReferences} references to ${mode} export "${localName}"` +
(exportedName != localName ? ` (exported as "${exportedName}")` : '')
);
} else dbg('no references to update');
referencePaths?.forEach((referencePath, ndx) => {
const prefix = `ref-${exportedName}-${(ndx + 1).toString()}`;
if (
// eslint-disable-next-line unicorn/prefer-array-some
!!referencePath.find(
(path) =>
path.isExportSpecifier() ||
path.isExportNamespaceSpecifier() ||
path.isExportDefaultSpecifier()
)
) {
dbg(`[${prefix}] reference skipped: part of an export specifier`);
return;
}
// eslint-disable-next-line unicorn/prefer-array-some
if (!!referencePath.find((path) => path.isTSType())) {
dbg(`[${prefix}] reference skipped: TypeScript type reference`);
return;
}
if (
referencePath.isJSXIdentifier() ||
referencePath.parentPath?.isJSXOpeningElement()
) {
dbg(`[${prefix}] transforming type "JSX identifier"`);
const jsxElement = template.expression.ast(
`<module.exports.${mode == 'default' ? mode : exportedName} />`,
{ plugins: ['jsx'] }
) as util.JSXElement;
const jsxMemberExpression = jsxElement.openingElement.name;
referencePath.replaceWith(jsxMemberExpression);
} else if (referencePath.isIdentifier()) {
dbg(`[${prefix}] transforming type "identifier"`);
referencePath.replaceWith(
template.expression.ast`module.exports.${mode == 'default' ? mode : exportedName}`
);
} else if (transformAssignExpr && referencePath.isAssignmentExpression()) {
dbg(`[${prefix}] transforming type "assignment expression"`);
referencePath
.get('left')
// TODO: needs to be more resilient, but we'll repeat this here for now
.replaceWith(
template.expression.ast`module.exports.${
mode == 'default' ? mode : exportedName
}`
);
} else dbg(`[${prefix}] reference skipped: unsupported type "${referencePath.type}"`);
});
}
export default function (): PluginObj<
PluginPass & { opts: { transformAssignExpr: boolean } }
> {
return {
name: 'explicit-exports-references',
visitor: {
Program(programPath) {
globalScope = programPath.scope;
},
ExportDefaultDeclaration(exportPath, state) {
const declaration = exportPath.get('declaration');
const transformAssignExpr = state.opts.transformAssignExpr;
const dbg = debug.extend('mode-default');
debug(`encountered default export declaration`);
if (declaration.isFunctionDeclaration() || declaration.isClassDeclaration()) {
const id = declaration.get('id') as NodePath<util.Identifier>;
if (id?.node?.name) updateExportReferences(id, 'default', transformAssignExpr);
else dbg('default declaration is anonymous, ignored');
} else dbg('default declaration not function or class, ignored');
},
ExportNamedDeclaration(exportPath, state) {
const declaration = exportPath.get('declaration');
const specifiers = exportPath.get('specifiers');
const transformAssignExpr = state.opts.transformAssignExpr;
const dbg = debug.extend('mode-named');
if (!declaration.node && !specifiers.length) {
dbg('ignored empty named export declaration');
return;
}
debug(`encountered named export node`);
dbg(`processing declaration`);
if (declaration.node) {
if (declaration.isFunctionDeclaration() || declaration.isClassDeclaration()) {
updateExportReferences(
declaration.get('id') as NodePath<util.Identifier>,
'named',
transformAssignExpr
);
} else if (declaration.isVariableDeclaration()) {
declaration.get('declarations').forEach((declarator) => {
const id = declarator.get('id');
if (id.isIdentifier())
updateExportReferences(id, 'named', transformAssignExpr);
else if (id.isObjectPattern()) {
id.get('properties').forEach((propertyPath) => {
if (propertyPath.isObjectProperty()) {
const propertyId = propertyPath.get('value');
if (propertyId.isIdentifier())
updateExportReferences(propertyId, 'named', transformAssignExpr);
} else if (propertyPath.isRestElement()) {
const argument = propertyPath.get('argument');
if (argument.isIdentifier())
updateExportReferences(argument, 'named', transformAssignExpr);
}
});
}
});
} else {
dbg(
'named declaration is not a function, class, or variable declaration; ignored'
);
}
}
specifiers.length && dbg(`processing ${specifiers.length} specifiers`);
// ? Later exports take precedence over earlier ones
specifiers.forEach((specifier) => {
if (!specifier.isExportSpecifier()) {
dbg(`ignored export specifier type "${specifier.type}"`);
} else {
const local = specifier.get('local');
const exported = specifier.get('exported');
dbg(`encountered specifier "${local} as ${exported}"`);
if (exported.isIdentifier()) {
const exportedName = exported.node.name;
updateExportReferences(
{
from: local,
to: exportedName
},
exportedName == 'default' ? 'default' : 'named',
transformAssignExpr
);
} else {
dbg(
'ignored export specifier because module string names are not supported'
);
}
}
});
}
}
};
}