forked from lyft/react-javascript-to-typescript-transform
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
197 lines (167 loc) · 5.72 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
import * as ts from 'typescript';
import * as kinds from './isKind';
export * from './isKind';
/**
* If a class declaration a react class?
* @param classDeclaration
* @param typeChecker
*/
export function isReactComponent(classDeclaration: ts.ClassDeclaration, typeChecker: ts.TypeChecker): boolean {
// Only classes that extend React.Component
if (!classDeclaration.heritageClauses) {
return false;
}
if (classDeclaration.heritageClauses.length !== 1) {
return false;
}
const firstHeritageClauses = classDeclaration.heritageClauses[0];
if (firstHeritageClauses.token !== ts.SyntaxKind.ExtendsKeyword) {
return false;
}
const expressionWithTypeArguments = firstHeritageClauses.types[0];
if (!expressionWithTypeArguments) {
return false;
}
// Try type checker and fallback to node text
const type = typeChecker.getTypeAtLocation(expressionWithTypeArguments);
let typeSymbol = type && type.symbol && type.symbol.name;
if (!typeSymbol) {
typeSymbol = expressionWithTypeArguments.expression.getText();
}
if (!/React\.Component|Component/.test(typeSymbol)) {
return false;
}
return true;
}
/**
* A temporary hack until ts allows creating type alias declaration nodes
* Creates a TypeAliasDeclaration
* @param name Name of type node
* @param type Type node
* @param sourceFile SourceFile for context
*/
export function createTypeDeclarationStatement(name: string, type: ts.TypeNode, sourceFile: ts.SourceFile) {
const printer = ts.createPrinter()
const typeString = printer.printNode(ts.EmitHint.Unspecified, type, sourceFile);
const newSource = ts.createSourceFile('temp.ts', `type ${name} = ${typeString}`, sourceFile.languageVersion);
return newSource.statements[0];
}
/**
* Determine if a ts.HeritageClause is React HeritageClause
*
* @example `extends React.Component<{}, void>` is a React HeritageClause
*
* @todo: this is lazy. Use the typeChecker instead
* @param clause
*/
export function isReactHeritageClause(clause: ts.HeritageClause) {
return clause.token === ts.SyntaxKind.ExtendsKeyword &&
clause.types.length === 1 &&
kinds.isExpressionWithTypeArguments(clause.types[0]) &&
/Component/.test(clause.types[0].expression.getText());
}
/**
* Return true if a statement is a React propType assignment statement
* @example
* SomeComponent.propTypes = { foo: React.PropTypes.string };
* @param statement
*/
export function isReactPropTypeAssignmentStatement(statement: ts.Statement): statement is ts.ExpressionStatement {
return kinds.isExpressionStatement(statement)
&& kinds.isBinaryExpression(statement.expression)
&& statement.expression.operatorToken.kind === ts.SyntaxKind.FirstAssignment
&& kinds.isPropertyAccessExpression(statement.expression.left)
&& /\.propTypes$|\.propTypes\..+$/.test(statement.expression.left.getText())
}
/**
* Does class member have a "static" member?
* @param classMember
*/
export function hasStaticModifier(classMember: ts.ClassElement) {
if (!classMember.modifiers) {
return false;
}
const staticModifier = find(classMember.modifiers, (modifier) => {
return modifier.kind == ts.SyntaxKind.StaticKeyword;
});
return staticModifier !== undefined;
}
/**
* Is class member a React "propTypes" member?
* @param classMember
* @param sourceFile
*/
export function isPropTypesMember(classMember: ts.ClassElement, sourceFile: ts.SourceFile) {
try {
return classMember.name !== undefined && classMember.name.getFullText(sourceFile) !== 'propTypes'
} catch (e) {
return false;
}
}
// TODO: replace following functions with Lodash?
// ---------------------------------------------------------------------------------------------------------
/**
* Find an item in a collection with a matcher
* @param collection
* @param matcher
*/
export function find<T>(collection: T[], matcher: (item: T) => boolean): T | undefined {
for (const item of collection) {
if (matcher(item)) { return item; }
}
return undefined;
}
/**
* Look in a collection and see if collection has a specific item
* @param collection
* @param matcher
*/
export function has<T>(collection: T[], matcher: (item: T) => boolean): boolean {
if (!collection || !collection.length) {
return false;
}
for (const item of collection) {
if (matcher(item)) { return true; }
}
return false;
}
/**
* Insert an item in middle of an array after a specific item
* @param collection
* @param afterItem
* @param newItem
*/
export function insertAfter<T>(collection: T[], afterItem: T, newItem: T) {
const index = collection.indexOf(afterItem) + 1;
return collection.slice(0, index).concat(newItem).concat(collection.slice(index));
}
/**
* Insert an item in middle of an array before a specific item
* @param collection
* @param beforeItem
* @param newItem
*/
export function insertBefore<T>(collection: T[], beforeItem: T, newItem: T) {
const index = collection.indexOf(beforeItem);
return collection.slice(0, index).concat(newItem).concat(collection.slice(index));
}
/**
* Replace an item in a collection with another item
* @param collection
* @param item
* @param newItem
*/
export function replaceItem<T>(collection: T[], item: T, newItem: T) {
const index = collection.indexOf(item);
return collection.slice(0, index).concat(newItem).concat(collection.slice(index + 1));
}
/**
* Remove an item from a collection
* @param collection
* @param item
* @param newItem
*/
export function removeItem<T>(collection: T[], item: T) {
const index = collection.indexOf(item);
return collection.slice(0, index).concat(collection.slice(index + 1));
}