-
Notifications
You must be signed in to change notification settings - Fork 220
/
react-default-props.ts
378 lines (328 loc) · 14.7 KB
/
react-default-props.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
import ts from 'typescript';
import { Plugin } from 'ts-migrate-server';
import updateSourceText, { SourceTextUpdate } from '../utils/updateSourceText';
import { createValidate, Properties } from '../utils/validateOptions';
type Options = {
useDefaultPropsHelper?: boolean;
};
const optionProperties: Properties = {
useDefaultPropsHelper: { type: 'boolean' },
};
/**
* At first, we are going to check is there any
* - `CompName.defaultProps = defaultPropsName;`
* - `static defaultProps = defaultPropsName`
* in the file
*/
const WITH_DEFAULT_PROPS_HELPER = `WithDefaultProps`;
const reactDefaultPropsPlugin: Plugin<Options> = {
name: 'react-default-props',
run({ sourceFile, text, options }) {
const importDeclarations = sourceFile.statements.filter(ts.isImportDeclaration);
const expressionStatements = sourceFile.statements.filter(ts.isExpressionStatement);
const classDeclarations = sourceFile.statements.filter(ts.isClassDeclaration);
const interfaceDeclarations = sourceFile.statements.filter(ts.isInterfaceDeclaration);
// sfcs default props assignments
const sfcsDefaultPropsAssignments = expressionStatements.filter(
(expressionStatement) =>
ts.isBinaryExpression(expressionStatement.expression) &&
ts.isPropertyAccessExpression(expressionStatement.expression.left) &&
expressionStatement.expression.left.name.getText() === 'defaultProps',
);
// class default props assigments
const classDefaultPropsAssignment = classDeclarations.filter(
(classDeclaration) =>
classDeclaration.members
.filter(ts.isPropertyDeclaration)
.filter((declaration) => declaration.name.getText() === 'defaultProps').length > 0,
);
if (sfcsDefaultPropsAssignments.length === 0 && classDefaultPropsAssignment.length === 0) {
return undefined;
}
const functionDeclarations = sourceFile.statements.filter(ts.isFunctionDeclaration);
const variableStatements = sourceFile.statements.filter(ts.isVariableStatement);
// will use Props type from here
const typeAliasDeclarations = sourceFile.statements.filter(ts.isTypeAliasDeclaration);
const updates: SourceTextUpdate[] = [];
const printer = ts.createPrinter();
const processedPropTypes = new Map<string, string>();
let shouldAddWithDefaultPropsImport = !importDeclarations.some((importDeclaration) =>
/WithDefaultProps/.test(importDeclaration.moduleSpecifier.getText()),
);
const insertWithDefaultPropsImport = () => {
if (shouldAddWithDefaultPropsImport) {
updates.push({
kind: 'insert',
index: 0,
text: `${printer.printNode(
ts.EmitHint.Unspecified,
getWithDefaultPropsImport(),
sourceFile,
)}\n`,
});
// it probably could be done in the better way :)
shouldAddWithDefaultPropsImport = false;
}
};
const modifyAndInsertPropsType = (
propsTypeAliasDeclaration: ts.TypeAliasDeclaration,
defaultPropsTypeName: string,
propsTypeName: string,
newTypeInsertPos: number,
componentTypeReference: ts.TypeReferenceNode,
componentName: string,
) => {
// we don't want process props types more than once
if (processedPropTypes.get(propsTypeName) === defaultPropsTypeName) return;
// prevent multiple usage of defalut props or WithDefaultProps
const alreadyHaveDefalutProps = ts.isIntersectionTypeNode(propsTypeAliasDeclaration.type)
? propsTypeAliasDeclaration.type.types.some(
(typeExp) =>
(ts.isTypeQueryNode(typeExp) &&
typeExp.exprName.getText() === defaultPropsTypeName) ||
typeExp.getText().includes(WITH_DEFAULT_PROPS_HELPER),
)
: ts.isTypeReferenceNode(propsTypeAliasDeclaration.type) &&
propsTypeAliasDeclaration.type.typeName.getText() === WITH_DEFAULT_PROPS_HELPER;
if (alreadyHaveDefalutProps) return;
if (options.useDefaultPropsHelper) insertWithDefaultPropsImport();
const indexOfTypeValue = ts.isIntersectionTypeNode(propsTypeAliasDeclaration.type)
? propsTypeAliasDeclaration.type.types.findIndex((typeEl) => ts.isTypeLiteralNode(typeEl))
: -1;
const propTypesAreOnlyReferences =
ts.isIntersectionTypeNode(propsTypeAliasDeclaration.type) && indexOfTypeValue === -1;
const propsTypeValueNode = ts.isIntersectionTypeNode(propsTypeAliasDeclaration.type)
? ts.factory.updateIntersectionTypeNode(
propsTypeAliasDeclaration.type,
ts.factory.createNodeArray(
propsTypeAliasDeclaration.type.types.filter(
(_, k) => propTypesAreOnlyReferences || indexOfTypeValue === k,
),
),
)
: propsTypeAliasDeclaration.type;
const doesPropsTypeHaveExport =
propsTypeAliasDeclaration.modifiers &&
propsTypeAliasDeclaration.modifiers.find(
(modifier) => modifier.kind === ts.SyntaxKind.ExportKeyword,
);
// rename type PropName -> type OwnPropName
let updatedProptypesName = `Own${propsTypeName}`;
// not an ideal way to prevent a double declaration of the OwnPropname,
// however, this should cover most of the cases
const alreadyHaveUpdatedName =
interfaceDeclarations.some((node) => node.name.text.includes(updatedProptypesName)) ||
typeAliasDeclarations.some((node) => node.name.text.includes(updatedProptypesName));
updatedProptypesName = alreadyHaveUpdatedName
? `Own${componentName}${propsTypeName}`
: updatedProptypesName;
const updatedPropTypesName = doesPropsTypeHaveExport ? propsTypeName : updatedProptypesName;
const updatedPropTypeAlias = ts.factory.updateTypeAliasDeclaration(
propsTypeAliasDeclaration,
propsTypeAliasDeclaration.decorators,
propsTypeAliasDeclaration.modifiers,
ts.factory.createIdentifier(updatedPropTypesName),
propsTypeAliasDeclaration.typeParameters,
propsTypeValueNode,
);
const index = propsTypeAliasDeclaration.pos;
const length = propsTypeAliasDeclaration.end - index;
const text = printer.printNode(ts.EmitHint.Unspecified, updatedPropTypeAlias, sourceFile);
updates.push({ kind: 'replace', index, length, text: `\n\n${text}` });
// create type Props = WithDefaultProps<OwnProps, typeof defaultProps> & types;
const newPropsTypeValue = options.useDefaultPropsHelper
? ts.factory.createTypeReferenceNode(WITH_DEFAULT_PROPS_HELPER, [
ts.factory.createTypeReferenceNode(updatedPropTypesName, undefined),
ts.factory.createTypeQueryNode(ts.factory.createIdentifier(defaultPropsTypeName)),
])
: ts.factory.createIntersectionTypeNode([
ts.factory.createTypeReferenceNode(updatedPropTypesName, undefined),
ts.factory.createTypeQueryNode(ts.factory.createIdentifier(defaultPropsTypeName)),
]);
const componentPropsTypeName = doesPropsTypeHaveExport
? `Private${propsTypeName}`
: propsTypeName;
const newPropsTypeAlias = ts.factory.createTypeAliasDeclaration(
undefined,
undefined,
ts.factory.createIdentifier(componentPropsTypeName),
undefined,
ts.isIntersectionTypeNode(propsTypeAliasDeclaration.type)
? ts.factory.createIntersectionTypeNode([
newPropsTypeValue,
...propsTypeAliasDeclaration.type.types.filter((el, k) =>
propTypesAreOnlyReferences
? ts.isIntersectionTypeNode(updatedPropTypeAlias) &&
!(updatedPropTypeAlias as ts.IntersectionTypeNode).types.includes(el)
: indexOfTypeValue !== k,
),
])
: newPropsTypeValue,
);
updates.push({
kind: 'insert',
index: newTypeInsertPos,
text: `\n\n${printer.printNode(ts.EmitHint.Unspecified, newPropsTypeAlias, sourceFile)}`,
});
// we should rename component prop type in that case
if (doesPropsTypeHaveExport) {
const updatedComponentTypeReference = ts.factory.updateTypeReferenceNode(
componentTypeReference,
ts.factory.createIdentifier(componentPropsTypeName),
undefined,
);
const index = componentTypeReference.pos;
const length = componentTypeReference.end - index;
const text = printer.printNode(
ts.EmitHint.Unspecified,
updatedComponentTypeReference,
sourceFile,
);
updates.push({ kind: 'replace', index, length, text: `${text}` });
}
processedPropTypes.set(propsTypeName, defaultPropsTypeName);
};
/* process all default props assignments:
* - find out, is it object literal expression or identifier obj reference
* - if it's obj literal - use `CompName.defaultProps`, otherwise - save variable name
* - create OwnProp type
* - create/modify type Props = WithDefaultProps
* - use a new Props type in the component
*/
sfcsDefaultPropsAssignments.forEach((defaultProp) => {
const expression = defaultProp.expression as ts.BinaryExpression;
const leftPart = expression.left as ts.PropertyAccessExpression;
const componentName = leftPart.expression.getText();
const isObjectIdentifier = ts.isIdentifier(expression.right);
const defaultPropsTypeName = isObjectIdentifier
? expression.right.getText()
: `${componentName}.defaultProps`;
const variableDeclaration = variableStatements.find(
(variableStatement) =>
!!variableStatement.declarationList.declarations.find(
(declaration) => !!(declaration.name && declaration.name.getText() === componentName),
),
);
const variableInitializer = variableDeclaration
? variableDeclaration.declarationList.declarations[0].initializer
: undefined;
const componentDeclaration =
functionDeclarations.find(
(functionDeclaration) =>
!!(functionDeclaration.name && functionDeclaration.name.getText() === componentName),
) ||
(variableInitializer &&
(ts.isArrowFunction(variableInitializer) || ts.isFunctionDeclaration(variableInitializer))
? variableInitializer
: undefined);
// hasPropsTypeReference
if (
componentDeclaration &&
componentDeclaration.parameters.length === 1 &&
componentDeclaration.parameters[0].type !== undefined &&
ts.isTypeReferenceNode(componentDeclaration.parameters[0].type)
) {
const propsTypeName = componentDeclaration.parameters[0].type.getText();
const propsTypeAliasDeclarations = typeAliasDeclarations.find(
(typeAlias) => typeAlias.name.getText() === propsTypeName,
);
if (propsTypeAliasDeclarations) {
const newTypeInsertPos =
variableDeclaration && variableInitializer
? variableDeclaration.pos
: componentDeclaration.pos;
modifyAndInsertPropsType(
propsTypeAliasDeclarations,
defaultPropsTypeName,
propsTypeName,
newTypeInsertPos,
componentDeclaration.parameters[0].type,
componentName,
);
}
}
});
classDefaultPropsAssignment.forEach((classDeclaration) => {
const componentName = classDeclaration.name && classDeclaration.name.getText();
const defaultPropsDeclaration = classDeclaration.members
.filter(ts.isPropertyDeclaration)
.filter((declaration) => declaration.name.getText() === 'defaultProps')[0];
if (!defaultPropsDeclaration) return;
const isObjectIdentifier =
defaultPropsDeclaration.initializer && ts.isIdentifier(defaultPropsDeclaration.initializer);
const defaultPropsTypeName =
isObjectIdentifier && defaultPropsDeclaration.initializer
? defaultPropsDeclaration.initializer.getText()
: `${componentName}.defaultProps`;
const variableDeclaration = variableStatements.find(
(variableStatement) =>
!!variableStatement.declarationList.declarations.find(
(declaration) => !!(declaration.name && declaration.name.getText() === componentName),
),
);
const variableInitializer = variableDeclaration
? variableDeclaration.declarationList.declarations[0].initializer
: undefined;
const heritageClause = classDeclaration.heritageClauses
? classDeclaration.heritageClauses.find((heritageClause) => heritageClause.types.length > 0)
: undefined;
const expressionWithTypeArguments =
heritageClause && heritageClause.types
? heritageClause.types.find(
(x) =>
ts.isExpressionWithTypeArguments(x) &&
(x.typeArguments ? x.typeArguments.length > 0 : false),
)
: undefined;
const propsTypeReferenceNode =
expressionWithTypeArguments && expressionWithTypeArguments.typeArguments
? (expressionWithTypeArguments.typeArguments.find((genericArgs) =>
ts.isTypeReferenceNode(genericArgs),
) as ts.TypeReferenceNode | undefined)
: undefined;
// wasn't able to find a typename of the props :(
if (!propsTypeReferenceNode || !componentName) return;
const propsTypeName = propsTypeReferenceNode.getText();
const propsTypeAliasDeclarations = typeAliasDeclarations.find(
(typeAlias) => typeAlias.name.getText() === propsTypeName,
);
if (propsTypeAliasDeclarations) {
const newTypeInsertPos =
variableDeclaration && variableInitializer
? variableDeclaration.pos
: classDeclaration.pos;
modifyAndInsertPropsType(
propsTypeAliasDeclarations,
defaultPropsTypeName,
propsTypeName,
newTypeInsertPos,
propsTypeReferenceNode,
componentName,
);
}
});
return updateSourceText(text, updates);
},
validate: createValidate(optionProperties),
};
// the target project might not have this as an internal dependency in project.json
// It would have to be manually added, otherwise CI will complain about it
function getWithDefaultPropsImport() {
return ts.factory.createImportDeclaration(
undefined,
undefined,
ts.factory.createImportClause(
false,
undefined,
ts.factory.createNamedImports([
ts.factory.createImportSpecifier(
false,
undefined,
ts.factory.createIdentifier('WithDefaultProps'),
),
]),
),
ts.factory.createStringLiteral(':ts-utils/types/WithDefaultProps'),
);
}
export default reactDefaultPropsPlugin;