Skip to content

Commit 185881d

Browse files
committed
Address logical changes found by no-unnecessary-condition lint
However, these are still minor and I would not consider them "bug fixes". That said the resulting code is still "cleaner".
1 parent f8e5d3b commit 185881d

File tree

8 files changed

+34
-25
lines changed

8 files changed

+34
-25
lines changed

src/common/resource.dto.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,12 @@ export class EnhancedResource<T extends ResourceShape<any>> {
110110
>();
111111

112112
static resolve(ref: ResourceLike) {
113-
if (ref && typeof ref !== 'string') {
113+
// Safety check; since this very dynamic code, it is very possible the types are lying.
114+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
115+
if (ref == null) {
116+
throw new ServerException('Resource reference is actually null');
117+
}
118+
if (typeof ref !== 'string') {
114119
return EnhancedResource.of(ref);
115120
}
116121
if (!EnhancedResource.resourcesHost) {

src/components/file/bucket/parse-uri.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ export const parseUri = (uri: string): ParsedBucketUri => {
1414
// Shouldn't ever happen
1515
throw new Error('Failed to parse Bucket URI');
1616
}
17-
const type = typeMatch[1]!;
17+
const type = typeMatch[1]?.toLowerCase() ?? '';
1818
const remainingSrc = typeMatch[2]!;
1919
const roMatch = /(:ro|:readonly)$/i.exec(remainingSrc);
2020
const readonly = !!roMatch;
2121
const path = roMatch
2222
? remainingSrc.slice(0, -roMatch[0].length)
2323
: remainingSrc;
24-
return { type: type?.toLowerCase() ?? '', path, readonly };
24+
return { type, path, readonly };
2525
};

src/components/file/file.service.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -327,13 +327,10 @@ export class FileService {
327327
tempUpload.status === 'fulfilled' &&
328328
existingUpload.status === 'fulfilled'
329329
) {
330-
if (tempUpload.value && existingUpload.value) {
331-
throw new InputException(
332-
'Upload request has already been used',
333-
'uploadId',
334-
);
335-
}
336-
throw new CreationFailed(FileVersion);
330+
throw new InputException(
331+
'Upload request has already been used',
332+
'uploadId',
333+
);
337334
} else if (
338335
tempUpload.status === 'rejected' &&
339336
existingUpload.status === 'fulfilled'

src/components/location/migrations/default-marketing-region.migration.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export class DefaultMarketingRegionMigration extends BaseMigration {
4747
const marketingRegionNameToId = mapEntries(marketingRegionList, (loc) => [
4848
loc.name,
4949
loc.id,
50-
]).asRecord;
50+
]).asMap;
5151

5252
const countries = await this.db
5353
.query()
@@ -72,7 +72,8 @@ export class DefaultMarketingRegionMigration extends BaseMigration {
7272
for (const country of countries) {
7373
const marketingRegionName =
7474
fieldRegionNameToMarketingRegionName[country.fieldRegionName];
75-
const marketingRegionId = marketingRegionNameToId[marketingRegionName];
75+
const marketingRegionId =
76+
marketingRegionNameToId.get(marketingRegionName);
7677
if (marketingRegionId === undefined) {
7778
continue;
7879
}

src/components/project/project-member/project-member.repository.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
isIdLike,
1515
NotFoundException,
1616
type Role,
17+
ServerException,
1718
type UnsecuredDto,
1819
} from '~/common';
1920
import { DtoRepository } from '~/core/database';
@@ -73,7 +74,11 @@ export class ProjectMemberRepository extends DtoRepository(ProjectMember) {
7374
'member',
7475
])
7576
.first();
76-
77+
if (!result) {
78+
throw new ServerException(
79+
'Failed to gather info for relationship verification',
80+
);
81+
}
7782
if (!result?.project) {
7883
throw new NotFoundException(
7984
'Could not find project',

src/core/authentication/session/session.initiator.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { forwardRef, Inject, Injectable } from '@nestjs/common';
2-
import { csv, setOf } from '@seedcompany/common';
2+
import { asNonEmptyArray, csv, setOf } from '@seedcompany/common';
33
import {
44
type ID,
55
InputException,
@@ -111,10 +111,10 @@ const assertValidRole = (role: string): Role => {
111111
);
112112
};
113113

114-
function csvHeader(headerVal: Many<string> | undefined) {
114+
function csvHeader(headerVal: Many<string | undefined> | undefined) {
115115
if (!headerVal) {
116116
return undefined;
117117
}
118118
const items = many(headerVal).flatMap((itemCsv) => csv(itemCsv ?? ''));
119-
return items && items.length > 0 ? items : undefined;
119+
return asNonEmptyArray(items);
120120
}

src/core/database/query/create-relationships.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { entries } from '@seedcompany/common';
12
import { createHash } from 'crypto';
23
import { node, type Query, relation } from 'cypher-query-builder';
34
import { type RelationDirection } from 'cypher-query-builder/dist/typings/clauses/relation-pattern';
@@ -90,20 +91,20 @@ export function createRelationships<TResourceStatic extends ResourceShape<any>>(
9091
maybeLabelsToRelationships?: RelationshipDefinition,
9192
) {
9293
resource = EnhancedResource.of(resource);
93-
const normalizedArgs =
94+
const normalizedArgs: AnyDirectionalDefinition =
9495
typeof directionOrDefinition === 'string'
9596
? { [directionOrDefinition]: maybeLabelsToRelationships }
9697
: directionOrDefinition;
9798

98-
const flattened = Object.entries(normalizedArgs).flatMap(
99+
const flattened = entries(normalizedArgs).flatMap(
99100
([direction, relationships]) =>
100-
Object.entries(relationships ?? {}).flatMap(([relLabel, varOrTuple]) =>
101+
Object.entries(relationships).flatMap(([relLabel, varOrTuple]) =>
101102
many(Array.isArray(varOrTuple) ? varOrTuple[1] ?? [] : varOrTuple).map(
102103
(id, i) => ({
103104
nodeLabel: Array.isArray(varOrTuple) ? varOrTuple[0] : undefined, // no labels for variables
104105
id,
105-
direction: direction as RelationDirection,
106-
relLabel: relLabel,
106+
direction,
107+
relLabel,
107108
variable: !Array.isArray(varOrTuple)
108109
? varOrTuple instanceof Variable
109110
? varOrTuple.value
@@ -123,9 +124,9 @@ export function createRelationships<TResourceStatic extends ResourceShape<any>>(
123124
return (query: Query) => query;
124125
}
125126

126-
// We are creating inside of changeset if there's a changeset relation into the node.
127+
// We're creating inside a changeset if there is a changeset relation into the node.
127128
const inChangeset = flattened.some(
128-
(f) => f.direction === 'in' && f.relLabel === 'changeset' && f.id,
129+
(f) => f.direction === 'in' && f.relLabel === 'changeset',
129130
);
130131

131132
const createdAt = DateTime.local();

src/core/database/query/filters.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,11 +176,11 @@ export const pathExists =
176176
): Builder<T, K> =>
177177
({ key, value }) => {
178178
const cond = pathPattern(isFunction(pattern) ? pattern(value) : pattern);
179-
return { [key]: value ? cond : not(cond) };
179+
return { [key]: value !== false ? cond : not(cond) };
180180
};
181181

182182
export const pathExistsWhenTrue: typeof pathExists = (pattern) => (args) =>
183-
args.value ? pathExists(pattern)(args) : null;
183+
args.value !== false ? pathExists(pattern)(args) : null;
184184

185185
export const isPinned = pathExists<{ pinned?: boolean }, 'pinned'>([
186186
currentUser,

0 commit comments

Comments
 (0)