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 core package to use Node types #2221

Merged
merged 18 commits into from
Mar 21, 2023

Conversation

jovyntls
Copy link
Contributor

@jovyntls jovyntls commented Mar 17, 2023

What is the purpose of this pull request?

  • Documentation update
  • Bug fix
  • Feature addition or enhancement
  • Code maintenance
  • DevOps
  • Improve developer experience
  • Others, please explain:

Implements #2114 on the rest of the core package

Overview of changes:

  • Use the newly introduced MbNode/TextElement/NodeOrText types throughout the core package. This allows us to remove unnecessary checks for attributes on nodes and enforce better typing throughout the package.
  • Add a parseHTML utility method. Wrapping cheerio.parseHTML allows us to avoid further as unknown as typecasts throughout the codebase.

Anything you'd like to highlight/discuss:
N/A

Testing instructions:
Should have no changes in behaviour

Proposed commit message: (wrap lines at 72 characters)

Refactor core package to use Node types

In #2201, we introduced a unifying type for nodes. Most of the core
package is not yet using the new types.

Let's refactor the core package to utilise these new types, and add a
utility parseHTML method that avoids further `as unknown as` typecasts
throughout the codebase. Doing so allows us to make full use of
TypeScript's type-checking abilities while avoiding code duplication.

For more information on the rationale of the unifying types, refer to
#2201.

Checklist: ☑️

  • Updated the documentation for feature additions and enhancements
  • Added tests for bug fixes or features
  • Linked all related issues
  • No unrelated changes

@jovyntls jovyntls changed the title Ts unify node types Refactor core package to use Node types Mar 17, 2023
@jovyntls jovyntls marked this pull request as ready for review March 17, 2023 09:40
Copy link
Contributor

@EltonGohJH EltonGohJH left a comment

Choose a reason for hiding this comment

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

LGTM!

packages/core/src/html/siteAndPageNavProcessor.ts Outdated Show resolved Hide resolved
packages/core/src/html/siteAndPageNavProcessor.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@raysonkoh raysonkoh left a comment

Choose a reason for hiding this comment

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

LGTM

@jovyntls jovyntls added this to the v4.1.1 milestone Mar 21, 2023
@jovyntls jovyntls merged commit bc1fcb2 into MarkBind:master Mar 21, 2023
@jovyntls jovyntls deleted the ts-unify-node-types branch March 21, 2023 01:46
yucheng11122017 pushed a commit to yucheng11122017/markbind that referenced this pull request Mar 22, 2023
Refactor core package to use Node types

In MarkBind#2201, we introduced a unifying type for nodes. Most of the core
package is not yet using the new types.

Let's refactor the core package to utilise these new types, and add a
utility parseHTML method that avoids further `as unknown as` typecasts
throughout the codebase. Doing so allows us to make full use of
TypeScript's type-checking abilities while avoiding code duplication.

For more information on the rationale of the unifying types, refer to
MarkBind#2201.
ong6 pushed a commit to ong6/markbind that referenced this pull request Mar 23, 2023
Refactor core package to use Node types

In MarkBind#2201, we introduced a unifying type for nodes. Most of the core
package is not yet using the new types.

Let's refactor the core package to utilise these new types, and add a
utility parseHTML method that avoids further `as unknown as` typecasts
throughout the codebase. Doing so allows us to make full use of
TypeScript's type-checking abilities while avoiding code duplication.

For more information on the rationale of the unifying types, refer to
MarkBind#2201.
tlylt pushed a commit that referenced this pull request Mar 31, 2023
Clean up & standardise TypeScript conventions

The core package is almost fully migrated to TypeScript. There are
several inconsistencies and redundancies in how we represent common
types, handle typecasts, and export/import files. These inconsistencies
may cause confusion, reduce code quality, and prevent us from fully
taking advantage of static typing. 

As of #2201 and #2221, we have a more mature typing system that we can
utilise to fix these inconsistencies as well as a general set of
conventions that can be followed across TS files. 

Let's,
- Standardise class exports
- Standardise key-value types to use `Record<K, V>`
- Remove unnecessary `as unknown as` typecasts
- Reduce the use of `any` types
- Fix outdated docs that refer to `.js` files

Using Record is preferable as `Record<K,V>` is interchangeable with
`{ [key: K]: V }` syntax, but is more extensible and arguably more
readable. `unknown` and `any` types also introduce uncertainty and
unsafe typecasts into the codebase.
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.

3 participants