TWNTY-3794 - ESLint rule: only take explicit boolean predicates in if statements (#4354)
* ESLint rule: only take explicit boolean predicates in if statements Co-authored-by: v1b3m <vibenjamin6@gmail.com> Co-authored-by: Toledodev <rafael.toledo@engenharia.ufjf.br> * Merge main Co-authored-by: v1b3m <vibenjamin6@gmail.com> Co-authored-by: Toledodev <rafael.toledo@engenharia.ufjf.br> * Fix frontend linter errors Co-authored-by: v1b3m <vibenjamin6@gmail.com> Co-authored-by: Toledodev <rafael.toledo@engenharia.ufjf.br> * Fix jest Co-authored-by: v1b3m <vibenjamin6@gmail.com> Co-authored-by: Toledodev <rafael.toledo@engenharia.ufjf.br> * Refactor according to review Co-authored-by: v1b3m <vibenjamin6@gmail.com> Co-authored-by: Toledodev <rafael.toledo@engenharia.ufjf.br> * Refactor according to review Co-authored-by: v1b3m <vibenjamin6@gmail.com> Co-authored-by: Toledodev <rafael.toledo@engenharia.ufjf.br> * Fix lint on new code Co-authored-by: v1b3m <vibenjamin6@gmail.com> Co-authored-by: Toledodev <rafael.toledo@engenharia.ufjf.br> --------- Co-authored-by: gitstart-twenty <gitstart-twenty@users.noreply.github.com> Co-authored-by: v1b3m <vibenjamin6@gmail.com> Co-authored-by: Toledodev <rafael.toledo@engenharia.ufjf.br>
This commit is contained in:
committed by
GitHub
parent
40bea0d95e
commit
17511be0cf
@ -1,6 +1,7 @@
|
||||
import { createContext } from 'react';
|
||||
|
||||
import { SpreadsheetOptions } from '@/spreadsheet-import/types';
|
||||
import { isNullable } from '~/utils/isNullable';
|
||||
|
||||
export const RsiContext = createContext({} as any);
|
||||
|
||||
@ -13,7 +14,7 @@ export const Providers = <T extends string>({
|
||||
children,
|
||||
values,
|
||||
}: ProvidersProps<T>) => {
|
||||
if (!values.fields) {
|
||||
if (isNullable(values.fields)) {
|
||||
throw new Error('Fields must be provided to spreadsheet-import');
|
||||
}
|
||||
|
||||
|
||||
@ -95,7 +95,7 @@ export const UploadFlow = ({ nextStep }: UploadFlowProps) => {
|
||||
const isSingleSheet = workbook.SheetNames.length === 1;
|
||||
if (isSingleSheet) {
|
||||
if (
|
||||
maxRecords &&
|
||||
maxRecords > 0 &&
|
||||
exceedsMaxRecords(
|
||||
workbook.Sheets[workbook.SheetNames[0]],
|
||||
maxRecords,
|
||||
@ -147,7 +147,7 @@ export const UploadFlow = ({ nextStep }: UploadFlowProps) => {
|
||||
sheetNames={state.workbook.SheetNames}
|
||||
onContinue={async (sheetName) => {
|
||||
if (
|
||||
maxRecords &&
|
||||
maxRecords > 0 &&
|
||||
exceedsMaxRecords(state.workbook.Sheets[sheetName], maxRecords)
|
||||
) {
|
||||
errorToast(
|
||||
|
||||
@ -14,6 +14,7 @@ import { useDialogManager } from '@/ui/feedback/dialog-manager/hooks/useDialogMa
|
||||
import { Button } from '@/ui/input/button/components/Button';
|
||||
import { Toggle } from '@/ui/input/components/Toggle';
|
||||
import { Modal } from '@/ui/layout/modal/components/Modal';
|
||||
import { isNonNullable } from '~/utils/isNonNullable';
|
||||
|
||||
import { generateColumns } from './components/columns';
|
||||
import { Meta } from './types';
|
||||
@ -94,7 +95,7 @@ export const ValidationStep = <T extends string>({
|
||||
);
|
||||
|
||||
const deleteSelectedRows = () => {
|
||||
if (selectedRows.size) {
|
||||
if (selectedRows.size > 0) {
|
||||
const newData = data.filter((value) => !selectedRows.has(value.__index));
|
||||
updateData(newData);
|
||||
setSelectedRows(new Set());
|
||||
@ -129,7 +130,7 @@ export const ValidationStep = <T extends string>({
|
||||
const tableData = useMemo(() => {
|
||||
if (filterByErrors) {
|
||||
return data.filter((value) => {
|
||||
if (value?.__errors) {
|
||||
if (isNonNullable(value?.__errors)) {
|
||||
return Object.values(value.__errors)?.filter(
|
||||
(err) => err.level === 'error',
|
||||
).length;
|
||||
@ -146,7 +147,7 @@ export const ValidationStep = <T extends string>({
|
||||
const calculatedData = data.reduce(
|
||||
(acc, value) => {
|
||||
const { __index, __errors, ...values } = value;
|
||||
if (__errors) {
|
||||
if (isNonNullable(__errors)) {
|
||||
for (const key in __errors) {
|
||||
if (__errors[key].level === 'error') {
|
||||
acc.invalidData.push(values as unknown as Data<T>);
|
||||
@ -165,7 +166,7 @@ export const ValidationStep = <T extends string>({
|
||||
};
|
||||
const onContinue = () => {
|
||||
const invalidData = data.find((value) => {
|
||||
if (value?.__errors) {
|
||||
if (isNonNullable(value?.__errors)) {
|
||||
return !!Object.values(value.__errors)?.filter(
|
||||
(err) => err.level === 'error',
|
||||
).length;
|
||||
|
||||
@ -9,6 +9,7 @@ import { AppTooltip } from '@/ui/display/tooltip/AppTooltip';
|
||||
import { Checkbox, CheckboxVariant } from '@/ui/input/components/Checkbox';
|
||||
import { TextInput } from '@/ui/input/components/TextInput';
|
||||
import { Toggle } from '@/ui/input/components/Toggle';
|
||||
import { isNonNullable } from '~/utils/isNonNullable';
|
||||
|
||||
import { Meta } from '../types';
|
||||
|
||||
@ -207,7 +208,7 @@ export const generateColumns = <T extends string>(
|
||||
);
|
||||
}
|
||||
|
||||
if (row.__errors?.[columnKey]) {
|
||||
if (isNonNullable(row.__errors?.[columnKey])) {
|
||||
return (
|
||||
<>
|
||||
{component}
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
import { isNonEmptyString } from '@sniptt/guards';
|
||||
import { v4 } from 'uuid';
|
||||
|
||||
import {
|
||||
@ -11,6 +12,8 @@ import {
|
||||
RowHook,
|
||||
TableHook,
|
||||
} from '@/spreadsheet-import/types';
|
||||
import { isNonNullable } from '~/utils/isNonNullable';
|
||||
import { isNullable } from '~/utils/isNullable';
|
||||
|
||||
export const addErrorsAndRunHooks = <T extends string>(
|
||||
data: (Data<T> & Partial<Meta>)[],
|
||||
@ -27,11 +30,11 @@ export const addErrorsAndRunHooks = <T extends string>(
|
||||
};
|
||||
};
|
||||
|
||||
if (tableHook) {
|
||||
if (isNonNullable(tableHook)) {
|
||||
data = tableHook(data, addHookError);
|
||||
}
|
||||
|
||||
if (rowHook) {
|
||||
if (isNonNullable(rowHook)) {
|
||||
data = data.map((value, index) =>
|
||||
rowHook(value, (...props) => addHookError(index, ...props), data),
|
||||
);
|
||||
@ -47,7 +50,10 @@ export const addErrorsAndRunHooks = <T extends string>(
|
||||
const duplicates = new Set(); // Set of items used multiple times
|
||||
|
||||
values.forEach((value) => {
|
||||
if (validation.allowEmpty && !value) {
|
||||
if (
|
||||
validation.allowEmpty === true &&
|
||||
(isNullable(value) || value === '' || !value)
|
||||
) {
|
||||
// If allowEmpty is set, we will not validate falsy fields such as undefined or empty string.
|
||||
return;
|
||||
}
|
||||
@ -95,7 +101,7 @@ export const addErrorsAndRunHooks = <T extends string>(
|
||||
data.forEach((entry, index) => {
|
||||
const value = entry[field.key]?.toString();
|
||||
|
||||
if (value && !value.match(regex)) {
|
||||
if (isNonEmptyString(value) && !value.match(regex)) {
|
||||
errors[index] = {
|
||||
...errors[index],
|
||||
[field.key]: {
|
||||
@ -113,7 +119,7 @@ export const addErrorsAndRunHooks = <T extends string>(
|
||||
data.forEach((entry, index) => {
|
||||
const value = entry[field.key]?.toString();
|
||||
|
||||
if (value && !validation.isValid(value)) {
|
||||
if (isNonEmptyString(value) && !validation.isValid(value)) {
|
||||
errors[index] = {
|
||||
...errors[index],
|
||||
[field.key]: {
|
||||
@ -136,10 +142,10 @@ export const addErrorsAndRunHooks = <T extends string>(
|
||||
}
|
||||
const newValue = value as Data<T> & Meta;
|
||||
|
||||
if (errors[index]) {
|
||||
if (isNonNullable(errors[index])) {
|
||||
return { ...newValue, __errors: errors[index] };
|
||||
}
|
||||
if (!errors[index] && value?.__errors) {
|
||||
if (isNullable(errors[index]) && isNonNullable(value?.__errors)) {
|
||||
return { ...newValue, __errors: null };
|
||||
}
|
||||
return newValue;
|
||||
|
||||
@ -6,6 +6,7 @@ import {
|
||||
MatchColumnsStepProps,
|
||||
} from '@/spreadsheet-import/steps/components/MatchColumnsStep/MatchColumnsStep';
|
||||
import { Field, Fields } from '@/spreadsheet-import/types';
|
||||
import { isNonNullable } from '~/utils/isNonNullable';
|
||||
|
||||
import { findMatch } from './findMatch';
|
||||
import { setColumn } from './setColumn';
|
||||
@ -18,7 +19,7 @@ export const getMatchedColumns = <T extends string>(
|
||||
) =>
|
||||
columns.reduce<Column<T>[]>((arr, column) => {
|
||||
const autoMatch = findMatch(column.header, fields, autoMapDistance);
|
||||
if (autoMatch) {
|
||||
if (isNonNullable(autoMatch)) {
|
||||
const field = fields.find((field) => field.key === autoMatch) as Field<T>;
|
||||
const duplicateIndex = arr.findIndex(
|
||||
(column) => 'value' in column && column.value === field.key,
|
||||
|
||||
@ -1,3 +1,5 @@
|
||||
import { isNonEmptyString } from '@sniptt/guards';
|
||||
|
||||
const booleanWhitelist: Record<string, boolean> = {
|
||||
yes: true,
|
||||
no: false,
|
||||
@ -6,7 +8,7 @@ const booleanWhitelist: Record<string, boolean> = {
|
||||
};
|
||||
|
||||
export const normalizeCheckboxValue = (value: string | undefined): boolean => {
|
||||
if (value && value.toLowerCase() in booleanWhitelist) {
|
||||
if (isNonEmptyString(value) && value.toLowerCase() in booleanWhitelist) {
|
||||
return booleanWhitelist[value.toLowerCase()];
|
||||
}
|
||||
return false;
|
||||
|
||||
@ -24,7 +24,7 @@ export const normalizeTableData = <T extends string>(
|
||||
|
||||
if (
|
||||
'booleanMatches' in field.fieldType &&
|
||||
Object.keys(field.fieldType).length
|
||||
Object.keys(field.fieldType).length > 0
|
||||
) {
|
||||
const booleanMatchKey = Object.keys(
|
||||
field.fieldType.booleanMatches || [],
|
||||
|
||||
Reference in New Issue
Block a user