Skip to content

Commit

Permalink
Make sure to lint the JS/TS code used in jupyter
Browse files Browse the repository at this point in the history
  • Loading branch information
Luthaf committed Nov 14, 2024
1 parent c6a1e0a commit f493599
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"url": "git+https://github.com/lab-cosmo/chemiscope.git"
},
"scripts": {
"test": "eslint src && prettier --check src",
"test": "eslint src python/jupyter/src && prettier --check src python/jupyter/src",
"build": "rimraf dist && webpack --mode production --config webpack.config.ts && npm run merge-dts",
"build:nbextension": "webpack --mode production --config python/webpack.config.nbextension.ts",
"build:labextension": "npm run build && jupyter labextension build .",
Expand Down
7 changes: 5 additions & 2 deletions python/jupyter/src/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class ChemiscopeBaseView extends DOMWidgetView {
}

const settings = this.model.get('settings') as Partial<Settings>;

// ignore pinned setting in jupyter, otherwise the pinned is changed
// by JS and then overwritten the first time by Python
delete settings.pinned;
Expand All @@ -51,7 +51,7 @@ class ChemiscopeBaseView extends DOMWidgetView {
}

protected _updatePythonSettings(): void {
if (this.visualizer !== undefined) {
if (this.visualizer !== undefined) {
const settings = this.visualizer.saveSettings();
// ignore pinned setting in jupyter, otherwise the pinned is changed
// by JS and then overwritten the first time by Python
Expand Down Expand Up @@ -144,6 +144,7 @@ export class ChemiscopeView extends ChemiscopeBaseView {
// and set them to the initial value right now
this._updatePythonSettings();
})
// eslint-disable-next-line @typescript-eslint/use-unknown-in-catch-callback-variable
.catch((e: Error) => {
// eslint-disable-next-line no-console
console.error(e);
Expand Down Expand Up @@ -225,6 +226,7 @@ export class StructureView extends ChemiscopeBaseView {
// and set them to the initial value right now
this._updatePythonSettings();
})
// eslint-disable-next-line @typescript-eslint/use-unknown-in-catch-callback-variable
.catch((e: Error) => {
// eslint-disable-next-line no-console
console.error(e);
Expand Down Expand Up @@ -306,6 +308,7 @@ export class MapView extends ChemiscopeBaseView {
// and set them to the initial value right now
this._updatePythonSettings();
})
// eslint-disable-next-line @typescript-eslint/use-unknown-in-catch-callback-variable
.catch((e: Error) => {
// eslint-disable-next-line no-console
console.error(e);
Expand Down
29 changes: 14 additions & 15 deletions src/options.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/**
* @packageDocumentation
* @module utils
Expand Down Expand Up @@ -152,7 +154,7 @@ export class HTMLOption<T extends OptionsType> {
option.selected = values.includes(option.value);
}
} else {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(bound.element as any)[bound.attribute] = this._value;
}
}
Expand Down Expand Up @@ -196,18 +198,17 @@ export class HTMLOption<T extends OptionsType> {
};
// also initializes the state of the option list
const values = (this._value as string).split(',');
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
// eslint-disable-next-line @typescript-eslint/no-explicit-any
for (const option of (element as any).options) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
option.selected = values.includes(option.value as string);
}
} else {
listener = (event: Event) => {
assert(event.target !== null);
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-argument
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-argument
this._update((event.target as any)[attribute].toString(), 'DOM');
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(element as any)[attribute] = this._value;
}
element.addEventListener('change', listener);
Expand Down Expand Up @@ -323,26 +324,24 @@ export abstract class OptionsGroup {
* values of all settings.
*/
public saveSettings(): Settings {
const settings = {};
/* eslint-disable */
const settings: Settings = {};
this.foreachOption((keys, option) => {
assert(keys.length >= 1);
const value = option.value;

// Save property value if valid
if (value !== undefined || value !== null || !Number.isNaN(value)) {
let root = settings as any;
let root = settings;
for (const key of keys.slice(0, keys.length - 1)) {
if (!(key in root)) {
root[key] = {};
}
root = root[key];
root = root[key] as unknown as Settings;
}
const lastKey = keys[keys.length - 1];
root[lastKey] = value;
}
});
/* eslint-enable */
return settings;
}

Expand All @@ -356,22 +355,21 @@ export abstract class OptionsGroup {
// make a copy of the settings since we will be changing it below
const copy = JSON.parse(JSON.stringify(settings)) as Settings;
this.foreachOption((keys, option) => {
/* eslint-disable */
assert(keys.length >= 1);
let root = copy as any;
let root = copy;
let parent;
for (const key of keys.slice(0, keys.length - 1)) {
if (!(key in root)) {
// this key is missing from the settings
return;
}
parent = root;
root = root[key];
root = root[key] as unknown as Settings;
}
const lastKey = keys[keys.length - 1];

if (lastKey in root) {
var value = root[lastKey];
let value = root[lastKey];

// convert null values for numeric options to NaN (useful for axis range)
if (typeof option.value === 'number' && value === null) {
Expand All @@ -387,14 +385,15 @@ export abstract class OptionsGroup {

// remove used keys from the settings to be able to warn on
// unused keys
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete root[lastKey];
if (parent !== undefined && Object.keys(root).length === 0) {
// if we removed all keys from a sub-object, remove the sub-object
assert(keys.length >= 2);
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete parent[keys[keys.length - 2]];
}
}
/* eslint-enable */
});
if (Object.keys(copy).length !== 0) {
sendWarning(`ignored unknown settings '${JSON.stringify(copy)}'`);
Expand Down

0 comments on commit f493599

Please sign in to comment.