Skip to content

Commit 34f8027

Browse files
zzxmingantfu
andauthored
fix(consistent-list-newline): when multiline nodes only have one item, line break should have same behavior (#40)
Co-authored-by: Anthony Fu <github@antfu.me>
1 parent 3bb369e commit 34f8027

File tree

2 files changed

+124
-3
lines changed

2 files changed

+124
-3
lines changed

src/rules/consistent-list-newline.test.ts

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,117 @@ const invalid: InvalidTestCase[] = [
540540
}"
541541
`),
542542
},
543+
{
544+
code: $`
545+
interface foo {
546+
a:1}
547+
`,
548+
output: o => expect(o).toMatchInlineSnapshot(`
549+
"interface foo {
550+
a:1
551+
}"
552+
`),
553+
},
554+
{
555+
code: $`
556+
interface foo {a:1
557+
}
558+
`,
559+
output: o => expect(o).toMatchInlineSnapshot(`
560+
"interface foo {a:1}"
561+
`),
562+
},
563+
{
564+
code: $`
565+
type foo = {
566+
a:1}
567+
`,
568+
output: o => expect(o).toMatchInlineSnapshot(`
569+
"type foo = {
570+
a:1
571+
}"
572+
`),
573+
},
574+
{
575+
code: $`
576+
type foo = {a:1
577+
}
578+
`,
579+
output: o => expect(o).toMatchInlineSnapshot(`
580+
"type foo = {a:1}"
581+
`),
582+
},
583+
{
584+
code: $`
585+
type foo = [
586+
1]
587+
`,
588+
output: o => expect(o).toMatchInlineSnapshot(`
589+
"type foo = [
590+
1
591+
]"
592+
`),
593+
},
594+
{
595+
code: $`
596+
type foo = [1
597+
]
598+
`,
599+
output: o => expect(o).toMatchInlineSnapshot(`
600+
"type foo = [1]"
601+
`),
602+
},
603+
{
604+
code: $`
605+
const foo = [
606+
1]
607+
`,
608+
output: o => expect(o).toMatchInlineSnapshot(`
609+
"const foo = [
610+
1
611+
]"
612+
`),
613+
},
614+
{
615+
code: $`
616+
const foo = {
617+
a:1}
618+
`,
619+
output: o => expect(o).toMatchInlineSnapshot(`
620+
"const foo = {
621+
a:1
622+
}"
623+
`),
624+
},
625+
{
626+
code: $`
627+
const foo = {a:1
628+
}
629+
`,
630+
output: o => expect(o).toMatchInlineSnapshot(`
631+
"const foo = {a:1}"
632+
`),
633+
},
634+
{
635+
code: $`
636+
function foo(a
637+
){}
638+
`,
639+
output: o => expect(o).toMatchInlineSnapshot(`
640+
"function foo(a){}"
641+
`),
642+
},
643+
{
644+
code: $`
645+
function foo(
646+
a){}
647+
`,
648+
output: o => expect(o).toMatchInlineSnapshot(`
649+
"function foo(
650+
a
651+
){}"
652+
`),
653+
},
543654
]
544655

545656
run({

src/rules/consistent-list-newline.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { TSESTree } from '@typescript-eslint/utils'
1+
import type { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/utils'
22
import type { RuleFix, RuleFixer, RuleListener } from '@typescript-eslint/utils/ts-eslint'
33
import { createEslintRule } from '../utils'
44

@@ -68,6 +68,15 @@ export default createEslintRule<Options, MessageIds>({
6868
},
6969
defaultOptions: [{}],
7070
create: (context, [options = {}] = [{}]) => {
71+
const multilineNodes = new Set([
72+
'ArrayExpression',
73+
'FunctionDeclaration',
74+
'ObjectExpression',
75+
'ObjectPattern',
76+
'TSTypeLiteral',
77+
'TSTupleType',
78+
'TSInterfaceDeclaration',
79+
])
7180
function removeLines(fixer: RuleFixer, start: number, end: number, delimiter?: string): RuleFix {
7281
const range = [start, end] as const
7382
const code = context.sourceCode.text.slice(...range)
@@ -197,7 +206,7 @@ export default createEslintRule<Options, MessageIds>({
197206
}
198207
else if (mode === 'inline' && endLoc.line !== lastLine) {
199208
// If there is only one multiline item, we allow the closing bracket to be on the a different line
200-
if (items.length === 1 && items[0].loc.start.line !== items[1]?.loc.start.line)
209+
if (items.length === 1 && !(multilineNodes as Set<AST_NODE_TYPES>).has(node.type))
201210
return
202211
if (context.sourceCode.getCommentsAfter(lastItem).length > 0)
203212
return
@@ -211,7 +220,8 @@ export default createEslintRule<Options, MessageIds>({
211220
name: node.type,
212221
},
213222
* fix(fixer) {
214-
yield removeLines(fixer, lastItem.range[1], endRange, getDelimiter(node, lastItem))
223+
const delimiter = items.length === 1 ? '' : getDelimiter(node, lastItem)
224+
yield removeLines(fixer, lastItem.range[1], endRange, delimiter)
215225
},
216226
})
217227
}

0 commit comments

Comments
 (0)