Skip to content

Commit

Permalink
style: eslint
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinzent03 committed Sep 19, 2022
1 parent 9282c60 commit c8453b8
Show file tree
Hide file tree
Showing 20 changed files with 212 additions and 155 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
npm node_modules
build
50 changes: 50 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"root": true,
"parser": "@typescript-eslint/parser",
"env": {
"node": true
},
"plugins": [
"@typescript-eslint",
"svelte3"
],
"overrides": [
{
"files": [
"*.svelte"
],
"processor": "svelte3/svelte3"
}
],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended"
],
"parserOptions": {
"sourceType": "module",
"project": [
"./tsconfig.json"
],
"tsconfigRootDir": "."
},
"settings": {
"svelte3/typescript": true
},
"rules": {
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": [
"error",
{
"args": "none"
}
],
"no-async-promise-executor": "off",
"no-case-declarations": "off",
"@typescript-eslint/no-non-null-assertion": "off",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/ban-ts-comment": "off",
"no-prototype-builtins": "off",
"@typescript-eslint/no-empty-function": "off"
}
}
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"scripts": {
"dev": "node esbuild.config.mjs dev",
"build": "node esbuild.config.mjs production",
"release": "standard-version"
"release": "standard-version",
"lint": "eslint --ext .ts,.svelte src"
},
"keywords": [],
"author": "",
Expand All @@ -16,6 +17,9 @@
},
"devDependencies": {
"@types/node": "^14.14.2",
"@typescript-eslint/eslint-plugin": "5.29.0",
"@typescript-eslint/parser": "5.29.0",
"eslint-plugin-svelte3": "^4.0.0",
"esbuild": "^0.12.26",
"esbuild-svelte": "^0.5.4",
"obsidian": "^0.16.0",
Expand All @@ -41,4 +45,4 @@
"ts",
"svelte"
]
}
}
30 changes: 15 additions & 15 deletions src/gitManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@ export abstract class GitManager {

abstract status(): Promise<Status>;

abstract commitAll({ }: { message?: string, status?: Status, unstagedFiles?: UnstagedFile[]; }): Promise<number | undefined>;
abstract commitAll(_: { message: string, status?: Status, unstagedFiles?: UnstagedFile[]; }): Promise<number | undefined>;

abstract commit(message?: string): Promise<number | undefined>;

abstract stageAll({ }: { dir?: string, status?: Status; }): Promise<void>;
abstract stageAll(_: { dir?: string, status?: Status; }): Promise<void>;

abstract unstageAll({ }: { dir?: string, status?: Status; }): Promise<void>;
abstract unstageAll(_: { dir?: string, status?: Status; }): Promise<void>;

abstract stage(filepath: string, relativeToVault: boolean): Promise<void>;

abstract unstage(filepath: string, relativeToVault: boolean): Promise<void>;

abstract discard(filepath: string): Promise<void>;

abstract pull(): Promise<FileStatusResult[]>;
abstract pull(): Promise<FileStatusResult[] | undefined>;

abstract push(): Promise<number>;

Expand All @@ -43,7 +43,7 @@ export abstract class GitManager {

abstract clone(url: string, dir: string): Promise<void>;

abstract setConfig(path: string, value: any): Promise<void>;
abstract setConfig(path: string, value: string | number | boolean): Promise<void>;

abstract getConfig(path: string): Promise<any>;

Expand All @@ -53,7 +53,7 @@ export abstract class GitManager {

abstract getRemotes(): Promise<string[]>;

abstract getRemoteUrl(remote: string): Promise<string>;
abstract getRemoteUrl(remote: string): Promise<string | undefined>;

abstract getRemoteBranches(remote: string): Promise<string[]>;

Expand All @@ -80,11 +80,11 @@ export abstract class GitManager {
return (relativeToVault && this.plugin.settings.basePath.length > 0) ? path.substring(this.plugin.settings.basePath.length + 1) : path;
}

getTreeStructure(children: FileStatusResult[], beginLength: number = 0): TreeItem[] {
let list: TreeItem[] = [];
getTreeStructure(children: FileStatusResult[], beginLength = 0): TreeItem[] {
const list: TreeItem[] = [];
children = [...children];
while (children.length > 0) {
const first = children.first();
const first = children.first()!;
const restPath = first.path.substring(beginLength);
if (restPath.contains("/")) {
const title = restPath.substring(0, restPath.indexOf("/"));
Expand All @@ -108,7 +108,7 @@ export abstract class GitManager {
let status: Status | undefined;
if (template.includes("{{numFiles}}")) {
status = await this.status();
let numFiles = status.staged.length;
const numFiles = status.staged.length;
template = template.replace("{{numFiles}}", String(numFiles));
}
if (template.includes("{{hostname}}")) {
Expand All @@ -119,7 +119,7 @@ export abstract class GitManager {
if (template.includes("{{files}}")) {
status = status ?? await this.status();

let changeset: { [key: string]: string[]; } = {};
const changeset: { [key: string]: string[]; } = {};
status.staged.forEach((value: FileStatusResult) => {
if (value.index in changeset) {
changeset[value.index].push(value.path);
Expand All @@ -128,17 +128,17 @@ export abstract class GitManager {
}
});

let chunks = [];
for (let [action, files] of Object.entries(changeset)) {
const chunks = [];
for (const [action, files] of Object.entries(changeset)) {
chunks.push(action + " " + files.join(" "));
}

let files = chunks.join(", ");
const files = chunks.join(", ");

template = template.replace("{{files}}", files);
}

let moment = (window as any).moment;
const moment = (window as any).moment;
template = template.replace(
"{{date}}",
moment().format(this.plugin.settings.commitDateFormat)
Expand Down
47 changes: 24 additions & 23 deletions src/isomorphicGit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ export class IsomorphicGit extends GitManager {
this.plugin.displayError(error);
throw error;
}
};
}

async commitAll({ message, status, unstagedFiles }: { message?: string, status?: Status, unstagedFiles?: UnstagedFile[]; }): Promise<number> {
async commitAll({ message, status, unstagedFiles }: { message: string, status?: Status, unstagedFiles?: UnstagedFile[]; }): Promise<number | undefined> {
try {
await this.stageAll({ status, unstagedFiles });
return this.commit(message);
Expand All @@ -138,16 +138,16 @@ export class IsomorphicGit extends GitManager {
}
}

async commit(message?: string): Promise<undefined> {
async commit(message: string): Promise<undefined> {
try {
this.plugin.setState(PluginState.commit);
const formatMessage = await this.formatCommitMessage(message);
const hadConflict = this.plugin.localStorage.getConflict() === "true";
let parent: string[] = undefined;
let parent: string[] | undefined = undefined;

if (hadConflict) {
const branchInfo = await this.branchInfo();
parent = [branchInfo.current, branchInfo.tracking];
parent = [branchInfo.current!, branchInfo.tracking!];
}

await this.wrapFS(git.commit({
Expand Down Expand Up @@ -184,7 +184,7 @@ export class IsomorphicGit extends GitManager {
}
}

async stageAll({ dir, status, unstagedFiles }: { dir?: string, status?: Status; unstagedFiles: UnstagedFile[]; }): Promise<void> {
async stageAll({ dir, status, unstagedFiles }: { dir?: string, status?: Status; unstagedFiles?: UnstagedFile[]; }): Promise<void> {
try {
if (status) {
await Promise.all(
Expand Down Expand Up @@ -272,7 +272,7 @@ export class IsomorphicGit extends GitManager {
await this.wrapFS(git.merge({
...this.getRepo(),
ours: branchInfo.current,
theirs: branchInfo.tracking,
theirs: branchInfo.tracking!,
abortOnConflict: false,
}));
await this.wrapFS(git.checkout({
Expand Down Expand Up @@ -317,7 +317,7 @@ export class IsomorphicGit extends GitManager {
const status = await this.branchInfo();
const trackingBranch = status.tracking;
const currentBranch = status.current;
const numChangedFiles = (await this.getFileChangesCount(currentBranch, trackingBranch)).length;
const numChangedFiles = (await this.getFileChangesCount(currentBranch!, trackingBranch!)).length;

this.plugin.setState(PluginState.push);

Expand All @@ -341,8 +341,8 @@ export class IsomorphicGit extends GitManager {
const trackingBranch = status.tracking;
const currentBranch = status.current;

const current = await this.resolveRef(currentBranch);
const tracking = await this.resolveRef(trackingBranch);
const current = await this.resolveRef(currentBranch!);
const tracking = await this.resolveRef(trackingBranch!);

return current != tracking;
}
Expand All @@ -365,7 +365,7 @@ export class IsomorphicGit extends GitManager {
const trackingBranch = (await this.getConfig(`branch.${current}.merge`))?.split("refs/heads")[1];


let tracking = trackingBranch ? remote + trackingBranch : undefined;
const tracking = trackingBranch ? remote + trackingBranch : undefined;

return {
current: current,
Expand Down Expand Up @@ -428,7 +428,7 @@ export class IsomorphicGit extends GitManager {
}
}

async setConfig(path: string, value: any): Promise<void> {
async setConfig(path: string, value: string | number | boolean): Promise<void> {
try {
return this.wrapFS(git.setConfig({
...this.getRepo(),
Expand All @@ -439,7 +439,7 @@ export class IsomorphicGit extends GitManager {
this.plugin.displayError(error);
throw error;
}
};
}

async getConfig(path: string): Promise<any> {
try {
Expand All @@ -451,7 +451,7 @@ export class IsomorphicGit extends GitManager {
this.plugin.displayError(error);
throw error;
}
};
}

async fetch(remote?: string): Promise<void> {
const progressNotice = new Notice("Initializing fetch", this.noticeLength);
Expand Down Expand Up @@ -529,10 +529,10 @@ export class IsomorphicGit extends GitManager {
}


async walkDifference({ walkers: walker, dir: base }: { walkers: Walker[]; dir?: string; }): Promise<WalkDifference[]> {
async walkDifference({ walkers, dir: base }: { walkers: Walker[]; dir?: string; }): Promise<WalkDifference[]> {
const res = await this.wrapFS(git.walk({
...this.getRepo(),
trees: walker,
trees: walkers,
map: async function (filepath, [A, B]) {

if (!worthWalking(filepath, base)) {
Expand Down Expand Up @@ -576,7 +576,7 @@ export class IsomorphicGit extends GitManager {
return res;
}

async getStagedFiles(dir: string = "."): Promise<{ vault_path: string, filepath: string; }[]> {
async getStagedFiles(dir = "."): Promise<{ vault_path: string, filepath: string; }[]> {
const res = await this.walkDifference({
walkers: [git.TREE({ ref: "HEAD" }), git.STAGE()],
dir,
Expand All @@ -589,7 +589,7 @@ export class IsomorphicGit extends GitManager {
});
}

async getUnstagedFiles(base: string = "."): Promise<UnstagedFile[]> {
async getUnstagedFiles(base = "."): Promise<UnstagedFile[]> {
const notice = new Notice("Getting status...", this.noticeLength);

try {
Expand All @@ -599,7 +599,7 @@ export class IsomorphicGit extends GitManager {
git.walk({
...repo,
trees: [git.WORKDIR(), git.STAGE()],
map: async function (filepath, [workdir, stage]): Promise<UnstagedFile> {
map: async function (filepath, [workdir, stage]): Promise<UnstagedFile | null | undefined> {
// Ignore ignored files, but only if they are not already tracked.
if (!stage && workdir) {

Expand Down Expand Up @@ -636,7 +636,7 @@ export class IsomorphicGit extends GitManager {
if ((stageType === 'tree' || stageType === 'special') && !isBlob) return;

// Figure out the oids for files, using the staged oid for the working dir oid if the stats match.
const stageOid = stageType === 'blob' ? await stage.oid() : undefined;
const stageOid = stageType === 'blob' ? await stage!.oid() : undefined;
let workdirOid;
if (
workdirType === 'blob' &&
Expand All @@ -645,7 +645,7 @@ export class IsomorphicGit extends GitManager {
// We don't actually NEED the sha. Any sha will do
workdirOid = '42';
} else if (workdirType === 'blob') {
workdirOid = await workdir.oid();
workdirOid = await workdir!.oid();
}
if (!workdirOid) {
return {
Expand Down Expand Up @@ -679,7 +679,7 @@ export class IsomorphicGit extends GitManager {

async getDiffString(filePath: string): Promise<string> {
throw new Error("Method not implemented.");
};
}

private getFileStatusResult(row: [string, 0 | 1, 0 | 1 | 2, 0 | 1 | 2 | 3]): FileStatusResult {

Expand All @@ -692,7 +692,7 @@ export class IsomorphicGit extends GitManager {
vault_path: this.getVaultPath(row[this.FILE])
};

};
}
}

// All because we can't use (for await)...
Expand Down Expand Up @@ -730,6 +730,7 @@ function getIterator(iterable: any) {

async function forAwait(iterable: any, cb: any) {
const iter = getIterator(iterable);
//eslint-disable-next-line no-constant-condition
while (true) {
const { value, done } = await iter.next();
if (value) await cb(value);
Expand Down
2 changes: 1 addition & 1 deletion src/localStorageSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export class LocalStorageSettings {
private prefix: string;
constructor(private readonly plugin: ObsidianGit) {
this.prefix = this.plugin.manifest.id;
};
}

getPassword(): string {
return localStorage.getItem(this.prefix + ":password");
Expand Down
Loading

0 comments on commit c8453b8

Please sign in to comment.