Skip to content

Commit

Permalink
fix: [sort-keys] handle method signatures (#459)
Browse files Browse the repository at this point in the history
* fix: annotate fixable rules

...and fix a few lint errors

The latest version of ESLint RuleTester has a hard requirement that all fixable rules declare `meta.fixable`.
I was going to work on an issue, but this was blocking all tests after doing a fresh npm install.

* fix: [sort-keys] handle method signatures

Fixes #455
  • Loading branch information
bradzacher committed Feb 18, 2021
1 parent 61c4a3c commit 04b9637
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/rules/sortKeys.js
Expand Up @@ -42,7 +42,8 @@ const generateOrderedList = (context, sort, properties) => {
commentsBefore[0].range[0] :
property.range[0];

if (property.type === 'ObjectTypeSpreadProperty' || !property.value) {
const isMethodProperty = property.value && property.value.type === 'FunctionTypeAnnotation';
if (property.type === 'ObjectTypeSpreadProperty' || !property.value || isMethodProperty) {
// NOTE: It could but currently does not fix recursive generic type arguments in GenericTypeAnnotation within ObjectTypeSpreadProperty.

// Maintain everything between the start of property including leading comments and the nextPunctuator `,` or `}`:
Expand Down
126 changes: 126 additions & 0 deletions tests/rules/assertions/sortKeys.js
Expand Up @@ -439,6 +439,124 @@ export default {
`,
},
/* eslint-enable no-restricted-syntax */

// https://github.com/gajus/eslint-plugin-flowtype/issues/455
{
code: `
type FooType = {
a(number): void,
c: number,
b(param: string): number,
}
`,
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,
}
`,
},
{
code: `
type FooType = {
a: number | string | boolean,
c: number,
b(param: string): number,
}
`,
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,
}
`,
},
{
code: `
type FooType = {
c: number,
a: number | string | boolean,
b(param: string): number,
}
`,
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,
}
`,
},
{
code: `
type FooType = {
c: {
z: number,
x: string,
y: boolean,
},
a: number | string | boolean,
b(param: string): number,
}
`,
errors: [
{message: 'Expected type annotations to be in ascending order. "x" should be before "z".'},
{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: {
x: string,
y: boolean,
z: number,
},
}
`,
},
{
code: `
type FooType = {
c: {
z: {
j: string,
l: number,
k: boolean,
},
x: string,
y: boolean,
},
a: number | string | boolean,
b(param: string): number,
}
`,
errors: [
{message: 'Expected type annotations to be in ascending order. "k" should be before "l".'},
{message: 'Expected type annotations to be in ascending order. "x" should be before "z".'},
{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: {
x: string,
y: boolean,
z: {
j: string,
k: boolean,
l: number,
},
},
}
`,
},
],
misconfigured: [
{
Expand Down Expand Up @@ -504,5 +622,13 @@ export default {
},
},
},

// https://github.com/gajus/eslint-plugin-flowtype/issues/455
{
code: 'type FooType = { a: string, b(): number, c: boolean }',
},
{
code: 'type FooType = { a(): string, b: number, c: boolean }',
},
],
};

0 comments on commit 04b9637

Please sign in to comment.