Skip to content

Add ESLint recommended rules #776

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Jul 16, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
4 changes: 2 additions & 2 deletions src/actions/WrapWithSnippet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ function transformSnippetVariables(
parsedSnippet: TextmateSnippet,
placeholderName: string
) {
var placeholderIndex = getMaxPlaceholderIndex(parsedSnippet) + 1;
let placeholderIndex = getMaxPlaceholderIndex(parsedSnippet) + 1;

parsedSnippet.walk((candidate) => {
if (candidate instanceof Variable) {
Expand All @@ -139,7 +139,7 @@ function transformSnippetVariables(
}

function getMaxPlaceholderIndex(parsedSnippet: TextmateSnippet) {
var placeholderIndex = 0;
let placeholderIndex = 0;
parsedSnippet.walk((candidate) => {
if (candidate instanceof Placeholder) {
placeholderIndex = Math.max(placeholderIndex, candidate.index);
Expand Down
2 changes: 0 additions & 2 deletions src/core/Debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ export default class Debug {
}
}

init() {}

log(...args: any[]) {
if (this.active) {
console.debug(...args);
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 @@ -158,7 +158,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 @@ -164,12 +164,13 @@ function getPreviousTarget(
return target.anchor;
}
break;
case "list":
case "list": {
const result = getPreviousTarget(target.elements, useTarget);
if (result != null) {
return result;
}
break;
}
}
}
return null;
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,
};
}
}
}
}
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,
};
}
}
}
}
5 changes: 3 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export async function activate(context: vscode.ExtensionContext) {
commandServerApi: () => commandServerApi,
getNodeAtLocation: () => getNodeAtLocation,
} as FactoryMap<Graph>);
graph.debug.init();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Debug#init was empty, so removed it altogether, since it's not implementing anything.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The way the graph works, accessing an attribute will cause the component to be instantiated, so it's possible this line was actually doing something

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this is the only remaining issue on this PR

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverted this change. I'm not convinced this line is doing much in practice, but we can look into it later.

graph.snippets.init();
await graph.decorations.init();
graph.hatTokenMap.init();
Expand Down Expand Up @@ -94,4 +93,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
}
2 changes: 1 addition & 1 deletion src/processTargets/getMarkStage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { MarkStage } from "./PipelineStages.types";
export default (mark: Mark): MarkStage => {
switch (mark.type) {
case "cursor":
return new CursorStage(mark);
return new CursorStage();
case "that":
return new ThatStage(mark);
case "source":
Expand Down
3 changes: 0 additions & 3 deletions src/processTargets/marks/CursorStage.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import { Target } from "../../typings/target.types";
import { 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";

export default class CursorStage implements MarkStage {
constructor(_modifier: CursorMark) {}

run(context: ProcessedTargetsContext): Target[] {
return context.currentSelections.map((selection) => {
return new WeakTarget({
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");
}
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,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
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" && forceDirection == null
? inferDelimiterSide(delimiterNode)
: delimiterInfo.side;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,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/processTargets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ function processVerticalRangeTarget(
const activeLine = activePosition.line - (excludeActive ? delta : 0);

const results: Target[] = [];
for (let i = anchorLine; true; i += delta) {
for (let i = anchorLine; ; i += delta) {
const contentRange = new Range(
i,
anchorTarget.contentRange.start.character,
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 @@ -86,7 +86,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 @@ -50,7 +50,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 @@ -79,7 +79,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 @@ -151,7 +151,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