Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strict nulls for everything except logconsole #7657

Merged
merged 16 commits into from Dec 23, 2019
2 changes: 1 addition & 1 deletion buildutils/src/ensure-repo.ts
Expand Up @@ -304,7 +304,7 @@ export async function ensureIntegrity(): Promise<boolean> {
return;
}
const depData = graph.getNodeData(depName);
if (depData.style) {
if (typeof depData.style === 'string') {
vidartf marked this conversation as resolved.
Show resolved Hide resolved
cssData[depName] = [depData.style];
}
});
Expand Down
12 changes: 7 additions & 5 deletions examples/cell/src/index.ts
Expand Up @@ -62,11 +62,13 @@ function main(): void {

// Handle the mimeType for the current kernel.
session.kernelChanged.connect(() => {
void session.kernel.ready.then(() => {
const lang = session.kernel.info.language_info;
const mimeType = mimeService.getMimeTypeByLanguage(lang);
cellWidget.model.mimeType = mimeType;
});
if (session.kernel) {
void session.kernel.ready.then(() => {
const lang = session.kernel!.info!.language_info;
const mimeType = mimeService.getMimeTypeByLanguage(lang);
cellWidget.model.mimeType = mimeType;
});
}
});

// Use the default kernel.
Expand Down
14 changes: 9 additions & 5 deletions packages/apputils/src/clipboard.ts
Expand Up @@ -77,17 +77,21 @@ export namespace Clipboard {
// Select the node content.
let range = document.createRange();
range.selectNodeContents(node);
sel.removeAllRanges();
sel.addRange(range);
if (sel) {
sel.removeAllRanges();
sel.addRange(range);
}

// Execute the command.
document.execCommand(type);

// Restore the previous selection.
sel = window.getSelection();
sel.removeAllRanges();
for (let i = 0, len = savedRanges.length; i < len; ++i) {
sel.addRange(savedRanges[i]);
if (sel) {
sel.removeAllRanges();
for (let i = 0, len = savedRanges.length; i < len; ++i) {
sel.addRange(savedRanges[i]);
}
}
}
}
Expand Down
14 changes: 11 additions & 3 deletions packages/apputils/src/mainareawidget.ts
Expand Up @@ -103,6 +103,9 @@ export class MainAreaWidget<T extends Widget = Widget> extends Widget
* Print method. Defered to content.
*/
[Printing.symbol](): Printing.OptionalAsyncThunk {
if (!this._content) {
return null;
}
return Printing.getPrintFunction(this._content);
}

Expand Down Expand Up @@ -158,14 +161,16 @@ export class MainAreaWidget<T extends Widget = Widget> extends Widget
* Handle `'update-request'` messages by forwarding them to the content.
*/
protected onUpdateRequest(msg: Message): void {
MessageLoop.sendMessage(this._content, msg);
if (this._content) {
MessageLoop.sendMessage(this._content, msg);
}
}

/**
* Update the title based on the attributes of the child widget.
*/
private _updateTitle(): void {
if (this._changeGuard) {
if (this._changeGuard || !this.content) {
return;
}
this._changeGuard = true;
Expand All @@ -184,7 +189,7 @@ export class MainAreaWidget<T extends Widget = Widget> extends Widget
* Update the content title based on attributes of the main widget.
*/
private _updateContentTitle(): void {
if (this._changeGuard) {
if (this._changeGuard || !this.content) {
return;
}
this._changeGuard = true;
Expand All @@ -203,6 +208,9 @@ export class MainAreaWidget<T extends Widget = Widget> extends Widget
* Give focus to the content.
*/
private _focusContent(): void {
if (!this.content) {
return;
}
// Focus the content node if we aren't already focused on it or a
// descendent.
if (!this.content.node.contains(document.activeElement)) {
Expand Down
4 changes: 2 additions & 2 deletions packages/apputils/src/windowresolver.ts
Expand Up @@ -158,7 +158,7 @@ namespace Private {
known[reported] = null;

// If a reported window name and candidate collide, reject the candidate.
if (candidate in known) {
if (!candidate || candidate in known) {
reject();
}
});
Expand Down Expand Up @@ -212,7 +212,7 @@ namespace Private {

// If the window name has not already been resolved, check one last time
// to confirm it is not a duplicate before resolving.
if (candidate in known) {
if (!candidate || candidate in known) {
return reject();
}

Expand Down
12 changes: 9 additions & 3 deletions packages/cells/src/widget.ts
Expand Up @@ -1224,15 +1224,19 @@ export abstract class AttachmentsCell extends Cell {
* Handle the `paste` event for the widget
*/
private _evtPaste(event: ClipboardEvent): void {
this._attachFiles(event.clipboardData.items);
if (event.clipboardData) {
this._attachFiles(event.clipboardData.items);
}
event.preventDefault();
}

/**
* Handle the `drop` event for the widget
*/
private _evtNativeDrop(event: DragEvent): void {
this._attachFiles(event.dataTransfer.items);
if (event.dataTransfer) {
this._attachFiles(event.dataTransfer.items);
}
event.preventDefault();
}

Expand Down Expand Up @@ -1297,7 +1301,9 @@ export abstract class AttachmentsCell extends Cell {
const item = items[i];
if (item.kind === 'file') {
const blob = item.getAsFile();
this._attachFile(blob);
if (blob) {
this._attachFile(blob);
}
}
}
}
Expand Down
17 changes: 10 additions & 7 deletions packages/codeeditor/src/widget.ts
Expand Up @@ -252,13 +252,6 @@ export class CodeEditorWrapper extends Widget {
if (data === undefined) {
return;
}
this.removeClass(DROP_TARGET_CLASS);
event.preventDefault();
event.stopPropagation();
if (event.proposedAction === 'none') {
event.dropAction = 'none';
return;
}
const coordinate = {
top: event.y,
bottom: event.y,
Expand All @@ -270,6 +263,16 @@ export class CodeEditorWrapper extends Widget {
height: 0
};
const position = this.editor.getPositionForCoordinate(coordinate);
if (position === null) {
return;
}
this.removeClass(DROP_TARGET_CLASS);
event.preventDefault();
event.stopPropagation();
if (event.proposedAction === 'none') {
event.dropAction = 'none';
return;
}
const offset = this.editor.getOffsetAt(position);
this.model.value.insert(offset, data);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/codemirror-extension/src/index.ts
Expand Up @@ -86,7 +86,7 @@ export const editorSyntaxStatus: JupyterFrontEndPlugin<void> = {
let item = new EditorSyntaxStatus({ commands: app.commands });
labShell.currentChanged.connect(() => {
const current = labShell.currentWidget;
if (current && tracker.has(current)) {
if (current && tracker.has(current) && item.model) {
item.model.editor = (current as IDocumentWidget<
FileEditor
>).content.editor;
Expand Down
5 changes: 3 additions & 2 deletions packages/completer/src/model.ts
Expand Up @@ -325,15 +325,16 @@ export class CompleterModel implements Completer.IModel {
createPatch(patch: string): Completer.IPatch | undefined {
const original = this._original;
const cursor = this._cursor;
const current = this._current;

if (!original || !cursor) {
if (!original || !cursor || !current) {
return undefined;
}

let { start, end } = cursor;
// Also include any filtering/additional-typing that has occurred
// since the completion request in the patched length.
end = end + (this.current.text.length - this.original.text.length);
end = end + (current.text.length - original.text.length);

return { start, end, value: patch };
}
Expand Down
5 changes: 4 additions & 1 deletion packages/completer/src/widget.ts
Expand Up @@ -820,7 +820,10 @@ namespace Private {
export function itemValues(items: NodeList): string[] {
let values: string[] = [];
for (let i = 0, len = items.length; i < len; i++) {
values.push((items[i] as HTMLElement).getAttribute('data-value'));
const attr = (items[i] as HTMLElement).getAttribute('data-value');
if (attr) {
values.push(attr);
}
}
return values;
}
Expand Down
8 changes: 6 additions & 2 deletions packages/console-extension/src/index.ts
Expand Up @@ -40,7 +40,7 @@ import { IRenderMimeRegistry } from '@jupyterlab/rendermime';

import { find } from '@lumino/algorithm';

import { ReadonlyJSONObject, JSONObject } from '@lumino/coreutils';
import { JSONObject, ReadonlyPartialJSONObject } from '@lumino/coreutils';

import { DisposableSet } from '@lumino/disposable';

Expand Down Expand Up @@ -333,7 +333,11 @@ async function activateConsole(
const kernelPreference = args[
'kernelPreference'
] as IClientSession.IKernelPreference;
return manager.specs.kernelspecs[kernelPreference.name].display_name;
return (
// TODO: Lumino command functions should probably be allowed to return undefined?
manager.specs?.kernelspecs[kernelPreference.name || '']
?.display_name ?? ''
);
}
return 'Console';
},
Expand Down
2 changes: 1 addition & 1 deletion packages/console/src/widget.ts
Expand Up @@ -799,7 +799,7 @@ export class CodeConsole extends Widget {
if (this.isDisposed || !kernel || !kernel.info) {
return;
}
this._handleInfo(this.session.kernel.info);
this._handleInfo(kernel.info);
})
.catch(err => {
console.error('could not get kernel info');
Expand Down
4 changes: 3 additions & 1 deletion packages/coreutils/src/statedb.ts
Expand Up @@ -190,7 +190,9 @@ export class StateDB<T extends ReadonlyJSONValue = ReadonlyJSONValue>
*/
private async _merge(contents: StateDB.Content<T>): Promise<void> {
await Promise.all(
Object.keys(contents).map(key => this._save(key, contents[key]))
Object.keys(contents).map(
key => contents[key] && this._save(key, contents[key]!)
)
);
}

Expand Down
4 changes: 2 additions & 2 deletions packages/csvviewer/src/parse.ts
Expand Up @@ -468,7 +468,7 @@ export function parseDSV(options: IParser.IOptions): IParser.IResults {
}
}

return { nrows, ncols: columnOffsets ? ncols : 0, offsets };
return { nrows, ncols: columnOffsets ? ncols ?? 0 : 0, offsets };
}

/**
Expand Down Expand Up @@ -571,5 +571,5 @@ export function parseDSVNoQuotes(options: IParser.IOptions): IParser.IResults {
currRow = rowEnd + rowDelimiterLength;
}

return { nrows, ncols: columnOffsets ? ncols : 0, offsets };
return { nrows, ncols: columnOffsets ? ncols ?? 0 : 0, offsets };
}
1 change: 1 addition & 0 deletions packages/csvviewer/src/widget.ts
Expand Up @@ -106,6 +106,7 @@ export class GridSearchService {
return config.matchBackgroundColor;
}
}
return '';
};
}

Expand Down
6 changes: 6 additions & 0 deletions packages/docmanager/src/manager.ts
Expand Up @@ -88,6 +88,9 @@ export class DocumentManager implements IDocumentManager {
// For each existing context, start/stop the autosave handler as needed.
this._contexts.forEach(context => {
const handler = Private.saveHandlerProperty.get(context);
if (!handler) {
return;
}
if (value === true && !handler.isActive) {
handler.start();
} else if (value === false && handler.isActive) {
Expand All @@ -109,6 +112,9 @@ export class DocumentManager implements IDocumentManager {
// For each existing context, set the save interval as needed.
this._contexts.forEach(context => {
const handler = Private.saveHandlerProperty.get(context);
if (!handler) {
return;
}
handler.saveInterval = value || 120;
});
}
Expand Down
10 changes: 6 additions & 4 deletions packages/docregistry/src/mimedocument.ts
Expand Up @@ -188,10 +188,12 @@ export class MimeContent extends Widget {
if (data !== this._context.model.toString()) {
this._context.model.fromString(data);
}
} else {
if (!JSONExt.deepEqual(data, this._context.model.toJSON())) {
this._context.model.fromJSON(data);
}
} else if (
data !== null &&
data !== undefined &&
!JSONExt.deepEqual(data, this._context.model.toJSON())
) {
this._context.model.fromJSON(data);
}
};

Expand Down
Expand Up @@ -307,7 +307,7 @@ export class CodeMirrorSearchProvider
private _onDocChanged(_: any, changeObj: CodeMirror.EditorChange) {
// If we get newlines added/removed, the line numbers across the
// match state are all shifted, so here we need to recalculate it
if (changeObj.text.length > 1 || changeObj.removed.length > 1) {
if (changeObj.text.length > 1 || (changeObj.removed?.length ?? 0) > 1) {
this._setInitialMatches(this._query);
this._changed.emit(undefined);
}
Expand Down
16 changes: 10 additions & 6 deletions packages/filebrowser-extension/src/index.ts
Expand Up @@ -469,11 +469,13 @@ function addCommands(
const item = await Private.navigateToPath(path, factory);
if (item.type !== 'directory') {
const browserForPath = Private.getBrowserForPath(path, factory);
browserForPath.clearSelectedItems();
const parts = path.split('/');
const name = parts[parts.length - 1];
if (name) {
await browserForPath.selectItemByName(name);
if (browserForPath) {
browserForPath.clearSelectedItems();
const parts = path.split('/');
const name = parts[parts.length - 1];
if (name) {
await browserForPath.selectItemByName(name);
}
}
}
} catch (reason) {
Expand Down Expand Up @@ -989,7 +991,9 @@ namespace Private {
.execute('launcher:create', { cwd: model.path })
.then((launcher: MainAreaWidget<Launcher>) => {
model.pathChanged.connect(() => {
launcher.content.cwd = model.path;
if (launcher.content) {
launcher.content.cwd = model.path;
}
}, launcher);
return launcher;
});
Expand Down