Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add initial guidelines for JavaScript and TypeScript #11

Merged
merged 12 commits into from
Jul 27, 2023

Conversation

mcmire
Copy link
Contributor

@mcmire mcmire commented Oct 11, 2022

These were copied from the notes that I've been keeping in Notion (see "Code Guidelines"). I tried to list best practices that I thought were already being followed in some form, so hopefully none of them should be too controversial.

Fixes #10.

👉🏻 See rendered JavaScript guidelines 👈🏻
👉🏻 See rendered TypeScript guidelines 👈🏻

@mcmire mcmire changed the title WIP: Add guides for JavaScript and TypeScript Add guides for JavaScript and TypeScript Oct 12, 2022
@mcmire mcmire changed the title Add guides for JavaScript and TypeScript Add code guidelines for JavaScript and TypeScript Oct 12, 2022
guides/javascript.md Outdated Show resolved Hide resolved
@mcmire mcmire marked this pull request as ready for review October 12, 2022 21:36
guides/javascript.md Outdated Show resolved Hide resolved
guides/typescript.md Outdated Show resolved Hide resolved
@mcmire mcmire changed the base branch from main to assign-guideline-categories November 14, 2022 16:20
Base automatically changed from assign-guideline-categories to main December 1, 2022 21:23
@mcmire mcmire marked this pull request as draft July 19, 2023 17:51
@mcmire mcmire changed the base branch from main to update-guidelines-for-guidelines July 19, 2023 19:49
@mcmire mcmire force-pushed the update-guidelines-for-guidelines branch 2 times, most recently from 72aadda to 7269c7c Compare July 19, 2023 23:23
Base automatically changed from update-guidelines-for-guidelines to main July 21, 2023 04:29
These were copied from the notes that I've been keeping in Notion (see
"Code Guidelines"). I tried to list best practices that I thought were
already being followed in some form, so hopefully none of them should be
too controversial.
@mcmire mcmire changed the title Add code guidelines for JavaScript and TypeScript Add initial guidelines for JavaScript and TypeScript Jul 21, 2023
@mcmire mcmire marked this pull request as ready for review July 21, 2023 21:45

These guidelines specifically apply to TypeScript.

## Provide explicit return types for functions/methods
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be enforced with ESLint.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! I've added the explicit-function-return-type rule here: MetaMask/eslint-config#314

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way, given that we have some ESLint rules that we've enabled ourselves, what do we feel about explicitly documenting the reason for doing so? That means we'd keep this here but then have something at the top that says something like:

> **Note**  
> This guideline is enforced via [`explicit-function-return-type`](/link/to/rule/in/our/eslint-config).

Copy link
Member

@Gudahtt Gudahtt Jul 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That seems like a good idea to me. We can remove it later if we don't think it has been useful.

Generally it would be nice to keep this guide shorter, and let ESLint explain things for us where possible. But the ESLint rules don't explain our reasoning very well. I wish we could.... explain the reasons for lint rules inline and advise a course of action, something like that. Ah well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed... okay. I can add this in a followup PR once we have a new version of eslint-config out.

guides/javascript.md Show resolved Hide resolved
guides/javascript.md Outdated Show resolved Hide resolved
guides/javascript.md Outdated Show resolved Hide resolved
guides/javascript.md Outdated Show resolved Hide resolved
guides/javascript.md Outdated Show resolved Hide resolved
guides/javascript.md Outdated Show resolved Hide resolved
Copy link
Member

@Gudahtt Gudahtt Jul 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Things that would be nice to have guidelines for:

  • prefer ts-expect-error over ts-ignore
  • When should we use ts-expect-error?
  • When is it OK to use any?
  • How to effectively narrow types (particularly when dealing with unknown).
    • Related: how to write type guard functions
  • No unnecessary generic parameters

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay! I will add these in a followup PR.

* `jest.spyOn(object, method)` can be used in place of `sinon.stub(object, method)` (with the caveat that the method being spied upon will still be called by default).
* `jest.useFakeTimers()` can be used in place of `sinon.useFakeTimers()` (though note that Jest's "clock" object had fewer features than Sinon's prior to Jest v29.5).

## Don't test private code
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to expand more on the reasoning behind this.
e.g.

  • Explain how this ties in with a broader BDD-like testing philosophy,
  • Tests are more useful for preventing regressions and helping document intended behavior when they only reflect the public API
  • This ensures the tests help make refactoring easier, rather than getting in the way

This is a great start though! I can follow up to add more detail later.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good!

guides/javascript.md Outdated Show resolved Hide resolved
Gudahtt
Gudahtt previously approved these changes Jul 26, 2023
Copy link
Member

@Gudahtt Gudahtt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

guides/javascript.md Outdated Show resolved Hide resolved
guides/javascript.md Outdated Show resolved Hide resolved
Co-authored-by: Maarten Zuidhoorn <maarten@zuidhoorn.com>
mcmire and others added 2 commits July 27, 2023 08:46
Co-authored-by: Maarten Zuidhoorn <maarten@zuidhoorn.com>
Co-authored-by: Mark Stacey <markjstacey@gmail.com>
Copy link
Contributor

@brad-decker brad-decker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⭐ 💯

@mcmire mcmire merged commit b485485 into main Jul 27, 2023
@mcmire mcmire deleted the add-js-and-ts-guidelines branch July 27, 2023 22:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add initial JavaScript and TypeScript guidelines
4 participants