forked from microsoft/TypeScript
/
fixImportNonExportedMember.ts
161 lines (138 loc) · 7.84 KB
/
fixImportNonExportedMember.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
/* @internal */
namespace ts.codefix {
const fixId = "fixImportNonExportedMember";
const errorCodes = [
Diagnostics.Module_0_declares_1_locally_but_it_is_not_exported.code,
];
registerCodeFix({
errorCodes,
fixIds: [fixId],
getCodeActions(context) {
const { sourceFile, span, program } = context;
const info = getInfo(sourceFile, span.start, program);
if (info === undefined) return undefined;
const changes = textChanges.ChangeTracker.with(context, t => doChange(t, info));
return [createCodeFixAction(fixId, changes, [Diagnostics.Export_0_from_module_1, info.exportName.node.text, info.moduleSpecifier], fixId, Diagnostics.Export_all_missing_members)];
},
getAllCodeActions(context) {
const { program } = context;
return createCombinedCodeActions(textChanges.ChangeTracker.with(context, changes => {
const exports = new Map<SourceFile, ModuleExports>();
eachDiagnostic(context, errorCodes, diag => {
const info = getInfo(diag.file, diag.start, program);
if (info === undefined) return undefined;
const { exportName, node, moduleSourceFile } = info;
if (tryGetExportDeclaration(moduleSourceFile, exportName.isTypeOnly) === undefined && canHaveExportModifier(node)) {
changes.insertExportModifier(moduleSourceFile, node);
}
else {
const moduleExports = exports.get(moduleSourceFile) || { typeOnlyExports: [], exports: [] };
if (exportName.isTypeOnly) {
moduleExports.typeOnlyExports.push(exportName);
}
else {
moduleExports.exports.push(exportName);
}
exports.set(moduleSourceFile, moduleExports);
}
});
exports.forEach((moduleExports, moduleSourceFile) => {
const exportDeclaration = tryGetExportDeclaration(moduleSourceFile, /*isTypeOnly*/ true);
if (exportDeclaration && exportDeclaration.isTypeOnly) {
doChanges(changes, moduleSourceFile, moduleExports.typeOnlyExports, exportDeclaration);
doChanges(changes, moduleSourceFile, moduleExports.exports, tryGetExportDeclaration(moduleSourceFile, /*isTypeOnly*/ false));
}
else {
doChanges(changes, moduleSourceFile, [...moduleExports.exports, ...moduleExports.typeOnlyExports], exportDeclaration);
}
});
}));
}
});
interface ModuleExports {
typeOnlyExports: ExportName[];
exports: ExportName[];
}
interface ExportName {
node: Identifier;
isTypeOnly: boolean;
}
interface Info {
exportName: ExportName;
node: Declaration | VariableStatement;
moduleSourceFile: SourceFile;
moduleSpecifier: string;
}
function getInfo(sourceFile: SourceFile, pos: number, program: Program): Info | undefined {
const token = getTokenAtPosition(sourceFile, pos);
if (isIdentifier(token)) {
const importDeclaration = findAncestor(token, isImportDeclaration);
if (importDeclaration === undefined) return undefined;
const moduleSpecifier = isStringLiteral(importDeclaration.moduleSpecifier) ? importDeclaration.moduleSpecifier.text : undefined;
if (moduleSpecifier === undefined) return undefined;
const resolvedModule = getResolvedModule(sourceFile, moduleSpecifier, /*mode*/ undefined);
if (resolvedModule === undefined) return undefined;
const moduleSourceFile = program.getSourceFile(resolvedModule.resolvedFileName);
if (moduleSourceFile === undefined || isSourceFileFromLibrary(program, moduleSourceFile)) return undefined;
const moduleSymbol = moduleSourceFile.symbol;
const locals = moduleSymbol.valueDeclaration?.locals;
if (locals === undefined) return undefined;
const localSymbol = locals.get(token.escapedText);
if (localSymbol === undefined) return undefined;
const node = getNodeOfSymbol(localSymbol);
if (node === undefined) return undefined;
const exportName = { node: token, isTypeOnly: isTypeDeclaration(node) };
return { exportName, node, moduleSourceFile, moduleSpecifier };
}
return undefined;
}
function doChange(changes: textChanges.ChangeTracker, { exportName, node, moduleSourceFile }: Info) {
const exportDeclaration = tryGetExportDeclaration(moduleSourceFile, exportName.isTypeOnly);
if (exportDeclaration) {
updateExport(changes, moduleSourceFile, exportDeclaration, [exportName]);
}
else if (canHaveExportModifier(node)) {
changes.insertExportModifier(moduleSourceFile, node);
}
else {
createExport(changes, moduleSourceFile, [exportName]);
}
}
function doChanges(changes: textChanges.ChangeTracker, sourceFile: SourceFile, moduleExports: ExportName[], node: ExportDeclaration | undefined) {
if (node) {
updateExport(changes, sourceFile, node, moduleExports);
}
else {
createExport(changes, sourceFile, moduleExports);
}
}
function tryGetExportDeclaration(sourceFile: SourceFile, isTypeOnly: boolean) {
const predicate = (node: Node): node is ExportDeclaration =>
isExportDeclaration(node) && (isTypeOnly && node.isTypeOnly || !node.isTypeOnly);
return findLast(sourceFile.statements, predicate);
}
function updateExport(changes: textChanges.ChangeTracker, sourceFile: SourceFile, node: ExportDeclaration, names: ExportName[]) {
const namedExports = node.exportClause && isNamedExports(node.exportClause) ? node.exportClause.elements : factory.createNodeArray([]);
const exportNames = map(names, n => ({ ...n, isTypeOnly: node.isTypeOnly ? false : n.isTypeOnly }));
changes.replaceNode(sourceFile, node,
factory.updateExportDeclaration(node, node.modifiers, node.isTypeOnly,
factory.createNamedExports(
factory.createNodeArray([...namedExports, ...createExportSpecifiers(exportNames)], /*hasTrailingComma*/ namedExports.hasTrailingComma)), node.moduleSpecifier, node.assertClause));
}
function createExport(changes: textChanges.ChangeTracker, sourceFile: SourceFile, names: ExportName[]) {
changes.insertNodeAtEndOfScope(sourceFile, sourceFile,
factory.createExportDeclaration(/*modifiers*/ undefined, /*isTypeOnly*/ false,
factory.createNamedExports(createExportSpecifiers(names)), /*moduleSpecifier*/ undefined, /*assertClause*/ undefined));
}
function createExportSpecifiers(names: ExportName[]) {
return factory.createNodeArray(map(names, n => factory.createExportSpecifier(n.isTypeOnly, /*propertyName*/ undefined, n.node)));
}
function getNodeOfSymbol(symbol: Symbol) {
if (symbol.valueDeclaration === undefined) {
return firstOrUndefined(symbol.declarations);
}
const declaration = symbol.valueDeclaration;
const variableStatement = isVariableDeclaration(declaration) ? tryCast(declaration.parent.parent, isVariableStatement) : undefined;
return variableStatement && length(variableStatement.declarationList.declarations) === 1 ? variableStatement : declaration;
}
}