Skip to content

Commit

Permalink
Add ESLint recommended rules (cursorless-dev#776)
Browse files Browse the repository at this point in the history
  • Loading branch information
auscompgeek authored Jul 16, 2022
1 parent 2cf0fac commit ca1aa9d
Show file tree
Hide file tree
Showing 35 changed files with 115 additions and 87 deletions.
16 changes: 15 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
{
"root": true,
"parser": "@typescript-eslint/parser",
"extends": ["prettier"],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"prettier"
],
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module"
},
"plugins": ["@typescript-eslint"],
"rules": {
"@typescript-eslint/consistent-type-assertions": [
"error",
{
"assertionStyle": "as"
}
],
"@typescript-eslint/naming-convention": "warn",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-inferrable-types": "off",
"@typescript-eslint/no-non-null-assertion": "off",
"@typescript-eslint/no-unused-vars": [
"warn",
{
Expand All @@ -25,6 +38,7 @@
"null": "never"
}
],
"no-constant-condition": ["error", { "checkLoops": false }],
"no-throw-literal": "warn",
"semi": "off"
},
Expand Down
8 changes: 4 additions & 4 deletions src/actions/GenerateSnippet/Substituter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ export default class Substituter {
* @returns A string of random digits of length {@param length}
*/
function makeid(length: number) {
var result = "";
var characters =
let result = "";
const characters =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
var charactersLength = characters.length;
for (var i = 0; i < length; i++) {
const charactersLength = characters.length;
for (let i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
Expand Down
4 changes: 3 additions & 1 deletion src/core/Debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ export default class Debug {
}
}

init() {}
init() {
// do nothing
}

log(...args: any[]) {
if (this.active) {
Expand Down
2 changes: 1 addition & 1 deletion src/core/FontMeasurements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default class FontMeasurements {
async calculate() {
const fontFamily = getFontFamily();
let widthRatio, heightRatio;
let fontRatiosCache = this.graph.extensionContext.globalState.get<{
const fontRatiosCache = this.graph.extensionContext.globalState.get<{
widthRatio: number;
heightRatio: number;
fontFamily: string;
Expand Down
11 changes: 6 additions & 5 deletions src/core/HatTokenMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,14 @@ export default class HatTokenMap {
}

static splitKey(key: string) {
let [hatStyle, character] = key.split(".");
if (character.length === 0) {
const [hatStyle, character] = key.split(".");

return {
hatStyle: hatStyle as HatStyleName,
// If the character is `.` then it will appear as a zero length string
// due to the way the split on `.` works
character = ".";
}
return { hatStyle: hatStyle as HatStyleName, character };
character: character.length === 0 ? "." : character,
};
}

private async getActiveMap() {
Expand Down
2 changes: 1 addition & 1 deletion src/core/Snippets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ export class Snippets {
];

entries.forEach(([key, value]) => {
if (this.mergedSnippets.hasOwnProperty(key)) {
if (Object.prototype.hasOwnProperty.call(this.mergedSnippets, key)) {
const { definitions, ...rest } = value;
const mergedSnippet = this.mergedSnippets[key];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,12 @@ function upgradePrimitiveTarget(
if (modifier?.type === "subpiece") {
break;
}
// fallthrough
case "line":
if (mark?.type === "lineNumber") {
break;
}
// fallthrough
default:
modifiers.push({
type: "containingScope",
Expand Down Expand Up @@ -172,7 +174,7 @@ function upgradeTarget(
| PartialRangeTargetDescriptor
),
};
case "range":
case "range": {
const { type, rangeType, start, end, excludeStart, excludeEnd } = target;
return {
type,
Expand All @@ -182,6 +184,7 @@ function upgradeTarget(
excludeAnchor: excludeStart ?? false,
excludeActive: excludeEnd ?? false,
};
}
case "primitive":
return upgradePrimitiveTarget(target, action);
}
Expand Down
3 changes: 2 additions & 1 deletion src/core/inferFullTargets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ function getPreviousTargetAttribute<T>(
}
break;
}
case "list":
case "list": {
const attributeValue = getPreviousTargetAttribute(
target.elements,
getAttribute
Expand All @@ -223,6 +223,7 @@ function getPreviousTargetAttribute<T>(
return attributeValue;
}
break;
}
}
}
return undefined;
Expand Down
6 changes: 4 additions & 2 deletions src/core/updateSelections/getOffsetsForEmptyRangeInsert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export default function getOffsetsForEmptyRangeInsert(
case "open":
return { start, end };

case "regex":
case "regex": {
const matches = text.match(leftAnchored(expansionBehavior.regex));

return matches == null
Expand All @@ -73,6 +73,7 @@ export default function getOffsetsForEmptyRangeInsert(
start,
end: start + matches[0].length,
};
}
}
} else {
// In this case the range moves to the right so we care about the start of the range
Expand All @@ -88,7 +89,7 @@ export default function getOffsetsForEmptyRangeInsert(
case "open":
return { start, end };

case "regex":
case "regex": {
const index = text.search(rightAnchored(expansionBehavior.regex));

return index === -1
Expand All @@ -100,6 +101,7 @@ export default function getOffsetsForEmptyRangeInsert(
start: start + index,
end,
};
}
}
}
}
6 changes: 4 additions & 2 deletions src/core/updateSelections/getOffsetsForNonEmptyRangeInsert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export default function getOffsetsForNonEmptyRangeInsert(
end: newRangeEnd,
};

case "regex":
case "regex": {
let text = insertedText + originalRangeText;
const regex = rightAnchored(expansionBehavior.regex);
let index = text.search(regex);
Expand All @@ -95,6 +95,7 @@ export default function getOffsetsForNonEmptyRangeInsert(
start: rangeStart + index,
end: newRangeEnd,
};
}
}
} else {
const expansionBehavior = rangeInfo.expansionBehavior.end;
Expand All @@ -113,7 +114,7 @@ export default function getOffsetsForNonEmptyRangeInsert(
end: rangeEnd + displacement,
};

case "regex":
case "regex": {
let text = originalRangeText + insertedText;
const regex = leftAnchored(expansionBehavior.regex);
let matches = text.match(regex);
Expand All @@ -134,6 +135,7 @@ export default function getOffsetsForNonEmptyRangeInsert(
start: newRangeStart,
end: rangeStart + matchLength,
};
}
}
}
}
4 changes: 3 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,6 @@ export async function activate(context: vscode.ExtensionContext) {
}

// this method is called when your extension is deactivated
export function deactivate() {}
export function deactivate() {
// do nothing
}
6 changes: 4 additions & 2 deletions src/processTargets/marks/CursorStage.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { Target } from "../../typings/target.types";
import { CursorMark } from "../../typings/targetDescriptor.types";
import type { CursorMark } from "../../typings/targetDescriptor.types";
import { ProcessedTargetsContext } from "../../typings/Types";
import { isReversed } from "../../util/selectionUtils";
import { MarkStage } from "../PipelineStages.types";
import WeakTarget from "../targets/WeakTarget";
import TokenTarget from "../targets/TokenTarget";

export default class CursorStage implements MarkStage {
constructor(_modifier: CursorMark) {}
constructor(_modifier: CursorMark) {
// takes mark for consistency and does nothing
}

run(context: ProcessedTargetsContext): Target[] {
return context.currentSelections.map((selection) => {
Expand Down
3 changes: 2 additions & 1 deletion src/processTargets/marks/LineNumberStage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const getLine = (editor: TextEditor, linePosition: LineNumberPosition) => {
return linePosition.lineNumber;
case "relative":
return editor.selection.active.line + linePosition.lineNumber;
case "modulo100":
case "modulo100": {
const stepSize = 100;
const startLine = editor.visibleRanges[0].start.line;
const endLine =
Expand Down Expand Up @@ -64,5 +64,6 @@ const getLine = (editor: TextEditor, linePosition: LineNumberPosition) => {
return invisibleLines[0];
}
throw new Error("Line is not in viewport");
}
}
};
8 changes: 5 additions & 3 deletions src/processTargets/modifiers/ItemStage/ItemStage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ export default class ItemStage implements ModifierStage {
// First try the language specific implementation of item
try {
return new ContainingSyntaxScopeStage(
<SimpleContainingScopeModifier>this.modifier
this.modifier as SimpleContainingScopeModifier
).run(context, target);
} catch (_error) {}
} catch (_error) {
// do nothing
}

// Then try the textual implementation
if (this.modifier.type === "everyScope") {
Expand Down Expand Up @@ -96,7 +98,7 @@ export default class ItemStage implements ModifierStage {
", "
);
return new ScopeTypeTarget({
scopeTypeType: <SimpleScopeTypeType>this.modifier.scopeType.type,
scopeTypeType: this.modifier.scopeType.type as SimpleScopeTypeType,
editor: target.editor,
isReversed: target.isReversed,
contentRange: itemInfo.contentRange,
Expand Down
2 changes: 1 addition & 1 deletion src/processTargets/modifiers/scopeTypeStages/RegexStage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export class NonWhitespaceSequenceStage extends RegexStage {

// taken from https://regexr.com/3e6m0
const URL_REGEX =
/(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/g;
/(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%._+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_+.~#?&//=]*)/g;

export type UrlModifier = (ContainingScopeModifier | EveryScopeModifier) & {
scopeType: { type: "url" };
Expand Down
2 changes: 1 addition & 1 deletion src/processTargets/modifiers/scopeTypeStages/TokenStage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ function getRelevantTokens(editor: TextEditor, range: Range) {
const startLine = range.start.line;
const endLine = range.end.line;

let tokens = getTokensInRange(
const tokens = getTokensInRange(
editor,
editor.document.lineAt(startLine).range
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,22 +66,22 @@ export function findDelimiterPairContainingSelection(

while (true) {
// Scan right until we find an acceptable unmatched closing delimiter
let rightNext = rightDelimiterGenerator.next();
const rightNext = rightDelimiterGenerator.next();
if (rightNext.done) {
return null;
}
let rightDelimiterOccurrence = rightNext.value!;
const rightDelimiterOccurrence = rightNext.value!;

// Then scan left until we find an unmatched delimiter matching the
// delimiter we found in our rightward pass.
acceptableLeftDelimiters = [
rightDelimiterOccurrence.delimiterInfo.delimiter,
];
let leftNext = leftDelimiterGenerator.next();
const leftNext = leftDelimiterGenerator.next();
if (leftNext.done) {
return null;
}
let leftDelimiterOccurrence = leftNext.value!;
const leftDelimiterOccurrence = leftNext.value!;

// If left delimiter is left of our selection, we return it. Otherwise
// loop back and continue scanning outwards.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ function findSurroundingPairContainedInNode(
// first child of its parent, and right delimiter otherwise. This
// approach might not always work, but seems to work in the
// languages we've tried.
let side =
const side =
delimiterInfo.side === "unknown" && scopeType.forceDirection == null
? inferDelimiterSide(delimiterNode)
: delimiterInfo.side;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ function getDelimiterPairOffsets(

const rawDelimiterInfo = delimiterTextToDelimiterInfoMap[matchText];

let side =
const side =
rawDelimiterInfo.side === "unknown" && forceDirection == null
? inferDelimiterSide(
text,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export function* generateUnmatchedDelimiters(
* right delimiters of the given type we've seen. If this number drops to
* -1 for any delimiter, we yield it.
*/
let delimiterBalances: Partial<Record<SimpleSurroundingPairName, number>> =
const delimiterBalances: Partial<Record<SimpleSurroundingPairName, number>> =
{};

/**
Expand Down
2 changes: 1 addition & 1 deletion src/processTargets/targets/ScopeTypeTarget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export default class ScopeTypeTarget extends BaseTarget {
includeEnd: boolean
): Target {
if (isSameType(this, endTarget)) {
const scopeTarget = <ScopeTypeTarget>endTarget;
const scopeTarget = endTarget;
if (this.scopeTypeType_ === scopeTarget.scopeTypeType_) {
const contentRemovalRange =
this.removalRange_ != null || scopeTarget.removalRange_ != null
Expand Down
2 changes: 1 addition & 1 deletion src/scripts/hatAdjustments/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function processProperty(
hatAdjustmentsList: HatAdjustments[],
propertyName: keyof HatAdjustments
) {
let value = sum(
const value = sum(
hatAdjustmentsList.map((adjustment) => adjustment[propertyName] ?? 0)
);

Expand Down
6 changes: 3 additions & 3 deletions src/test/suite/breakpoints.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ async function breakpointHarpAdd() {
const breakpoints = vscode.debug.breakpoints;
assert.deepStrictEqual(breakpoints.length, 1);
assert.ok(breakpoints[0] instanceof vscode.SourceBreakpoint);
const breakpoint = <vscode.SourceBreakpoint>breakpoints[0];
const breakpoint = breakpoints[0];
assert.ok(breakpoint.location.range.isEqual(new vscode.Range(0, 0, 0, 0)));
}

Expand Down Expand Up @@ -74,7 +74,7 @@ async function breakpointTokenHarpAdd() {
const breakpoints = vscode.debug.breakpoints;
assert.deepStrictEqual(breakpoints.length, 1);
assert.ok(breakpoints[0] instanceof vscode.SourceBreakpoint);
const breakpoint = <vscode.SourceBreakpoint>breakpoints[0];
const breakpoint = breakpoints[0];
assert.ok(breakpoint.location.range.isEqual(new vscode.Range(0, 2, 0, 7)));
}

Expand Down Expand Up @@ -146,7 +146,7 @@ async function breakpointTokenHarpRemove() {
const breakpoints = vscode.debug.breakpoints;
assert.deepStrictEqual(breakpoints.length, 1);
assert.ok(breakpoints[0] instanceof vscode.SourceBreakpoint);
const breakpoint = <vscode.SourceBreakpoint>breakpoints[0];
const breakpoint = breakpoints[0];
assert.ok(breakpoint.location.range.isEqual(new vscode.Range(0, 0, 0, 0)));
}

Expand Down
Loading

0 comments on commit ca1aa9d

Please sign in to comment.