Skip to content

Commit

Permalink
fix: variance issues with babel 7 (#339)
Browse files Browse the repository at this point in the history
  • Loading branch information
pnevyk authored and gajus committed Jun 1, 2018
1 parent 647807f commit 3cd70b8
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/rules/sortKeys.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ const variances = {
plus: '+'
};

const getVariance = (node) => {
if (_.isString(node.variance)) {
return variances[node.variance] || '';
} else if (_.get(node, 'variance.type') === 'Variance') {
return variances[node.variance.kind] || '';
} else {
return '';
}
};

const generateOrderedList = (context, sort, properties) => {
return properties.map((property) => {
const name = getParameterName(property, context);
Expand All @@ -84,7 +94,7 @@ const generateOrderedList = (context, sort, properties) => {
value = context.getSourceCode().getText(property.value);
}

return [(variances[property.variance] || '') + name + (property.optional ? '?' : ''), value];
return [name, getVariance(property) + name + (property.optional ? '?' : ''), value];
})
.sort((first, second) => {
return sort(first[0], second[0]) ? -1 : 1;
Expand All @@ -94,7 +104,7 @@ const generateOrderedList = (context, sort, properties) => {
return item[0];
}

return item[0] + ': ' + item[1];
return item[1] + ': ' + item[2];
});
};

Expand Down
40 changes: 40 additions & 0 deletions tests/rules/assertions/sortKeys.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,46 @@ export default {
},
}
`
},
{
code: `
type FooType = {
+c: number,
-b: number,
a: number,
}
`,
errors: [
{message: 'Expected type annotations to be in ascending order. "b" should be before "c".'},
{message: 'Expected type annotations to be in ascending order. "a" should be before "b".'}
],
output: `
type FooType = {
a: number,
-b: number,
+c: number,
}
`
},
{
code: `
type FooType = {|
+c: number,
-b: number,
a: number,
|}
`,
errors: [
{message: 'Expected type annotations to be in ascending order. "b" should be before "c".'},
{message: 'Expected type annotations to be in ascending order. "a" should be before "b".'}
],
output: `
type FooType = {|
a: number,
-b: number,
+c: number,
|}
`
}
/* eslint-enable no-restricted-syntax */
],
Expand Down

0 comments on commit 3cd70b8

Please sign in to comment.