Skip to content

Commit 43c1898

Browse files
committed
refactor(lint): attempt to resolve type from both accessors if available
1 parent a954b78 commit 43c1898

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

projects/igniteui-angular/rules/boolean-input-transform.mjs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ASTUtils, Selectors } from '@angular-eslint/utils';
22
// import type { TSESTree } from '@typescript-eslint/utils';
3-
import { ESLintUtils } from '@typescript-eslint/utils';
3+
import { ESLintUtils, AST_NODE_TYPES } from '@typescript-eslint/utils';
44
import { TypeFlags } from 'typescript';
55

66
// export type Options = [
@@ -13,6 +13,29 @@ import { TypeFlags } from 'typescript';
1313

1414
export const RULE_NAME = 'boolean-input-transform';
1515

16+
/**
17+
* Get the sibling accessor (getter/setter) of a property, if any.
18+
* @param {import('@typescript-eslint/utils').TSESTree.ClassDeclaration} classDeclaration
19+
* @param {import('@typescript-eslint/utils').TSESTree.PropertyDefinition | import('@typescript-eslint/utils').TSESTree.MethodDefinition} property
20+
* @returns {import('@typescript-eslint/utils').TSESTree.MethodDefinition | undefined} sibling accessor or undefined
21+
*/
22+
function getSiblingProperty(classDeclaration, property) {
23+
const members = classDeclaration.body.body;
24+
const index = members.indexOf(property);
25+
return [members[index - 1], members[index + 1]]
26+
.filter(Boolean)
27+
.find(sibling => isAccessor(sibling) && sibling.key.name === property.key.name);
28+
}
29+
30+
/**
31+
* Check if a property is a getter or setter.
32+
* @param {import('@typescript-eslint/utils').TSESTree.PropertyDefinition | import('@typescript-eslint/utils').TSESTree.MethodDefinition} property
33+
* @returns {property is import('@typescript-eslint/utils').TSESTree.MethodDefinition}
34+
*/
35+
function isAccessor(property) {
36+
return property.type === AST_NODE_TYPES.MethodDefinition && (property.kind === 'get' || property.kind === 'set');
37+
}
38+
1639
/**
1740
* Check if a property is of boolean type.
1841
* @param {import('@typescript-eslint/utils').TSESTree.PropertyDefinition | import('@typescript-eslint/utils').TSESTree.MethodDefinition} property
@@ -23,9 +46,11 @@ function isBooleanProperty(property, parserServices) {
2346
let isBoolean = false;
2447
let typeAnnotation = null;
2548

26-
if (property.type === 'MethodDefinition' && (property.kind === 'get' || property.kind === 'set')) {
49+
if (!property) return false;
50+
51+
if (isAccessor(property)) {
2752
// getter/setter
28-
const typeAnnotation = property.value.returnType?.typeAnnotation || property.value.params[0]?.typeAnnotation?.typeAnnotation;
53+
const typeAnnotation = property.value.returnType?.typeAnnotation || property.value.params[0]?.typeAnnotation?.typeAnnotation.type;
2954
isBoolean = typeAnnotation
3055
? typeAnnotation === 'TSBooleanKeyword'
3156
: isBooleanType(property, parserServices);
@@ -87,7 +112,9 @@ export const rule = ESLintUtils.RuleCreator.withoutDocs({
87112
);
88113
// classDeclaration.body.body.indexOf(property);
89114

90-
let isBoolean = isBooleanProperty(property/*, parserServices*/);
115+
let isBoolean = isAccessor(property)
116+
? isBooleanProperty(property) || isBooleanProperty(getSiblingProperty(classDeclaration, property))
117+
: isBooleanProperty(property/*, parserServices*/);
91118

92119
if (!isBoolean) return;
93120

0 commit comments

Comments
 (0)