Skip to content

Commit

Permalink
Fix or ignore eslint/type issues
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeSidSmith committed Jun 23, 2024
1 parent cad02ed commit f930f98
Show file tree
Hide file tree
Showing 12 changed files with 39 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/@types/mustachex.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ declare module 'mustachex' {
export const express: (
path: string,
options: object,
callback: (e: any, rendered?: string) => void
callback: (e: unknown, rendered?: string) => void
) => void;
}
4 changes: 3 additions & 1 deletion src/challenges/c10-array-flatten/solution.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
declare function play(n: any[][]): any[];
import { Primitive } from '../types.js';

declare function play(n: Primitive[][]): Primitive[];

export default play;
2 changes: 1 addition & 1 deletion src/challenges/c6-binary-tree/solution.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ declare function play(root: Node): number;
declare interface Node {
a?: Node;
b?: Node;
c?: any; // Evilness
c?: Node;
}

export default play;
12 changes: 9 additions & 3 deletions src/challenges/c9-object-merge/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import type { Challenge } from '../types.js';
import type { Challenge, Primitive } from '../types.js';

const challenge: Challenge<
[input: [Record<string, any>, Record<string, any>, Record<string, any>]],
Record<string, any>
[
input: [
Record<string, Primitive>,
Record<string, Primitive>,
Record<string, Primitive>,
],
],
Record<string, Primitive>
> = {
title: 'Object Merge',
description: `Write a function that takes an array containing three objects and merges them together, returning the single merged object. The three objects passed in (including any child objects) will not contain keys of the same name.`,
Expand Down
6 changes: 5 additions & 1 deletion src/challenges/c9-object-merge/solution.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
declare function play(n: Object[]): Object;
import { Primitive } from '../types.js';

declare function play(
n: Record<string, Primitive>[]
): Record<string, Primitive>;

export default play;
2 changes: 1 addition & 1 deletion src/challenges/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ export interface Challenge<
example: Example<A, R>;
assertions: readonly Assertion<A, R>[];
assertRules?: (playString: string) => void;
context?: Record<string, any>;
context?: Record<string, unknown>;
timeLimitMinutes?: number;
}
4 changes: 4 additions & 0 deletions src/challenges/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,25 @@ export const getChallenges = async (challengeDir: string) => {
}

if (!fs.existsSync(solutionPath)) {
// eslint-disable-next-line no-console
console.error(`No solution at ${solutionPath}`);
return null;
}

if (!fs.lstatSync(solutionPath).isFile()) {
// eslint-disable-next-line no-console
console.error(`Solution was not a file at ${solutionPath}`);
return null;
}

if (!fs.existsSync(detailsPath)) {
// eslint-disable-next-line no-console
console.log(`No challenge details at ${detailsPath}`);
return null;
}

if (!fs.lstatSync(detailsPath).isFile()) {
// eslint-disable-next-line no-console
console.log(`Challenge details was not a file at ${detailsPath}`);
return null;
}
Expand Down
7 changes: 4 additions & 3 deletions src/game/game-verifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import * as url from 'url';
import type { VerifyJob } from './verifier.js';
import * as game from './game.js';
import { challenges } from '../challenges/index.js';
import { Primitive } from '../challenges/types.js';

const __dirname = url.fileURLToPath(new URL('.', import.meta.url));

const MAX_RUNTIME = 10000;

export const verify = (
file: string,
callback: (res: { valid: false; err: string }) => void
callback: (res: { valid: boolean; err: string }) => void
) => {
const currentGame = game.getOrError();
const verifier = child_process.fork(path.resolve(__dirname, 'verifier.ts'));
Expand All @@ -22,7 +23,7 @@ export const verify = (
throw new Error('Challenge not found');
}

const job: VerifyJob<any, any> = {
const job: VerifyJob<readonly Primitive[], Primitive> = {
file,
challenge,
};
Expand All @@ -38,7 +39,7 @@ export const verify = (
});
}, MAX_RUNTIME);

verifier.on('message', (result: any) => {
verifier.on('message', (result: { valid: boolean; err: string }) => {
clearTimeout(timer);
return callback({ valid: result.valid, err: result.err });
});
Expand Down
4 changes: 3 additions & 1 deletion src/game/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Primitive } from '../challenges/types.js';

export const formatValue = (value: unknown): string => {
if (typeof value === 'undefined') {
return 'undefined';
Expand Down Expand Up @@ -32,7 +34,7 @@ export const formatValue = (value: unknown): string => {
return `Unknown value: ${value}`;
};

export const formatTypeAndValue = (value: unknown, actual: any) => {
export const formatTypeAndValue = (value: unknown, actual: Primitive) => {
if (value === null) {
return 'null';
}
Expand Down
10 changes: 5 additions & 5 deletions src/game/verifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const hasKey = <K extends string>(
key: K
): obj is { [P in K]: unknown } => key in obj;

process.on('message', (entry: VerifyJob<any, any>) => {
process.on('message', (entry: VerifyJob<readonly Primitive[], Primitive>) => {
try {
const script = readFileSync(entry.file, 'utf8');
const context: { play?: unknown; module: { exports: unknown } } = {
Expand All @@ -35,9 +35,8 @@ process.on('message', (entry: VerifyJob<any, any>) => {
"'use strict'\n" +
script +
'\ntry{if (!module.exports.play) {module.exports.play = play}} catch(e) {}';
console.log(toRun);

runInNewContext(toRun, context);
console.log(context);

const play =
typeof context.module.exports === 'function'
Expand All @@ -63,7 +62,7 @@ process.on('message', (entry: VerifyJob<any, any>) => {
const expected =
typeof assertion.output === 'function'
? assertion.output(
play as (...args: readonly any[]) => any,
play as (...args: readonly Primitive[]) => Primitive,
assertion.input
)
: assertion.output;
Expand Down Expand Up @@ -96,7 +95,8 @@ process.on('message', (entry: VerifyJob<any, any>) => {

process.send?.({ valid: true });
} catch (err) {
console.error('verifier failed with error:', err);
// eslint-disable-next-line no-console
console.error('Verifier failed with error:', err);

process.send?.({
err: 'Your script contains an error',
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const __dirname = url.fileURLToPath(new URL('.', import.meta.url));
const PORT = 1122;

if (!process.env['CG_ADMIN_PASSWORD']) {
// eslint-disable-next-line no-console
console.error('Please set the CG_ADMIN_PASSWORD environment variable');
process.exit(1);
}
Expand All @@ -28,4 +29,5 @@ app.use(admin);

app.listen(PORT);

// eslint-disable-next-line no-console
console.log(`Server running at http://localhost:${PORT}`);
2 changes: 1 addition & 1 deletion src/routes/play.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ app.get('/solution/:key', (req, res) => {
});

app.post('/submit', (req, res) => {
const redirect = (result: Partial<game.Entry>, err: {}) => {
const redirect = (result: Partial<game.Entry>, err: string) => {
const url = [
'/?email=',
result.email,
Expand Down

0 comments on commit f930f98

Please sign in to comment.