Skip to content

Commit df40df4

Browse files
authoredFeb 11, 2025··
fix(sort-classes): fix dependency detection regression
1 parent 00a8080 commit df40df4

File tree

3 files changed

+88
-84
lines changed

3 files changed

+88
-84
lines changed
 

‎rules/sort-classes.ts

+73-79
Original file line numberDiff line numberDiff line change
@@ -332,8 +332,74 @@ export default createEslintRule<SortClassesOptions, MESSAGE_ID>({
332332
let selectors: Selector[] = []
333333
let addSafetySemicolonWhenInline: boolean = true
334334
switch (member.type) {
335+
case 'TSAbstractPropertyDefinition':
336+
case 'PropertyDefinition':
337+
/**
338+
* Member is necessarily a property similarly to above for methods,
339+
* prioritize 'static', 'declare', 'decorated', 'abstract',
340+
* 'override' and 'readonly' over accessibility modifiers.
341+
*/
342+
if ('static' in member && member.static) {
343+
modifiers.push('static')
344+
}
345+
346+
if ('declare' in member && member.declare) {
347+
modifiers.push('declare')
348+
}
349+
350+
if (member.type === 'TSAbstractPropertyDefinition') {
351+
modifiers.push('abstract')
352+
}
353+
354+
if (decorated) {
355+
modifiers.push('decorated')
356+
}
357+
358+
if ('override' in member && member.override) {
359+
modifiers.push('override')
360+
}
361+
362+
if ('readonly' in member && member.readonly) {
363+
modifiers.push('readonly')
364+
}
365+
366+
if (
367+
'accessibility' in member &&
368+
member.accessibility === 'protected'
369+
) {
370+
modifiers.push('protected')
371+
} else if (
372+
('accessibility' in member &&
373+
member.accessibility === 'private') ||
374+
isPrivateHash
375+
) {
376+
modifiers.push('private')
377+
} else {
378+
modifiers.push('public')
379+
}
380+
381+
if ('optional' in member && member.optional) {
382+
modifiers.push('optional')
383+
}
384+
385+
if (
386+
member.value?.type === 'ArrowFunctionExpression' ||
387+
member.value?.type === 'FunctionExpression'
388+
) {
389+
if (member.value.async) {
390+
modifiers.push('async')
391+
}
392+
selectors.push('function-property')
393+
} else if (member.value) {
394+
memberValue = sourceCode.getText(member.value)
395+
dependencies = extractDependencies(member.value, member.static)
396+
}
397+
398+
selectors.push('property')
399+
break
400+
335401
case 'TSAbstractMethodDefinition':
336-
case 'MethodDefinition': {
402+
case 'MethodDefinition':
337403
/**
338404
* By putting the static modifier before accessibility modifiers, we
339405
* prioritize 'static' over those in cases like:
@@ -389,9 +455,9 @@ export default createEslintRule<SortClassesOptions, MESSAGE_ID>({
389455
selectors.push('method')
390456

391457
break
392-
}
458+
393459
case 'TSAbstractAccessorProperty':
394-
case 'AccessorProperty': {
460+
case 'AccessorProperty':
395461
if (member.static) {
396462
modifiers.push('static')
397463
}
@@ -418,8 +484,8 @@ export default createEslintRule<SortClassesOptions, MESSAGE_ID>({
418484
selectors.push('accessor-property')
419485

420486
break
421-
}
422-
case 'TSIndexSignature': {
487+
488+
case 'TSIndexSignature':
423489
if (member.static) {
424490
modifiers.push('static')
425491
}
@@ -431,87 +497,15 @@ export default createEslintRule<SortClassesOptions, MESSAGE_ID>({
431497
selectors.push('index-signature')
432498

433499
break
434-
}
435-
case 'StaticBlock': {
500+
501+
case 'StaticBlock':
436502
addSafetySemicolonWhenInline = false
437503

438504
selectors.push('static-block')
439505

440506
dependencies = extractDependencies(member, true)
441507

442508
break
443-
}
444-
default: {
445-
/**
446-
* Member is necessarily a property similarly to above for methods,
447-
* prioritize 'static', 'declare', 'decorated', 'abstract',
448-
* 'override' and 'readonly' over accessibility modifiers.
449-
*/
450-
if ('static' in member && member.static) {
451-
modifiers.push('static')
452-
}
453-
454-
if ('declare' in member && member.declare) {
455-
modifiers.push('declare')
456-
}
457-
458-
if (member.type === 'TSAbstractPropertyDefinition') {
459-
modifiers.push('abstract')
460-
}
461-
462-
if (decorated) {
463-
modifiers.push('decorated')
464-
}
465-
466-
if ('override' in member && member.override) {
467-
modifiers.push('override')
468-
}
469-
470-
if ('readonly' in member && member.readonly) {
471-
modifiers.push('readonly')
472-
}
473-
474-
if (
475-
'accessibility' in member &&
476-
member.accessibility === 'protected'
477-
) {
478-
modifiers.push('protected')
479-
} else if (
480-
('accessibility' in member &&
481-
member.accessibility === 'private') ||
482-
isPrivateHash
483-
) {
484-
modifiers.push('private')
485-
} else {
486-
modifiers.push('public')
487-
}
488-
489-
if ('optional' in member && member.optional) {
490-
modifiers.push('optional')
491-
}
492-
493-
if ('value' in member && member.value) {
494-
if (
495-
member.value.type === 'ArrowFunctionExpression' ||
496-
member.value.type === 'FunctionExpression'
497-
) {
498-
if (member.value.async) {
499-
modifiers.push('async')
500-
}
501-
selectors.push('function-property')
502-
} else {
503-
memberValue = sourceCode.getText(member.value)
504-
}
505-
if (member.value.type !== 'TSEmptyBodyFunctionExpression') {
506-
dependencies = extractDependencies(
507-
member.value,
508-
member.static,
509-
)
510-
}
511-
}
512-
513-
selectors.push('property')
514-
}
515509
}
516510

517511
let predefinedGroups = generatePredefinedGroups({

‎rules/sort-maps.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { TSESLint } from '@typescript-eslint/utils'
2-
3-
import { TSESTree } from '@typescript-eslint/types'
2+
import type { TSESTree } from '@typescript-eslint/types'
43

54
import type { SortingNode } from '../types/sorting-node'
65
import type { Options } from './sort-maps/types'
@@ -83,9 +82,7 @@ export default createEslintRule<Options, MESSAGE_ID>({
8382
let matchedContextOptions = getMatchingContextOptions({
8483
nodeNames: elements
8584
.filter(
86-
element =>
87-
element !== null &&
88-
element.type !== TSESTree.AST_NODE_TYPES.SpreadElement,
85+
element => element !== null && element.type !== 'SpreadElement',
8986
)
9087
.map(element => getNodeName({ sourceCode, element })),
9188
contextOptions: context.options,

‎test/rules/sort-classes.test.ts

+13
Original file line numberDiff line numberDiff line change
@@ -2784,6 +2784,19 @@ describe(ruleName, () => {
27842784
},
27852785
],
27862786
},
2787+
{
2788+
code: dedent`
2789+
class MyClass {
2790+
a = () => this.b()
2791+
b = () => null
2792+
}
2793+
`,
2794+
options: [
2795+
{
2796+
...options,
2797+
},
2798+
],
2799+
},
27872800
],
27882801
invalid: [],
27892802
},

0 commit comments

Comments
 (0)
Please sign in to comment.