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

refactor(theme-classic): completely migrate package to TypeScript #5459

Merged
merged 8 commits into from
Sep 1, 2021

Conversation

Josh-Cena
Copy link
Collaborator

@Josh-Cena Josh-Cena commented Sep 1, 2021

Motivation

Although this package is almost all TS, there are a few JS files + untyped dependencies. This PR completes the TS infrastructure, and thus removed the noImplicitAny: false option in tsconfig.json.

Have you read the Contributing Guidelines on pull requests?

Yes

Test Plan

CI

Related PRs

Duplicated work with #5442, should merge that first

@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label Sep 1, 2021
@netlify
Copy link

netlify bot commented Sep 1, 2021

✔️ [V2]
Built without sensitive environment variables

🔨 Explore the source changes: 288f191

🔍 Inspect the deploy log: https://app.netlify.com/sites/docusaurus-2/deploys/612f6c3f7ba3750008995896

😎 Browse the preview: https://deploy-preview-5459--docusaurus-2.netlify.app

@github-actions
Copy link

github-actions bot commented Sep 1, 2021

⚡️ Lighthouse report for the changes in this PR:

Category Score
🟢 Performance 98
🟢 Accessibility 98
🟢 Best practices 100
🟢 SEO 100
🟢 PWA 95

Lighthouse ran on https://deploy-preview-5459--docusaurus-2.netlify.app/

Signed-off-by: Josh-Cena <[email protected]>
Signed-off-by: Josh-Cena <[email protected]>
Signed-off-by: Josh-Cena <[email protected]>
Signed-off-by: Josh-Cena <[email protected]>
Signed-off-by: Josh-Cena <[email protected]>
@@ -18,52 +18,63 @@ import styles from './styles.module.css';

import {useThemeConfig, parseCodeBlockTitle} from '@docusaurus/theme-common';

const highlightLinesRangeRegex = /{([\d,-]+)}/;
const languageTypes = ['js', 'jsBlock', 'jsx', 'python', 'html'] as const;
const HighlightLinesRangeRegex = /{([\d,-]+)}/;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

As I understand PascalCased variables should be objects right? (Also noticed this in docusaurus-init where TypeScriptTemplateSuffix is PascalCased)

Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't have a strong opinion about JS constant namings as long as it's clear enough those are constants. We already have this convention in other places so just made it consistent, but we can also redefine those conventions if needed

Copy link
Collaborator

@slorber slorber left a comment

Choose a reason for hiding this comment

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

LGTM thanks 👍

Also refactored a bit the versionBanner prop because handling 'none' with Exclude was not very convenient

@Josh-Cena
Copy link
Collaborator Author

Yeah, I was thinking about using null as well, but not sure if that would break any exposed API

Signed-off-by: Josh-Cena <[email protected]>
@Josh-Cena
Copy link
Collaborator Author

Slightly off-topic: I prefer removing inferable types wherever possible, and it also seems to be the case for the majority of TS code across the repo, except for docusaurus-core where I spotted a lot of redundant annotation. However it seems there isn't an ESLint rule to enforce this 🤷‍♂️

@slorber
Copy link
Collaborator

slorber commented Sep 1, 2021

I feel inferable types have their place in a codebase to avoid some useless duplication (cf highlighted languages using as const), as long as the public API surface is not inferred (ie API changes must be explicit when reviewing code). It's probably worth discussing this on a case-by-case basis.

@slorber slorber added the pr: polish This PR adds a very minor behavior improvement that users will enjoy. label Sep 1, 2021
@slorber slorber changed the title chore(theme-classic): completely migrate package to TypeScript refactor(theme-classic): completely migrate package to TypeScript Sep 1, 2021
@slorber slorber merged commit 78d8400 into facebook:main Sep 1, 2021
@Josh-Cena Josh-Cena deleted the ts-theme-classic branch September 1, 2021 12:37
@Josh-Cena
Copy link
Collaborator Author

Josh-Cena commented Sep 1, 2021

the public API surface is not inferred

This is taken care of by the explicit-boundary-types rule which I find reasonable. I was just talking about no-inferable-types rule being too limited in its scope (only catching the really "trivial" types like const a: string = 'Hello')... There are some feature requests on making a no-unnecessary-annotation rule but seems no implementation yet

@slorber
Copy link
Collaborator

slorber commented Sep 1, 2021

I don't always agree with that, extra type annotations on intermediate variables can significantly help readability and also help split a complex data processing algo in multiple "steps" in which you can more easily see which step doesn't typecheck (instead of having the full algo not typechecking). I'd only remove types on a case-by-case basis and limit to the trival types inferences only.

@slorber
Copy link
Collaborator

slorber commented Sep 1, 2021

A good example where an algo can be seen as a concatenation of 2 steps, where explicit (but unnecessary) types can help figure out which step fails to typecheck:

function collectSidebarItemsOfType<
  Type extends SidebarItemType,
  Item extends SidebarItem & {type: SidebarItemType}
>(type: Type, sidebar: Sidebar): Item[] {
  function collectRecursive(item: SidebarItem): Item[] {
    const currentItemsCollected: Item[] =
      item.type === type ? [item as Item] : [];

    const childItemsCollected: Item[] =
      item.type === 'category' ? flatten(item.items.map(collectRecursive)) : [];

    return [...currentItemsCollected, ...childItemsCollected];
  }

  return flatten(sidebar.map(collectRecursive));
}

@Josh-Cena
Copy link
Collaborator Author

Yeah, this one makes sense 👍 Thanks for the MWE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed Signed Facebook CLA pr: polish This PR adds a very minor behavior improvement that users will enjoy.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants