Skip to content

Commit b14fd3f

Browse files
authored
fix: prevent newlines fixes between different partitions
1 parent 05b4797 commit b14fd3f

39 files changed

+1000
-86
lines changed

rules/sort-array-includes.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ export let sortArray = <MessageIds extends string>({
229229
options,
230230
})
231231

232-
let sortingNode: SortArrayIncludesSortingNode = {
232+
let sortingNode: Omit<SortArrayIncludesSortingNode, 'partitionId'> = {
233233
isEslintDisabled: isNodeEslintDisabled(element, eslintDisabledLines),
234234
size: rangeToDiff(element, sourceCode),
235235
node: element,
@@ -250,7 +250,10 @@ export let sortArray = <MessageIds extends string>({
250250
accumulator.push([])
251251
}
252252

253-
accumulator.at(-1)!.push(sortingNode)
253+
accumulator.at(-1)!.push({
254+
...sortingNode,
255+
partitionId: accumulator.length,
256+
})
254257

255258
return accumulator
256259
},

rules/sort-classes.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ export default createEslintRule<SortClassesOptions, MESSAGE_ID>({
542542
let overloadSignatureGroupMember =
543543
overloadSignatureGroups[overloadSignatureGroupMemberIndex]?.at(-1)
544544

545-
let sortingNode: SortClassSortingNodes = {
545+
let sortingNode: Omit<SortClassSortingNodes, 'partitionId'> = {
546546
dependencyNames: [
547547
getDependencyName({
548548
nodeNameWithoutStartingHash: name.startsWith('#')
@@ -580,7 +580,10 @@ export default createEslintRule<SortClassesOptions, MESSAGE_ID>({
580580
accumulator.push([])
581581
}
582582

583-
accumulator.at(-1)!.push(sortingNode)
583+
accumulator.at(-1)!.push({
584+
...sortingNode,
585+
partitionId: accumulator.length,
586+
})
584587

585588
return accumulator
586589
},

rules/sort-decorators.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ let sortDecorators = (
211211
name,
212212
})
213213

214-
let sortingNode: SortDecoratorsSortingNode = {
214+
let sortingNode: Omit<SortDecoratorsSortingNode, 'partitionId'> = {
215215
isEslintDisabled: isNodeEslintDisabled(decorator, eslintDisabledLines),
216216
size: rangeToDiff(decorator, sourceCode),
217217
node: decorator,
@@ -231,7 +231,10 @@ let sortDecorators = (
231231
accumulator.push([])
232232
}
233233

234-
accumulator.at(-1)!.push(sortingNode)
234+
accumulator.at(-1)!.push({
235+
...sortingNode,
236+
partitionId: accumulator.length,
237+
})
235238

236239
return accumulator
237240
},

rules/sort-enums.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ export default createEslintRule<Options, MESSAGE_ID>({
161161
})
162162

163163
let lastSortingNode = accumulator.at(-1)?.at(-1)
164-
let sortingNode: SortEnumsSortingNode = {
164+
let sortingNode: Omit<SortEnumsSortingNode, 'partitionId'> = {
165165
numericValue: member.initializer
166166
? getExpressionNumberValue(
167167
member.initializer,
@@ -191,7 +191,10 @@ export default createEslintRule<Options, MESSAGE_ID>({
191191
accumulator.push([])
192192
}
193193

194-
accumulator.at(-1)!.push(sortingNode)
194+
accumulator.at(-1)!.push({
195+
...sortingNode,
196+
partitionId: accumulator.length,
197+
})
195198
return accumulator
196199
},
197200
[[]],

rules/sort-exports.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ export default createEslintRule<Options, MESSAGE_ID>({
125125
options,
126126
})
127127

128-
let sortingNode: SortExportsSortingNode = {
128+
let sortingNode: Omit<SortExportsSortingNode, 'partitionId'> = {
129129
isEslintDisabled: isNodeEslintDisabled(node, eslintDisabledLines),
130130
groupKind: node.exportKind === 'value' ? 'value' : 'type',
131131
size: rangeToDiff(node, sourceCode),
@@ -134,11 +134,11 @@ export default createEslintRule<Options, MESSAGE_ID>({
134134
name,
135135
node,
136136
}
137-
let lastNode = formattedMembers.at(-1)?.at(-1)
137+
let lastSortingNode = formattedMembers.at(-1)?.at(-1)
138138

139139
if (
140140
shouldPartition({
141-
lastSortingNode: lastNode,
141+
lastSortingNode,
142142
sortingNode,
143143
sourceCode,
144144
options,
@@ -147,7 +147,10 @@ export default createEslintRule<Options, MESSAGE_ID>({
147147
formattedMembers.push([])
148148
}
149149

150-
formattedMembers.at(-1)!.push(sortingNode)
150+
formattedMembers.at(-1)!.push({
151+
...sortingNode,
152+
partitionId: formattedMembers.length,
153+
})
151154
}
152155

153156
return {

rules/sort-heritage-clauses.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ let sortHeritageClauses = (
133133
),
134134
size: rangeToDiff(heritageClause, sourceCode),
135135
node: heritageClause,
136+
partitionId: 0,
136137
group,
137138
name,
138139
}

rules/sort-imports.ts

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,10 @@ export default createEslintRule<Options, MESSAGE_ID>({
143143
ruleName: id,
144144
sourceCode,
145145
})
146-
let sortingNodes: SortImportsSortingNode[] = []
146+
let sortingNodesWithoutPartitionId: Omit<
147+
SortImportsSortingNode,
148+
'partitionId'
149+
>[] = []
147150

148151
let flatGroups = new Set(options.groups.flat())
149152
let shouldRegroupSideEffectNodes = flatGroups.has('side-effect')
@@ -271,7 +274,18 @@ export default createEslintRule<Options, MESSAGE_ID>({
271274
name,
272275
}) ?? 'unknown'
273276

274-
sortingNodes.push({
277+
let hasMultipleImportDeclarations = isSortable(
278+
(node as TSESTree.ImportDeclaration).specifiers,
279+
)
280+
let size = rangeToDiff(node, sourceCode)
281+
if (
282+
hasMultipleImportDeclarations &&
283+
options.maxLineLength &&
284+
size > options.maxLineLength
285+
) {
286+
size = name.length + 10
287+
}
288+
sortingNodesWithoutPartitionId.push({
275289
isIgnored:
276290
!options.sortSideEffects &&
277291
isSideEffect &&
@@ -280,17 +294,11 @@ export default createEslintRule<Options, MESSAGE_ID>({
280294
isEslintDisabled: isNodeEslintDisabled(node, eslintDisabledLines),
281295
dependencyNames: computeDependencyNames({ sourceCode, node }),
282296
dependencies: computeDependencies(node),
283-
size: rangeToDiff(node, sourceCode),
284297
addSafetySemicolonWhenInline: true,
285298
group,
299+
size,
286300
name,
287301
node,
288-
...(options.type === 'line-length' &&
289-
options.maxLineLength && {
290-
hasMultipleImportDeclarations: isSortable(
291-
(node as TSESTree.ImportDeclaration).specifiers,
292-
),
293-
}),
294302
})
295303
}
296304

@@ -299,15 +307,19 @@ export default createEslintRule<Options, MESSAGE_ID>({
299307
let contentSeparatedSortingNodeGroups: SortImportsSortingNode[][][] = [
300308
[[]],
301309
]
302-
for (let sortingNode of sortingNodes) {
310+
for (let sortingNodeWithoutPartitionId of sortingNodesWithoutPartitionId) {
303311
let lastGroupWithNoContentBetween =
304312
contentSeparatedSortingNodeGroups.at(-1)!
305313
let lastGroup = lastGroupWithNoContentBetween.at(-1)!
306314
let lastSortingNode = lastGroup.at(-1)
307315

308316
if (
309317
lastSortingNode &&
310-
hasContentBetweenNodes(sourceCode, lastSortingNode, sortingNode)
318+
hasContentBetweenNodes(
319+
sourceCode,
320+
lastSortingNode,
321+
sortingNodeWithoutPartitionId,
322+
)
311323
) {
312324
lastGroup = []
313325
lastGroupWithNoContentBetween = [lastGroup]
@@ -316,8 +328,8 @@ export default createEslintRule<Options, MESSAGE_ID>({
316328
)
317329
} else if (
318330
shouldPartition({
331+
sortingNode: sortingNodeWithoutPartitionId,
319332
lastSortingNode,
320-
sortingNode,
321333
sourceCode,
322334
options,
323335
})
@@ -326,7 +338,10 @@ export default createEslintRule<Options, MESSAGE_ID>({
326338
lastGroupWithNoContentBetween.push(lastGroup)
327339
}
328340

329-
lastGroup.push(sortingNode)
341+
lastGroup.push({
342+
...sortingNodeWithoutPartitionId,
343+
partitionId: lastGroupWithNoContentBetween.length,
344+
})
330345
}
331346

332347
for (let sortingNodeGroups of contentSeparatedSortingNodeGroups) {
@@ -517,8 +532,8 @@ export default createEslintRule<Options, MESSAGE_ID>({
517532

518533
let hasContentBetweenNodes = (
519534
sourceCode: TSESLint.SourceCode,
520-
left: SortImportsSortingNode,
521-
right: SortImportsSortingNode,
535+
left: Pick<SortImportsSortingNode, 'node'>,
536+
right: Pick<SortImportsSortingNode, 'node'>,
522537
): boolean =>
523538
sourceCode.getTokensBetween(left.node, right.node, {
524539
includeComments: false,

rules/sort-jsx-props.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ export default createEslintRule<Options, MESSAGE_ID>({
164164
name,
165165
})
166166

167-
let sortingNode: SortingNode = {
167+
let sortingNode: Omit<SortingNode, 'partitionId'> = {
168168
isEslintDisabled: isNodeEslintDisabled(
169169
attribute,
170170
eslintDisabledLines,
@@ -187,7 +187,10 @@ export default createEslintRule<Options, MESSAGE_ID>({
187187
accumulator.push([])
188188
}
189189

190-
accumulator.at(-1)!.push(sortingNode)
190+
accumulator.at(-1)!.push({
191+
...sortingNode,
192+
partitionId: accumulator.length,
193+
})
191194

192195
return accumulator
193196
},

rules/sort-maps.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ export default createEslintRule<Options, MESSAGE_ID>({
136136
options,
137137
})
138138

139-
let sortingNode: SortingNode = {
139+
let sortingNode: Omit<SortingNode, 'partitionId'> = {
140140
isEslintDisabled: isNodeEslintDisabled(
141141
element,
142142
eslintDisabledLines,
@@ -158,7 +158,10 @@ export default createEslintRule<Options, MESSAGE_ID>({
158158
formattedMembers.push([])
159159
}
160160

161-
formattedMembers.at(-1)!.push(sortingNode)
161+
formattedMembers.at(-1)!.push({
162+
...sortingNode,
163+
partitionId: formattedMembers.length,
164+
})
162165
}
163166

164167
for (let nodes of formattedMembers) {

rules/sort-modules.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ let analyzeModule = ({
307307
options,
308308
})
309309

310-
let sortingNode: SortingNodeWithDependencies = {
310+
let sortingNode: Omit<SortingNodeWithDependencies, 'partitionId'> = {
311311
isEslintDisabled: isNodeEslintDisabled(node, eslintDisabledLines),
312312
size: rangeToDiff(node, sourceCode),
313313
addSafetySemicolonWhenInline,
@@ -330,7 +330,10 @@ let analyzeModule = ({
330330
formattedNodes.push([])
331331
}
332332

333-
formattedNodes.at(-1)?.push(sortingNode)
333+
formattedNodes.at(-1)?.push({
334+
...sortingNode,
335+
partitionId: formattedNodes.length,
336+
})
334337
}
335338

336339
let sortNodesExcludingEslintDisabled = (

0 commit comments

Comments
 (0)