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

Create unified types for nodes #2201

Merged
merged 5 commits into from
Mar 17, 2023
Merged

Conversation

jovyntls
Copy link
Contributor

@jovyntls jovyntls commented Mar 10, 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:

Fixes #2114 (the creation part)

Overview of changes:
Creates MbNode, TextElement and NodeOrText types. This avoids unnecessary and unsafe typecasting later on, and creates clearer distinction between how we handle nodes.
This PR implements a minimal "demo" to preview how the changes will look like. Further refactoring may be done in future PRs.

Anything you'd like to highlight/discuss:

  • Naming of the type Node - seems to clash with many other types. Would NodeElement be a better name? Renamed MbNode
  • General feedback/suggestions are welcome

Testing instructions:
N/A, there should be no difference in behaviour

Proposed commit message: (wrap lines at 72 characters)

Create unified types for nodes

There is no standardised type for node variables, resulting in several
undesirable issues:
* `as unknown as` typecasts throughout the codebase 
* unnecessary checks on node attributes to satisfy the linter
* incompatible types, treated as interchangeable in conversions

Let's add common MbNode, TextElement and NodeOrText types that enforce
attributes for nodes throughout the codebase. Doing so avoids the
abovementioned issues, and formalises our view of the relationship
between cheerio.Element, DomElement, and all the other attributes
expected from node variables.

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 Add unified Node type Create unified types for nodes Mar 14, 2023
@jovyntls jovyntls requested review from raysonkoh and ong6 March 15, 2023 02:11
Copy link
Contributor

@ong6 ong6 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 requested a review from tlylt March 16, 2023 03:04
Co-authored-by: Liu Yongliang <41845017+tlylt@users.noreply.github.com>
@tlylt
Copy link
Contributor

tlylt commented Mar 16, 2023

LGTM, @raysonkoh want to take a last look?

@tlylt tlylt merged commit 3ca03b5 into MarkBind:master Mar 17, 2023
jovyntls added a commit that referenced this pull request Mar 21, 2023
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.
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
Create unified types for nodes

There is no standardised type for node variables, resulting in several
undesirable issues:
* `as unknown as` typecasts throughout the codebase 
* unnecessary checks on node attributes to satisfy the linter
* incompatible types, treated as interchangeable in conversions

Let's add common MbNode, TextElement and NodeOrText types that enforce
attributes for nodes throughout the codebase. Doing so avoids the
abovementioned issues, and formalises our view of the relationship
between cheerio.Element, DomElement, and all the other attributes
expected from node variables.
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.

Consider creating unified types for common variables
4 participants