Skip to content

Commit 0ae2dce

Browse files
committed
fix: switch to @stylistic/exp-list-style to resolve array edge-cases
Signed-off-by: Ferdinand Thiessen <[email protected]>
1 parent f9c106c commit 0ae2dce

File tree

5 files changed

+102
-44
lines changed

5 files changed

+102
-44
lines changed

lib/configs/codeStyle.ts

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -93,25 +93,15 @@ export function codeStyle(options: ConfigOptions): Linter.Config[] {
9393
{ avoidQuotes: true },
9494
],
9595

96-
// Enforce consistent new lines after [ and before ]
97-
'@stylistic/array-bracket-newline': [
98-
'error',
99-
'consistent',
100-
],
101-
// Enforce new lines between array elements (better git diff) but allow to have single line arrays
102-
'@stylistic/array-element-newline': ['error', 'consistent'],
103-
// Same for objects as for arrays
104-
'@stylistic/object-curly-newline': [
105-
'error',
106-
{
107-
consistent: true,
108-
multiline: true,
109-
},
110-
],
111-
'@stylistic/object-property-newline': [
112-
'error',
113-
{ allowAllPropertiesOnSameLine: true },
114-
],
96+
// Enforce consistent new lines after brackets
97+
'@stylistic/array-bracket-newline': 'off',
98+
'@stylistic/array-bracket-spacing': 'off',
99+
'@stylistic/array-element-newline': 'off',
100+
'@stylistic/jsx-function-call-newline': 'off',
101+
'@stylistic/object-curly-newline': 'off',
102+
'@stylistic/object-curly-spacing': 'off',
103+
'@stylistic/object-property-newline': 'off',
104+
'@stylistic/exp-list-style': 'error',
115105

116106
// No space between function name and parenthesis. Enforce fn() instead of fn ()
117107
'@stylistic/function-call-spacing': [

lib/configs/vue.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@ export function vue(options: ConfigOptions): Linter.Config[] {
143143
'vue/prefer-separate-static-class': 'error',
144144
// For consistent layout of components
145145
'vue/define-macros-order': [
146-
'error', {
146+
'error',
147+
{
147148
order: [
148149
'defineOptions',
149150
'defineModel',
Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,52 @@
1-
// This can be a single line
2-
const arr = ['first', 'second']
1+
/* eslint-disable no-unused-vars */
32

4-
// This is not a single line already and should be one element per line
5-
const arr2 = [
6-
'first', 'second',
7-
'third', 'and',
8-
'so', 'on'
9-
]
10-
11-
// This has a missing trailing comma causing too much git diff
12-
const arr3 = [
3+
// ❌ Trailing comma is required to make multi-line array diff-safe on changing the last element
4+
// Here the trailing comma is missing on a multi-line array and extra comma is present on a single-line array
5+
const items = [
136
'first',
147
'second',
158
'third'
169
]
10+
const LATIN_VOWELS = ['a', 'e', 'i', 'o', 'u',]
11+
12+
// ❌ Multi-line array should have a single element per line to make it diff-safe and readable
13+
// Here the last two elements are on the same line
14+
const VARIANTS = [
15+
'primary',
16+
'secondary',
17+
'tertiary', 'tertiary-no-background',
18+
]
19+
20+
// ✅ In general it is recommended to prefer a multi-line array to make it diff-safe on adding/removing elements
21+
// It is especially important for dynamic array which entries may change
22+
const sampleUsers = [
23+
'admin',
24+
'alice',
25+
'bob',
26+
]
27+
// Even if there is only one element at the moment (more elements may be added later)
28+
const MUTE_NOTIFICATIONS_USER_STATUSES = [
29+
'dnd',
30+
]
31+
32+
// ✅ Single-line arrays are also fine for short and stable arrays which are not expected to change
33+
// This is a developer's choice
34+
const selectedItems = ['default_item']
35+
const HTML_FORM_ACTIONS = ['POST', 'GET']
36+
37+
// ❌ Single-line array should have brackets on the same line while multi-line array should have brackets on the next line
38+
const USER_STATUSES = [
39+
'online',
40+
'away',
41+
'dnd',
42+
'invisible',
43+
'offline']
44+
// 🚧 Currently this is an edge case and isn't fixed properly...
45+
const WEEKDAYS = ['Monday',
46+
'Tuesday',
47+
'Wednesday',
48+
'Thursday',
49+
'Friday',
50+
'Saturday',
51+
'Sunday',
52+
]
Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,47 @@
1-
// This can be a single line
2-
const arr = ['first', 'second']
1+
/* eslint-disable no-unused-vars */
32

4-
// This is not a single line already and should be one element per line
5-
const arr2 = [
3+
// ❌ Trailing comma is required to make multi-line array diff-safe on changing the last element
4+
// Here the trailing comma is missing on a multi-line array and extra comma is present on a single-line array
5+
const items = [
66
'first',
77
'second',
88
'third',
9-
'and',
10-
'so',
11-
'on',
129
]
10+
const LATIN_VOWELS = ['a', 'e', 'i', 'o', 'u']
1311

14-
// This has a missing trailing comma causing too much git diff
15-
const arr3 = [
16-
'first',
17-
'second',
18-
'third',
12+
// ❌ Multi-line array should have a single element per line to make it diff-safe and readable
13+
// Here the last two elements are on the same line
14+
const VARIANTS = [
15+
'primary',
16+
'secondary',
17+
'tertiary',
18+
'tertiary-no-background',
19+
]
20+
21+
// ✅ In general it is recommended to prefer a multi-line array to make it diff-safe on adding/removing elements
22+
// It is especially important for dynamic array which entries may change
23+
const sampleUsers = [
24+
'admin',
25+
'alice',
26+
'bob',
27+
]
28+
// Even if there is only one element at the moment (more elements may be added later)
29+
const MUTE_NOTIFICATIONS_USER_STATUSES = [
30+
'dnd',
31+
]
32+
33+
// ✅ Single-line arrays are also fine for short and stable arrays which are not expected to change
34+
// This is a developer's choice
35+
const selectedItems = ['default_item']
36+
const HTML_FORM_ACTIONS = ['POST', 'GET']
37+
38+
// ❌ Single-line array should have brackets on the same line while multi-line array should have brackets on the next line
39+
const USER_STATUSES = [
40+
'online',
41+
'away',
42+
'dnd',
43+
'invisible',
44+
'offline',
1945
]
46+
// 🚧 Currently this is an edge case and isn't fixed properly...
47+
const WEEKDAYS = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']

tests/fixtures/codestyle/output/function.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ export function doSomething(
4848
* @param num
4949
* @param enable
5050
*/
51-
export function doSomethingDifferent(num: number, enable: boolean) {
51+
export function doSomethingDifferent(
52+
num: number,
53+
enable: boolean,
54+
) {
5255
// ...
5356
}
5457

0 commit comments

Comments
 (0)