Skip to content

Commit feb3d2d

Browse files
authored
Fix null ref in attachments check (microsoft#162396)
Fixes microsoft#162391 Also reduces use of `any`
1 parent 6e040c2 commit feb3d2d

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

extensions/ipynb/src/notebookAttachmentCleaner.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -153,16 +153,16 @@ export class AttachmentCleaner implements vscode.CodeActionProvider {
153153
this.saveAllAttachmentsToCache(cell.metadata, notebookUri, cellFragment);
154154
}
155155

156-
if (this.checkMetadataAttachmentsExistence(cell.metadata)) {
156+
if (this.checkMetadataHasAttachmentsField(cell.metadata)) {
157157
// the cell metadata contains attachments, check if any are used in the markdown source
158158

159-
for (const currFilename of Object.keys(cell.metadata.attachments)) {
159+
for (const [currFilename, attachment] of Object.entries(cell.metadata.attachments)) {
160160
// means markdown reference is present in the metadata, rendering will work properly
161161
// therefore, we don't need to check it in the next loop either
162162
if (markdownAttachmentsRefedInCell.has(currFilename)) {
163163
// attachment reference is present in the markdown source, no need to cache it
164164
markdownAttachmentsRefedInCell.get(currFilename)!.valid = true;
165-
markdownAttachmentsInUse[currFilename] = cell.metadata.attachments[currFilename];
165+
markdownAttachmentsInUse[currFilename] = attachment as IAttachmentData;
166166
} else {
167167
// attachment reference is not present in the markdown source, cache it
168168
this.saveAttachmentToCache(notebookUri, cellFragment, currFilename, cell.metadata);
@@ -227,7 +227,7 @@ export class AttachmentCleaner implements vscode.CodeActionProvider {
227227

228228
const diagnostics: IAttachmentDiagnostic[] = [];
229229
const markdownAttachments = this.getAttachmentNames(document);
230-
if (this.checkMetadataAttachmentsExistence(activeCell.metadata)) {
230+
if (this.checkMetadataHasAttachmentsField(activeCell.metadata)) {
231231
for (const [currFilename, attachment] of markdownAttachments) {
232232
if (!activeCell.metadata.attachments[currFilename]) {
233233
// no attachment reference in the metadata
@@ -295,8 +295,8 @@ export class AttachmentCleaner implements vscode.CodeActionProvider {
295295
* @param metadata metadata of cell
296296
* @returns boolean representing the presence of any attachments
297297
*/
298-
private checkMetadataAttachmentsExistence(metadata: { [key: string]: any }): boolean {
299-
return !!(metadata.attachments);
298+
private checkMetadataHasAttachmentsField(metadata: { [key: string]: unknown }): metadata is { readonly attachments: Record<string, unknown> } {
299+
return !!metadata.attachments && typeof metadata.attachments === 'object';
300300
}
301301

302302
/**
@@ -305,14 +305,16 @@ export class AttachmentCleaner implements vscode.CodeActionProvider {
305305
* @param notebookUri uri for the notebook being edited
306306
* @param cellFragment fragment of cell being edited
307307
*/
308-
private saveAllAttachmentsToCache(metadata: { [key: string]: any }, notebookUri: string, cellFragment: string): void {
308+
private saveAllAttachmentsToCache(metadata: { [key: string]: unknown }, notebookUri: string, cellFragment: string): void {
309309
const documentCache = this._attachmentCache.get(notebookUri) ?? new Map();
310310
this._attachmentCache.set(notebookUri, documentCache);
311311
const cellCache = documentCache.get(cellFragment) ?? new Map<string, IAttachmentData>();
312312
documentCache.set(cellFragment, cellCache);
313313

314-
for (const currFilename of Object.keys(metadata.attachments)) {
315-
cellCache.set(currFilename, metadata.attachments[currFilename]);
314+
if (metadata.attachments && typeof metadata.attachments === 'object') {
315+
for (const [currFilename, attachment] of Object.entries(metadata.attachments)) {
316+
cellCache.set(currFilename, attachment);
317+
}
316318
}
317319
}
318320

0 commit comments

Comments
 (0)