Skip to content

Commit

Permalink
Type more strongly
Browse files Browse the repository at this point in the history
Signed-off-by: Sora Morimoto <[email protected]>
  • Loading branch information
smorimoto committed Apr 7, 2024
1 parent 04fe971 commit d582000
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 20 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ jobs:
- name: Install dependencies
run: pnpm install --frozen-lockfile

- name: Typecheck
run: pnpm typecheck

- name: Test
run: pnpm test -- --coverage

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"prepack": "pnpm build",
"release": "np",
"start": "pnpm build -- --watch",
"test": "vitest run"
"test": "vitest run",
"typecheck": "tsc --noEmit"
},
"dependencies": {
"chalk": "^5.3.0",
Expand Down
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as fs from 'fs/promises';
import { Plugin } from 'vite';
import { execaCommand } from 'execa';
import { npmRunPathEnv } from 'npm-run-path';
import * as stream from 'stream';
import chalk from 'chalk';
import parseCompilerLog from './parseCompilerLog.js';

Expand Down Expand Up @@ -31,7 +32,7 @@ async function launchReScript(

let compileOnce = (_value: unknown) => {};

function dataListener(chunk: any) {
function dataListener(chunk: stream.Readable) {
const output = chunk.toString().trimEnd();
if (!silent) {
// eslint-disable-next-line no-console
Expand Down Expand Up @@ -134,7 +135,7 @@ export default function createReScriptPlugin(config?: Config): Plugin {
);
},
// Hook that resolves `.bs.js` imports to their `.res` counterpart
async resolveId(source, importer, options: any) {
async resolveId(source, importer, options) {
if (source.endsWith('.res')) usingLoader = true;
if (options.isEntry || !importer) return null;
if (!importer.endsWith('.res')) return null;
Expand Down
34 changes: 18 additions & 16 deletions src/parseCompilerLog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ const codeRegex = /^ +([0-9]+| +|\.) (│|┆)/;
const warningErrorRegex = /Warning number \d+ \(configured as error\)/;

// Returns true if the line indicates the start of an error block
function isErrorLine(line: string) {
if (line.startsWith(" We've found a bug for you!")) return true;
if (line.startsWith(' Syntax error!')) return true;
if (warningErrorRegex.test(line)) return true;
function isErrorLine(line: string | undefined) {
if (line?.startsWith(" We've found a bug for you!")) return true;
if (line?.startsWith(' Syntax error!')) return true;
if (line && warningErrorRegex.test(line)) return true;
return false;
}

Expand All @@ -34,7 +34,7 @@ export default function parseCompilerLog(
// Optimization; only parse log when compiler is done
if (lines[lines.length - 1]?.startsWith('#Done(')) {
let foundError = false;
let path = '';
let path: string | undefined;
let startLine = 0;

// There can be additional messages such as hints
Expand All @@ -58,39 +58,41 @@ export default function parseCompilerLog(

// Optimization; the next line is always the file + range, which means it
// can be parsed now and the next iteration (line) can be skipped.
path = lines[i + 1].trim();
path = lines?.[i + 1]?.trim();

// Extract the start line
const match = path.match(fileAndRangeRegex);
if (match) startLine = parseInt(match[2], 10);
const match = path?.match(fileAndRangeRegex);
if (match) startLine = Number(match[2]);

// Skip the next line since it was handled here
i += 1;
} else if (!foundError) {
// Only run below checks once an error has been found
} else if (line.startsWith(' Warning number ')) {
} else if (line?.startsWith(' Warning number ')) {
// Reached the end of the error
break;
} else if (line.startsWith('#Done(')) {
} else if (line?.startsWith('#Done(')) {
// Reached the end of the log file
break;
} else {
// This can now only be code lines or messages
const match = line.match(codeRegex);
const match = line?.match(codeRegex);
if (match) {
// Replace strange vertical bars with regular ones in order to match
// the code frame regex defined in the vite overlay file:
// https://github.com/vitejs/vite/blob/96591bf9989529de839ba89958755eafe4c445ae/packages/vite/src/client/overlay.ts#L116
let codeFrameLine = line.replace('┆', '|').replace('│', '|');
let codeFrameLine = line?.replace('┆', '|').replace('│', '|');

// Since the red color indicator is lost when parsing the log file,
// this adds a pointer (`>`) to the line where the error starts.
if (parseInt(match[1], 10) === startLine) {
codeFrameLine = `> ${codeFrameLine.substring(2)}`;
if (Number(match[1]) === startLine) {
codeFrameLine = `> ${codeFrameLine?.substring(2)}`;
}

frame.push(codeFrameLine);
} else if (line.startsWith(' ')) {
if (codeFrameLine) {
frame.push(codeFrameLine);
}
} else if (line?.startsWith(' ')) {
// It has to be a message by now
messages.push(line.trim());
}
Expand Down
5 changes: 4 additions & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ describe('@jihchi/vite-plugin-rescript', () => {
it('invokes closeBundle hook without crashing', async () => {
const actual = Plugin();
expect(actual).toHaveProperty('closeBundle');
await expect(actual.closeBundle()).resolves.toEqual(undefined);
await expect(
// @ts-expect-error
actual.closeBundle()
).resolves.toEqual(undefined);
});
});

0 comments on commit d582000

Please sign in to comment.