Add ESLint to repository and fix style issues.

This commit is contained in:
Grégoire Geis 2020-03-07 00:08:35 +01:00
parent 8cec3650af
commit bd4bbe3029
16 changed files with 811 additions and 71 deletions

42
.eslintrc.js Normal file
View File

@ -0,0 +1,42 @@
module.exports = {
root: true,
parser: '@typescript-eslint/parser',
plugins: [
'@typescript-eslint',
],
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/eslint-recommended',
],
parserOptions: {
ecmaVersion: 2019,
sourceType: 'module',
},
rules: {
'dot-location': ['error', 'property'],
'eqeqeq': ['error', 'always', { null: 'ignore' }],
'require-await': 'error',
'array-bracket-spacing': ['error', 'never'],
'block-spacing': ['error', 'always'],
'brace-style': ['error', '1tbs', { allowSingleLine: true }],
'comma-dangle': ['error', 'always-multiline'],
'linebreak-style': ['error', 'unix'],
'no-tabs': 'error',
'no-trailing-spaces': 'error',
'no-unexpected-multiline': 'error',
'no-unneeded-ternary': 'error',
'object-curly-spacing': ['error', 'always'],
'object-shorthand': 'error',
'one-var-declaration-per-line': ['error', 'always'],
'quotes': ['error', 'single', { avoidEscape: true, allowTemplateLiterals: true }],
'semi': ['error', 'never'],
'space-before-function-paren': ['error', { anonymous: 'never', named: 'never', asyncArrow: 'always' }],
'space-before-blocks': 'error',
'space-infix-ops': 'error',
'unicode-bom': 'error',
'keyword-spacing': ['error', { before: true, after: true }],
'no-unused-vars': 'off',
'no-case-declarations': 'off',
'no-cond-assign': 'off',
},
}

View File

@ -184,12 +184,12 @@ for (const id in yaml) {
stream.write(`
/** All defined commands. */
export const commands = {
${commands.map(x => ` /** ${yaml[x].descr} */\n ${writable(x)}`).join(',\n')}
${commands.map(x => ` /** ${yaml[x].descr} */\n ${writable(x)}`).join(',\n')},
}
/** An enum which maps command names to command IDs. */
export const enum Command {
${commands.map(x => ` /** ${yaml[x].descr} */\n ${writable(x)} = '${prefix}.${x}'`).join(',\n')}
${commands.map(x => ` /** ${yaml[x].descr} */\n ${writable(x)} = '${prefix}.${x}'`).join(',\n')},
}
`)

View File

@ -2416,7 +2416,7 @@ export const commands = {
/** Adds 8 to the current counter for the next operation. */
count8,
/** Adds 9 to the current counter for the next operation. */
count9
count9,
}
/** An enum which maps command names to command IDs. */
@ -2742,5 +2742,5 @@ export const enum Command {
/** Adds 8 to the current counter for the next operation. */
count8 = 'dance.count.8',
/** Adds 9 to the current counter for the next operation. */
count9 = 'dance.count.9'
count9 = 'dance.count.9',
}

View File

@ -96,10 +96,12 @@ registerCommand(Command.pasteAfter, CommandFlags.Edit, async (editor, state, und
if (contents === undefined)
return
const selections = editor.selections
await editor.edit(builder => {
for (let i = 0; i < contents.length; i++) {
const content = contents[i],
selection = editor.selections[i]
selection = selections[i]
if (content.endsWith('\n'))
builder.insert(selection.end.with(selection.end.line + 1, 0), content)
@ -111,16 +113,16 @@ registerCommand(Command.pasteAfter, CommandFlags.Edit, async (editor, state, und
// Restore selections that were extended automatically.
for (let i = 0; i < contents.length; i++) {
const content = contents[i],
selection = editor.selections[i]
selection = selections[i]
if (!content.endsWith('\n')) {
const previousEnd = offsetPosition(editor.document, content, selection.end, Backward)
editor.selections[i] = makeSelection(selection.isEmpty ? previousEnd : selection.start, previousEnd, selection)
selections[i] = makeSelection(selection.isEmpty ? previousEnd : selection.start, previousEnd, selection)
}
}
editor.selections = editor.selections
editor.selections = selections
})
registerCommand(Command.pasteBefore, CommandFlags.Edit, async (editor, state, undoStops, ctx) => {
@ -129,10 +131,12 @@ registerCommand(Command.pasteBefore, CommandFlags.Edit, async (editor, state, un
if (contents === undefined)
return
const selections = editor.selections
await editor.edit(builder => {
for (let i = 0; i < contents.length; i++) {
const content = contents[i],
selection = editor.selections[i]
selection = selections[i]
if (content.endsWith('\n'))
builder.replace(selection.start.with(undefined, 0), content)
@ -144,16 +148,16 @@ registerCommand(Command.pasteBefore, CommandFlags.Edit, async (editor, state, un
// Restore selections that were extended automatically.
for (let i = 0; i < contents.length; i++) {
const content = contents[i],
selection = editor.selections[i]
selection = selections[i]
if (!content.endsWith('\n')) {
const previousStart = offsetPosition(editor.document, content, selection.start, Forward)
editor.selections[i] = makeSelection(previousStart, selection.isEmpty ? previousStart : selection.end, selection)
selections[i] = makeSelection(previousStart, selection.isEmpty ? previousStart : selection.end, selection)
}
}
editor.selections = editor.selections
editor.selections = selections
})
registerCommand(Command.pasteSelectAfter, CommandFlags.ChangeSelections | CommandFlags.Edit, async (editor, state, undoStops, ctx) => {
@ -162,12 +166,13 @@ registerCommand(Command.pasteSelectAfter, CommandFlags.ChangeSelections | Comman
if (contents === undefined)
return
const reverseSelection = [] as boolean[]
const selections = editor.selections,
reverseSelection = [] as boolean[]
await editor.edit(builder => {
for (let i = 0; i < contents.length; i++) {
const content = contents[i],
selection = editor.selections[i]
selection = selections[i]
if (content.endsWith('\n'))
builder.replace(selection.end.with(selection.end.line + 1, 0), content)
@ -181,14 +186,14 @@ registerCommand(Command.pasteSelectAfter, CommandFlags.ChangeSelections | Comman
// Reverse selections that were empty, since they are now extended in the wrong way.
for (let i = 0; i < contents.length; i++) {
const content = contents[i],
selection = editor.selections[i]
selection = selections[i]
if (!content.endsWith('\n') && reverseSelection[i]) {
editor.selections[i] = new vscode.Selection(selection.active, selection.anchor)
selections[i] = new vscode.Selection(selection.active, selection.anchor)
}
}
editor.selections = editor.selections
editor.selections = selections
})
registerCommand(Command.pasteSelectBefore, CommandFlags.ChangeSelections | CommandFlags.Edit, async (editor, state, undoStops, ctx) => {
@ -239,7 +244,7 @@ registerCommand(Command.pasteReplaceEvery, CommandFlags.Edit, async (editor, sta
})
registerCommand(Command.replaceCharacters, CommandFlags.Edit, InputKind.Key, undefined, async (editor, { currentCount, input: key }) => {
registerCommand(Command.replaceCharacters, CommandFlags.Edit, InputKind.Key, undefined, (editor, { currentCount, input: key }) => {
const string = key.repeat(currentCount || 1)
return (builder: vscode.TextEditorEdit) => {

View File

@ -244,7 +244,7 @@ export class CommandDescriptor<Input extends InputKind = InputKind> {
}
register(state: Extension) {
return vscode.commands.registerCommand(this.command, async () => {
return vscode.commands.registerCommand(this.command, () => {
const editor = vscode.window.activeTextEditor
if (editor !== undefined)

View File

@ -1,17 +1,12 @@
// Marks: https://github.com/mawww/kakoune/blob/master/doc/pages/keys.asciidoc#marks
import * as vscode from 'vscode'
import { Extension } from '../extension'
import { keypress, promptInList } from '../utils/prompt'
import { Command, registerCommand, CommandFlags, InputKind } from '.'
registerCommand(Command.registersSelect, CommandFlags.IgnoreInHistory, InputKind.Key, undefined, async (_, { input: key }, __, ctx) => {
registerCommand(Command.registersSelect, CommandFlags.IgnoreInHistory, InputKind.Key, undefined, (_, { input: key }, __, ctx) => {
ctx.currentRegister = ctx.registers.get(key)
})
registerCommand(Command.registersInsert, CommandFlags.None, async (editor, state) => {
registerCommand(Command.registersInsert, CommandFlags.None, (editor, state) => {
throw new Error('Not implemented.')
})

View File

@ -168,7 +168,7 @@ registerCommand(Command.downExtend , CommandFlags.ChangeSelections, (editor, { c
// ===============================================================================================
function registerSelectTo(commandName: Command, diff: number, extend: boolean, direction: Direction) {
registerCommand(commandName, CommandFlags.ChangeSelections, InputKind.Key, undefined, async (editor, { currentCount, input: key }) => {
registerCommand(commandName, CommandFlags.ChangeSelections, InputKind.Key, undefined, (editor, { currentCount, input: key }) => {
editor.selections = editor.selections.map(selection => {
const active = getActiveForExtending(selection, direction)
@ -223,11 +223,11 @@ export function isAlphaWord(c: string) {
}
function isNonWsWord(c: string) {
return c != ' ' && c != '\t'
return c !== ' ' && c !== '\t'
}
function isBlank(c: string) {
return c == ' ' || c == '\t'
return c === ' ' || c === '\t'
}
function skipWhile(document: vscode.TextDocument, pos: vscode.Position, direction: Direction, dontSkipLine: boolean, cond: (c: string) => boolean) {
@ -464,8 +464,8 @@ registerCommand(Command.expandLines, CommandFlags.ChangeSelections, editor => {
registerCommand(Command.trimLines, CommandFlags.ChangeSelections, editor => {
editor.selections = editor.selections.map(x => {
const start = x.start.character == 0 ? x.start : x.start.translate(1, 0)
const end = x.end.character == editor.document.lineAt(x.end).range.end.character ? x.end : x.end.translate(-1, 0)
const start = x.start.character === 0 ? x.start : x.start.translate(1, 0)
const end = x.end.character === editor.document.lineAt(x.end).range.end.character ? x.end : x.end.translate(-1, 0)
return x.isReversed ? new vscode.Selection(end, start) : new vscode.Selection(start, end)
})

View File

@ -4,7 +4,7 @@ import * as vscode from 'vscode'
import { registerCommand, Command, CommandFlags, InputKind } from '.'
registerCommand(Command.select, CommandFlags.ChangeSelections, InputKind.RegExp, 'gm', async (editor, { input: regex }) => {
registerCommand(Command.select, CommandFlags.ChangeSelections, InputKind.RegExp, 'gm', (editor, { input: regex }) => {
const newSelections = [] as vscode.Selection[]
for (let i = 0; i < editor.selections.length; i++) {
@ -29,7 +29,7 @@ registerCommand(Command.select, CommandFlags.ChangeSelections, InputKind.RegExp,
editor.selections = newSelections
})
registerCommand(Command.split, CommandFlags.ChangeSelections, InputKind.RegExp, 'gm', async (editor, { input: regex }) => {
registerCommand(Command.split, CommandFlags.ChangeSelections, InputKind.RegExp, 'gm', (editor, { input: regex }) => {
const newSelections = [] as vscode.Selection[]
for (let i = 0; i < editor.selections.length; i++) {

View File

@ -265,7 +265,7 @@ function registerSearchCommand(command: Command, backward: boolean, extend: bool
editor.revealRange(editor.selection)
return undefined
}
},
}, () => {})
}

View File

@ -29,7 +29,7 @@ registerCommand(Command.selectionsMerge, CommandFlags.ChangeSelections, editor =
const refSel = selections[i]
const refLine = editor.document.lineAt(refSel.start)
if (refSel.end.character != refLine.range.end.character)
if (refSel.end.character !== refLine.range.end.character)
// Not at the end of the line? We don't care
continue
@ -92,7 +92,7 @@ registerCommand(Command.selectionsClearMain, CommandFlags.ChangeSelections, edit
editor.selections = editor.selections.slice(1)
})
registerCommand(Command.selectionsKeepMatching, CommandFlags.ChangeSelections, InputKind.RegExp, '', async (editor, { input: regex }) => {
registerCommand(Command.selectionsKeepMatching, CommandFlags.ChangeSelections, InputKind.RegExp, '', (editor, { input: regex }) => {
const newSelections = editor.selections.filter(x => regex.test(editor.document.getText(x)))
if (newSelections.length === 0)
@ -101,7 +101,7 @@ registerCommand(Command.selectionsKeepMatching, CommandFlags.ChangeSelections, I
editor.selections = newSelections
})
registerCommand(Command.selectionsClearMatching, CommandFlags.ChangeSelections, InputKind.RegExp, '', async (editor, { input: regex }) => {
registerCommand(Command.selectionsClearMatching, CommandFlags.ChangeSelections, InputKind.RegExp, '', (editor, { input: regex }) => {
const newSelections = editor.selections.filter(x => !regex.test(editor.document.getText(x)))
if (newSelections.length === 0)

View File

@ -56,7 +56,7 @@ class ModeConfiguration {
updateLineNumbers(extension: Extension, defaultValue: LineNumbers) {
this.lineNumbers = this.lineNumbersStringToLineNumbersStyle(
extension.configuration.get(this.modePrefix + '.lineNumbers') ?? defaultValue
extension.configuration.get(this.modePrefix + '.lineNumbers') ?? defaultValue,
)
}
@ -68,7 +68,7 @@ class ModeConfiguration {
updateCursorStyle(extension: Extension, defaultValue: CursorStyle) {
this.cursorStyle = this.cursorStyleStringToCursorStyle(
extension.configuration.get(this.modePrefix + '.cursorStyle') ?? defaultValue
extension.configuration.get(this.modePrefix + '.cursorStyle') ?? defaultValue,
)
}
@ -360,7 +360,7 @@ export class Extension implements vscode.Disposable {
this.changeEditorCommand!.dispose()
this.subscriptions.push(
vscode.window.onDidChangeVisibleTextEditors(restoreLineNumbering)
vscode.window.onDidChangeVisibleTextEditors(restoreLineNumbering),
)
restoreLineNumbering(vscode.window.visibleTextEditors)
@ -515,7 +515,7 @@ export function activate(context: vscode.ExtensionContext) {
if (process.env.VERBOSE_LOGGING === 'true') {
// Log all commands we need to implement
Promise.all([ vscode.commands.getCommands(true), import('../commands/index') ])
Promise.all([vscode.commands.getCommands(true), import('../commands/index')])
.then(([registeredCommands, { commands }]) => {
for (const command of Object.values(commands)) {
if (registeredCommands.indexOf(command.id) === -1)

View File

@ -104,10 +104,10 @@ export class Registers {
readonly caret = new GeneralPurposeRegister('^')
readonly pipe = new GeneralPurposeRegister('|')
readonly percent = new SpecialRegister('%', async editor => [editor.document.fileName])
readonly dot = new SpecialRegister('.', async editor => editor.selections.map(editor.document.getText))
readonly hash = new SpecialRegister('#', async editor => editor.selections.map((_, i) => i.toString()))
readonly underscore = new SpecialRegister('_', async ______ => [''])
readonly percent = new SpecialRegister('%', editor => Promise.resolve([editor.document.fileName]))
readonly dot = new SpecialRegister('.', editor => Promise.resolve(editor.selections.map(editor.document.getText)))
readonly hash = new SpecialRegister('#', editor => Promise.resolve(editor.selections.map((_, i) => i.toString())))
readonly underscore = new SpecialRegister('_', ______ => Promise.resolve(['']))
readonly colon = new GeneralPurposeRegister(':', true)
get(key: string) {

View File

@ -16,7 +16,7 @@ export function promptRegex(flags?: string) {
} catch {
return 'Invalid ECMA RegExp.'
}
}
},
}).then(x => x === undefined ? undefined : new RegExp(x, flags))
}
@ -24,7 +24,7 @@ export function keypress(cancellationToken?: vscode.CancellationToken): Thenable
return new Promise(resolve => {
try {
let done = false
let subscription = vscode.commands.registerCommand('type', async ({ text }: { text: string }) => {
let subscription = vscode.commands.registerCommand('type', ({ text }: { text: string }) => {
if (!done) {
subscription.dispose()
done = true
@ -73,7 +73,7 @@ export function promptInList(canPickMany: boolean, items: [string, string][]): T
quickPick.dispose()
if (canPickMany)
resolve(index === -1 ? undefined : [ index ])
resolve(index === -1 ? undefined : [index])
else
resolve(index === -1 ? undefined : index)
})

View File

@ -1,7 +1,7 @@
import * as assert from 'assert'
suite("Dummy Tests", () => {
test("Dummy", () => {
suite('Dummy Tests', () => {
test('Dummy', () => {
assert.equal(-1, [1, 2, 3].indexOf(5))
assert.equal(-1, [1, 2, 3].indexOf(0))
})

View File

@ -1,15 +0,0 @@
{
"rules": {
"no-string-throw": true,
"no-unused-expression": true,
"no-duplicate-variable": true,
"curly": true,
"class-name": true,
"semicolon": [
false,
"always"
],
"triple-equals": true
},
"defaultSeverity": "warning"
}

725
yarn.lock

File diff suppressed because it is too large Load Diff