Skip to content

Commit

Permalink
Merge pull request #38 from jtpio/eslint
Browse files Browse the repository at this point in the history
Switch to ESLint
  • Loading branch information
jtpio authored Apr 2, 2020
2 parents 79d9dd9 + adf6a2f commit 574c087
Show file tree
Hide file tree
Showing 14 changed files with 308 additions and 208 deletions.
4 changes: 4 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
dist
coverage
**/*.d.ts
47 changes: 47 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
module.exports = {
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/eslint-recommended',
'plugin:@typescript-eslint/recommended',
'plugin:jsdoc/recommended',
'plugin:prettier/recommended',
'plugin:react/recommended',
],
parser: '@typescript-eslint/parser',
parserOptions: {
sourceType: 'module',
ecmaVersion: 2017,
},
plugins: ['@typescript-eslint', 'jsdoc'],
rules: {
'@typescript-eslint/interface-name-prefix': [
'error',
{ prefixWithI: 'always' },
],
'@typescript-eslint/no-unused-vars': ['warn', { args: 'none' }],
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/camelcase': 'off',
'@typescript-eslint/no-namespace': 'off',
'@typescript-eslint/no-use-before-define': 'off',
'@typescript-eslint/quotes': [
'error',
'single',
{ avoidEscape: true, allowTemplateLiterals: false },
],
curly: ['error', 'all'],
'jsdoc/require-param-type': 'off',
'jsdoc/require-property-type': 'off',
'jsdoc/require-returns-type': 'off',
'jsdoc/require-returns': 'off',
'jsdoc/no-types': 'warn',
'prefer-arrow-callback': 'error',
},
settings: {
jsdoc: {
mode: 'typescript',
},
react: {
version: 'detect',
},
},
};
23 changes: 13 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@
"build:labextension": "npm run clean:labextension && mkdirp ipylab/labextension && cd ipylab/labextension && npm pack ../..",
"clean": "rimraf lib tsconfig.tsbuildinfo",
"clean:labextension": "rimraf ipylab/labextension",
"lint": "jlpm && jlpm run prettier && jlpm run tslint",
"lint:check": "jlpm run prettier:check && jlpm run tslint:check",
"eslint": "eslint . --ext .ts,.tsx --fix",
"eslint:check": "eslint . --ext .ts,.tsx",
"lint": "jlpm && jlpm run prettier && jlpm run eslint",
"lint:check": "jlpm run prettier:check && jlpm run eslint:check",
"prepack": "npm run build",
"prettier": "prettier --write \"**/*{.ts,.tsx,.js,.jsx,.css,.json,.md}\" \"!dist/**\" \"!docs/**\"",
"prettier:check": "prettier --list-different \"**/*{.ts,.tsx,.js,.jsx,.css,.json,.md}\" \"!dist/**\" \"!docs/**\"",
"tslint": "tslint --fix -c tslint.json --project tsconfig.json \"**/*{.ts,.tsx}\"",
"tslint:check": "tslint -c tslint.json --project tsconfig.json \"**/*{.ts,.tsx}\"",
"watch": "npm-run-all -p watch:*",
"watch:lib": "tsc -w"
},
Expand Down Expand Up @@ -74,24 +74,27 @@
"@types/expect.js": "^0.3.29",
"@types/mocha": "^5.2.5",
"@types/node": "^10.11.6",
"@typescript-eslint/eslint-plugin": "^2.26.0",
"@typescript-eslint/parser": "^2.26.0",
"css-loader": "^3.2.0",
"eslint": "^6.8.0",
"eslint-config-prettier": "^6.10.1",
"eslint-plugin-jsdoc": "^22.1.0",
"eslint-plugin-prettier": "^3.1.2",
"eslint-plugin-react": "^7.18.3",
"expect.js": "^0.3.1",
"fs-extra": "^7.0.0",
"husky": "^3.1.0",
"lint-staged": "^9.4.3",
"mkdirp": "^0.5.1",
"mocha": "^5.2.0",
"npm-run-all": "^4.1.3",
"prettier": "^1.19.1",
"prettier": "^2.0.2",
"rimraf": "^2.6.2",
"source-map-loader": "^0.2.4",
"style-loader": "^1.0.0",
"ts-loader": "^5.2.1",
"tslint": "^5.20.1",
"tslint-config-prettier": "^1.18.0",
"tslint-plugin-prettier": "^2.0.1",
"tslint-react": "^4.1.0",
"typescript": "~3.7.2"
"typescript": "~3.8.3"
},
"jupyterlab": {
"extension": "lib/plugin"
Expand Down
17 changes: 10 additions & 7 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import {
JupyterFrontEndPlugin,
JupyterFrontEnd,
ILabShell
ILabShell,
} from '@jupyterlab/application';

import { ICommandPalette } from '@jupyterlab/apputils';
Expand All @@ -17,6 +17,9 @@ import { MODULE_NAME, MODULE_VERSION } from './version';

const EXTENSION_ID = 'ipylab:plugin';

/**
* The default plugin.
*/
const extension: JupyterFrontEndPlugin<void> = {
id: EXTENSION_ID,
autoStart: true,
Expand All @@ -29,17 +32,17 @@ const extension: JupyterFrontEndPlugin<void> = {
palette: ICommandPalette
): void => {
// add globals
widgetExports.JupyterFrontEndModel._app = app;
widgetExports.ShellModel._shell = shell;
widgetExports.CommandRegistryModel._commands = app.commands;
widgetExports.CommandPaletteModel._palette = palette;
widgetExports.JupyterFrontEndModel.app = app;
widgetExports.ShellModel.shell = shell;
widgetExports.CommandRegistryModel.commands = app.commands;
widgetExports.CommandPaletteModel.palette = palette;

registry.registerWidget({
name: MODULE_NAME,
version: MODULE_VERSION,
exports: widgetExports
exports: widgetExports,
});
}
},
};

export default extension;
2 changes: 1 addition & 1 deletion src/version.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Jeremy Tuloup
// Distributed under the terms of the Modified BSD License.

// tslint:disable-next-line: no-var-requires
// eslint-disable-next-line @typescript-eslint/no-var-requires
const data = require('../package.json');

/**
Expand Down
2 changes: 1 addition & 1 deletion src/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ export {
ShellModel,
SplitPanelModel,
SplitPanelView,
TitleModel
TitleModel,
};
87 changes: 65 additions & 22 deletions src/widgets/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,54 @@ import { ISerializers, WidgetModel } from '@jupyter-widgets/base';

import { CommandRegistry } from '@lumino/commands';

import { ReadonlyPartialJSONObject } from '@lumino/coreutils';

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

import { MODULE_NAME, MODULE_VERSION } from '../version';

/**
* The model for a command registry.
*/
export class CommandRegistryModel extends WidgetModel {
defaults() {
/**
* The default attributes.
*/
defaults(): any {
return {
...super.defaults(),
_model_name: CommandRegistryModel.model_name,
_model_module: CommandRegistryModel.model_module,
_model_module_version: CommandRegistryModel.model_module_version
_model_module_version: CommandRegistryModel.model_module_version,
};
}

initialize(attributes: any, options: any) {
this.commands = CommandRegistryModel._commands;
/**
* Initialize a CommandRegistryModel instance.
*
* @param attributes The base attributes.
* @param options The initialization options.
*/
initialize(attributes: any, options: any): void {
this._commands = CommandRegistryModel.commands;
super.initialize(attributes, options);
this.on('msg:custom', this._onMessage.bind(this));
this.on('comm_live_update', () => {
if (this.comm_live) {
return;
}
this._customCommands.values().forEach(command => command.dispose());
this._customCommands.values().forEach((command) => command.dispose());
this._sendCommandList();
});
this._sendCommandList();
}

private _onMessage(msg: any) {
/**
* Handle a custom message from the backend.
*
* @param msg The message to handle.
*/
private _onMessage(msg: any): void {
switch (msg.func) {
case 'execute':
this._execute(msg.payload);
Expand All @@ -51,24 +70,42 @@ export class CommandRegistryModel extends WidgetModel {
}
}

private _sendCommandList() {
this.commands.notifyCommandChanged();
this.set('_commands', this.commands.listCommands());
/**
* Send the list of commands to the backend.
*/
private _sendCommandList(): void {
this._commands.notifyCommandChanged();
this.set('_commands', this._commands.listCommands());
this.save_changes();
}

private _execute(payload: any) {
const { id, args } = payload;
void this.commands.execute(id, args);
/**
* Execute a command
*
* @param bundle The command bundle.
*/
private _execute(bundle: {
id: string;
args: ReadonlyPartialJSONObject;
}): void {
const { id, args } = bundle;
void this._commands.execute(id, args);
}

private async _addCommand(payload: any): Promise<void> {
const { id, caption, label, iconClass } = payload;
if (this.commands.hasCommand(id)) {
/**
* Add a new command to the command registry.
*
* @param options The command options.
*/
private async _addCommand(
options: CommandRegistry.ICommandOptions & { id: string }
): Promise<void> {
const { id, caption, label, iconClass } = options;
if (this._commands.hasCommand(id)) {
// TODO: handle this?
return;
}
const command = this.commands.addCommand(id, {
const command = this._commands.addCommand(id, {
caption,
label,
iconClass,
Expand All @@ -78,22 +115,27 @@ export class CommandRegistryModel extends WidgetModel {
return;
}
this.send({ event: 'execute', id }, {});
}
},
});
this._customCommands.set(id, command);
this._sendCommandList();
}

private _removeCommand(payload: any) {
const { id } = payload;
/**
* Remove a command from the command registry.
*
* @param bundle The command bundle.
*/
private _removeCommand(bundle: { id: string }): void {
const { id } = bundle;
if (this._customCommands.has(id)) {
this._customCommands.get(id).dispose();
}
this._sendCommandList();
}

static serializers: ISerializers = {
...WidgetModel.serializers
...WidgetModel.serializers,
};

static model_name = 'CommandRegistryModel';
Expand All @@ -103,7 +145,8 @@ export class CommandRegistryModel extends WidgetModel {
static view_module: string = null;
static view_module_version = MODULE_VERSION;

private commands: CommandRegistry;
static _commands: CommandRegistry;
private _commands: CommandRegistry;
private _customCommands = new ObservableMap<IDisposable>();

static commands: CommandRegistry;
}
30 changes: 21 additions & 9 deletions src/widgets/frontend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,43 @@ import { JupyterFrontEnd } from '@jupyterlab/application';
import {
DOMWidgetModel,
ISerializers,
WidgetModel
WidgetModel,
} from '@jupyter-widgets/base';

import { MODULE_NAME, MODULE_VERSION } from '../version';

/**
* The model for a JupyterFrontEnd.
*/
export class JupyterFrontEndModel extends WidgetModel {
defaults() {
/**
* The default attributes.
*/
defaults(): any {
return {
...super.defaults(),
_model_name: JupyterFrontEndModel.model_name,
_model_module: JupyterFrontEndModel.model_module,
_model_module_version: JupyterFrontEndModel.model_module_version
_model_module_version: JupyterFrontEndModel.model_module_version,
};
}

initialize(attributes: any, options: any) {
this.app = JupyterFrontEndModel._app;
/**
* Initialize a JupyterFrontEndModel instance.
*
* @param attributes The base attributes.
* @param options The initialization options.
*/
initialize(attributes: any, options: any): void {
this._app = JupyterFrontEndModel.app;
super.initialize(attributes, options);
this.send({ event: 'lab_ready' }, {});
this.set('version', this.app.version);
this.set('version', this._app.version);
this.save_changes();
}

static serializers: ISerializers = {
...DOMWidgetModel.serializers
...DOMWidgetModel.serializers,
};

static model_name = 'JupyterFrontEndModel';
Expand All @@ -40,6 +52,6 @@ export class JupyterFrontEndModel extends WidgetModel {
static view_module: string = null;
static view_module_version = MODULE_VERSION;

private app: JupyterFrontEnd;
static _app: JupyterFrontEnd;
private _app: JupyterFrontEnd;
static app: JupyterFrontEnd;
}
Loading

0 comments on commit 574c087

Please sign in to comment.