require-signal-this-arg
Require this as the second argument when a class method that uses this is connected to a Lumino signal.
Examples
Connect a method that uses the instance
_onChanged() calls this.update(), so it needs the view as its receiver.
Incorrect
class NotebookView extends Widget {
constructor(model: INotebookModel) {
super();
model.contentChanged.connect(this._onChanged);
}
private _onChanged(): void {
this.update();
}
}
Correct
class NotebookView extends Widget {
constructor(model: INotebookModel) {
super();
model.contentChanged.connect(this._onChanged, this);
}
private _onChanged(): void {
this.update();
}
}
Connect a function stored in a field
A regular function stored in a class field does not capture this as an arrow function would.
Incorrect
class NotebookView extends Widget {
constructor(model: INotebookModel) {
super();
model.contentChanged.connect(this._onChanged);
}
private _onChanged = function (this: NotebookView): void {
this.update();
};
}
Correct
class NotebookView extends Widget {
constructor(model: INotebookModel) {
super();
model.contentChanged.connect(this._onChanged, this);
}
private _onChanged = function (this: NotebookView): void {
this.update();
};
}
Use an arrow callback
The arrow captures this for execution. The second argument still registers a receiver so Widget.dispose() can clean up.
Allowed
class NotebookView extends Widget {
constructor(model: INotebookModel) {
super();
model.contentChanged.connect(() => this.update(), this);
}
}
Use an arrow-function field
An arrow-function field also captures the instance. Without the second argument there is no unbound-method error, but prefer-signal-this-arg can still report a cleanup problem.
Allowed
class NotebookView extends Widget {
constructor(model: INotebookModel) {
super();
model.contentChanged.connect(this._onChanged, this);
}
private _onChanged = (): void => {
this.update();
};
}
Why
Passing a class method to connect() does not bind it to the instance. If that method uses this, it can throw or read the wrong object when the signal fires. Passing this as the second argument gives it the intended receiver.
Arrow functions already capture this, but a receiver can still help with cleanup; see prefer-signal-this-arg and the JupyterLab signal patterns.
Options
This rule has no options.
Fixing a report
The editor suggestion adds this as the second argument. Review the change: if you also call signal.disconnect(this._onChanged), update it to signal.disconnect(this._onChanged, this) so it still matches the connection. This is a suggestion rather than an automatic fix.
Scope and limitations
The rule checks class methods that use this and are passed to connect() without a second argument. It skips arrow-function properties, methods that do not use this, accessors and methods it cannot find in the enclosing class.
Calls inside nested regular functions are skipped because their this may refer to another object. When type information is available, APIs known not to be Lumino signals are ignored.