1
1
import * as ts from 'typescript' ;
2
- import * as kinds from './isKind' ;
3
-
4
- export * from './isKind' ;
2
+ import * as _ from 'lodash' ;
5
3
6
4
/**
7
5
* If a class declaration a react class?
@@ -54,7 +52,7 @@ export function isReactComponent(classDeclaration: ts.ClassDeclaration, typeChec
54
52
export function isReactHeritageClause ( clause : ts . HeritageClause ) {
55
53
return clause . token === ts . SyntaxKind . ExtendsKeyword &&
56
54
clause . types . length === 1 &&
57
- kinds . isExpressionWithTypeArguments ( clause . types [ 0 ] ) &&
55
+ ts . isExpressionWithTypeArguments ( clause . types [ 0 ] ) &&
58
56
/ C o m p o n e n t / . test ( clause . types [ 0 ] . expression . getText ( ) ) ;
59
57
}
60
58
@@ -65,10 +63,10 @@ export function isReactHeritageClause(clause: ts.HeritageClause) {
65
63
* @param statement
66
64
*/
67
65
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 )
70
68
&& statement . expression . operatorToken . kind === ts . SyntaxKind . FirstAssignment
71
- && kinds . isPropertyAccessExpression ( statement . expression . left )
69
+ && ts . isPropertyAccessExpression ( statement . expression . left )
72
70
&& / \. p r o p T y p e s $ | \. p r o p T y p e s \. .+ $ / . test ( statement . expression . left . getText ( ) )
73
71
}
74
72
@@ -80,7 +78,7 @@ export function hasStaticModifier(classMember: ts.ClassElement) {
80
78
if ( ! classMember . modifiers ) {
81
79
return false ;
82
80
}
83
- const staticModifier = find ( classMember . modifiers , ( modifier ) => {
81
+ const staticModifier = _ . find ( classMember . modifiers , ( modifier ) => {
84
82
return modifier . kind == ts . SyntaxKind . StaticKeyword ;
85
83
} ) ;
86
84
return staticModifier !== undefined ;
@@ -99,49 +97,18 @@ export function isPropTypesMember(classMember: ts.ClassElement, sourceFile: ts.S
99
97
}
100
98
}
101
99
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
-
135
100
/**
136
101
* Insert an item in middle of an array after a specific item
137
102
* @param collection
138
103
* @param afterItem
139
104
* @param newItem
140
105
*/
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 ;
143
108
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 ) ) ;
145
112
}
146
113
147
114
/**
@@ -150,10 +117,12 @@ export function insertAfter<T>(collection: T[], afterItem: T, newItem: T) {
150
117
* @param beforeItem
151
118
* @param newItem
152
119
*/
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 ) ;
155
122
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 ) ) ;
157
126
}
158
127
159
128
/**
@@ -162,10 +131,11 @@ export function insertBefore<T>(collection: T[], beforeItem: T, newItem: T) {
162
131
* @param item
163
132
* @param newItem
164
133
*/
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 ) ) ;
169
139
}
170
140
171
141
/**
@@ -174,8 +144,7 @@ export function replaceItem<T>(collection: T[], item: T, newItem: T) {
174
144
* @param item
175
145
* @param newItem
176
146
*/
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 ) ) ;
181
150
}
0 commit comments