Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add prefer-at condition to eslint #49534

Draft
wants to merge 15 commits into
base: main
Choose a base branch
from

Conversation

zfurtak
Copy link
Contributor

@zfurtak zfurtak commented Sep 20, 2024

Details

Fixed Issues

$ #43055
PROPOSAL:

Tests

  • Verify that no errors appear in the JS console

Offline tests

QA Steps

  • Verify that no errors appear in the JS console

PR Author Checklist

  • I linked the correct issue in the ### Fixed Issues section above
  • I wrote clear testing steps that cover the changes made in this PR
    • I added steps for local testing in the Tests section
    • I added steps for the expected offline behavior in the Offline steps section
    • I added steps for Staging and/or Production testing in the QA steps section
    • I added steps to cover failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
    • I tested this PR with a High Traffic account against the staging or production API to ensure there are no regressions (e.g. long loading states that impact usability).
  • I included screenshots or videos for tests on all platforms
  • I ran the tests on all platforms & verified they passed on:
    • Android: Native
    • Android: mWeb Chrome
    • iOS: Native
    • iOS: mWeb Safari
    • MacOS: Chrome / Safari
    • MacOS: Desktop
  • I verified there are no console errors (if there's a console error not related to the PR, report it or open an issue for it to be fixed)
  • I followed proper code patterns (see Reviewing the code)
    • I verified that any callback methods that were added or modified are named for what the method does and never what callback they handle (i.e. toggleReport and not onIconClick)
    • I verified that the left part of a conditional rendering a React component is a boolean and NOT a string, e.g. myBool && <MyComponent />.
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified any copy / text shown in the product is localized by adding it to src/languages/* files and using the translation method
      • If any non-english text was added/modified, I verified the translation was requested/reviewed in #expensify-open-source and it was approved by an internal Expensify engineer. Link to Slack message:
    • I verified all numbers, amounts, dates and phone numbers shown in the product are using the localization methods
    • I verified any copy / text that was added to the app is grammatically correct in English. It adheres to proper capitalization guidelines (note: only the first word of header/labels should be capitalized), and is either coming verbatim from figma or has been approved by marketing (in order to get marketing approval, ask the Bug Zero team member to add the Waiting for copy label to the issue)
    • I verified proper file naming conventions were followed for any new files or renamed files. All non-platform specific files are named after what they export and are not named "index.js". All platform-specific files are named for the platform the code supports as outlined in the README.
    • I verified the JSDocs style guidelines (in STYLE.md) were followed
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I followed the guidelines as stated in the Review Guidelines
  • I tested other components that can be impacted by my changes (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar are working as expected)
  • I verified all code is DRY (the PR doesn't include any logic written more than once, with the exception of tests)
  • I verified any variables that can be defined as constants (ie. in CONST.js or at the top of the file that uses the constant) are defined as such
  • I verified that if a function's arguments changed that all usages have also been updated correctly
  • If any new file was added I verified that:
    • The file has a description of what it does and/or why is needed at the top of the file if the code is not self explanatory
  • If a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(theme.componentBG))
  • If the PR modifies code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If the PR modifies a component or page that can be accessed by a direct deeplink, I verified that the code functions as expected when the deeplink is used - from a logged in and logged out account.
  • If the PR modifies the UI (e.g. new buttons, new UI components, changing the padding/spacing/sizing, moving components, etc) or modifies the form input styles:
    • I verified that all the inputs inside a form are aligned with each other.
    • I added Design label and/or tagged @Expensify/design so the design team can review the changes.
  • If a new page is added, I verified it's using the ScrollView component to make it scrollable when more elements are added to the page.
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.

Screenshots/Videos

Android: Native
Android: mWeb Chrome
iOS: Native
iOS: mWeb Safari
MacOS: Chrome / Safari
MacOS: Desktop

Copy link
Contributor

@VickyStash VickyStash left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewing... 📝

.github/actions/javascript/proposalPoliceComment/index.js Outdated Show resolved Hide resolved
.storybook/webpack.config.ts Outdated Show resolved Hide resolved
src/components/ActiveGuidesEventListener.tsx Outdated Show resolved Hide resolved
src/CONST.ts Show resolved Hide resolved
@@ -20,6 +20,7 @@ import CONST from '@src/CONST';
import type {Icon as IconType} from '@src/types/onyx/OnyxCommon';
import type {TooltipAnchorAlignment} from '@src/types/utils/AnchorAlignment';
import type IconAsset from '@src/types/utils/IconAsset';
import getDefaultIcon from '@src/utils/getDefaultIcon';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import getDefaultIcon from '@src/utils/getDefaultIcon';
import fallbackIcon from '@src/utils/getDefaultIcon';

src/components/MenuItem.tsx Outdated Show resolved Hide resolved
src/components/MagicCodeInput.tsx Show resolved Hide resolved
src/components/MagicCodeInput.tsx Show resolved Hide resolved
src/components/OptionsList/BaseOptionsList.tsx Outdated Show resolved Hide resolved
@@ -309,8 +309,12 @@ function MoneyRequestView({report, shouldShowAnimatedBackground, readonly = fals

// Return violations if there are any
if (hasViolations(field, data, policyHasDependentTags, tagValue)) {
const violations = getViolationsForField(field, data, policyHasDependentTags, tagValue);
return ViolationsUtils.getViolationTranslation(violations[0], translate);
const violations = getViolationsForField(field, data, policyHasDependentTags, tagValue) as OnyxTypes.TransactionViolation[];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, make sure getViolationsForField returns OnyxTypes.TransactionViolation[] same way as it was before.

Suggested change
const violations = getViolationsForField(field, data, policyHasDependentTags, tagValue) as OnyxTypes.TransactionViolation[];
const violations = getViolationsForField(field, data, policyHasDependentTags, tagValue);

@@ -651,7 +655,7 @@ function MoneyRequestView({report, shouldShowAnimatedBackground, readonly = fals
<Text color={!transactionBillable ? theme.textSupporting : undefined}>{translate('common.billable')}</Text>
{!!getErrorForField('billable') && (
<ViolationMessages
violations={getViolationsForField('billable')}
violations={getViolationsForField('billable') as OnyxTypes.TransactionViolation[]}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same

Suggested change
violations={getViolationsForField('billable') as OnyxTypes.TransactionViolation[]}
violations={getViolationsForField('billable')}

Comment on lines -100 to 114
if (policyHasDependentTags && currentViolations[0]?.name === CONST.VIOLATIONS.MISSING_TAG && data?.tagListName) {
if (policyHasDependentTags && currentViolations.at(0)?.name === CONST.VIOLATIONS.MISSING_TAG && data?.tagListName) {
return [
{
...currentViolations[0],
...currentViolations.at(0),
data: {
...currentViolations[0].data,
...currentViolations.at(0)?.data,
tagName: data?.tagListName,
},
},
];
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like due to these updates the return type of getViolationsForField has changed. To fix it you can put
currentViolations.at(0) into const and reuse it:

            const firstViolation = currentViolations.at(0);
           ...
            if (policyHasDependentTags && firstViolation?.name === CONST.VIOLATIONS.MISSING_TAG && data?.tagListName) {
                return [
                    {
                        ...firstViolation,
                        data: {
                            ...firstViolation.data,
                            tagName: data?.tagListName,
                        },
                    },
                ];
            }


src/components/RoomHeaderAvatars.tsx Show resolved Hide resolved
@@ -242,7 +242,7 @@ function BaseSelectionList<TItem extends ListItem>(
*/
const scrollToIndex = useCallback(
(index: number, animated = true) => {
const item = flattenedSections.allOptions[index];
const item = flattenedSections.allOptions.at(index) ?? 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make sure indexes in this file won't be negative since it can affect the logic then.

src/utils/getDefaultIcon.ts Outdated Show resolved Hide resolved
src/libs/PaginationUtils.ts Show resolved Hide resolved
src/libs/EmojiUtils.ts Show resolved Hide resolved
src/libs/PolicyUtils.ts Show resolved Hide resolved
@@ -847,7 +851,7 @@ function getFirstVisibleReportActionID(sortedReportActions: ReportAction[] = [],
return '';
}
const sortedFilterReportActions = sortedReportActions.filter((action) => !isDeletedAction(action) || (action?.childVisibleActionCount ?? 0) > 0 || isOffline);
return sortedFilterReportActions.length > 1 ? sortedFilterReportActions[sortedFilterReportActions.length - 2].reportActionID : '';
return sortedFilterReportActions.length > 1 ? sortedFilterReportActions.at(sortedFilterReportActions.length - 2)?.reportActionID ?? '-1' : '';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sure that sortedFilterReportActions.length - 2 won't be negative, since the update can affect the logic then.

@@ -1437,7 +1441,7 @@ function isCurrentActionUnread(report: OnyxEntry<Report>, reportAction: ReportAc
if (currentActionIndex === -1) {
return false;
}
const prevReportAction = sortedReportActions[currentActionIndex - 1];
const prevReportAction = sortedReportActions.at(currentActionIndex - 1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sure that currentActionIndex - 1 won't be negative, since it can affect the logic then.

src/libs/actions/Policy/Tag.ts Show resolved Hide resolved
src/libs/actions/User.ts Outdated Show resolved Hide resolved
src/pages/Debug/DebugDetails.tsx Outdated Show resolved Hide resolved
@@ -139,7 +155,7 @@ function SearchTypeMenuNarrow({typeMenuItems, activeItemIndex, queryJSON, title,
<View style={[styles.pb4, styles.flexRow, styles.alignItemsCenter, styles.justifyContentBetween, styles.ph5, styles.gap2]}>
<PressableWithFeedback
accessible
accessibilityLabel={popoverMenuItems[activeItemIndex]?.text ?? ''}
accessibilityLabel={popoverMenuItems.at(activeItemIndex)?.text ?? ''}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make sure activeItemIndex won't be negative since it can affect the logic then.

@@ -357,7 +360,7 @@ function SuggestionMention(
const leftString = newValue.substring(afterLastBreakLineIndex, selectionEnd);
const words = leftString.split(CONST.REGEX.SPACE_OR_EMOJI);
const lastWord: string = words.at(-1) ?? '';
const secondToLastWord = words[words.length - 3];
const secondToLastWord = words.at(words.length - 3);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, make sure it doesn't break anything, cause .at() accepts negative integers while [] not. See #49534 (comment)

src/pages/home/report/ReportActionsList.tsx Show resolved Hide resolved
assignedCardsGrouped[domainGroupIndex].errors = {...assignedCardsGrouped[domainGroupIndex].errors, ...card.errors};
if (card.fraud === CONST.EXPENSIFY_CARD.FRAUD_TYPES.DOMAIN || card.fraud === CONST.EXPENSIFY_CARD.FRAUD_TYPES.INDIVIDUAL) {
assignedCardsGrouped[domainGroupIndex].brickRoadIndicator = CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR;
const assignedCardsGroupedItem = assignedCardsGrouped.at(domainGroupIndex);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, make sure it doesn't break anything, cause .at() accepts negative integers while [] not. See #49534 (comment)

Comment on lines +117 to +170
<OfflineWithFeedback
errors={policy?.errorFields?.requiresTag}
pendingAction={policy?.pendingFields?.requiresTag}
errorRowStyles={styles.mh5}
>
<View style={[styles.flexRow, styles.mh5, styles.mv4, styles.alignItemsCenter, styles.justifyContentBetween]}>
<Text style={[styles.textNormal]}>{translate('workspace.tags.requiresTag')}</Text>
<Switch
isOn={policy?.requiresTag ?? false}
accessibilityLabel={translate('workspace.tags.requiresTag')}
onToggle={updateWorkspaceRequiresTag}
disabled={!policy?.areTagsEnabled || !hasEnabledOptions}
/>
</View>
</OfflineWithFeedback>
{canUseWorkspaceRules && policy?.areRulesEnabled && (
<OfflineWithFeedback pendingAction={billableExpensesPending(policy)}>
<View style={[styles.flexRow, styles.mh5, styles.mv4, styles.alignItemsCenter, styles.justifyContentBetween]}>
<Text style={[styles.textNormal]}>{translate('workspace.tags.trackBillable')}</Text>
<Switch
isOn={!(policy?.disabledFields?.defaultBillable ?? false)}
accessibilityLabel={translate('workspace.tags.trackBillable')}
onToggle={() => toggleBillableExpenses(policy)}
/>
</View>
</OfflineWithFeedback>
)}
<OfflineWithFeedback
errors={policy?.errorFields?.requiresTag}
pendingAction={policy?.pendingFields?.requiresTag}
errorRowStyles={styles.mh5}
>
<View style={[styles.flexRow, styles.mh5, styles.mv4, styles.alignItemsCenter, styles.justifyContentBetween]}>
<Text style={[styles.textNormal]}>{translate('workspace.tags.requiresTag')}</Text>
<Switch
isOn={policy?.requiresTag ?? false}
accessibilityLabel={translate('workspace.tags.requiresTag')}
onToggle={updateWorkspaceRequiresTag}
disabled={!policy?.areTagsEnabled || !hasEnabledOptions}
/>
</View>
</OfflineWithFeedback>
{canUseWorkspaceRules && policy?.areRulesEnabled && (
<OfflineWithFeedback pendingAction={billableExpensesPending(policy)}>
<View style={[styles.flexRow, styles.mh5, styles.mv4, styles.alignItemsCenter, styles.justifyContentBetween]}>
<Text style={[styles.textNormal]}>{translate('workspace.tags.trackBillable')}</Text>
<Switch
isOn={!(policy?.disabledFields?.defaultBillable ?? false)}
accessibilityLabel={translate('workspace.tags.trackBillable')}
onToggle={() => toggleBillableExpenses(policy)}
/>
</View>
</OfflineWithFeedback>
)}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it be in this PR?

src/styles/utils/index.ts Outdated Show resolved Hide resolved
src/types/onyx/User.ts Outdated Show resolved Hide resolved
@@ -1,4 +1,6 @@
/* eslint-disable @typescript-eslint/naming-convention */

/* eslint-disable rulesdir/prefer-at */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why we disable this rule here?

measurePerformance(<BaseOptionsListWrapper />);
measureRenders(<BaseOptionsListWrapper />);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was it changed in this PR? (relates to all these changes in this PR)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah then I just wanted to fix to check and it seemed like a quick change but I can revert it

@@ -1,3 +1,4 @@
/* eslint-disable rulesdir/prefer-at */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why we disable this rule?

@@ -1,3 +1,4 @@
/* eslint-disable rulesdir/prefer-at */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same, why we disable it?

Comment on lines +2 to +3

/* eslint-disable rulesdir/prefer-at */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was it disabled?

Copy link
Contributor

@Kicu Kicu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Related to this comment: #49534 (comment) and, more general - default icon and avatars, a few cents from me.

First of all I agree with Błażej to improve naming. This should be correctly named fallbackIcon and not a defaultIcon.

Second of all we cannot treat this:

    source: FallbackAvatar,
    type: CONST.ICON_TYPE_AVATAR,

as universal fallback icon everywhere, because this is specifically a fallback Avatar, not a "default" icon or avatar.

This means that if something is to display a different TYPE of icon, and we fallback to FallbackAvatar then it might be incorrect.
For the record this is fallback avatar:
Screenshot 2024-09-24 at 14 46 39

As you can see displaying this image in place of for example something report or workspace related might be problematic.

Because of ☝️ my suggestion would be:

  • do not add default value for mainAvatar inside SubscriptAvatar
  • do not create a separate file with fallbackIcon

Even though defaulting mainAvatar sounds like a good idea, it will hide a lot of wrong usages of SubscriptAvatar.
I think we should go case by case and verify where the fallback value is even needed. I did a quick check and found several usages where we don't have to fallback, but instead we should refactor code:

  • in OptionRow we have this check: {!!option.icons?.length && ... so we don't have to even display Avatar if there is no icons - we should clean this code and not fallback
  • in MenuItem we have floatRightAvatars?.length > 0 - same as above

Everywhere else define fallbackIcon locally and use it for the specific files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants