-
Notifications
You must be signed in to change notification settings - Fork 220
/
react-class-lifecycle-methods.ts
127 lines (109 loc) · 4.74 KB
/
react-class-lifecycle-methods.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
import ts from 'typescript';
import { Plugin } from 'ts-migrate-server';
import { getReactComponentHeritageType, isReactClassComponent } from './utils/react';
import updateSourceText, { SourceTextUpdate } from '../utils/updateSourceText';
type Options = { force?: boolean };
const reactClassLifecycleMethodsPlugin: Plugin<Options> = {
name: 'react-class-lifecycle-methods',
run({ fileName, text, options }) {
return /\.tsx$/.test(fileName)
? annotateReactComponentLifecycleMethods(fileName, text, options.force)
: undefined;
},
};
export default reactClassLifecycleMethodsPlugin;
enum AnnotationKind {
Props = 'Props',
State = 'State',
Context = 'Context',
}
const reactLifecycleMethodAnnotations: { [method: string]: AnnotationKind[] } = {
// shouldComponentUpdate?(nextProps: Readonly<P>, nextState: Readonly<S>, nextContext: any): boolean;
shouldComponentUpdate: [AnnotationKind.Props, AnnotationKind.State, AnnotationKind.Context],
// componentDidUpdate?(prevProps: Readonly<P>, prevState: Readonly<S>, snapshot?: SS): void;
componentDidUpdate: [AnnotationKind.Props, AnnotationKind.State],
// componentWillReceiveProps?(nextProps: Readonly<P>, nextContext: any): void;
componentWillReceiveProps: [AnnotationKind.Props, AnnotationKind.Context],
// componentWillUpdate?(nextProps: Readonly<P>, nextState: Readonly<S>, nextContext: any): void;
componentWillUpdate: [AnnotationKind.Props, AnnotationKind.State, AnnotationKind.Context],
};
function updateParameterType(parameter: ts.ParameterDeclaration, type: ts.TypeNode | undefined) {
return ts.updateParameter(
parameter,
parameter.decorators,
parameter.modifiers,
parameter.dotDotDotToken,
parameter.name,
parameter.questionToken,
type,
parameter.initializer,
);
}
function annotateReactComponentLifecycleMethods(
fileName: string,
sourceText: string,
force = false,
) {
const sourceFile = ts.createSourceFile(fileName, sourceText, ts.ScriptTarget.Latest, true);
const printer = ts.createPrinter();
const updates: SourceTextUpdate[] = [];
sourceFile.statements.forEach((statement) => {
if (ts.isClassDeclaration(statement) && isReactClassComponent(statement)) {
const heritageType = getReactComponentHeritageType(statement)!;
const heritageTypeArgs = heritageType.typeArguments || [];
const propsType = heritageTypeArgs[0] || ts.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword);
const stateType = heritageTypeArgs[1] || ts.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword);
const annotationToType = {
[AnnotationKind.Props]: propsType,
[AnnotationKind.State]: stateType,
[AnnotationKind.Context]: ts.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword),
};
statement.members.forEach((member) => {
if (
ts.isConstructorDeclaration(member) &&
member.parameters.length === 1 &&
(member.parameters[0].type == null || force)
) {
const parameter = member.parameters[0];
const updatedParameter = updateParameterType(parameter, propsType);
updates.push({
kind: 'replace',
index: parameter.pos,
length: parameter.end - parameter.pos,
text: printer.printNode(ts.EmitHint.Unspecified, updatedParameter, sourceFile),
});
} else if (
ts.isMethodDeclaration(member) &&
ts.isIdentifier(member.name) &&
reactLifecycleMethodAnnotations[member.name.text] != null
) {
const annotations = reactLifecycleMethodAnnotations[member.name.text];
let didUpdateParameters = false;
const parametersToPrint: ts.ParameterDeclaration[] = [...member.parameters];
for (let i = 0; i < member.parameters.length; i += 1) {
const parameter = member.parameters[i];
const annotation = annotationToType[annotations[i]];
if (annotation != null && (parameter.type == null || force)) {
const updatedParameter = updateParameterType(parameter, annotation);
parametersToPrint[i] = updatedParameter;
didUpdateParameters = true;
}
}
if (didUpdateParameters) {
const start = member.parameters[0].pos;
const { end } = member.parameters[member.parameters.length - 1];
let text = printer.printList(
ts.ListFormat.Parameters,
ts.createNodeArray(parametersToPrint),
sourceFile,
);
// Remove surrounding parentheses
text = text.slice(1, text.length - 1);
updates.push({ kind: 'replace', index: start, length: end - start, text });
}
}
});
}
});
return updateSourceText(sourceText, updates);
}