Skip to content

Commit

Permalink
Setup eslint
Browse files Browse the repository at this point in the history
Enabled default rules plus `require-await` to catch any promises that
are left un-awaited.
  • Loading branch information
banga committed Jul 5, 2024
1 parent c6fd6cc commit 7774067
Show file tree
Hide file tree
Showing 11 changed files with 1,218 additions and 501 deletions.
14 changes: 14 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import globals from 'globals';
import pluginJs from '@eslint/js';
import tseslint from 'typescript-eslint';

/** @type {import('eslint').Linter.FlatConfig[]} */
const config = [
{ files: ['src/**/*.ts'], ignores: ['build/**/*'] },
{ languageOptions: { globals: globals.node } },
{ rules: { 'require-await': 'error' } },
pluginJs.configs.recommended,
...tseslint.configs.recommended,
];

export default config;
1,630 changes: 1,162 additions & 468 deletions package-lock.json

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,17 @@
},
"scripts": {
"clean": "rimraf './build'",
"lint": "cross-env ./node_modules/.bin/eslint src scripts",
"build": "npm run clean && node ./scripts/build.mjs",
"build:dev": "npm run build -- --watch",
"build:publish": "npm run build -- --prod",
"test": "cross-env ./node_modules/.bin/tsc && cross-env NODE_OPTIONS=\"--experimental-vm-modules --no-warnings\" ./node_modules/.bin/jest",
"previewTheme": "nodemon -q build/previewTheme.mjs",
"benchmark": "npm run build && node build/benchmark.mjs",
"prepublishOnly": "npm run build:publish && npm run test"
"prepublishOnly": "npm run lint && npm run build:publish && npm run test"
},
"devDependencies": {
"@eslint/js": "^9.6.0",
"@types/benchmark": "^2.1.0",
"@types/diff": "^5.0.0",
"@types/jest": "^29.5.1",
Expand All @@ -53,12 +55,15 @@
"benchmark": "^2.1.4",
"cross-env": "^7.0.3",
"esbuild": "^0.17.18",
"eslint": "^9.6.0",
"globals": "^15.8.0",
"jest": "^29.7.0",
"nodemon": "^2.0.7",
"prettier": "^2.2.1",
"rimraf": "^5.0.5",
"ts-jest": "^29.1.0",
"typescript": "^5.0.4"
"typescript": "^5.0.4",
"typescript-eslint": "^7.15.0"
},
"packageManager": "npm@9.8.1"
}
32 changes: 16 additions & 16 deletions src/__snapshots__/index.test.ts.snap

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions src/getGitConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const DEFAULT_THEME_NAME = 'dark';

const GIT_CONFIG_KEY_PREFIX = 'split-diffs';
const GIT_CONFIG_LINE_REGEX = new RegExp(
`${GIT_CONFIG_KEY_PREFIX}\.([^=]+)=(.*)`
`${GIT_CONFIG_KEY_PREFIX}\\.([^=]+)=(.*)`
);

function extractFromGitConfigString(configString: string) {
Expand All @@ -36,7 +36,9 @@ export function getGitConfig(configString: string): GitConfig {
if (!isNaN(parsedMinLineWidth)) {
minLineWidth = parsedMinLineWidth;
}
} catch {}
} catch {
// Ignore invalid values
}

return {
MIN_LINE_WIDTH: minLineWidth,
Expand Down
9 changes: 5 additions & 4 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const TEST_THEME = Object.fromEntries(
) as Theme;

const replaceColoredText =
// eslint-disable-next-line @typescript-eslint/no-unused-vars
(r: number, g: number, b: number) => (text: string) =>
text.replace(/./g, '░');

Expand Down Expand Up @@ -164,7 +165,7 @@ index 9f14e96..eaf3730 100644
).toMatchSnapshot();
});

test('commit with deletion', async function () {
test('commit with deletion', function () {
expect(`commit eccfb5a2b3d76ba53df315f977da74b18d50113e
Author: Shrey Banga <shrey@quip.com>
Date: Thu Aug 22 10:07:25 2019 -0700
Expand Down Expand Up @@ -316,7 +317,7 @@ index 0000000..6499edf
@@ -0,0 +1,2 @@
+node_modules/**
+build/**
\ No newline at end of file`)
\\ No newline at end of file`)
).toMatchSnapshot();
});

Expand Down Expand Up @@ -380,7 +381,7 @@ index ca15c64..0000000
- end tell
- end timeout
-end run
\ No newline at end of file`)
\\ No newline at end of file`)
).toMatchSnapshot();
});

Expand Down Expand Up @@ -465,7 +466,7 @@ index d88c464..6901818 100644
+++ b/file1
@@ -1 +1,2 @@
-This is file1
\ No newline at end of file
\\ No newline at end of file
+This is file1
+Experimental change
diff --git a/file2 b/file2
Expand Down
9 changes: 5 additions & 4 deletions src/iterFormatHunkUnified.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ export async function* iterFormatUnifiedDiffHunkUnified(
): AsyncIterable<FormattedString> {
const lineWidth = context.SCREEN_WIDTH;

let [
{ fileName: fileNameA, lines: hunkLinesA, startLineNo: lineNoA },
{ fileName: fileNameB, lines: hunkLinesB, startLineNo: lineNoB },
const [
{ fileName: fileNameA, lines: hunkLinesA },
{ fileName: fileNameB, lines: hunkLinesB },
] = hunkParts;
let [{ startLineNo: lineNoA }, { startLineNo: lineNoB }] = hunkParts;

let indexA = 0,
indexB = 0;
Expand Down Expand Up @@ -108,7 +109,7 @@ export async function* iterFormatCombinedDiffHunkUnified(

// The final hunk part shows the current state of the file, so we just
// display that with additions and deletions highlighted.
let { fileName, lines, startLineNo } = hunkParts[hunkParts.length - 1];
const { fileName, lines, startLineNo } = hunkParts[hunkParts.length - 1];
let lineNo = startLineNo;
let numDeletes = 0;

Expand Down
2 changes: 1 addition & 1 deletion src/themes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ function hexToRgba(hex: string): ColorRgba {
hex = hex.slice(1);

let hexNo = parseInt(hex, 16);
let bits = hex.length === 3 ? 4 : 8;
const bits = hex.length === 3 ? 4 : 8;

let a = 255;
if (hex.length === 8) {
Expand Down
2 changes: 1 addition & 1 deletion src/transformContentsStreaming.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { iterReplaceTabsWithSpaces } from './iterReplaceTabsWithSpaces';
import { iterSideBySideDiffs } from './iterSideBySideDiffs';
import { iterWithNewlines } from './iterWithNewlines';

export async function transformContentsStreaming(
export function transformContentsStreaming(
context: Context,
input: Readable,
output: Writable
Expand Down
4 changes: 2 additions & 2 deletions src/zip.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export function* zip<T extends Array<any>>(
export function* zip<T extends Array<unknown>>(
...iterables: { [K in keyof T]: Iterable<T[K]> }
): Iterable<{ [K in keyof T]: T[K] | undefined }> {
const iterators = iterables.map((iterable) => iterable[Symbol.iterator]());
Expand All @@ -18,7 +18,7 @@ export function* zip<T extends Array<any>>(
}
}

export async function* zipAsync<T extends Array<any>>(
export async function* zipAsync<T extends Array<unknown>>(
...iterables: { [K in keyof T]: AsyncIterable<T[K]> }
): AsyncIterable<{ [K in keyof T]: T[K] | undefined }> {
const iterators = iterables.map((iterable) =>
Expand Down
2 changes: 1 addition & 1 deletion todo.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@
- [ ] Support custom themes
- [x] Calculate correct display widths, e.g. https://github.com/microsoft/MS-DOS/commit/29a0f9c130b6883080046b830e27403a289de72d
- [ ] Add a theme without background colors
- [ ] Add eslint rule to check for async/await gotchas
- [x] Add eslint rule to check for async/await gotchas

0 comments on commit 7774067

Please sign in to comment.