Skip to content

Commit

Permalink
fix: sort-keys auto-fix works in more situations
Browse files Browse the repository at this point in the history
  • Loading branch information
deecewan committed Sep 19, 2017
1 parent 047758f commit 98cee9a
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/rules/sortKeys.js
Expand Up @@ -68,9 +68,9 @@ const isValidOrders = {
const generateOrderedList = (context, sort, properties) => {
return properties.map((property) => {
const name = getParameterName(property, context);
const value = context.getSourceCode().getFirstToken(property.value).value;
const value = context.getSourceCode().getText(property.value);

return [name, value];
return [name + (property.optional ? '?' : ''), value];
})
.sort((first, second) => { return sort(first[0], second[0]) ? -1 : 1; })
.map((item) => { return item[0] + ': ' + item[1]; });
Expand Down
70 changes: 70 additions & 0 deletions tests/rules/assertions/sortKeys.js
Expand Up @@ -63,6 +63,76 @@ export default {
' c: number,',
'}'
].join('\n')
},
{
code: [
'type FooType = {',
' a?: number,',
' c: ?number,',
' b: string,',
'}'
].join('\n'),
errors: [{message: 'Expected type annotations to be in ascending order. "b" should be before "c".'}],
output: [
'type FooType = {',
' a?: number,',
' b: string,',
' c: ?number,',
'}'
].join('\n')
},
{
code: [
'type FooType = {',
' a: (number) => void,',
' c: number,',
' b: (param: string) => number,',
'}'
].join('\n'),
errors: [{message: 'Expected type annotations to be in ascending order. "b" should be before "c".'}],
output: [
'type FooType = {',
' a: (number) => void,',
' b: (param: string) => number,',
' c: number,',
'}'
].join('\n')
},
{
code: [
'type FooType = {',
' a: number | string | boolean,',
' c: number,',
' b: (param: string) => number,',
'}'
].join('\n'),
errors: [{message: 'Expected type annotations to be in ascending order. "b" should be before "c".'}],
output: [
'type FooType = {',
' a: number | string | boolean,',
' b: (param: string) => number,',
' c: number,',
'}'
].join('\n')
},
{
code: [
'type FooType = {',
' c: number,',
' a: number ',
' | string | boolean,',
' b: (param: string) => number,',
'}'
].join('\n'),
errors: [{message: 'Expected type annotations to be in ascending order. "a" should be before "c".'}],
output: [
'type FooType = {',
' a: number ',
' | string | boolean,',
' b: (param: string) => number,',
' c: number,',
'}'
].join('\n')
}
],
misconfigured: [
Expand Down

0 comments on commit 98cee9a

Please sign in to comment.