Skip to content

Commit 3cd70b8

Browse files
pnevykgajus
authored andcommittedJun 1, 2018
fix: variance issues with babel 7 (#339)
1 parent 647807f commit 3cd70b8

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed
 

‎src/rules/sortKeys.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ const variances = {
7070
plus: '+'
7171
};
7272

73+
const getVariance = (node) => {
74+
if (_.isString(node.variance)) {
75+
return variances[node.variance] || '';
76+
} else if (_.get(node, 'variance.type') === 'Variance') {
77+
return variances[node.variance.kind] || '';
78+
} else {
79+
return '';
80+
}
81+
};
82+
7383
const generateOrderedList = (context, sort, properties) => {
7484
return properties.map((property) => {
7585
const name = getParameterName(property, context);
@@ -84,7 +94,7 @@ const generateOrderedList = (context, sort, properties) => {
8494
value = context.getSourceCode().getText(property.value);
8595
}
8696

87-
return [(variances[property.variance] || '') + name + (property.optional ? '?' : ''), value];
97+
return [name, getVariance(property) + name + (property.optional ? '?' : ''), value];
8898
})
8999
.sort((first, second) => {
90100
return sort(first[0], second[0]) ? -1 : 1;
@@ -94,7 +104,7 @@ const generateOrderedList = (context, sort, properties) => {
94104
return item[0];
95105
}
96106

97-
return item[0] + ': ' + item[1];
107+
return item[1] + ': ' + item[2];
98108
});
99109
};
100110

‎tests/rules/assertions/sortKeys.js

+40
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,46 @@ export default {
231231
},
232232
}
233233
`
234+
},
235+
{
236+
code: `
237+
type FooType = {
238+
+c: number,
239+
-b: number,
240+
a: number,
241+
}
242+
`,
243+
errors: [
244+
{message: 'Expected type annotations to be in ascending order. "b" should be before "c".'},
245+
{message: 'Expected type annotations to be in ascending order. "a" should be before "b".'}
246+
],
247+
output: `
248+
type FooType = {
249+
a: number,
250+
-b: number,
251+
+c: number,
252+
}
253+
`
254+
},
255+
{
256+
code: `
257+
type FooType = {|
258+
+c: number,
259+
-b: number,
260+
a: number,
261+
|}
262+
`,
263+
errors: [
264+
{message: 'Expected type annotations to be in ascending order. "b" should be before "c".'},
265+
{message: 'Expected type annotations to be in ascending order. "a" should be before "b".'}
266+
],
267+
output: `
268+
type FooType = {|
269+
a: number,
270+
-b: number,
271+
+c: number,
272+
|}
273+
`
234274
}
235275
/* eslint-enable no-restricted-syntax */
236276
],

0 commit comments

Comments
 (0)
Please sign in to comment.