Skip to content

Commit f06bcd6

Browse files
author
Keyan Zhang
committed
fix lint errors
1 parent 3cc6d71 commit f06bcd6

File tree

4 files changed

+107
-103
lines changed

4 files changed

+107
-103
lines changed

.eslintrc.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,8 @@ module.exports = {
1010
ecmaFeatures: {
1111
modules: false
1212
},
13+
14+
rules: {
15+
'no-use-before-define': 2,
16+
},
1317
};

transforms/class.js

Lines changed: 88 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,75 @@ module.exports = (file, api, options) => {
7373
}
7474
}
7575

76+
// ---------------------------------------------------------------------------
77+
// Helpers
78+
const createFindPropFn = prop => property => (
79+
property.key &&
80+
property.key.type === 'Identifier' &&
81+
property.key.name === prop
82+
);
83+
84+
const filterDefaultPropsField = node =>
85+
createFindPropFn(DEFAULT_PROPS_FIELD)(node);
86+
87+
const filterGetInitialStateField = node =>
88+
createFindPropFn(GET_INITIAL_STATE_FIELD)(node);
89+
90+
const findGetInitialState = specPath =>
91+
specPath.properties.find(createFindPropFn(GET_INITIAL_STATE_FIELD));
92+
93+
const withComments = (to, from) => {
94+
to.comments = from.comments;
95+
return to;
96+
};
97+
98+
const isPrimExpression = node => (
99+
node.type === 'Literal' || ( // NOTE this might change in babylon v6
100+
node.type === 'Identifier' &&
101+
node.name === 'undefined'
102+
));
103+
104+
const isFunctionExpression = node => (
105+
node.key &&
106+
node.key.type === 'Identifier' &&
107+
node.value &&
108+
node.value.type === 'FunctionExpression'
109+
);
110+
111+
const isPrimProperty = prop => (
112+
prop.key &&
113+
prop.key.type === 'Identifier' &&
114+
prop.value &&
115+
isPrimExpression(prop.value)
116+
);
117+
118+
const isPrimPropertyWithTypeAnnotation = prop => (
119+
prop.key &&
120+
prop.key.type === 'Identifier' &&
121+
prop.value &&
122+
prop.value.type === 'TypeCastExpression' &&
123+
isPrimExpression(prop.value.expression)
124+
);
125+
126+
const hasSingleReturnStatementWithObject = value => (
127+
value.type === 'FunctionExpression' &&
128+
value.body &&
129+
value.body.type === 'BlockStatement' &&
130+
value.body.body &&
131+
value.body.body.length === 1 &&
132+
value.body.body[0].type === 'ReturnStatement' &&
133+
value.body.body[0].argument &&
134+
value.body.body[0].argument.type === 'ObjectExpression'
135+
);
136+
137+
const isInitialStateLiftable = getInitialState => {
138+
if (!getInitialState || !(getInitialState.value)) {
139+
return true;
140+
}
141+
142+
return hasSingleReturnStatementWithObject(getInitialState.value);
143+
};
144+
76145
// ---------------------------------------------------------------------------
77146
// Checks if the module uses mixins or accesses deprecated APIs.
78147
const checkDeprecatedAPICalls = classPath =>
@@ -232,57 +301,28 @@ module.exports = (file, api, options) => {
232301
return true;
233302
};
234303

235-
// ---------------------------------------------------------------------------
236-
// Helpers
237-
const createFindPropFn = prop => property => (
238-
property.key &&
239-
property.key.type === 'Identifier' &&
240-
property.key.name === prop
241-
);
242-
243-
const filterDefaultPropsField = node =>
244-
createFindPropFn(DEFAULT_PROPS_FIELD)(node);
245-
246-
const filterGetInitialStateField = node =>
247-
createFindPropFn(GET_INITIAL_STATE_FIELD)(node);
248-
249-
const findGetInitialState = specPath =>
250-
specPath.properties.find(createFindPropFn(GET_INITIAL_STATE_FIELD));
251-
252-
const withComments = (to, from) => {
253-
to.comments = from.comments;
254-
return to;
255-
};
256-
257304
// ---------------------------------------------------------------------------
258305
// Collectors
259-
const isFunctionExpression = node => (
260-
node.key &&
261-
node.key.type === 'Identifier' &&
262-
node.value &&
263-
node.value.type === 'FunctionExpression'
264-
);
265-
266-
const isPrimProperty = prop => (
267-
prop.key &&
268-
prop.key.type === 'Identifier' &&
269-
prop.value &&
270-
isPrimExpression(prop.value)
271-
);
272-
273-
const isPrimPropertyWithTypeAnnotation = prop => (
274-
prop.key &&
275-
prop.key.type === 'Identifier' &&
276-
prop.value &&
277-
prop.value.type === 'TypeCastExpression' &&
278-
isPrimExpression(prop.value.expression)
279-
);
306+
const pickReturnValueOrCreateIIFE = value => {
307+
if (hasSingleReturnStatementWithObject(value)) {
308+
return value.body.body[0].argument;
309+
} else {
310+
return j.callExpression(
311+
value,
312+
[]
313+
);
314+
}
315+
};
280316

281-
const isPrimExpression = node => (
282-
node.type === 'Literal' || ( // NOTE this might change in babylon v6
283-
node.type === 'Identifier' &&
284-
node.name === 'undefined'
285-
));
317+
const createDefaultProps = prop =>
318+
withComments(
319+
j.property(
320+
'init',
321+
j.identifier(DEFAULT_PROPS_KEY),
322+
pickReturnValueOrCreateIIFE(prop.value)
323+
),
324+
prop
325+
);
286326

287327
// Collects `childContextTypes`, `contextTypes`, `displayName`, and `propTypes`;
288328
// simplifies `getDefaultProps` or converts it to an IIFE;
@@ -361,14 +401,6 @@ module.exports = (file, api, options) => {
361401
fn.value
362402
), fn);
363403

364-
const isInitialStateLiftable = getInitialState => {
365-
if (!getInitialState || !(getInitialState.value)) {
366-
return true;
367-
}
368-
369-
return hasSingleReturnStatementWithObject(getInitialState.value);
370-
};
371-
372404
const updatePropsAccess = getInitialState =>
373405
j(getInitialState)
374406
.find(j.MemberExpression, {
@@ -440,17 +472,6 @@ module.exports = (file, api, options) => {
440472
}).getAST()[0].value.body.body;
441473
};
442474

443-
const pickReturnValueOrCreateIIFE = value => {
444-
if (hasSingleReturnStatementWithObject(value)) {
445-
return value.body.body[0].argument;
446-
} else {
447-
return j.callExpression(
448-
value,
449-
[]
450-
);
451-
}
452-
};
453-
454475
const convertInitialStateToClassProperty = getInitialState =>
455476
withComments(j.classProperty(
456477
j.identifier('state'),
@@ -853,27 +874,6 @@ module.exports = (file, api, options) => {
853874
const createStaticClassProperties = statics =>
854875
statics.map(createStaticClassProperty);
855876

856-
const hasSingleReturnStatementWithObject = value => (
857-
value.type === 'FunctionExpression' &&
858-
value.body &&
859-
value.body.type === 'BlockStatement' &&
860-
value.body.body &&
861-
value.body.body.length === 1 &&
862-
value.body.body[0].type === 'ReturnStatement' &&
863-
value.body.body[0].argument &&
864-
value.body.body[0].argument.type === 'ObjectExpression'
865-
);
866-
867-
const createDefaultProps = prop =>
868-
withComments(
869-
j.property(
870-
'init',
871-
j.identifier(DEFAULT_PROPS_KEY),
872-
pickReturnValueOrCreateIIFE(prop.value)
873-
),
874-
prop
875-
);
876-
877877
const getComments = classPath => {
878878
if (classPath.value.comments) {
879879
return classPath.value.comments;

transforms/sort-comp.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,16 @@ module.exports = function(fileInfo, api, options) {
3636
const printOptions =
3737
options.printOptions || {quote: 'single', trailingComma: true};
3838

39-
const methodsOrder = getMethodsOrder(fileInfo, options);
39+
const methodsOrder = getMethodsOrder(fileInfo, options); // eslint-disable-line no-use-before-define
4040

4141
const root = j(fileInfo.source);
4242

4343
const propertyComparator = (a, b) => {
4444
const nameA = a.key.name;
4545
const nameB = b.key.name;
4646

47-
const indexA = getCorrectIndex(methodsOrder, a);
48-
const indexB = getCorrectIndex(methodsOrder, b);
47+
const indexA = getCorrectIndex(methodsOrder, a); // eslint-disable-line no-use-before-define
48+
const indexB = getCorrectIndex(methodsOrder, b); // eslint-disable-line no-use-before-define
4949

5050
const sameLocation = indexA === indexB;
5151

transforms/utils/ReactUtils.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,18 @@ module.exports = function(j) {
8686
},
8787
});
8888

89+
const getReactCreateClassSpec = classPath => {
90+
const {value} = classPath;
91+
const args = (value.init || value.right || value.declaration).arguments;
92+
if (args) {
93+
const spec = args[0];
94+
if (spec.type === 'ObjectExpression' && Array.isArray(spec.properties)) {
95+
return spec;
96+
}
97+
}
98+
return null;
99+
};
100+
89101
// ---------------------------------------------------------------------------
90102
// Finds alias for React.Component if used as named import.
91103
const findReactComponentName = path => {
@@ -181,18 +193,6 @@ module.exports = function(j) {
181193

182194
// ---------------------------------------------------------------------------
183195
// Others
184-
const getReactCreateClassSpec = classPath => {
185-
const {value} = classPath;
186-
const args = (value.init || value.right || value.declaration).arguments;
187-
if (args) {
188-
const spec = args[0];
189-
if (spec.type === 'ObjectExpression' && Array.isArray(spec.properties)) {
190-
return spec;
191-
}
192-
}
193-
return null;
194-
};
195-
196196
const getClassExtendReactSpec = classPath => classPath.value.body;
197197

198198
const createCreateReactClassCallExpression = properties =>

0 commit comments

Comments
 (0)