Skip to content

Commit

Permalink
Enable linting with type information.
Browse files Browse the repository at this point in the history
  • Loading branch information
SyntaxColoring committed Nov 25, 2023
1 parent 2e818d2 commit 0af9f65
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 20 deletions.
6 changes: 5 additions & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ module.exports = {
env: { browser: true, es2020: true },
extends: [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:@typescript-eslint/recommended-type-checked",
"plugin:react-hooks/recommended",
"prettier", // Comes last, to turn off rules from above that conflict with Prettier.
],
ignorePatterns: ["dist", ".eslintrc.cjs"],
parser: "@typescript-eslint/parser",
parserOptions: {
project: ["./tsconfig.json", "./tsconfig.node.json"],
tsconfigRootDir: __dirname,
},
plugins: ["react-refresh"],
rules: {
"react-refresh/only-export-components": [
Expand Down
4 changes: 4 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ function useIndex(file: File | null): IndexState {
};

if (file) {
// TODO: Handle errors from this floating promise.
// eslint-disable-next-line @typescript-eslint/no-floating-promises
buildIndex(file, debounceProgress(handleProgress)).then((index) => {
if (!ignore) setIndexState({ status: "indexed", index });
});
Expand Down Expand Up @@ -170,6 +172,8 @@ function useSearch(indexState: IndexState, query: string): SearchResult {
}
}
};
// TODO: Handle errors from this floating promise.
// eslint-disable-next-line @typescript-eslint/no-floating-promises
doSearch();
}
return () => {
Expand Down
12 changes: 10 additions & 2 deletions src/ResourceMonitor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@ import * as React from "react";

const MEASUREMENT_INTERVAL = 2000;

// Inform TypeScript of the not-universally-supported performance.memory API.
// https://www.typescriptlang.org/docs/handbook/namespaces.html#working-with-other-javascript-libraries.
// Unclear why the linter is complaining about this.
// eslint-disable-next-line @typescript-eslint/no-namespace
declare namespace performance {
const memory: {
usedJSHeapSize: number;
};
}

export function ResourceMonitor(): JSX.Element {
const [bytes, setBytes] = React.useState<number | null>(null);
React.useEffect(() => {
const interval = window.setInterval(() => {
// @ts-expect-error performance.memory is Chrome-only.
if (performance.memory) {
// @ts-expect-error performance.memory is Chrome-only.
setBytes(performance.memory.usedJSHeapSize);
}
}, MEASUREMENT_INTERVAL);
Expand Down
2 changes: 1 addition & 1 deletion src/journalParsing/jsonRecordSplitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ interface SplitLine {
}

export interface ParsedJSON {
parsedJSON: object;
parsedJSON: unknown;
beginByteIndex: number;
endByteIndex: number;
}
Expand Down
2 changes: 1 addition & 1 deletion src/journalParsing/parseNative.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ test.each(testCases)(
}
expect(events).toStrictEqual(expectedEvents);
} finally {
file.close();
await file.close();
}
},
);
5 changes: 1 addition & 4 deletions src/journalParsing/parseNative.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,7 @@ async function readNewline(bytes: AsyncIterator<number>): Promise<void> {

async function readFieldName(
bytes: AsyncIterable<number>,
): Promise<
| "gracefulEOF"
| { fieldName: string; termination: typeof EQUALS | typeof NEWLINE }
> {
): Promise<"gracefulEOF" | { fieldName: string; termination: number }> {
const bytesToDecode: number[] = [];

for await (const byte of bytes) {
Expand Down
23 changes: 12 additions & 11 deletions src/logAccess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,19 +114,20 @@ export async function buildIndex(

function parseEntry(parsed: ParsedJSON): LogEntry {
// TODO: We should probably validate this. The input file is untrusted.
const json = parsed.parsedJSON;
// @ts-expect-error JSON object is untyped.
const epochMicroseconds = parseInt(json["__REALTIME_TIMESTAMP"]);
const json = parsed.parsedJSON as {
__REALTIME_TIMESTAMP: string;
PRIORITY: string;
_SYSTEMD_UNIT: string;
SYSLOG_IDENTIFIER: string;
MESSAGE: string;
};
const epochMicroseconds = parseInt(json.__REALTIME_TIMESTAMP);
return {
timestamp: new Date(epochMicroseconds / 1000),
// @ts-expect-error JSON object is untyped.
priority: parseInt(json["PRIORITY"]),
// @ts-expect-error JSON object is untyped.
unit: json["_SYSTEMD_UNIT"], // TODO: Also support _SYSTEMD_USER_UNIT?
// @ts-expect-error JSON object is untyped.
syslogIdentifier: json["SYSLOG_IDENTIFIER"],
// @ts-expect-error JSON object is untyped.
message: json["MESSAGE"],
priority: parseInt(json.PRIORITY),
unit: json._SYSTEMD_UNIT, // TODO: Also support _SYSTEMD_USER_UNIT?
syslogIdentifier: json.SYSLOG_IDENTIFIER,
message: json.MESSAGE,
};
}

Expand Down

0 comments on commit 0af9f65

Please sign in to comment.