Skip to content

Commit 8b49c23

Browse files
jtpiofcollonval
andauthored
Enable strict null checks (#15)
* Enable strict null checks * Keep the undefined sentinel value in ordered tags * Improve ordering code Co-authored-by: Frederic COLLONVAL <fcollonval@gmail.com>
1 parent ce39b41 commit 8b49c23

File tree

5 files changed

+27
-19
lines changed

5 files changed

+27
-19
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"@jupyterlab/settingregistry": "^3.0.0",
5656
"@jupyterlab/ui-components": "^3.0.0",
5757
"@lumino/commands": "^1.12.0",
58+
"@lumino/coreutils": "^1.5.3",
5859
"@lumino/disposable": "^1.4.3",
5960
"@lumino/widgets": "^1.16.1"
6061
},

src/celltoolbartracker.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,10 @@ export class CellToolbarTracker implements IDisposable {
109109
this._settings.changed.disconnect(this._onSettingsChanged, this);
110110
}
111111

112-
const cells = this._panel.context.model.cells;
113-
cells.changed.disconnect(this.updateConnectedCells, this);
112+
const cells = this._panel?.context.model.cells;
113+
if (cells) {
114+
cells.changed.disconnect(this.updateConnectedCells, this);
115+
}
114116
this._panel = null;
115117
}
116118

@@ -162,8 +164,8 @@ export class CellToolbarTracker implements IDisposable {
162164
}
163165
}
164166

165-
private _getCell(model: ICellModel): Cell {
166-
return this._panel.content.widgets.find(widget => widget.model === model);
167+
private _getCell(model: ICellModel): Cell | undefined {
168+
return this._panel?.content.widgets.find(widget => widget.model === model);
167169
}
168170

169171
private _findToolbarWidgets(cell: Cell): Widget[] {
@@ -185,7 +187,7 @@ export class CellToolbarTracker implements IDisposable {
185187
*/
186188
private _onSettingsChanged(): void {
187189
// Update menu items
188-
const leftItems = (this._settings.composite['leftMenu'] as any) as
190+
const leftItems = (this._settings?.composite['leftMenu'] as any) as
189191
| ICellMenuItem[]
190192
| null;
191193
// Test to avoid useless signal emission
@@ -199,7 +201,7 @@ export class CellToolbarTracker implements IDisposable {
199201
} else {
200202
this._leftMenuItems.pushAll(DEFAULT_LEFT_MENU);
201203
}
202-
const rightItems = ((this._settings.composite['rightMenu'] as any) ||
204+
const rightItems = ((this._settings?.composite['rightMenu'] as any) ||
203205
[]) as ICellMenuItem[];
204206
// Test to avoid useless signal emission
205207
if (this._rightMenuItems.length > 0) {
@@ -211,7 +213,7 @@ export class CellToolbarTracker implements IDisposable {
211213

212214
// Update tags
213215
const newDefaultTags =
214-
(this._settings.composite['defaultTags'] as string[]) || [];
216+
(this._settings?.composite['defaultTags'] as string[]) || [];
215217
// Update default tag in shared tag list
216218
const toAdd = newDefaultTags.filter(
217219
tag => !this._previousDefaultTags.includes(tag)
@@ -226,11 +228,13 @@ export class CellToolbarTracker implements IDisposable {
226228

227229
// Update left space
228230
const leftSpace = (this._settings?.composite['leftSpace'] as number) || 0;
229-
this._panel.node
230-
.querySelectorAll(`div.${LEFT_SPACER_CLASSNAME}`)
231-
.forEach(node => {
232-
(node as HTMLElement).style.width = `${leftSpace}px`;
233-
});
231+
if (this._panel) {
232+
this._panel.node
233+
.querySelectorAll(`div.${LEFT_SPACER_CLASSNAME}`)
234+
.forEach(node => {
235+
(node as HTMLElement).style.width = `${leftSpace}px`;
236+
});
237+
}
234238
}
235239

236240
private _allTags: ObservableList<string> = new ObservableList<string>();
@@ -240,7 +244,7 @@ export class CellToolbarTracker implements IDisposable {
240244
ICellMenuItem
241245
>();
242246
private _previousDefaultTags = new Array<string>();
243-
private _panel: NotebookPanel;
247+
private _panel: NotebookPanel | null;
244248
private _rightMenuItems: ObservableList<ICellMenuItem> = new ObservableList<
245249
ICellMenuItem
246250
>();

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ const extension: JupyterFrontEndPlugin<void> = {
1616
app: JupyterFrontEnd,
1717
settingRegistry: ISettingRegistry | null
1818
) => {
19-
const settings = await settingRegistry.load(`${EXTENSION_ID}:plugin`);
19+
const settings =
20+
(await settingRegistry?.load(`${EXTENSION_ID}:plugin`)) ?? null;
2021
app.docRegistry.addWidgetExtension(
2122
'Notebook',
2223
new CellBarExtension(app.commands, settings)

src/tagbar.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,14 @@ export class TagTool extends Widget {
133133
});
134134

135135
// Sort the widgets in tag alphabetical order
136-
// - undefined matches the AddTag widget
137-
const orderedTags = allTags.concat([undefined]);
138136
[...layout.widgets].forEach((widget: Widget, index: number) => {
139-
const tagIndex = orderedTags.findIndex(
137+
let tagIndex = allTags.findIndex(
140138
tag => (widget as TagWidget).name === tag
141139
);
140+
// Handle AddTag widget case
141+
if (tagIndex === -1) {
142+
tagIndex = allTags.length;
143+
}
142144
if (tagIndex !== index) {
143145
layout.insertWidget(tagIndex, widget);
144146
}
@@ -186,7 +188,7 @@ export class TagTool extends Widget {
186188
*/
187189
protected onCellMetadataChanged(
188190
metadata: IObservableJSON,
189-
changes: IObservableMap.IChangedArgs<ReadonlyPartialJSONValue>
191+
changes: IObservableMap.IChangedArgs<ReadonlyPartialJSONValue | undefined>
190192
): void {
191193
if (changes.key === 'tags') {
192194
const oldTags = [...new Set((changes.oldValue as string[]) || [])];

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"outDir": "lib",
1717
"rootDir": "src",
1818
"strict": true,
19-
"strictNullChecks": false,
19+
"strictNullChecks": true,
2020
"target": "es2017",
2121
"types": []
2222
},

0 commit comments

Comments
 (0)