From 9fa21f6831cfe6f67085a1d6bb450472b9e42f9e Mon Sep 17 00:00:00 2001 From: Tom Quist Date: Mon, 27 Jan 2025 12:15:28 +0100 Subject: [PATCH] Reorder comments and jsdoc Fixes #8 --- src/rules/sort-keys.ts | 4 +- src/rules/sort.ts | 5 +- src/utils/fix.ts | 12 +++++ tests/sort-keys-deep.test.ts | 30 ++++++++++++ tests/sort-keys-default.test.ts | 86 +++++++++++++++++++++++++++++++++ tests/sort.test.ts | 30 ++++++++++++ 6 files changed, 163 insertions(+), 4 deletions(-) diff --git a/src/rules/sort-keys.ts b/src/rules/sort-keys.ts index 6b2be33..31883ba 100644 --- a/src/rules/sort-keys.ts +++ b/src/rules/sort-keys.ts @@ -70,8 +70,8 @@ const checkAndReport = ( 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) diff --git a/src/rules/sort.ts b/src/rules/sort.ts index 80b2681..52edf14 100644 --- a/src/rules/sort.ts +++ b/src/rules/sort.ts @@ -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 = [] @@ -51,8 +52,8 @@ export default createRule({ 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) diff --git a/src/utils/fix.ts b/src/utils/fix.ts index 28b1b88..e8ceb9e 100644 --- a/src/utils/fix.ts +++ b/src/utils/fix.ts @@ -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() @@ -26,5 +37,6 @@ const getFixedText = (sourceCode: SourceCode, nodeRange: Range, diffRange: { fro } export const FixUtils = { + getRangeIncludingComments, getFixedText, } diff --git a/tests/sort-keys-deep.test.ts b/tests/sort-keys-deep.test.ts index 4932d9e..92c7dfa 100644 --- a/tests/sort-keys-deep.test.ts +++ b/tests/sort-keys-deep.test.ts @@ -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 diff --git a/tests/sort-keys-default.test.ts b/tests/sort-keys-default.test.ts index c62c64e..3bc84cf 100644 --- a/tests/sort-keys-default.test.ts +++ b/tests/sort-keys-default.test.ts @@ -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' @@ -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'), + }, ], }) diff --git a/tests/sort.test.ts b/tests/sort.test.ts index 6ae2a10..6c05e52 100644 --- a/tests/sort.test.ts +++ b/tests/sort.test.ts @@ -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: ` /*