Skip to content

Commit cc9e677

Browse files
committed
fix: Sort nested type properties
fixes #21
1 parent 0af61b0 commit cc9e677

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

src/__tests__/type-properties.spec.ts

+9
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,15 @@ ruleTester.run("sort/type-properties", rule, {
6969
errors: [{ messageId: "unsorted" }],
7070
},
7171

72+
// Nested
73+
{
74+
code: "interface A {b:string; a:{b:string; a:number}}",
75+
output: "interface A {a:{b:string; a:number}; b:string}",
76+
// Rule tester will not run multiple fix iterations, but as long as we
77+
// have two errors, we know the nested rule was caught.
78+
errors: [{ messageId: "unsorted" }, { messageId: "unsorted" }],
79+
},
80+
7281
// Weights
7382
{
7483
code: `

src/rules/type-properties.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
ESLintUtils,
44
TSESTree,
55
} from "@typescript-eslint/experimental-utils"
6-
import { getName, getNodeRange, getNodeText } from "../ts-utils"
6+
import { getName, getNodeRange, getNodeText, isDelimiter } from "../ts-utils"
77
import { docsURL, enumerate, isUnsorted } from "../utils"
88

99
/**
@@ -42,7 +42,7 @@ export default ESLintUtils.RuleCreator.withoutDocs({
4242
function getRangeWithoutDelimiter(node: TSESTree.Node): TSESTree.Range {
4343
const range = getNodeRange(source, node)
4444

45-
return source.getLastToken(node)?.type === "Punctuator"
45+
return isDelimiter(source.getLastToken(node))
4646
? [range[0], range[1] - 1]
4747
: range
4848
}

src/ts-utils.ts

+6
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,9 @@ export function getNodeRange(source: TSESLint.SourceCode, node: TSESTree.Node) {
4040
export function getNodeText(source: TSESLint.SourceCode, node: TSESTree.Node) {
4141
return source.getText().slice(...getNodeRange(source, node))
4242
}
43+
44+
/**
45+
* Returns true if the token is a delimiter (comma or semicolon).
46+
*/
47+
export const isDelimiter = (token: TSESTree.Token | null) =>
48+
token?.type === "Punctuator" && (token.value === "," || token.value === ";")

0 commit comments

Comments
 (0)