Skip to content

Commit f2c997a

Browse files
Fix ESLint warnings (#1444)
* Fix ESLint prefer-nullish-coalescing warnings * Correctly type request bodies in API routers * stringify error for logging in epicRouter.ts
1 parent 0838872 commit f2c997a

File tree

13 files changed

+45
-93
lines changed

13 files changed

+45
-93
lines changed

.changeset/serious-ducks-check.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@guardian/support-dotcom-components': patch
3+
---
4+
5+
prefer-nullish-coalescing lint rule violations fix

eslint-suppressions.json

Lines changed: 0 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -2,51 +2,6 @@
22
"src/server/api/bannerRouter.ts": {
33
"@typescript-eslint/no-unnecessary-condition": {
44
"count": 1
5-
},
6-
"@typescript-eslint/no-unsafe-assignment": {
7-
"count": 7
8-
},
9-
"@typescript-eslint/no-unsafe-argument": {
10-
"count": 1
11-
},
12-
"@typescript-eslint/no-unsafe-member-access": {
13-
"count": 6
14-
}
15-
},
16-
"src/server/api/epicRouter.ts": {
17-
"@typescript-eslint/restrict-template-expressions": {
18-
"count": 1
19-
},
20-
"@typescript-eslint/prefer-nullish-coalescing": {
21-
"count": 2
22-
},
23-
"@typescript-eslint/no-unsafe-assignment": {
24-
"count": 3
25-
},
26-
"@typescript-eslint/no-unsafe-argument": {
27-
"count": 2
28-
},
29-
"@typescript-eslint/no-unsafe-call": {
30-
"count": 2
31-
},
32-
"@typescript-eslint/no-unsafe-member-access": {
33-
"count": 3
34-
}
35-
},
36-
"src/server/api/gutterRouter.ts": {
37-
"@typescript-eslint/no-unsafe-assignment": {
38-
"count": 1
39-
},
40-
"@typescript-eslint/no-unsafe-argument": {
41-
"count": 1
42-
}
43-
},
44-
"src/server/api/headerRouter.ts": {
45-
"@typescript-eslint/no-unsafe-assignment": {
46-
"count": 1
47-
},
48-
"@typescript-eslint/no-unsafe-argument": {
49-
"count": 1
505
}
516
},
527
"src/server/factories/test.ts": {
@@ -74,16 +29,6 @@
7429
"count": 1
7530
}
7631
},
77-
"src/server/lib/targeting.ts": {
78-
"@typescript-eslint/prefer-nullish-coalescing": {
79-
"count": 1
80-
}
81-
},
82-
"src/server/lib/tracking.ts": {
83-
"@typescript-eslint/prefer-nullish-coalescing": {
84-
"count": 1
85-
}
86-
},
8732
"src/server/middleware/errorHandling.ts": {
8833
"@typescript-eslint/no-unsafe-assignment": {
8934
"count": 1
@@ -102,9 +47,6 @@
10247
"src/server/selection/ab.ts": {
10348
"@typescript-eslint/no-unnecessary-condition": {
10449
"count": 1
105-
},
106-
"@typescript-eslint/prefer-nullish-coalescing": {
107-
"count": 1
10850
}
10951
},
11052
"src/server/selection/epsilonGreedySelection.ts": {
@@ -144,24 +86,13 @@
14486
}
14587
},
14688
"src/server/tests/epics/epicSelection.ts": {
147-
"@typescript-eslint/prefer-nullish-coalescing": {
148-
"count": 4
149-
},
15089
"@typescript-eslint/no-unnecessary-condition": {
15190
"count": 1
15291
}
15392
},
154-
"src/server/tests/gutters/gutterSelection.ts": {
155-
"@typescript-eslint/prefer-nullish-coalescing": {
156-
"count": 2
157-
}
158-
},
15993
"src/server/tests/headers/headerSelection.ts": {
16094
"@typescript-eslint/no-unnecessary-condition": {
16195
"count": 2
162-
},
163-
"@typescript-eslint/prefer-nullish-coalescing": {
164-
"count": 2
16596
}
16697
},
16798
"src/server/utils/logging.ts": {
@@ -197,9 +128,6 @@
197128
}
198129
},
199130
"src/shared/lib/geolocation.ts": {
200-
"@typescript-eslint/prefer-nullish-coalescing": {
201-
"count": 1
202-
},
203131
"@typescript-eslint/no-unnecessary-condition": {
204132
"count": 1
205133
}

src/server/api/bannerRouter.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,11 @@ export const buildBannerRouter = (
167167

168168
router.post(
169169
'/banner',
170-
(req: express.Request, res: express.Response, next: express.NextFunction) => {
170+
(
171+
req: express.Request<Record<string, never>, unknown, { targeting: BannerTargeting }>,
172+
res: express.Response,
173+
next: express.NextFunction,
174+
) => {
171175
try {
172176
const { targeting } = req.body;
173177
const params = getQueryParams(req.query);

src/server/api/epicRouter.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ export const buildEpicRouter = (
7474

7575
return [...hardcodedTests, ...articleEpicTests.get()];
7676
} catch (err) {
77-
logWarn(`Error getting article epic tests: ${err}`);
78-
77+
logWarn(`Error getting article epic tests: ${String(err)}`);
7978
return [];
8079
}
8180
};
@@ -96,7 +95,7 @@ export const buildEpicRouter = (
9695
return {};
9796
}
9897

99-
const targetingMvtId = targeting.mvtId || 1;
98+
const targetingMvtId = targeting.mvtId ?? 1;
10099

101100
const tests =
102101
type === 'ARTICLE'
@@ -166,7 +165,7 @@ export const buildEpicRouter = (
166165
abTestName: test.name,
167166
abTestVariant: variant.name,
168167
campaignCode: buildEpicCampaignCode(test, variant),
169-
campaignId: `epic_${test.campaignId || test.name}`,
168+
campaignId: `epic_${test.campaignId ?? test.name}`,
170169
componentType: 'ACQUISITIONS_EPIC',
171170
products: ['CONTRIBUTION', 'MEMBERSHIP_SUPPORTER'],
172171
labels: test.isSuperMode ? ['SUPER_MODE'] : undefined,
@@ -198,7 +197,11 @@ export const buildEpicRouter = (
198197

199198
router.post(
200199
'/epic',
201-
(req: express.Request, res: express.Response, next: express.NextFunction): void => {
200+
(
201+
req: express.Request<Record<string, never>, unknown, { targeting: EpicTargeting }>,
202+
res: express.Response,
203+
next: express.NextFunction,
204+
): void => {
202205
try {
203206
const epicType: EpicType = 'ARTICLE';
204207

@@ -228,7 +231,11 @@ export const buildEpicRouter = (
228231

229232
router.post(
230233
'/liveblog-epic',
231-
(req: express.Request, res: express.Response, next: express.NextFunction) => {
234+
(
235+
req: express.Request<Record<string, never>, unknown, { targeting: EpicTargeting }>,
236+
res: express.Response,
237+
next: express.NextFunction,
238+
) => {
232239
try {
233240
const epicType: EpicType = 'LIVEBLOG';
234241

src/server/api/gutterRouter.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,11 @@ export const buildGutterRouter = (
8484
router.post(
8585
'/gutter-liveblog',
8686
bodyContainsAllFields(['targeting']),
87-
(req: express.Request, res: express.Response, next: express.NextFunction) => {
87+
(
88+
req: express.Request<Record<string, never>, unknown, { targeting: GutterTargeting }>,
89+
res: express.Response,
90+
next: express.NextFunction,
91+
) => {
8892
try {
8993
const { targeting } = req.body;
9094
const params = getQueryParams(req.query);

src/server/api/headerRouter.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,11 @@ export const buildHeaderRouter = (
7979
router.post(
8080
'/header',
8181
bodyContainsAllFields(['targeting']),
82-
(req: express.Request, res: express.Response, next: express.NextFunction) => {
82+
(
83+
req: express.Request<Record<string, never>, unknown, { targeting: HeaderTargeting }>,
84+
res: express.Response,
85+
next: express.NextFunction,
86+
) => {
8387
try {
8488
const { targeting } = req.body;
8589
const params = getQueryParams(req.query);

src/server/lib/targeting.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export const shouldThrottle = (
5757
};
5858

5959
export const shouldNotRenderEpic = (meta: EpicTargeting): boolean => {
60-
const section = meta.sectionId || meta.sectionName;
60+
const section = meta.sectionId ?? meta.sectionName;
6161
const isLowValueSection = !!section && lowValueSections.includes(section);
6262
const isLowValueTag = lowValueTags.some((id) => meta.tags.some((pageTag) => pageTag.id === id));
6363

src/server/lib/tracking.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import type {
1010
const campaignPrefix = 'gdnwb_copts_memco';
1111

1212
export const buildEpicCampaignCode = (test: EpicTest, variant: EpicVariant): string =>
13-
`${campaignPrefix}_${test.campaignId || test.name}_${variant.name}`;
13+
`${campaignPrefix}_${test.campaignId ?? test.name}_${variant.name}`;
1414

1515
export const buildBannerCampaignCode = (test: BannerTest, variant: BannerVariant): string =>
1616
`${test.name}_${variant.name}`;

src/server/selection/ab.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ export const selectAmountsTestVariant = (
112112

113113
// Test is running and user has been successfully assigned to a control/variant
114114
return {
115-
testName: liveTestName || `${testName}_AB_TEST`,
115+
testName: liveTestName ?? `${testName}_AB_TEST`,
116116
...variant,
117117
};
118118
};

src/server/tests/epics/epicSelection.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ export const withinMaxViews = (log: EpicViewLog, now: Date = new Date()): Filter
109109

110110
const testId = test.useLocalViewLog ? test.name : undefined;
111111

112-
return !shouldThrottle(log, test.maxViews || defaultMaxViews, testId, now);
112+
return !shouldThrottle(log, test.maxViews ?? defaultMaxViews, testId, now);
113113
},
114114
});
115115

@@ -203,9 +203,9 @@ export const findTestAndVariant = (
203203
hasCountryCode,
204204
isCountryTargetedForEpic,
205205
// For the super mode pass, we treat all tests as "always ask" so disable this filter
206-
...(isSuperModePass ? [] : [withinMaxViews(targeting.epicViewLog || [])]),
206+
...(isSuperModePass ? [] : [withinMaxViews(targeting.epicViewLog ?? [])]),
207207
respectArticleCountOptOut,
208-
withinArticleViewedSettings(targeting.weeklyArticleHistory || []),
208+
withinArticleViewedSettings(targeting.weeklyArticleHistory ?? []),
209209
deviceTypeMatchesFilter(userDeviceType),
210210
correctSignedInStatusFilter,
211211
momentumMatches,
@@ -278,7 +278,7 @@ function selectEpicVariant(
278278
banditData: BanditData[],
279279
targeting: EpicTargeting,
280280
): Result {
281-
const result = selectVariant<EpicVariant, EpicTest>(test, targeting.mvtId || 1, banditData);
281+
const result = selectVariant<EpicVariant, EpicTest>(test, targeting.mvtId ?? 1, banditData);
282282
return {
283283
result,
284284
};

0 commit comments

Comments
 (0)