Skip to content

Commit be138ee

Browse files
authored
fix(lint): adjust eslint settings and fix warnings (#304)
Defaults are coming from eslint:recommended from this section here: ``` "extends": [ "eslint:recommended", "plugin:@typescript-eslint/recommended" ], ``` With a few changes specific to the Google style guide and our codebase. The config for naming conventions is mostly a copy of the default [here](https://typescript-eslint.io/rules/naming-convention#options) But with a few adjustments to allow snake_case properties, PascalCase globals, and PascalCase enum members. Eventually we will turn off explicit-any when our protocol messages are properly typed.
1 parent 01f4937 commit be138ee

15 files changed

+270
-263
lines changed

.eslintrc.json

+34-2
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,44 @@
88
"plugins": [
99
"@typescript-eslint"
1010
],
11+
"extends": [
12+
"eslint:recommended",
13+
"plugin:@typescript-eslint/recommended"
14+
],
1115
"rules": {
12-
"@typescript-eslint/naming-convention": "warn",
16+
"@typescript-eslint/naming-convention": [
17+
"warn",
18+
{
19+
"selector": "default",
20+
"format": ["camelCase", "snake_case"], // protocol messages use snake_case
21+
"leadingUnderscore": "allow"
22+
},
23+
{
24+
"selector": "enumMember",
25+
"format": ["PascalCase"],
26+
"leadingUnderscore": "allow"
27+
},
28+
{
29+
"selector": "variable",
30+
"format": ["UPPER_CASE", "PascalCase"], // TODO: remove PascalCase uses
31+
"modifiers": ["global", "const"]
32+
},
33+
{
34+
"selector": "typeLike",
35+
"format": ["PascalCase"]
36+
}
37+
],
38+
"@typescript-eslint/no-explicit-any": "off", // TODO: enable when messages are typed
39+
"@typescript-eslint/no-non-null-assertion": "off",
1340
"@typescript-eslint/semi": ["warn", "always"],
1441
"curly": "warn",
1542
"eqeqeq": "warn",
16-
"no-throw-literal": "warn"
43+
// indent 4 spaces
44+
"indent": ["warn", 4, { "SwitchCase": 1 }],
45+
"no-throw-literal": "warn",
46+
// turn off base rules as they can report incorrect errors
47+
"no-unused-vars": "off",
48+
"@typescript-eslint/no-unused-vars": ["warn", { "argsIgnorePattern": "^_" }]
1749
},
1850
"ignorePatterns": [
1951
"**/*.d.ts"

src/config-provider.ts

+22-22
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
// Copyright (C) Microsoft Corporation. All rights reserved.
32

43
import * as vscode from 'vscode';
@@ -7,26 +6,27 @@ import * as vscode from 'vscode';
76
// Custom fields specified in package.json.
87
//
98
export class ConfigProvider implements vscode.DebugConfigurationProvider {
9+
resolveDebugConfiguration(
10+
folder: vscode.WorkspaceFolder | undefined,
11+
config: vscode.DebugConfiguration
12+
): vscode.ProviderResult<vscode.DebugConfiguration> {
13+
// set some defaults to allow attach without a launch.json
14+
if (!config.type) {
15+
config.type = 'minecraft-js';
16+
}
17+
if (!config.request) {
18+
config.request = 'attach';
19+
}
20+
if (!config.name) {
21+
config.name = 'Attach to Minecraft';
22+
}
23+
if (!config.localRoot) {
24+
config.localRoot = '${workspaceFolder}/scripts/';
25+
}
26+
if (!config.port) {
27+
config.inputPort = '${command:PromptForPort}'; // prompt user for port
28+
}
1029

11-
resolveDebugConfiguration(folder: vscode.WorkspaceFolder | undefined, config: vscode.DebugConfiguration, token?: vscode.CancellationToken): vscode.ProviderResult<vscode.DebugConfiguration> {
12-
13-
// set some defaults to allow attach without a launch.json
14-
if (!config.type) {
15-
config.type = 'minecraft-js';
16-
}
17-
if (!config.request) {
18-
config.request = 'attach';
19-
}
20-
if (!config.name) {
21-
config.name = 'Attach to Minecraft';
22-
}
23-
if (!config.localRoot) {
24-
config.localRoot = "${workspaceFolder}/scripts/";
25-
}
26-
if (!config.port) {
27-
config.inputPort = "${command:PromptForPort}"; // prompt user for port
28-
}
29-
30-
return config;
31-
}
30+
return config;
31+
}
3232
}

src/extension.ts

+5-9
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { ReplayStatsProvider } from './stats/replay-stats-provider';
1111

1212
// called when extension is activated
1313
//
14-
export function activate(context: vscode.ExtensionContext) {
14+
export function activate(context: vscode.ExtensionContext): void {
1515
const liveStatsProvider = new StatsProvider('Live', 'minecraftDiagnosticsLive');
1616
const eventEmitter: EventEmitter = new EventEmitter();
1717

@@ -24,7 +24,7 @@ export function activate(context: vscode.ExtensionContext) {
2424
context.subscriptions.push(vscode.debug.registerDebugConfigurationProvider('minecraft-js', configProvider));
2525

2626
// register a debug adapter descriptor factory for 'minecraft-js', this factory creates the DebugSession
27-
let descriptorFactory = new ServerDebugAdapterFactory(homeViewProvider, liveStatsProvider, eventEmitter);
27+
const descriptorFactory = new ServerDebugAdapterFactory(homeViewProvider, liveStatsProvider, eventEmitter);
2828
context.subscriptions.push(vscode.debug.registerDebugAdapterDescriptorFactory('minecraft-js', descriptorFactory));
2929
if ('dispose' in descriptorFactory) {
3030
context.subscriptions.push(descriptorFactory);
@@ -51,7 +51,7 @@ export function activate(context: vscode.ExtensionContext) {
5151
// Create a command to allow keyboard shortcuts to run Minecraft commands
5252
const runMinecraftCommand = vscode.commands.registerCommand(
5353
'minecraft-debugger.runMinecraftCommand',
54-
(...args: any[]) => {
54+
(...args: unknown[]) => {
5555
if (args.length === 0) {
5656
vscode.window.showErrorMessage('No command provided.');
5757
return;
@@ -79,8 +79,8 @@ export function activate(context: vscode.ExtensionContext) {
7979
canSelectMany: false,
8080
openLabel: 'Open',
8181
filters: {
82-
'MC Stats Files': ['mcstats'],
83-
'All Files': ['*'],
82+
'MC Stats Files': ['mcstats'], // eslint-disable-line @typescript-eslint/naming-convention
83+
'All Files': ['*'], // eslint-disable-line @typescript-eslint/naming-convention
8484
},
8585
});
8686
if (!fileUri || fileUri.length === 0) {
@@ -101,7 +101,3 @@ export function activate(context: vscode.ExtensionContext) {
101101
replayDiagnosticsCommand
102102
);
103103
}
104-
105-
// called when extension is deactivated
106-
//
107-
export function deactivate() {}

src/message-stream-parser.ts

+17-16
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,31 @@
1-
21
// Copyright (C) Microsoft Corporation. All rights reserved.
32

3+
// eslint-disable-next-line @typescript-eslint/no-var-requires
44
const Parser = require('stream-parser');
5+
// eslint-disable-next-line @typescript-eslint/no-var-requires
56
const Transform = require('stream').Transform;
67

78
// Data transform attached to socket.
89
// Parses messages to json as they arrive from debugee,
910
// then raises them as events for consumption by the DA.
1011
//
1112
export class MessageStreamParser extends Transform {
12-
constructor() {
13-
super();
14-
this._bytes(9, this.onLength);
15-
}
13+
constructor() {
14+
super();
15+
this._bytes(9, this.onLength);
16+
}
1617

17-
private onLength(buffer: Buffer) {
18-
let length = parseInt(buffer.toString(), 16);
19-
this.emit('length', length);
20-
this._bytes(length, this.onMessage);
21-
}
18+
private onLength(buffer: Buffer) {
19+
const length = parseInt(buffer.toString(), 16);
20+
this.emit('length', length);
21+
this._bytes(length, this.onMessage);
22+
}
2223

23-
private onMessage(buffer: Buffer) {
24-
let json = JSON.parse(buffer.toString());
25-
this.emit('message', json);
26-
this._bytes(9, this.onLength);
27-
}
24+
private onMessage(buffer: Buffer) {
25+
const json = JSON.parse(buffer.toString());
26+
this.emit('message', json);
27+
this._bytes(9, this.onLength);
28+
}
2829
}
2930

30-
Parser(MessageStreamParser.prototype);
31+
Parser(MessageStreamParser.prototype);

src/panels/home-view-provider.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export class HomeViewProvider implements vscode.WebviewViewProvider {
2020
this._eventEmitter = eventEmitter;
2121
}
2222

23-
public setDebuggerStatus(isConnected: boolean, minecraftCapabilities: MinecraftCapabilities) {
23+
public setDebuggerStatus(isConnected: boolean, minecraftCapabilities: MinecraftCapabilities): void {
2424
this._view?.webview.postMessage({
2525
type: 'debugger-status',
2626
isConnected: isConnected,
@@ -33,11 +33,7 @@ export class HomeViewProvider implements vscode.WebviewViewProvider {
3333
this._eventEmitter.emit('request-debugger-status');
3434
}
3535

36-
public resolveWebviewView(
37-
webviewView: vscode.WebviewView,
38-
_context: vscode.WebviewViewResolveContext,
39-
_token: vscode.CancellationToken
40-
) {
36+
public resolveWebviewView(webviewView: vscode.WebviewView): void {
4137
this._view = webviewView;
4238

4339
this._view.webview.options = {

src/panels/minecraft-diagnostics.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ export class MinecraftDiagnosticsPanel {
9999
this._statsTracker.addStatListener(this._statsCallback);
100100
}
101101

102-
public static render(extensionUri: Uri, statsTracker: StatsProvider) {
102+
public static render(extensionUri: Uri, statsTracker: StatsProvider): void {
103103
const statsTrackerId = statsTracker.uniqueId;
104104
const existingPanel = MinecraftDiagnosticsPanel.activeDiagnosticsPanels.find(
105105
panel => panel._statsTracker.uniqueId === statsTrackerId
@@ -126,7 +126,7 @@ export class MinecraftDiagnosticsPanel {
126126
}
127127
}
128128

129-
public dispose() {
129+
public dispose(): void {
130130
if (this._statsCallback !== undefined) {
131131
this._statsTracker.removeStatListener(this._statsCallback);
132132
this._statsCallback = undefined;

src/server-debug-adapter-factory.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@ export class ServerDebugAdapterFactory implements vscode.DebugAdapterDescriptorF
2121
this._eventEmitter = eventEmitter;
2222
}
2323

24-
createDebugAdapterDescriptor(
25-
_session: vscode.DebugSession,
26-
_executable: vscode.DebugAdapterExecutable | undefined
27-
): vscode.ProviderResult<vscode.DebugAdapterDescriptor> {
24+
createDebugAdapterDescriptor(): vscode.ProviderResult<vscode.DebugAdapterDescriptor> {
2825
if (!this.server) {
2926
// start listening on a random port
3027
this.server = Net.createServer(socket => {
@@ -38,7 +35,7 @@ export class ServerDebugAdapterFactory implements vscode.DebugAdapterDescriptorF
3835
return new vscode.DebugAdapterServer((this.server.address() as Net.AddressInfo).port);
3936
}
4037

41-
dispose() {
38+
dispose(): void {
4239
if (this.server) {
4340
this.server.close();
4441
}

0 commit comments

Comments
 (0)