From 39a11582fd5c98074c7033f84b1e2c1a47592a83 Mon Sep 17 00:00:00 2001 From: Guillaume Fraux Date: Wed, 13 Nov 2024 12:16:37 +0100 Subject: [PATCH] Make sure to lint the JS/TS code used in jupyter --- package.json | 2 +- python/jupyter/src/widget.ts | 7 +++++-- src/options.ts | 29 ++++++++++++++--------------- 3 files changed, 20 insertions(+), 18 deletions(-) diff --git a/package.json b/package.json index 3fe9c7030..920b20fd7 100644 --- a/package.json +++ b/package.json @@ -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 .", diff --git a/python/jupyter/src/widget.ts b/python/jupyter/src/widget.ts index 8303e9580..5280aaf8b 100644 --- a/python/jupyter/src/widget.ts +++ b/python/jupyter/src/widget.ts @@ -39,7 +39,7 @@ class ChemiscopeBaseView extends DOMWidgetView { } const settings = this.model.get('settings') as Partial; - + // ignore pinned setting in jupyter, otherwise the pinned is changed // by JS and then overwritten the first time by Python delete settings.pinned; @@ -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 @@ -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); @@ -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); @@ -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); diff --git a/src/options.ts b/src/options.ts index 5b9018175..18ea3a73e 100644 --- a/src/options.ts +++ b/src/options.ts @@ -1,3 +1,5 @@ +/* eslint-disable @typescript-eslint/no-unsafe-member-access */ +/* eslint-disable @typescript-eslint/no-unsafe-assignment */ /** * @packageDocumentation * @module utils @@ -154,7 +156,7 @@ export class HTMLOption { 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; } } @@ -198,18 +200,17 @@ export class HTMLOption { }; // 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); @@ -325,26 +326,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; } @@ -358,9 +357,8 @@ 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)) { @@ -368,12 +366,12 @@ export abstract class OptionsGroup { 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) { @@ -389,14 +387,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)}'`);