Skip to content

Commit

Permalink
Reorder comments and jsdoc
Browse files Browse the repository at this point in the history
Fixes #8
  • Loading branch information
tomquist committed Jan 27, 2025
1 parent 9839523 commit 9fa21f6
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/rules/sort-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ const checkAndReport = <N extends TSESTree.Node, P extends TSESTree.Node>(

if (needSort) {
const diffRanges = ArrayUtils.zip2(properties, sortedProperties).map(([from, to]) => ({
from: from.range,
to: to.range,
from: FixUtils.getRangeIncludingComments(sourceCode, from!),
to: FixUtils.getRangeIncludingComments(sourceCode, to!),
}))

const fixedText = FixUtils.getFixedText(sourceCode, node.range, diffRanges)
Expand Down
5 changes: 3 additions & 2 deletions src/rules/sort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ComparerUtils } from '../utils/comparer'
import { ConfigUtils } from '../utils/config'
import { createRule } from '../utils/createRule'
import { FixUtils } from '../utils/fix'
import { Range } from '@typescript-eslint/types/dist/generated/ast-spec'

type Options = []

Expand Down Expand Up @@ -51,8 +52,8 @@ export default createRule<Options, MessageIds>({

if (needSort) {
const diffRanges = ArrayUtils.zip2(node.elements, sortedElements).map(([from, to]) => ({
from: from!.range,
to: to!.range,
from: FixUtils.getRangeIncludingComments(sourceCode, from!),
to: FixUtils.getRangeIncludingComments(sourceCode, to!),
}))

const fixedText = FixUtils.getFixedText(sourceCode, node.range, diffRanges)
Expand Down
12 changes: 12 additions & 0 deletions src/utils/fix.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
import type { TSESTree } from '@typescript-eslint/utils'
import { Range } from '@typescript-eslint/types/dist/generated/ast-spec'
import { SourceCode } from '@typescript-eslint/utils/dist/ts-eslint'

const START = 0
const END = 1

const getRangeIncludingComments = (sourceCode: SourceCode, element: TSESTree.Node | TSESTree.Token): Range => {
const start = element.range[START]
const end = element.range[END]
let commentsBefore = sourceCode.getCommentsBefore(element)
if (commentsBefore.length === 0) {
return [start, end]
}
return [commentsBefore[0].range[START], end]
}

const getFixedText = (sourceCode: SourceCode, nodeRange: Range, diffRange: { from: Range; to: Range }[]) => {
const allText = sourceCode.getText()

Expand All @@ -26,5 +37,6 @@ const getFixedText = (sourceCode: SourceCode, nodeRange: Range, diffRange: { fro
}

export const FixUtils = {
getRangeIncludingComments,
getFixedText,
}
30 changes: 30 additions & 0 deletions tests/sort-keys-deep.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,36 @@ ruleTester.run('sort-keys', rule, {
`,
filename: getFilename('main.ts'),
},
{
code: `
// @sort-keys:deep
const object = {
A1: {
A2: {
/** Comment for b */
b: string,
// Comment for a
a: string,
},
},
}
`,
errors: [{ messageId: 'hasUnsortedKeys', type: AST_NODE_TYPES.ObjectExpression }],
output: `
// @sort-keys:deep
const object = {
A1: {
A2: {
// Comment for a
a: string,
/** Comment for b */
b: string,
},
},
}
`,
filename: getFilename('main.ts'),
},
{
code: `
// @sort-keys:deep
Expand Down
86 changes: 86 additions & 0 deletions tests/sort-keys-default.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,40 @@ ruleTester.run('sort-keys', rule, {
`,
filename: getFilename('main.ts'),
},
{
code: `
// @sort-keys
enum Keys {
/**
* Comment for B
*/
B,
// Comment
// for
// C
C = 1,
/** Comment for A */
A,
}
`,
errors: [{ messageId: HAS_UNSORTED_KEYS_MESSAGE_ID, type: AST_NODE_TYPES.TSEnumDeclaration }],
output: `
// @sort-keys
enum Keys {
/** Comment for A */
A,
/**
* Comment for B
*/
B,
// Comment
// for
// C
C = 1,
}
`,
filename: getFilename('main.ts'),
},
{
code: `
const A = 'A'
Expand Down Expand Up @@ -316,5 +350,57 @@ ruleTester.run('sort-keys', rule, {
`,
filename: getFilename('main.ts'),
},
{
code: `
enum AKeys {
A = 'A',
B = 'B',
}
enum BKeys {
A = 'A',
B = 'B',
}
// @sort-keys
const object = {
// Comment
[AKeys.A]: string,
/** Single-line block comment */
[BKeys.A]: string,
/**
* Multi-line block comment
*/
[AKeys.B]: string,
// Multi-line
// comment
[BKeys.B]: string,
}
`,
errors: [{ messageId: HAS_UNSORTED_KEYS_MESSAGE_ID, type: AST_NODE_TYPES.ObjectExpression }],
output: `
enum AKeys {
A = 'A',
B = 'B',
}
enum BKeys {
A = 'A',
B = 'B',
}
// @sort-keys
const object = {
// Comment
[AKeys.A]: string,
/**
* Multi-line block comment
*/
[AKeys.B]: string,
/** Single-line block comment */
[BKeys.A]: string,
// Multi-line
// comment
[BKeys.B]: string,
}
`,
filename: getFilename('main.ts'),
},
],
})
30 changes: 30 additions & 0 deletions tests/sort.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,36 @@ ruleTester.run('sort', rule, {
`,
filename: getFilename('main.ts'),
},
{
code: `
/*
@sort
*/
const simpleArray = [
// Value 2
2,
/**
* Value 1
*/
1
]
`,
errors: [{ messageId: HAS_UNSORTED_KEYS_MESSAGE_ID, type: AST_NODE_TYPES.ArrayExpression }],
output: `
/*
@sort
*/
const simpleArray = [
/**
* Value 1
*/
1,
// Value 2
2
]
`,
filename: getFilename('main.ts'),
},
{
code: `
/*
Expand Down

0 comments on commit 9fa21f6

Please sign in to comment.