From e6b431d92f20a48421e5721f77c7f33f4906989b Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Sat, 5 Aug 2017 02:42:49 -0700 Subject: [PATCH] Add one-variable-per-declaration tslint rule Multiple declarations per line makes it more difficult to read what exactly is declared and generally makes code more verbose and less likely to fill in types --- src/InputHandler.ts | 66 +++++++++++++++------------------- src/Parser.ts | 7 +++- src/Terminal.ts | 32 ++++++++--------- src/handlers/Clipboard.test.ts | 6 ++-- tslint.json | 4 +++ 5 files changed, 57 insertions(+), 58 deletions(-) diff --git a/src/InputHandler.ts b/src/InputHandler.ts index 64da3b3f..e119df32 100644 --- a/src/InputHandler.ts +++ b/src/InputHandler.ts @@ -189,14 +189,12 @@ export class InputHandler implements IInputHandler { * Insert Ps (Blank) Character(s) (default = 1) (ICH). */ public insertChars(params: number[]): void { - let param, row, j, ch; - - param = params[0]; + let param = params[0]; if (param < 1) param = 1; - row = this._terminal.buffer.y + this._terminal.buffer.ybase; - j = this._terminal.buffer.x; - ch = [this._terminal.eraseAttr(), ' ', 1]; // xterm + const row = this._terminal.buffer.y + this._terminal.buffer.ybase; + let j = this._terminal.buffer.x; + const ch = [this._terminal.eraseAttr(), ' ', 1]; // xterm while (param-- && j < this._terminal.cols) { this._terminal.buffer.lines.get(row).splice(j++, 0, ch); @@ -325,9 +323,8 @@ export class InputHandler implements IInputHandler { * Cursor Position [row;column] (default = [1,1]) (CUP). */ public cursorPosition(params: number[]): void { - let row, col; - - row = params[0] - 1; + let col: number; + let row: number = params[0] - 1; if (params.length >= 2) { col = params[1] - 1; @@ -439,14 +436,13 @@ export class InputHandler implements IInputHandler { * Insert Ps Line(s) (default = 1) (IL). */ public insertLines(params: number[]): void { - let param, row, j; - - param = params[0]; + let param: number = params[0]; if (param < 1) { param = 1; } - row = this._terminal.buffer.y + this._terminal.buffer.ybase; + let row: number = this._terminal.buffer.y + this._terminal.buffer.ybase; + let j: number; j = this._terminal.rows - 1 - this._terminal.buffer.scrollBottom; j = this._terminal.rows - 1 + this._terminal.buffer.ybase - j + 1; @@ -475,14 +471,13 @@ export class InputHandler implements IInputHandler { * Delete Ps Line(s) (default = 1) (DL). */ public deleteLines(params: number[]): void { - let param, row, j; - - param = params[0]; + let param = params[0]; if (param < 1) { param = 1; } - row = this._terminal.buffer.y + this._terminal.buffer.ybase; + const row: number = this._terminal.buffer.y + this._terminal.buffer.ybase; + let j: number; j = this._terminal.rows - 1 - this._terminal.buffer.scrollBottom; j = this._terminal.rows - 1 + this._terminal.buffer.ybase - j; @@ -509,15 +504,13 @@ export class InputHandler implements IInputHandler { * Delete Ps Character(s) (default = 1) (DCH). */ public deleteChars(params: number[]): void { - let param, row, ch; - - param = params[0]; + let param: number = params[0]; if (param < 1) { param = 1; } - row = this._terminal.buffer.y + this._terminal.buffer.ybase; - ch = [this._terminal.eraseAttr(), ' ', 1]; // xterm + const row = this._terminal.buffer.y + this._terminal.buffer.ybase; + const ch = [this._terminal.eraseAttr(), ' ', 1]; // xterm while (param--) { this._terminal.buffer.lines.get(row).splice(this._terminal.buffer.x, 1); @@ -558,16 +551,14 @@ export class InputHandler implements IInputHandler { * Erase Ps Character(s) (default = 1) (ECH). */ public eraseChars(params: number[]): void { - let param, row, j, ch; - - param = params[0]; + let param = params[0]; if (param < 1) { param = 1; } - row = this._terminal.buffer.y + this._terminal.buffer.ybase; - j = this._terminal.buffer.x; - ch = [this._terminal.eraseAttr(), ' ', 1]; // xterm + const row = this._terminal.buffer.y + this._terminal.buffer.ybase; + let j = this._terminal.buffer.x; + const ch = [this._terminal.eraseAttr(), ' ', 1]; // xterm while (param-- && j < this._terminal.cols) { this._terminal.buffer.lines.get(row)[j++] = ch; @@ -619,9 +610,9 @@ export class InputHandler implements IInputHandler { * CSI Ps b Repeat the preceding graphic character Ps times (REP). */ public repeatPrecedingCharacter(params: number[]): void { - let param = params[0] || 1 - , line = this._terminal.buffer.lines.get(this._terminal.buffer.ybase + this._terminal.buffer.y) - , ch = line[this._terminal.buffer.x - 1] || [this._terminal.defAttr, ' ', 1]; + let param = params[0] || 1; + const line = this._terminal.buffer.lines.get(this._terminal.buffer.ybase + this._terminal.buffer.y); + const ch = line[this._terminal.buffer.x - 1] || [this._terminal.defAttr, ' ', 1]; while (param--) { line[this._terminal.buffer.x++] = ch; @@ -1203,14 +1194,13 @@ export class InputHandler implements IInputHandler { return; } - let l = params.length - , i = 0 - , flags = this._terminal.curAttr >> 18 - , fg = (this._terminal.curAttr >> 9) & 0x1ff - , bg = this._terminal.curAttr & 0x1ff - , p; + const l = params.length; + let flags = this._terminal.curAttr >> 18; + let fg = (this._terminal.curAttr >> 9) & 0x1ff; + let bg = this._terminal.curAttr & 0x1ff; + let p; - for (; i < l; i++) { + for (let i = 0; i < l; i++) { p = params[i]; if (p >= 30 && p <= 37) { // fg color 8 diff --git a/src/Parser.ts b/src/Parser.ts index 1c2fb6b0..bec0af7b 100644 --- a/src/Parser.ts +++ b/src/Parser.ts @@ -182,7 +182,12 @@ export class Parser { * @param data The data to parse. */ public parse(data: string): ParserState { - let l = data.length, j, cs, ch, code, low; + const l = data.length; + let j; + let cs; + let ch; + let code; + let low; if (this._terminal.debug) { this._terminal.log('data: ' + data); diff --git a/src/Terminal.ts b/src/Terminal.ts index 839ad568..e1633033 100644 --- a/src/Terminal.ts +++ b/src/Terminal.ts @@ -95,9 +95,9 @@ const tangoColors = [ // Colors 0-15 + 16-255 // Much thanks to TooTallNate for writing this. const defaultColors = (function() { - let colors = tangoColors.slice() - , r = [0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff] - , i; + let colors = tangoColors.slice(); + let r = [0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff]; + let i; // 16-231 i = 0; @@ -971,11 +971,11 @@ export class Terminal extends EventEmitter implements ITerminal { } function getButton(ev) { - let button - , shift - , meta - , ctrl - , mod; + let button; + let shift; + let meta; + let ctrl; + let mod; // two low bits: // 0 = left @@ -1918,12 +1918,12 @@ export class Terminal extends EventEmitter implements ITerminal { this.setOption('scrollback', y); } - let line - , el - , i - , j - , ch - , addToY; + let line; + let el; + let i; + let j; + let ch; + let addToY; if (x === this.cols && y === this.rows) { // Check if we still need to measure the char size (fixes #785). @@ -2426,8 +2426,8 @@ function wasMondifierKeyOnlyEvent(ev) { function keys(obj) { if (Object.keys) return Object.keys(obj); - let key, keys = []; - for (key in obj) { + const keys = []; + for (let key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { keys.push(key); } diff --git a/src/handlers/Clipboard.test.ts b/src/handlers/Clipboard.test.ts index 740bee3d..a91ab87c 100644 --- a/src/handlers/Clipboard.test.ts +++ b/src/handlers/Clipboard.test.ts @@ -4,9 +4,9 @@ import * as Clipboard from './Clipboard'; describe('evaluatePastedTextProcessing', function () { it('should replace carriage return + line feed with line feed on windows', function () { - const pastedText = 'foo\r\nbar\r\n', - processedText = Clipboard.prepareTextForTerminal(pastedText, false), - windowsProcessedText = Clipboard.prepareTextForTerminal(pastedText, true); + const pastedText = 'foo\r\nbar\r\n'; + const processedText = Clipboard.prepareTextForTerminal(pastedText, false); + const windowsProcessedText = Clipboard.prepareTextForTerminal(pastedText, true); assert.equal(processedText, 'foo\r\nbar\r\n'); assert.equal(windowsProcessedText, 'foo\rbar\r'); diff --git a/tslint.json b/tslint.json index d571d9c5..f98fd4c4 100644 --- a/tslint.json +++ b/tslint.json @@ -13,6 +13,10 @@ "no-eval": true, "no-internal-module": true, "no-trailing-whitespace": true, + "one-variable-per-declaration": [ + true, + true + ], "no-unsafe-finally": true, "no-var-keyword": true, "quotemark": [