Skip to content

Commit ff4082d

Browse files
resolve a few edge cases
1 parent 4089aed commit ff4082d

File tree

2 files changed

+36
-10
lines changed

2 files changed

+36
-10
lines changed

__tests__/src/rules/has-valid-accessibility-ignores-invert-colors-test.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ const invalidCustomImport = [
214214
},
215215
},
216216
{
217-
title: 'supports module imports',
217+
title: 'supports linting module imports',
218218
code: `import * as RN from 'react-native';
219219
const { Image } = RN;
220220
@@ -227,6 +227,28 @@ const invalidCustomImport = [
227227
sourceType: 'module',
228228
},
229229
},
230+
{
231+
title:
232+
'supports linting on Custom Invertable ImageComponents without react-native imported',
233+
code: `import { FastImage } from './fast-image'
234+
235+
const Component = (props) => (
236+
<>
237+
<FastImage />
238+
<FastImage accessibilityIgnoresInvertColors={'true'} />
239+
</>
240+
);
241+
`,
242+
errors: [missingPropError, typeError],
243+
parserOptions: {
244+
sourceType: 'module',
245+
},
246+
options: [
247+
{
248+
invertableComponents: ['FastImage'],
249+
},
250+
],
251+
},
230252
];
231253

232254
const invalid = [

src/rules/has-valid-accessibility-ignores-invert-colors.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,38 +70,42 @@ module.exports = {
7070
create: ({ options, report, getSourceCode }: ESLintContext) => {
7171
// Checks to see if there are valid imports and if so verifies that those imports related to 'react-native' or if a custom module exists
7272
const { text } = getSourceCode();
73-
let hasCustomImage = false;
73+
let hasImageImportAlias = false;
7474
if (text.match(new RegExp(/import/, 'g'))) {
7575
const hasReactNativeImage = verifyReactNativeImage(text);
7676
if (!hasReactNativeImage) {
77-
hasCustomImage = true;
77+
hasImageImportAlias = true;
7878
}
7979
}
8080

8181
// avoid directly mutating a constant as it ends up stacking up duplicate strings
82-
const elementsToCheck = !hasCustomImage
82+
const elementsToCheck = !hasImageImportAlias
8383
? [...defaultInvertableComponents]
8484
: [];
8585
if (options.length > 0) {
8686
const { invertableComponents } = options[0];
8787
if (invertableComponents) {
8888
elementsToCheck.push(...invertableComponents);
8989
}
90-
} else if (hasCustomImage) {
90+
} else if (hasImageImportAlias && options.length === 0) {
9191
// Exit process if there is nothing to check
92-
// return {};
92+
return {};
9393
}
9494

9595
return {
9696
JSXElement: (node: JSXElement) => {
97-
/**
98-
* @description RegExp to select single or multiline 'Image' component and determine if the import origin path is 'react-native' or a custom module
99-
* */
10097
// $FlowFixMe
10198
const { children, openingElement, parent } = node;
10299

100+
// $FlowFixMe
101+
const { name } = openingElement.name;
102+
103+
// escape out if the item is an ignored JSXElement
104+
if (hasImageImportAlias && name === 'Image') {
105+
return;
106+
}
107+
103108
if (
104-
!hasCustomImage &&
105109
hasProp(openingElement.attributes, propName) &&
106110
!isNodePropValueBoolean(getProp(openingElement.attributes, propName))
107111
) {

0 commit comments

Comments
 (0)