Skip to content

Commit 37208fe

Browse files
authored
Fix a number of strictNullChecks-related issues (#35795)
In preparation to work on enabling https://www.typescriptlang.org/tsconfig/#strictNullChecks, I fixed all the issues outside of `web_src` that came up when the option was enabled. There was also one lint issue in web_src that apparently only came up with the option enabled, so I fixed that as well. `isTruthy` is introduced because Typescript has a bug regarding `filter(Boolean)` which they are seemingly unwilling to fix. --------- Signed-off-by: silverwind <[email protected]>
1 parent aa7ec64 commit 37208fe

File tree

8 files changed

+32
-15
lines changed

8 files changed

+32
-15
lines changed

tailwind.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export default {
3737
'./{build,models,modules,routers,services}/**/*.go',
3838
'./templates/**/*.tmpl',
3939
'./web_src/js/**/*.{ts,js,vue}',
40-
].filter(Boolean),
40+
].filter(Boolean as unknown as <T>(x: T | boolean) => x is T),
4141
blocklist: [
4242
// classes that don't work without CSS variables from "@tailwind base" which we don't use
4343
'transform', 'shadow', 'ring', 'blur', 'grayscale', 'invert', '!invert', 'filter', '!filter',

tests/e2e/utils_e2e.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ export async function login_user(browser: Browser, workerInfo: WorkerInfo, user:
3333
}
3434

3535
export async function load_logged_in_context(browser: Browser, workerInfo: WorkerInfo, user: string) {
36-
let context;
3736
try {
38-
context = await browser.newContext({storageState: `${ARTIFACTS_PATH}/state-${user}-${workerInfo.workerIndex}.json`});
37+
return await browser.newContext({storageState: `${ARTIFACTS_PATH}/state-${user}-${workerInfo.workerIndex}.json`});
3938
} catch (err) {
4039
if (err.code === 'ENOENT') {
4140
throw new Error(`Could not find state for '${user}'. Did you call login_user(browser, workerInfo, '${user}') in test.beforeAll()?`);
41+
} else {
42+
throw err;
4243
}
4344
}
44-
return context;
4545
}
4646

4747
export async function save_visual(page: Page) {

tools/generate-svg.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ function processAssetsSvgFiles(pattern: string, opts: Opts = {}) {
4747
return glob(pattern).map((path) => processAssetsSvgFile(path, opts));
4848
}
4949

50+
function lowercaseKeys(obj: Record<string, any>) {
51+
return Object.fromEntries(Object.entries(obj).map(([key, value]) => [key.toLowerCase(), value]));
52+
}
53+
5054
async function processMaterialFileIcons() {
5155
const paths = glob('node_modules/material-icon-theme/icons/*.svg');
5256
const svgSymbols: Record<string, string> = {};
@@ -76,18 +80,30 @@ async function processMaterialFileIcons() {
7680
// * https://code.visualstudio.com/docs/languages/identifiers#_known-language-identifiers
7781
// * https://github.com/microsoft/vscode/tree/1.98.0/extensions
7882
delete iconRules.iconDefinitions;
79-
for (const [k, v] of Object.entries(iconRules.fileNames)) iconRules.fileNames[k.toLowerCase()] = v;
80-
for (const [k, v] of Object.entries(iconRules.folderNames)) iconRules.folderNames[k.toLowerCase()] = v;
81-
for (const [k, v] of Object.entries(iconRules.fileExtensions)) iconRules.fileExtensions[k.toLowerCase()] = v;
83+
84+
if (iconRules.fileNames) {
85+
iconRules.fileNames = lowercaseKeys(iconRules.fileNames);
86+
}
87+
if (iconRules.folderNames) {
88+
iconRules.folderNames = lowercaseKeys(iconRules.folderNames);
89+
}
90+
if (iconRules.fileExtensions) {
91+
iconRules.fileExtensions = lowercaseKeys(iconRules.fileExtensions);
92+
}
93+
8294
// Use VSCode's "Language ID" mapping from its extensions
8395
for (const [_, langIdExtMap] of Object.entries(vscodeExtensions)) {
8496
for (const [langId, names] of Object.entries(langIdExtMap)) {
8597
for (const name of names) {
8698
const nameLower = name.toLowerCase();
8799
if (nameLower[0] === '.') {
88-
iconRules.fileExtensions[nameLower.substring(1)] ??= langId;
100+
if (iconRules.fileExtensions) {
101+
iconRules.fileExtensions[nameLower.substring(1)] ??= langId;
102+
}
89103
} else {
90-
iconRules.fileNames[nameLower] ??= langId;
104+
if (iconRules.fileNames) {
105+
iconRules.fileNames[nameLower] ??= langId;
106+
}
91107
}
92108
}
93109
}

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"strictBindCallApply": true,
4141
"strictBuiltinIteratorReturn": true,
4242
"strictFunctionTypes": true,
43+
"strictNullChecks": false,
4344
"stripInternal": true,
4445
"verbatimModuleSyntax": true,
4546
"types": [

web_src/js/features/repo-issue.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ export function initRepoPullRequestReview() {
261261
if (commentDiv) {
262262
// get the name of the parent id
263263
const groupID = commentDiv.closest('div[id^="code-comments-"]')?.getAttribute('id');
264-
if (groupID && groupID.startsWith('code-comments-')) {
264+
if (groupID?.startsWith('code-comments-')) {
265265
const id = groupID.slice(14);
266266
const ancestorDiffBox = commentDiv.closest<HTMLElement>('.diff-file-box');
267267

web_src/js/features/repo-settings.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ function initRepoSettingsBranches() {
101101
// show the `Matched` mark for the status checks that match the pattern
102102
const markMatchedStatusChecks = () => {
103103
const patterns = (document.querySelector<HTMLTextAreaElement>('#status_check_contexts').value || '').split(/[\r\n]+/);
104-
const validPatterns = patterns.map((item) => item.trim()).filter(Boolean);
104+
const validPatterns = patterns.map((item) => item.trim()).filter(Boolean as unknown as <T>(x: T | boolean) => x is T);
105105
const marks = document.querySelectorAll('.status-check-matched-mark');
106106

107107
for (const el of marks) {

web_src/js/svg.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ export function svg(name: SvgName, size = 16, classNames?: string | string[]): s
181181
svgNode.setAttribute('width', String(size));
182182
svgNode.setAttribute('height', String(size));
183183
}
184-
if (className) svgNode.classList.add(...className.split(/\s+/).filter(Boolean));
184+
if (className) svgNode.classList.add(...className.split(/\s+/).filter(Boolean as unknown as <T>(x: T | boolean) => x is T));
185185
return serializeXml(svgNode);
186186
}
187187

webpack.config.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const isProduction = env.NODE_ENV !== 'development';
3030
// false - all disabled
3131
let sourceMaps;
3232
if ('ENABLE_SOURCEMAP' in env) {
33-
sourceMaps = ['true', 'false'].includes(env.ENABLE_SOURCEMAP) ? env.ENABLE_SOURCEMAP : 'reduced';
33+
sourceMaps = ['true', 'false'].includes(env.ENABLE_SOURCEMAP || '') ? env.ENABLE_SOURCEMAP : 'reduced';
3434
} else {
3535
sourceMaps = isProduction ? 'reduced' : 'true';
3636
}
@@ -95,7 +95,7 @@ export default {
9595
path: fileURLToPath(new URL('public/assets', import.meta.url)),
9696
filename: () => 'js/[name].js',
9797
chunkFilename: ({chunk}) => {
98-
const language = (/monaco.*languages?_.+?_(.+?)_/.exec(String(chunk.id)) || [])[1];
98+
const language = (/monaco.*languages?_.+?_(.+?)_/.exec(String(chunk?.id)) || [])[1];
9999
return `js/${language ? `monaco-language-${language.toLowerCase()}` : `[name]`}.[contenthash:8].js`;
100100
},
101101
},
@@ -270,7 +270,7 @@ export default {
270270
excludeAssets: [
271271
/^js\/monaco-language-.+\.js$/,
272272
!isProduction && /^licenses.txt$/,
273-
].filter(Boolean),
273+
].filter(Boolean as unknown as <T>(x: T | boolean) => x is T),
274274
groupAssetsByChunk: false,
275275
groupAssetsByEmitStatus: false,
276276
groupAssetsByInfo: false,

0 commit comments

Comments
 (0)