Skip to content

Commit 4fe6211

Browse files
authored
Lint no floating promises 2 (#370)
* await creating the default list * for completeness: also assert organization * lint for await * ooops - put the eslint config for ts in the right place * eslint should ignore .mailing directories * add tsconfig's to parserOptions.project in @typescript-eslint/parser block * await async functions * don't ignore __test__ from tsconfig in core * linter says that this expect must be awaited * tsconfig covers all the .ts files in the root directory: jest.config.ts testSetup.integration.ts testUtilIntegration.ts jest.integration.config.ts testSetup.ts * tsconfig should include e2e/**/* * src/cli/generator_templates/**/* is not a directory in packages/cli * await some async functions * we're using plain js in e2e/mailing_tests so don't need a tsconfig * fix typo * await analytics calls * eslintignore these 2 files: packages/cli/src/generator_templates packages/cli/src/emails because they are ignored in tsconfig.json * update files attribute in package.json * don't actually want to ignore those * this is my workaround for typescript-eslint/typescript-eslint#2987 * noop? * simplify tsconfigs? * fix all the lints * oops * try void * async await * smaller change * fix server build * Revert "fix server build" This reverts commit e24e0ed. * await -> void in init * remove trailing comma in json * cypress does not actually need to reset the web db, so remove * server: try cd into .mailing and then npx next build with the current directory * pull posthog api key into its own file so it does not bloat the next build by pulling in posthog-node * import from the righ tplaÿce
1 parent e155434 commit 4fe6211

30 files changed

+65
-61
lines changed

.eslintrc.json

+10
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,23 @@
3535
{
3636
"files": ["**/*.ts", "**/*.tsx"],
3737
"parser": "@typescript-eslint/parser",
38+
"parserOptions": {
39+
"project": [
40+
"./tsconfig.json",
41+
"./packages/cli/tsconfig.json",
42+
"./packages/core/tsconfig.json",
43+
"./packages/web/tsconfig.json",
44+
"./packages/cli/cypress/tsconfig.json"
45+
]
46+
},
3847
"plugins": ["@typescript-eslint"],
3948
"extends": [
4049
"eslint:recommended",
4150
"plugin:@typescript-eslint/recommended"
4251
],
4352
"rules": {
4453
"no-unused-vars": "off",
54+
"@typescript-eslint/no-floating-promises": "error",
4555
"@typescript-eslint/no-explicit-any": "off",
4656
"@typescript-eslint/no-unused-vars": [
4757
"error",

.husky/pre-commit

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#!/usr/bin/env sh
22
. "$(dirname "$0")/_/husky.sh"
33

4+
yarn lint:unlink
45
yarn lint-staged
6+
yarn lint:link

.lintstagedrc

-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,3 @@
33
"*.rb": "rubocop --autocorrect",
44
"packages/cli/src/generator_templates/ts/**/*.{ts,tsx}": "scripts/build-js-templates"
55
}
6-

e2e/mailing_tests/cypress/tsconfig.json

-8
This file was deleted.

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
"packages/*"
1010
],
1111
"scripts": {
12-
"lint": "eslint .",
12+
"lint": "yarn lint:unlink; eslint .; yarn lint:link",
13+
"lint:unlink": "rm packages/cli/src/emails",
14+
"lint:link": "ln -s 'generator_templates/ts/emails/' packages/cli/src/emails",
1315
"dev": "cd packages/cli && yarn dev",
1416
"dev:js": "yarn link:emails:js && yarn dev",
1517
"dev:init": "cd packages/cli && rm -rf previews_html; rm -rf .mailing emails mailing.config.json; src/dev.js",

packages/cli/cypressIntegration.config.ts

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { defineConfig } from "cypress";
22
import cliPrisma from "./prisma";
3-
import webPrisma from "../../packages/web/prisma";
4-
import type { PrismaTableName } from "../../testUtilIntegration";
3+
4+
interface PrismaTableName {
5+
table_name: string;
6+
}
57

68
const resetDb = async () => {
79
await truncateDatabases();
@@ -31,12 +33,8 @@ export async function truncateCliDatabase() {
3133
await truncateTables(cliPrisma);
3234
}
3335

34-
export async function truncateWebDatabase() {
35-
await truncateTables(webPrisma);
36-
}
37-
3836
export async function truncateDatabases() {
39-
return Promise.all([truncateCliDatabase(), truncateWebDatabase()]);
37+
await truncateCliDatabase();
4038
}
4139

4240
async function truncateTables(client: any) {

packages/cli/src/commands/init.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export const handler = buildHandler(
8686
try {
8787
const url = `${getMailingAPIBaseURL()}/api/newsletterSubscribers`;
8888

89-
fetch(url, {
89+
void fetch(url, {
9090
method: "POST",
9191
headers: { "Content-Type": "application/json" },
9292
body: JSON.stringify({ email }),
@@ -109,7 +109,7 @@ export const handler = buildHandler(
109109
_: argv._,
110110
};
111111

112-
previewHandler(previewHandlerArgv);
112+
await previewHandler(previewHandlerArgv);
113113
},
114114
{
115115
captureOptions: () => {

packages/cli/src/commands/preview/server/__integration__/livereload.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ describe("livereload", () => {
3434
done();
3535
});
3636

37-
touchTemplate();
37+
void touchTemplate();
3838
});
3939
});
4040
});

packages/cli/src/commands/preview/server/livereload.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export function startChangeWatcher(server: Server, emailsDir: string) {
5050
if (WATCH_IGNORE.test(filename)) return;
5151
log(`detected ${eventType} on ${filename}, reloading`);
5252
delete require.cache[resolve(changeWatchPath, filename)];
53-
reload();
53+
void reload();
5454
}
5555
);
5656

packages/cli/src/commands/server.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export const handler = buildHandler(
6666
stdio: "inherit",
6767
});
6868

69-
execSync("npx next build .mailing", {
69+
execSync("cd .mailing && npx next build", {
7070
stdio: "inherit",
7171
});
7272
}

packages/cli/src/components/IndexPane/hooks/usePreviewTree.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export type TreeRoute = {
1313

1414
const safeReplaceState = debounce((router: NextRouter | null, path: string) => {
1515
try {
16-
router?.replace(path, undefined, { shallow: true });
16+
void router?.replace(path, undefined, { shallow: true });
1717
} catch (e) {
1818
// Debounce should be avoiding this error, but catch just in case:
1919
// SecurityError: Attempt to use history.replaceState() more than 100 times per 30 seconds

packages/cli/src/components/PosthogScript.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { POSTHOG_API_KEY } from "../util/postHog/client";
1+
import { POSTHOG_API_KEY } from "../util/postHog/posthogApiKey";
22

33
export default function PosthogScript() {
44
if (process.env.NODE_ENV !== "production") {

packages/cli/src/components/PreviewViewer.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ const PreviewViewer: React.FC<PreviewViewerProps> = ({ initialData }) => {
4949

5050
const fetchData = useCallback(async () => {
5151
// fire this one async, no loader
52-
fetchPreviews();
52+
await fetchPreviews();
5353

5454
if (!(previewClass && previewFunction)) {
5555
setData((data: Data) => ({

packages/cli/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const config = existsSync(MAILING_CONFIG_FILE)
1313
: {};
1414

1515
// prettier-ignore
16-
yargs(process.argv.slice(2))
16+
void yargs(process.argv.slice(2))
1717
.config(config)
1818
.command(init)
1919
.command(preview)

packages/cli/src/pages/api/lists/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ const ApiLists = withSessionAPIRoute(async function (
7171

7272
switch (req.method) {
7373
case "GET":
74-
handleGetLists(user, req, res);
74+
await handleGetLists(user, req, res);
7575
break;
7676
case "POST":
77-
handleCreateList(user, req, res);
77+
await handleCreateList(user, req, res);
7878
break;
7979
default:
8080
return res.status(404).end();

packages/cli/src/pages/api/pageSnap.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export default async function PageSnap(
2424

2525
// Create a page with the Open Graph image size best practice
2626
const page = await context.newPage();
27-
page.setViewportSize({ width: 1200, height: 630 });
27+
await page.setViewportSize({ width: 1200, height: 630 });
2828
await page.goto(url, { timeout: 15 * 1000, waitUntil: "networkidle" });
2929
// Press Command+Period to go full screen
3030
await page.keyboard.press("Meta+.");

packages/cli/src/pages/intercepts/[interceptId].tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const ShowIntercept = () => {
1010
setData(await res.json());
1111
}, [query.interceptId]);
1212
useEffect(() => {
13-
if (query.interceptId) fetchData();
13+
if (query.interceptId) void fetchData();
1414
}, [query.interceptId, fetchData]);
1515

1616
return <Intercept data={data} />;

packages/cli/src/pages/unsubscribe/[memberId].tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,11 @@ const Unsubscribe = (props: UnsubscribeProps) => {
221221

222222
// the "Subscribe" or "Unsubscribe" button was clicked on the form with only the default list
223223
const subscribeUnsubscribeButtonClick = useCallback(
224-
(e: React.MouseEvent<HTMLElement>) => {
224+
async (e: React.MouseEvent<HTMLElement>) => {
225225
e.preventDefault();
226226
setFormSaving(true);
227227
const newFormState = onChange(defaultList.id, true)();
228-
serializeAndSubmitForm(newFormState);
228+
await serializeAndSubmitForm(newFormState);
229229
},
230230
[defaultList.id, onChange, serializeAndSubmitForm]
231231
);

packages/cli/src/util/analytics/__test__/analytics.test.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ describe("analytics", () => {
2222
});
2323

2424
describe("#trackMany", () => {
25-
it("should call fetch with the correct arguments", () => {
26-
analytics.trackMany([
25+
it("should call fetch with the correct arguments", async () => {
26+
await analytics.trackMany([
2727
{ event: "test.event.one", properties: { foo: "bar.one" } },
2828
{ event: "test.event.two", properties: { foo: "bar.two" } },
2929
]);
@@ -75,8 +75,11 @@ describe("analytics", () => {
7575
});
7676

7777
describe("#track", () => {
78-
it("should call fetch with the correct arguments", () => {
79-
analytics.track({ event: "test.event", properties: { foo: "bar" } });
78+
it("should call fetch with the correct arguments", async () => {
79+
await analytics.track({
80+
event: "test.event",
81+
properties: { foo: "bar" },
82+
});
8083
expect(fetch).toHaveBeenCalledTimes(2);
8184

8285
// Axiom call

packages/cli/src/util/analytics/providers/__test__/Axiom.test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ describe("Axiom", () => {
1010
});
1111

1212
describe("#trackMany", () => {
13-
it("should call fetch with the correct arguments", () => {
14-
axiom.trackMany([
13+
it("should call fetch with the correct arguments", async () => {
14+
await axiom.trackMany([
1515
{ event: "test.event.one", properties: { foo: "bar.one" } },
1616
{ event: "test.event.two", properties: { foo: "bar.two" } },
1717
]);
@@ -40,8 +40,8 @@ describe("Axiom", () => {
4040
});
4141

4242
describe("#track", () => {
43-
it("should call fetch with the correct arguments", () => {
44-
axiom.track({ event: "test.event", properties: { foo: "bar" } });
43+
it("should call fetch with the correct arguments", async () => {
44+
await axiom.track({ event: "test.event", properties: { foo: "bar" } });
4545
expect(fetch).toHaveBeenCalledTimes(1);
4646
expect(fetch).toHaveBeenCalledWith(
4747
"https://cloud.axiom.co/api/v1/datasets/datasetName/ingest",

packages/cli/src/util/analytics/providers/__test__/Posthog.test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ describe("Posthog", () => {
1010
});
1111

1212
describe("#trackMany", () => {
13-
it("should call fetch with the correct arguments", () => {
14-
posthog.trackMany([
13+
it("should call fetch with the correct arguments", async () => {
14+
await posthog.trackMany([
1515
{ event: "test.event.one", properties: { foo: "bar.one" } },
1616
{ event: "test.event.two", properties: { foo: "bar.two" } },
1717
]);
@@ -39,8 +39,8 @@ describe("Posthog", () => {
3939
});
4040

4141
describe("#track", () => {
42-
it("should call fetch with the correct arguments", () => {
43-
posthog.track({ event: "test.event", properties: { foo: "bar" } });
42+
it("should call fetch with the correct arguments", async () => {
43+
await posthog.track({ event: "test.event", properties: { foo: "bar" } });
4444
expect(fetch).toHaveBeenCalledTimes(1);
4545
expect(fetch).toHaveBeenCalledWith("https://app.posthog.com/capture/", {
4646
method: "POST",

packages/cli/src/util/buildHandler.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type BuildHandlerOptions = {
1616
};
1717

1818
export function buildHandler(
19-
handler: (argv: any) => void,
19+
handler: (argv: any) => Promise<void>,
2020
options: BuildHandlerOptions
2121
) {
2222
return async (argv: any) => {
@@ -51,7 +51,7 @@ export function buildHandler(
5151

5252
await handler(argv);
5353
} finally {
54-
shutdownAnalytics();
54+
await shutdownAnalytics();
5555
}
5656
};
5757
}

packages/cli/src/util/postHog/client.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { PostHog } from "posthog-node";
2-
3-
export const POSTHOG_API_KEY = process.env.POSTHOG_API_KEY;
2+
import { POSTHOG_API_KEY } from "./posthogApiKey";
43

54
let client: PostHog | undefined;
65

Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const POSTHOG_API_KEY = process.env.POSTHOG_API_KEY;

packages/cli/tsconfig.json

+1-3
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,9 @@
2121
"baseUrl": "./"
2222
},
2323
"$schema": "https://json.schemastore.org/tsconfig",
24-
"include": ["src/**/*.ts", "src/**/*.tsx", "prisma/**/*.ts"],
24+
"include": ["./*.ts", "src/**/*.ts", "src/**/*.tsx", "prisma/**/*.ts"],
2525
"exclude": [
26-
"src/emails/**/*",
2726
"dist/**/*",
28-
"src/cli/generator_templates/**/*",
2927
"node_modules"
3028
]
3129
}

packages/core/src/__test__/index.test.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ describe("index", () => {
3434
});
3535

3636
describe("subject", () => {
37-
it("throws an error if missing subject", () => {
37+
it("throws an error if missing subject", async () => {
3838
const sendMail = buildSendMail({
3939
transport,
4040
defaultFrom: "[email protected]",
@@ -50,7 +50,7 @@ describe("index", () => {
5050
html: "ok",
5151
});
5252

53-
expect(callSendMail()).rejects.toThrowError(
53+
await expect(callSendMail()).rejects.toThrowError(
5454
"sendMail couldn't find a subject for your email"
5555
);
5656
});
@@ -500,7 +500,7 @@ describe("index", () => {
500500
subject: "hello",
501501
});
502502

503-
expect(callSendMail).rejects.toThrowError(
503+
await expect(callSendMail).rejects.toThrowError(
504504
"Templates sent to a list must include an unsubscribe link. Add an unsubscribe link or remove the list parameter from your sendMail call."
505505
);
506506
});

packages/core/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ export function buildSendMail<T>(options: BuildSendMailOptions<T>) {
173173
});
174174
if (response.status === 201) {
175175
const { id } = (await response.json()) as { id: string };
176-
open(`${PREVIEW_SERVER_URL}/${id}`);
176+
await open(`${PREVIEW_SERVER_URL}/${id}`);
177177
} else {
178178
error(`Error hitting ${PREVIEW_SERVER_URL}`);
179179
error(response);

packages/core/tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@
2121
},
2222
"$schema": "https://json.schemastore.org/tsconfig",
2323
"include": ["src/**/*"],
24-
"exclude": ["src/**/__test__/*", "dist/**/*"]
24+
"exclude": ["dist", "node_modules"]
2525
}

packages/web/tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@
1616
"incremental": true
1717
},
1818
"include": ["global.d.ts", "next-env.d.ts", "**/*.ts", "**/*.tsx"],
19-
"exclude": ["node_modules"]
19+
"exclude": ["dist", "node_modules"]
2020
}

tsconfig.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@
1717
},
1818
"$schema": "https://json.schemastore.org/tsconfig",
1919
"include": [
20+
"./*.ts",
2021
"packages/*/src/**/*",
2122
"packages/web/**/*",
2223
"testSetup.ts",
23-
"emails/**/*"
24+
"emails/**/*",
25+
"e2e"
2426
],
2527
"exclude": [
2628
"packages/**/__test__/*",
2729
"packages/**/__integration__/*",
28-
"packages/*/src/emails/**/*",
29-
"packages/*/dist/**/*",
30-
"packages/cli/src/generator_templates/**/*"
30+
"packages/*/dist/**/*"
3131
],
3232
"ts-node": {
3333
"files": true

0 commit comments

Comments
 (0)