Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reorder comments and jsdoc #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
4 changes: 2 additions & 2 deletions src/rules/sort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,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
Loading