Skip to content

Commit

Permalink
♻️ Require await in async functions
Browse files Browse the repository at this point in the history
This commit enables the ESLint rule `require-await` which requires the
use of `await` in async functions and fixes issues that have been
uncovered by this rule. This rule helps to spot unintentional and
leftover `async` keywords.
  • Loading branch information
ralfstx committed Oct 3, 2024
1 parent 8d060fc commit c3583f2
Show file tree
Hide file tree
Showing 14 changed files with 22 additions and 21 deletions.
1 change: 1 addition & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export default tseslint.config(
'no-else-return': ['error', { allowElseIf: false }],
'dot-notation': 'error',
'one-var': ['error', 'never'],
'require-await': 'error',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/consistent-type-imports': ['error', { prefer: 'type-imports' }],
'simple-import-sort/imports': 'error',
Expand Down
2 changes: 1 addition & 1 deletion src/font-loader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ describe('font-loader', () => {
beforeEach(() => {
testFont = fakeFont('Test');
fontLoader = new FontLoader([]);
fontLoader.loadFont = vi.fn(async (selector: FontSelector) => {
fontLoader.loadFont = vi.fn((selector: FontSelector) => {
if (selector.fontFamily === 'Test') return testFont;
throw new Error('No such font defined');
});
Expand Down
2 changes: 1 addition & 1 deletion src/font-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class FontLoader {
this.#fontDefs = fontDefs;
}

async loadFont(selector: FontSelector): Promise<LoadedFont> {
loadFont(selector: FontSelector): LoadedFont {
if (!this.#fontDefs.length) {
throw new Error('No fonts defined');

Check failure on line 23 in src/font-loader.ts

View workflow job for this annotation

GitHub Actions / build

src/font-loader.test.ts > font-loader > new FontLoader > rejects when no fonts defined

Error: No fonts defined ❯ FontLoader.loadFont src/font-loader.ts:23:13 ❯ src/font-loader.test.ts:33:27
}
Expand Down
6 changes: 3 additions & 3 deletions src/image-loader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ describe('image-loader', () => {

beforeEach(() => {
imageLoader = new ImageLoader([]);
imageLoader.loadImage = vi.fn(async (selector: ImageSelector) => {
if (selector.name === 'liberty') return { data: libertyJpg };
if (selector.name === 'torus') return { data: torusPng };
imageLoader.loadImage = vi.fn((selector: ImageSelector) => {
if (selector.name === 'liberty') return Promise.resolve({ data: libertyJpg });
if (selector.name === 'torus') return Promise.resolve({ data: torusPng });
throw new Error('No such image');
});
});
Expand Down
2 changes: 1 addition & 1 deletion src/image-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class ImageStore {
this.#imageLoader = imageLoader;
}

async selectImage(selector: ImageSelector): Promise<Image> {
selectImage(selector: ImageSelector): Promise<Image> {
const cacheKey = selector.name;
return (this.#imageCache[cacheKey] ??= this.loadImage(selector));
}
Expand Down
4 changes: 2 additions & 2 deletions src/images/jpeg.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import { isJpeg, readJpegInfo } from './jpeg.ts';

describe('jpeg', () => {
describe('isJpeg', () => {
it('returns true for JPEG header', async () => {
it('returns true for JPEG header', () => {
const data = new Uint8Array([0xff, 0xd8, 0xff]);

expect(isJpeg(data)).toBe(true);
});

it('returns false for other data', async () => {
it('returns false for other data', () => {
expect(isJpeg(new Uint8Array())).toBe(false);
expect(isJpeg(new Uint8Array([1, 2, 3]))).toBe(false);
});
Expand Down
4 changes: 2 additions & 2 deletions src/images/png.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import { isPng, readPngInfo } from './png.ts';

describe('png', () => {
describe('isPng', () => {
it('returns true if PNG header found', async () => {
it('returns true if PNG header found', () => {
const info = isPng(new Uint8Array([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]));

expect(info).toBe(true);
});

it('returns false for other data', async () => {
it('returns false for other data', () => {
expect(isPng(new Uint8Array())).toBe(false);
expect(isPng(new Uint8Array([1, 2, 3, 4, 5]))).toBe(false);
});
Expand Down
4 changes: 2 additions & 2 deletions src/layout/layout-columns.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ describe('layout-columns', () => {

beforeEach(() => {
const fontStore = new FontStore(new FontLoader([]));
fontStore.selectFont = async () => {
return fakeFont('Test');
fontStore.selectFont = () => {
return Promise.resolve(fakeFont('Test'));
};
ctx = { fontStore } as MakerCtx;
box = { x: 20, y: 30, width: 400, height: 700 };
Expand Down
4 changes: 2 additions & 2 deletions src/layout/layout-image.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ describe('layout-image', () => {

beforeEach(() => {
const imageStore = new ImageStore(new ImageLoader([]));
imageStore.selectImage = vi.fn(async (selector: ImageSelector) => {
imageStore.selectImage = vi.fn((selector: ImageSelector) => {
const match = /^img-(\d+)-(\d+)$/.exec(selector.name);
if (match) {
return fakeImage(selector.name, Number(match[1]), Number(match[2]));
return Promise.resolve(fakeImage(selector.name, Number(match[1]), Number(match[2])));
}
throw new Error(`Unknown image: ${selector.name}`);
});
Expand Down
2 changes: 1 addition & 1 deletion src/layout/layout-rows.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('layout-rows', () => {

beforeEach(() => {
const fontStore = new FontStore(new FontLoader([]));
fontStore.selectFont = async () => fakeFont('Test');
fontStore.selectFont = () => Promise.resolve(fakeFont('Test'));
ctx = { fontStore } as MakerCtx;
box = { x: 20, y: 30, width: 400, height: 700 };
});
Expand Down
4 changes: 2 additions & 2 deletions src/layout/layout-text.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ describe('layout-text', () => {
defaultFont = fakeFont('Test');
const italicFont = fakeFont('Test', { style: 'italic' });
const fontStore = new FontStore(new FontLoader([]));
fontStore.selectFont = async (selector: FontSelector) => {
return selector.fontStyle === 'italic' ? italicFont : defaultFont;
fontStore.selectFont = (selector: FontSelector) => {
return Promise.resolve(selector.fontStyle === 'italic' ? italicFont : defaultFont);
};
box = { x: 20, y: 30, width: 400, height: 700 };
ctx = { fontStore } as MakerCtx;
Expand Down
2 changes: 1 addition & 1 deletion src/layout/layout.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('layout', () => {

beforeEach(() => {
const fontStore = new FontStore(new FontLoader([]));
fontStore.loadFont = async () => fakeFont('Test');
fontStore.loadFont = () => Promise.resolve(fakeFont('Test'));
ctx = { fontStore } as MakerCtx;
box = { x: 20, y: 30, width: 400, height: 700 };
});
Expand Down
2 changes: 1 addition & 1 deletion src/print-value.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('print-value', () => {

it('prints functions', () => {
expect(printValue((x: unknown) => x)).toEqual('anonymous function');
expect(printValue(async (x: unknown) => x)).toEqual('anonymous function');
expect(printValue((x: unknown) => x)).toEqual('anonymous function');
expect(printValue(printValue)).toEqual('function printValue');
});

Expand Down
4 changes: 2 additions & 2 deletions src/text.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ describe('text', () => {
normalFont = fakeFont('Test');
const italicFont = fakeFont('Test', { style: 'italic' });
fontStore = new FontStore(new FontLoader([]));
fontStore.selectFont = async (selector) => {
return selector.fontStyle === 'italic' ? italicFont : normalFont;
fontStore.selectFont = (selector) => {
return Promise.resolve(selector.fontStyle === 'italic' ? italicFont : normalFont);
};
});

Expand Down

0 comments on commit c3583f2

Please sign in to comment.