-
Notifications
You must be signed in to change notification settings - Fork 12.8k
/
Copy pathconvertTypedefToType.ts
253 lines (218 loc) · 8.32 KB
/
convertTypedefToType.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import {
codeFixAll,
createCodeFixAction,
registerCodeFix,
} from "../_namespaces/ts.codefix.js";
import {
Diagnostics,
factory,
flatMap,
getNewLineOrDefaultFromHost,
getSynthesizedDeepClone,
getTokenAtPosition,
hasJSDocNodes,
InterfaceDeclaration,
isJSDocTypedefTag,
isJSDocTypeLiteral,
JSDoc,
JSDocPropertyLikeTag,
JSDocTag,
JSDocTypedefTag,
JSDocTypeExpression,
JSDocTypeLiteral,
mapDefined,
Node,
PropertySignature,
some,
SourceFile,
SyntaxKind,
textChanges,
TypeAliasDeclaration,
} from "../_namespaces/ts.js";
const fixId = "convertTypedefToType";
const errorCodes = [Diagnostics.JSDoc_typedef_may_be_converted_to_TypeScript_type.code];
registerCodeFix({
fixIds: [fixId],
errorCodes,
getCodeActions(context) {
const newLineCharacter = getNewLineOrDefaultFromHost(context.host, context.formatContext.options);
const node = getTokenAtPosition(
context.sourceFile,
context.span.start,
);
if (!node) return;
const changes = textChanges.ChangeTracker.with(context, t => doChange(t, node, context.sourceFile, newLineCharacter));
if (changes.length > 0) {
return [
createCodeFixAction(
fixId,
changes,
Diagnostics.Convert_typedef_to_TypeScript_type,
fixId,
Diagnostics.Convert_all_typedef_to_TypeScript_types,
),
];
}
},
getAllCodeActions: context =>
codeFixAll(
context,
errorCodes,
(changes, diag) => {
const newLineCharacter = getNewLineOrDefaultFromHost(context.host, context.formatContext.options);
const node = getTokenAtPosition(diag.file, diag.start);
const fixAll = true;
if (node) doChange(changes, node, diag.file, newLineCharacter, fixAll);
},
),
});
function doChange(
changes: textChanges.ChangeTracker,
node: Node,
sourceFile: SourceFile,
newLine: string,
fixAll = false,
) {
if (!isJSDocTypedefTag(node)) return;
const declaration = createDeclaration(node);
if (!declaration) return;
const commentNode = node.parent;
const { leftSibling, rightSibling } = getLeftAndRightSiblings(node);
let pos = commentNode.getStart();
let prefix = "";
// the first @typedef is the comment block with a text comment above
if (!leftSibling && commentNode.comment) {
pos = findEndOfTextBetween(commentNode, commentNode.getStart(), node.getStart());
prefix = `${newLine} */${newLine}`;
}
if (leftSibling) {
if (fixAll && isJSDocTypedefTag(leftSibling)) {
// Don't need to keep empty comment clock between created interfaces
pos = node.getStart();
prefix = "";
}
else {
pos = findEndOfTextBetween(commentNode, leftSibling.getStart(), node.getStart());
prefix = `${newLine} */${newLine}`;
}
}
let end = commentNode.getEnd();
let suffix = "";
if (rightSibling) {
if (fixAll && isJSDocTypedefTag(rightSibling)) {
// Don't need to keep empty comment clock between created interfaces
end = rightSibling.getStart();
suffix = `${newLine}${newLine}`;
}
else {
end = rightSibling.getStart();
suffix = `${newLine}/**${newLine} * `;
}
}
changes.replaceRange(sourceFile, { pos, end }, declaration, { prefix, suffix });
}
function getLeftAndRightSiblings(typedefNode: JSDocTypedefTag): { leftSibling?: Node; rightSibling?: Node; } {
const commentNode = typedefNode.parent;
const maxChildIndex = commentNode.getChildCount() - 1;
const currentNodeIndex = commentNode.getChildren().findIndex(
n => n.getStart() === typedefNode.getStart() && n.getEnd() === typedefNode.getEnd(),
);
const leftSibling = currentNodeIndex > 0 ? commentNode.getChildAt(currentNodeIndex - 1) : undefined;
const rightSibling = currentNodeIndex < maxChildIndex ? commentNode.getChildAt(currentNodeIndex + 1) : undefined;
return { leftSibling, rightSibling };
}
/**
* Finds the index of the last meaningful symbol (except empty spaces, * and /) in the comment
* between start and end positions
*/
function findEndOfTextBetween(jsDocComment: JSDoc, from: number, to: number): number {
const comment = jsDocComment.getText().substring(from - jsDocComment.getStart(), to - jsDocComment.getStart());
for (let i = comment.length; i > 0; i--) {
if (!/[*/\s]/.test(comment.substring(i - 1, i))) {
return from + i;
}
}
return to;
}
function createDeclaration(tag: JSDocTypedefTag): InterfaceDeclaration | TypeAliasDeclaration | undefined {
const { typeExpression } = tag;
if (!typeExpression) return;
const typeName = tag.name?.getText();
if (!typeName) return;
// For use case @typedef {object}Foo @property{bar}number
// But object type can be nested, meaning the value in the k/v pair can be the object itself
if (typeExpression.kind === SyntaxKind.JSDocTypeLiteral) {
return createInterfaceForTypeLiteral(typeName, typeExpression);
}
// for use case @typedef {(number|string|undefined)} Foo or @typedef {number|string|undefined} Foo
// In this case, we reach the leaf node of AST.
if (typeExpression.kind === SyntaxKind.JSDocTypeExpression) {
return createTypeAliasForTypeExpression(typeName, typeExpression);
}
}
function createInterfaceForTypeLiteral(
typeName: string,
typeLiteral: JSDocTypeLiteral,
): InterfaceDeclaration | undefined {
const propertySignatures = createSignatureFromTypeLiteral(typeLiteral);
if (!some(propertySignatures)) return;
return factory.createInterfaceDeclaration(
/*modifiers*/ undefined,
typeName,
/*typeParameters*/ undefined,
/*heritageClauses*/ undefined,
propertySignatures,
);
}
function createTypeAliasForTypeExpression(
typeName: string,
typeExpression: JSDocTypeExpression,
): TypeAliasDeclaration | undefined {
const typeReference = getSynthesizedDeepClone(typeExpression.type);
if (!typeReference) return;
return factory.createTypeAliasDeclaration(
/*modifiers*/ undefined,
factory.createIdentifier(typeName),
/*typeParameters*/ undefined,
typeReference,
);
}
function createSignatureFromTypeLiteral(typeLiteral: JSDocTypeLiteral): PropertySignature[] | undefined {
const propertyTags = typeLiteral.jsDocPropertyTags;
if (!some(propertyTags)) return;
const getSignature = (tag: JSDocPropertyLikeTag) => {
const name = getPropertyName(tag);
const type = tag.typeExpression?.type;
const isOptional = tag.isBracketed;
let typeReference;
// Recursively handle nested object type
if (type && isJSDocTypeLiteral(type)) {
const signatures = createSignatureFromTypeLiteral(type);
typeReference = factory.createTypeLiteralNode(signatures);
}
// Leaf node, where type.kind === SyntaxKind.JSDocTypeExpression
else if (type) {
typeReference = getSynthesizedDeepClone(type);
}
if (typeReference && name) {
const questionToken = isOptional ? factory.createToken(SyntaxKind.QuestionToken) : undefined;
return factory.createPropertySignature(
/*modifiers*/ undefined,
name,
questionToken,
typeReference,
);
}
};
return mapDefined(propertyTags, getSignature);
}
function getPropertyName(tag: JSDocPropertyLikeTag): string | undefined {
return tag.name.kind === SyntaxKind.Identifier ? tag.name.text : tag.name.right.text;
}
/** @internal */
export function getJSDocTypedefNodes(node: Node): readonly JSDocTag[] {
if (hasJSDocNodes(node)) {
return flatMap(node.jsDoc, doc => doc.tags?.filter(tag => isJSDocTypedefTag(tag)));
}
return [];
}