Skip to content

Commit 245d430

Browse files
committed
self review comments/tweaks
1 parent f1b1050 commit 245d430

File tree

18 files changed

+92
-79
lines changed

18 files changed

+92
-79
lines changed

docs/rules/enum.md

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,17 @@ enum U {
1818
}
1919

2020
// Case-sensitive by default.
21-
enum U {
21+
enum V {
2222
a = 'T',
23-
b = 'T',
24-
C = 'T',
23+
B = 'T',
24+
c = 'T',
2525
}
2626

27-
enum U {
27+
enum W {
2828
a = 'T',
29-
'c' = 'T',
29+
'c+-' = 'T',
3030
b = 'T',
31+
d = 'T',
3132
}
3233
```
3334

@@ -43,25 +44,30 @@ enum U {
4344
}
4445

4546
// Case-sensitive by default.
46-
enum U {
47-
C = 'T',
47+
enum V {
48+
B = 'T',
4849
a = 'T',
49-
b = 'T',
50+
c = 'T',
5051
}
5152

5253
// This rule sorts members which have an arbitrary string literal name as well.
53-
enum U {
54+
enum W {
5455
a = 'T',
55-
'b+-' = 'T',
56-
c = 'T',
56+
b = 'T',
57+
'c+-' = 'T',
58+
d = 'T',
5759
}
5860
```
5961

6062
## Options
6163

6264
```json
6365
{
64-
"typescript-sort-keys/enum": ["error", "asc", { "caseSensitive": true, "natural": false }]
66+
"typescript-sort-keys/enum": [
67+
"error",
68+
"asc",
69+
{ "caseSensitive": true, "natural": false }
70+
]
6571
}
6672
```
6773

@@ -82,19 +88,14 @@ Examples of **incorrect** code for the `"desc"` option:
8288
```ts
8389
/* eslint typescript-sort-keys/enum: ["error", "desc"] */
8490

85-
enum U {
86-
b = 'T',
87-
c = 'T',
88-
a = 'T',
89-
}
9091
enum U {
9192
b = 'T',
9293
c = 'T',
9394
a = 'T',
9495
}
9596

9697
// Case-sensitive by default.
97-
enum U {
98+
enum V {
9899
a = 'T',
99100
B = 'T',
100101
c = 'T',
@@ -106,19 +107,14 @@ Examples of **correct** code for the `"desc"` option:
106107
```ts
107108
/* eslint typescript-sort-keys/enum: ["error", "desc"] */
108109

109-
enum U {
110-
c = 'T',
111-
b = 'T',
112-
a = 'T',
113-
}
114110
enum U {
115111
c = 'T',
116112
b = 'T',
117113
a = 'T',
118114
}
119115

120116
// Case-sensitive by default.
121-
enum U {
117+
enum V {
122118
c = 'T',
123119
a = 'T',
124120
B = 'T',
@@ -138,7 +134,7 @@ enum U {
138134
C = 'T',
139135
b = 'T',
140136
}
141-
enum U {
137+
enum V {
142138
a = 'T',
143139
C = 'T',
144140
c = 'T',
@@ -157,7 +153,7 @@ enum U {
157153
c = 'T',
158154
C = 'T',
159155
}
160-
enum U {
156+
enum V {
161157
a = 'T',
162158
b = 'T',
163159
C = 'T',

docs/rules/string-enum.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
---
44

5-
# require string-enum members to be sorted (string-enum)
5+
# require string enum members to be sorted (string-enum)
66

7-
When declaring multiple members on a string-enum, some developers prefer to sort string-enum member names alphabetically to be able to find necessary members easier at the later time. Others feel that it adds complexity and becomes burden to maintain.
7+
When declaring multiple members on a string enum, some developers prefer to sort string enum member names alphabetically to be able to find necessary members easier at the later time. Others feel that it adds complexity and becomes burden to maintain.
88

99
## Rule Details
1010

11-
This rule checks all members of a string-enum declaration and verifies that all keys are sorted alphabetically.
11+
This rule checks all members of a string enum declaration and verifies that all keys are sorted alphabetically.
1212

1313
Examples of **incorrect** code for this rule:
1414

@@ -81,13 +81,13 @@ enum U {
8181

8282
The 1st option is `"asc"` or `"desc"`.
8383

84-
- `"asc"` (default) - enforce string-enum members to be in ascending order.
85-
- `"desc"` - enforce string-enum members to be in descending order.
84+
- `"asc"` (default) - enforce string enum members to be in ascending order.
85+
- `"desc"` - enforce string enum members to be in descending order.
8686

8787
The 2nd option is an object which has 2 properties.
8888

89-
- `caseSensitive` - if `true`, enforce string-enum members to be in case-sensitive order. Default is `true`.
90-
- `natural` - if `true`, enforce string-enum members to be in natural order. Default is `false`. Natural Order compares strings containing combination of letters and numbers in the way a human being would sort. It basically sorts numerically, instead of sorting alphabetically. So the number 10 comes after the number 3 in Natural Sorting.
89+
- `caseSensitive` - if `true`, enforce string enum members to be in case-sensitive order. Default is `true`.
90+
- `natural` - if `true`, enforce string enum members to be in natural order. Default is `false`. Natural Order compares strings containing combination of letters and numbers in the way a human being would sort. It basically sorts numerically, instead of sorting alphabetically. So the number 10 comes after the number 3 in Natural Sorting.
9191

9292
### desc
9393

@@ -209,4 +209,4 @@ enum U {
209209

210210
## When Not To Use It
211211

212-
If you don't want to notify about string-enum members' order, then it's safe to disable this rule.
212+
If you don't want to notify about string enum members' order, then it's safe to disable this rule.

src/common/options.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
1-
import {
2-
AllRuleOptions,
3-
CreateReporterArgs,
4-
SortingOrder,
5-
SortingParamsOptions,
6-
} from '../types'
1+
import { AllRuleOptions, CreateReporterArgs, SortingOrder, SortingParams } from '../types'
72

83
export const defaultSortingOrder = SortingOrder.Ascending
9-
export const defaultOptions: SortingParamsOptions = {
4+
export const defaultOptions: SortingParams = {
105
caseSensitive: true,
116
natural: false,
127
requiredFirst: false,

src/fixer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export const getFixerFunction = (
1212
sortedBody: TSType[],
1313
): ReportFixFunction =>
1414
function* (fixer: TSESLint.RuleFixer) {
15-
const sourceCode = createReporterArgs.context.getSourceCode() as SourceCode
15+
const sourceCode = createReporterArgs.context.sourceCode as SourceCode
1616

1717
const bodyRange = memoize(`bodyRange_${baseMemoKey}`, () =>
1818
getBodyRange(sourceCode, body as unknown as Node[]),

src/plugin.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { TSESTree } from '@typescript-eslint/utils'
2+
23
import { getOptions } from './common/options'
34
import { getFixerFunction } from './fixer'
45
import { reportBodyNodes, reportParentNode } from './report'
@@ -47,7 +48,7 @@ export function createReporter(
4748
return
4849
}
4950

50-
const sourceCode = createReporterArgs.context.getSourceCode()
51+
const sourceCode = createReporterArgs.context.sourceCode
5152
// Create a key for memoizing results based on plugin context & input
5253
const baseMemoKey = JSON.stringify({
5354
body: sourceCode.getText(bodyParent), // Disambiguator when body has code embedded in body

src/rules/enum.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
RuleOptionsGeneric,
88
SortingOrder,
99
sortingOrderOptionSchema,
10-
SortingParamsOptions,
10+
SortingParams,
1111
} from '../types'
1212
import { getObjectBody } from '../utils/ast'
1313
import { createRule, RuleMetaData } from '../utils/rule'
@@ -16,12 +16,11 @@ import { createRule, RuleMetaData } from '../utils/rule'
1616
* The name of this rule.
1717
*/
1818
export const name = 'enum' as const
19-
export const nameDeprecated = 'string-enum' as const
2019

2120
/**
2221
* The options this rule can take.
2322
*/
24-
export type RuleOptions = RuleOptionsGeneric<Omit<SortingParamsOptions, 'requiredFirst'>>
23+
export type RuleOptions = RuleOptionsGeneric<Omit<SortingParams, 'requiredFirst'>>
2524

2625
const sortingParamsOptionSchema: JSONSchema4 = {
2726
type: 'object',
@@ -62,7 +61,7 @@ type errorMessageKeys = keyof typeof errorMessages
6261
* The meta data for this rule.
6362
*/
6463
const meta: RuleMetaData<errorMessageKeys> = {
65-
type: 'layout',
64+
type: 'suggestion',
6665
docs: {
6766
description: 'require enum members to be sorted',
6867
recommended: 'stylistic',

src/rules/interface.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
RuleOptionsGeneric,
88
SortingOrder,
99
sortingOrderOptionSchema,
10-
SortingParamsOptions,
10+
SortingParams,
1111
} from '../types'
1212
import { getObjectBody } from '../utils/ast'
1313
import { createRule, RuleMetaData } from '../utils/rule'
@@ -20,7 +20,7 @@ export const name = 'interface' as const
2020
/**
2121
* The options this rule can take.
2222
*/
23-
export type RuleOptions = RuleOptionsGeneric<SortingParamsOptions>
23+
export type RuleOptions = RuleOptionsGeneric<SortingParams>
2424

2525
const sortingParamsOptionSchema: JSONSchema4 = {
2626
type: 'object',
@@ -67,7 +67,7 @@ type errorMessageKeys = keyof typeof errorMessages
6767
* The meta data for this rule.
6868
*/
6969
const meta: RuleMetaData<errorMessageKeys> = {
70-
type: 'layout',
70+
type: 'suggestion',
7171
docs: {
7272
description: 'require interface keys to be sorted',
7373
recommended: 'stylistic',

src/rules/string-enum.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
RuleOptionsGeneric,
88
SortingOrder,
99
sortingOrderOptionSchema,
10-
SortingParamsOptions,
10+
SortingParams,
1111
} from '../types'
1212
import { getObjectBody } from '../utils/ast'
1313
import { createRule, RuleMetaData } from '../utils/rule'
@@ -22,7 +22,7 @@ export const name = 'string-enum' as const
2222
* @deprecated
2323
* The options this rule can take.
2424
*/
25-
export type RuleOptions = RuleOptionsGeneric<Omit<SortingParamsOptions, 'requiredFirst'>>
25+
export type RuleOptions = RuleOptionsGeneric<Omit<SortingParams, 'requiredFirst'>>
2626

2727
const sortingParamsOptionSchema: JSONSchema4 = {
2828
type: 'object',
@@ -66,7 +66,7 @@ type errorMessageKeys = keyof typeof errorMessages
6666
* The meta data for this rule.
6767
*/
6868
const meta: RuleMetaData<errorMessageKeys> = {
69-
type: 'layout',
69+
type: 'suggestion',
7070
docs: {
7171
description: 'require string enum members to be sorted',
7272
recommended: 'stylistic',

src/types/options.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ export const sortingOrderOptionSchema: JSONSchema4 = {
1010
type: 'string',
1111
}
1212

13-
export type SortingOrderOption = SortingOrder
14-
15-
export interface SortingParamsOptions {
13+
export interface SortingParams {
1614
readonly caseSensitive: boolean
1715
readonly natural: boolean
1816
readonly requiredFirst: boolean

src/types/rule.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
import { SortingOrderOption, SortingParamsOptions } from './options'
1+
import { SortingOrder, SortingParams } from './options'
22

3-
export type RuleOptionsGeneric<T> =
4-
| []
5-
| [SortingOrderOption]
6-
| [SortingOrderOption, Partial<T>]
3+
export type RuleOptionsGeneric<T> = [] | [SortingOrder] | [SortingOrder, Partial<T>]
74

8-
export type AllRuleOptions = RuleOptionsGeneric<SortingParamsOptions>
5+
export type AllRuleOptions = RuleOptionsGeneric<SortingParams>

src/utils/reportUtils.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { PLUGIN_NAME } from 'config/constants'
2-
import {
3-
name as enumRuleName,
4-
nameDeprecated as enumRuleNameDeprecated,
5-
} from 'rules/enum'
2+
import { name as enumRuleName } from 'rules/enum'
3+
import { name as enumRuleNameDeprecated } from 'rules/string-enum'
64
import { NodePositionInfo, TSType } from 'types'
75

86
import { getRuleDocsUrl } from './rule'
@@ -52,7 +50,7 @@ export function getUnsortedInfo(
5250
export function getDeprecationMessage(name: string) {
5351
switch (name) {
5452
case enumRuleNameDeprecated:
55-
return `DEPRECATED: see [${PLUGIN_NAME}/enum](${getRuleDocsUrl(enumRuleName)})`
53+
return `DEPRECATED: see [${PLUGIN_NAME}/${enumRuleName}](${getRuleDocsUrl(enumRuleName)})`
5654
default:
5755
return ''
5856
}

src/utils/sourcecode/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import { AST_NODE_TYPES, AST_TOKEN_TYPES } from '@typescript-eslint/utils'
21
import { Node, SourceCode } from 'types'
2+
3+
import { AST_NODE_TYPES, AST_TOKEN_TYPES } from '@typescript-eslint/utils'
4+
35
import {
46
getCommentsTextAfter,
57
getCommentsTextBefore,
@@ -11,6 +13,8 @@ import { getProcessedText } from './textHelpers'
1113

1214
/**
1315
* Returns the text of the entire body, rebuilt from the source code in order given.
16+
* Preserves location of comments in relation to members, and ensures line comments
17+
* don't move and comment out other members.
1418
*/
1519
export function getFixedBodyText(
1620
sourceCode: SourceCode,

tests/helpers/cases/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ export { processInvalidTestCase, processValidTestCase } from './process'
1111

1212
/**
1313
* Helper for splitting up test cases by options and displaying info about which
14-
* set of options the test is running with. Sacrifices some parallelizatiom for debuggability.
14+
* set of options the test is running with. Sacrifices some parallelizatiom for
15+
* powerful concise expression in test files.
1516
*/
1617
export function runCases(
1718
{
@@ -24,11 +25,10 @@ export function runCases(
2425
runOptions: { withRequiredFirstOption: boolean; category: CaseCategory },
2526
) {
2627
const usedOptionsKeys = new Set(Object.keys(invalid).concat(Object.keys(valid)))
28+
const ruleTester = new RuleTester(typescriptConfig)
2729

2830
for (const optionsSetKey of usedOptionsKeys) {
2931
describe(optionsSetKey, () => {
30-
const ruleTester = new RuleTester(typescriptConfig)
31-
3232
ruleTester.run(name, rule as unknown as Rule.RuleModule, {
3333
valid: processValidTestCase(
3434
{ [optionsSetKey]: valid[optionsSetKey] },

tests/helpers/cases/preprocess.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ function processErrorArgs(
4040
errorMessages.push(getCountErrorString(category, errorArgs.length))
4141

4242
for (const args of errorArgs) {
43+
// At this point args is number or string[]
4344
if (Array.isArray(args)) {
4445
switch (args.length) {
4546
case 1:
@@ -83,6 +84,7 @@ export function preProcessInvalidTestCases(
8384
return processedCases
8485
}
8586

87+
// Take from shape PreInvalidTestCase[] to InvalidTestCase[]
8688
function convertPreInvalidCasesToProcessed(
8789
cases: PreInvalidTestCaseList,
8890
category: CaseCategory,
@@ -130,6 +132,7 @@ export function preProcessValidTestCases(
130132
return processedCases
131133
}
132134

135+
// Take from shape PreValidTestCase[] to ValidTestCase[]
133136
function convertPreValidCasesToProcessed(
134137
cases: string[],
135138
withRequiredFirstOption: boolean,

0 commit comments

Comments
 (0)