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:
gitstart-app[bot]
2024-03-09 10:48:19 +01:00
committed by GitHub
parent 40bea0d95e
commit 17511be0cf
164 changed files with 655 additions and 367 deletions

View File

@ -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;

View File

@ -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,

View File

@ -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;

View File

@ -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 || [],