Skip to content

Commit aaf86ed

Browse files
vincentbelmohsen1
authored andcommitted
Update TypeScript to 2.6 (lyft#16)
* ts 2.6 * fix: TransformerFactory type should be SourceFile rather than Node TypeScript will complain about: "Type 'Node' is not assignable to type 'SourceFile'." * fix: use lodash to fix type error ts.NodeArray now is ReadonlyArray, helper functions should accept ArrayLike arguments. * flatten function structure * fix ts type mismatch * update test output * replace helpers.isKind with ts.isXXX * namespace import lodash & use lodash.find directly
1 parent cff8772 commit aaf86ed

File tree

36 files changed

+407
-3610
lines changed

36 files changed

+407
-3610
lines changed

package-lock.json

+13-2,008
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,15 @@
3333
"chalk": "^1.1.3",
3434
"commander": "^2.10.0",
3535
"glob": "^7.1.2",
36-
"typescript": "^2.4.0"
36+
"lodash": "^4.17.4",
37+
"typescript": "^2.6.2"
3738
},
3839
"devDependencies": {
3940
"@types/chalk": "^0.4.31",
4041
"@types/commander": "^2.9.1",
4142
"@types/glob": "^5.0.30",
4243
"@types/jest": "^20.0.2",
44+
"@types/lodash": "^4.14.93",
4345
"@types/node": "^8.0.2",
4446
"@types/react": "^15.0.31",
4547
"jest": "^20.0.4",

src/helpers/index.ts

+24-55
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import * as ts from 'typescript';
2-
import * as kinds from './isKind';
3-
4-
export * from './isKind';
2+
import * as _ from 'lodash';
53

64
/**
75
* If a class declaration a react class?
@@ -54,7 +52,7 @@ export function isReactComponent(classDeclaration: ts.ClassDeclaration, typeChec
5452
export function isReactHeritageClause(clause: ts.HeritageClause) {
5553
return clause.token === ts.SyntaxKind.ExtendsKeyword &&
5654
clause.types.length === 1 &&
57-
kinds.isExpressionWithTypeArguments(clause.types[0]) &&
55+
ts.isExpressionWithTypeArguments(clause.types[0]) &&
5856
/Component/.test(clause.types[0].expression.getText());
5957
}
6058

@@ -65,10 +63,10 @@ export function isReactHeritageClause(clause: ts.HeritageClause) {
6563
* @param statement
6664
*/
6765
export function isReactPropTypeAssignmentStatement(statement: ts.Statement): statement is ts.ExpressionStatement {
68-
return kinds.isExpressionStatement(statement)
69-
&& kinds.isBinaryExpression(statement.expression)
66+
return ts.isExpressionStatement(statement)
67+
&& ts.isBinaryExpression(statement.expression)
7068
&& statement.expression.operatorToken.kind === ts.SyntaxKind.FirstAssignment
71-
&& kinds.isPropertyAccessExpression(statement.expression.left)
69+
&& ts.isPropertyAccessExpression(statement.expression.left)
7270
&& /\.propTypes$|\.propTypes\..+$/.test(statement.expression.left.getText())
7371
}
7472

@@ -80,7 +78,7 @@ export function hasStaticModifier(classMember: ts.ClassElement) {
8078
if (!classMember.modifiers) {
8179
return false;
8280
}
83-
const staticModifier = find(classMember.modifiers, (modifier) => {
81+
const staticModifier = _.find(classMember.modifiers, (modifier) => {
8482
return modifier.kind == ts.SyntaxKind.StaticKeyword;
8583
});
8684
return staticModifier !== undefined;
@@ -99,49 +97,18 @@ export function isPropTypesMember(classMember: ts.ClassElement, sourceFile: ts.S
9997
}
10098
}
10199

102-
// TODO: replace following functions with Lodash?
103-
// ---------------------------------------------------------------------------------------------------------
104-
105-
/**
106-
* Find an item in a collection with a matcher
107-
* @param collection
108-
* @param matcher
109-
*/
110-
export function find<T>(collection: T[], matcher: (item: T) => boolean): T | undefined {
111-
for (const item of collection) {
112-
if (matcher(item)) { return item; }
113-
}
114-
115-
return undefined;
116-
}
117-
118-
/**
119-
* Look in a collection and see if collection has a specific item
120-
* @param collection
121-
* @param matcher
122-
*/
123-
export function has<T>(collection: T[], matcher: (item: T) => boolean): boolean {
124-
if (!collection || !collection.length) {
125-
return false;
126-
}
127-
128-
for (const item of collection) {
129-
if (matcher(item)) { return true; }
130-
}
131-
132-
return false;
133-
}
134-
135100
/**
136101
* Insert an item in middle of an array after a specific item
137102
* @param collection
138103
* @param afterItem
139104
* @param newItem
140105
*/
141-
export function insertAfter<T>(collection: T[], afterItem: T, newItem: T) {
142-
const index = collection.indexOf(afterItem) + 1;
106+
export function insertAfter<T>(collection: ArrayLike<T>, afterItem: T, newItem: T) {
107+
const index = _.indexOf(collection, afterItem) + 1;
143108

144-
return collection.slice(0, index).concat(newItem).concat(collection.slice(index));
109+
return _.slice(collection, 0, index)
110+
.concat(newItem)
111+
.concat(_.slice(collection, index));
145112
}
146113

147114
/**
@@ -150,10 +117,12 @@ export function insertAfter<T>(collection: T[], afterItem: T, newItem: T) {
150117
* @param beforeItem
151118
* @param newItem
152119
*/
153-
export function insertBefore<T>(collection: T[], beforeItem: T, newItem: T) {
154-
const index = collection.indexOf(beforeItem);
120+
export function insertBefore<T>(collection: ArrayLike<T>, beforeItem: T, newItems: T | T[]) {
121+
const index = _.indexOf(collection, beforeItem);
155122

156-
return collection.slice(0, index).concat(newItem).concat(collection.slice(index));
123+
return _.slice(collection, 0, index)
124+
.concat(newItems)
125+
.concat(_.slice(collection, index));
157126
}
158127

159128
/**
@@ -162,10 +131,11 @@ export function insertBefore<T>(collection: T[], beforeItem: T, newItem: T) {
162131
* @param item
163132
* @param newItem
164133
*/
165-
export function replaceItem<T>(collection: T[], item: T, newItem: T) {
166-
const index = collection.indexOf(item);
167-
168-
return collection.slice(0, index).concat(newItem).concat(collection.slice(index + 1));
134+
export function replaceItem<T>(collection: ArrayLike<T>, item: T, newItem: T) {
135+
const index = _.indexOf(collection, item);
136+
return _.slice(collection, 0, index)
137+
.concat(newItem)
138+
.concat(_.slice(collection, index + 1));
169139
}
170140

171141
/**
@@ -174,8 +144,7 @@ export function replaceItem<T>(collection: T[], item: T, newItem: T) {
174144
* @param item
175145
* @param newItem
176146
*/
177-
export function removeItem<T>(collection: T[], item: T) {
178-
const index = collection.indexOf(item);
179-
180-
return collection.slice(0, index).concat(collection.slice(index + 1));
147+
export function removeItem<T>(collection: ArrayLike<T>, item: T) {
148+
const index = _.indexOf(collection, item);
149+
return _.slice(collection, 0, index).concat(_.slice(collection, index + 1));
181150
}

0 commit comments

Comments
 (0)