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

fix: Migrate to Typescript #2805

Merged
merged 9 commits into from
Jul 29, 2023
Merged

fix: Migrate to Typescript #2805

merged 9 commits into from
Jul 29, 2023

Conversation

DreierF
Copy link
Contributor

@DreierF DreierF commented May 14, 2023

Description

This PR migrated the codebase to Typescript and automatically generates and publishes a declaration file which fixes #1732.
I tried to keep the actual code changes that change semantics of the code as small as possible. To simplify the publishing I replaced the rollup setup with tsup, which makes bundling the declaration files a lot easier. I started off with the declarations from Definitely Typed pulled them into the actual code as far as it made sense to me. I also copied over the tests from Definitely Typed.

In contrast to the Definitely Typed types the types are no longer part of the marked Namespace i.e. marked.MarkedOptions -> MarkedOptions as I did not find a way to preserve both the call signature on the namespace (marked(...)) in addition to exporting the types within the namespace. As this has no effect on runtime behavior and only requires trivial changes in Typescript code on user side this was the best tradeoff I could think of. Also namespaces are generally no longer recommended in Typescript so this seemed like a good direction anyway.

I also had to rename most internal symbols i.e. Tokenizer to _Tokenizer to fix an issue in the generated .d.ts file, which would otherwise look like

namespace marked {
   var Tokenizer: typeof Tokenizer; // Cyclic reference while the second Tokenizer actually did not mean the variable, but the import within the file. Also aliasing the classes on import did not fix the issue.
}

Future work

Obviously there is a bunch of places that could be more strictly/cleanly typed, but for now I focused on the exposed API as fixing the type errors within the functions will most of the time require code refactorings which would have made the PR un-reviewable.

@UziTech Please have a look and let me know what you think :)
I have pulled out the renaming from *.js to *.ts to a separate commit to make it easier to review.

Contributor

  • Test(s) exist to ensure functionality and minimize regression (if no tests added, list tests covering this PR); or,
  • no tests required for this PR.
  • If submitting new feature, it has been documented in the appropriate places.

Committer

In most cases, this should be a different person than the contributor.

@vercel
Copy link

vercel bot commented May 14, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
marked-website ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 29, 2023 5:14am

@UziTech
Copy link
Member

UziTech commented May 15, 2023

Thank you for this! 💯

This looks like a great start. I have skimmed it, but I will do a more thorough review within the next week.

A couple things I see that could change:

  • try to remove any wherever possible. If it requires any add a comment about why and maybe what needs to change to update it in the future.
  • instead of @ts-ignore can we do @ts-expect-error with a comment why it errors. That will allow us to fix it easier in the future.

@DreierF
Copy link
Contributor Author

DreierF commented May 16, 2023

Most of the any/ts-ignore were there because you'd have to change the actual code (not just the type annotations) to make it work with Typescript. I got rid of most any and ts-ignore now and replaced the remaining with ts-expect-error.

Copy link
Member

@UziTech UziTech left a comment

Choose a reason for hiding this comment

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

This is going to be a breaking change so I think it will take some time to actually get merged in.

docs/demo/preview.html Outdated Show resolved Hide resolved
src/Lexer.ts Outdated Show resolved Hide resolved
src/Lexer.ts Outdated Show resolved Hide resolved
src/Parser.ts Outdated Show resolved Hide resolved
src/Parser.ts Outdated Show resolved Hide resolved
src/Renderer.ts Outdated Show resolved Hide resolved
src/helpers.ts Outdated Show resolved Hide resolved
@DreierF
Copy link
Contributor Author

DreierF commented May 29, 2023

Thanks for the review. I hope addressed all your comments now and rebased the changes on master.

@UziTech
Copy link
Member

UziTech commented Jun 10, 2023

@styfle @calculuschild should we focus on this for v6? and setting deprecated options to false?

@calculuschild
Copy link
Contributor

Yeah, sounds good.

Copy link
Member

@UziTech UziTech left a comment

Choose a reason for hiding this comment

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

@DreierF if you can rebase this I think we can merge this next so we don't have to keep rebasing.

@DreierF
Copy link
Contributor Author

DreierF commented Jun 11, 2023

I'm done with the rebase. I squashed all the rework commits into a single commit to make the rebase less painful.

@DreierF DreierF requested a review from UziTech June 11, 2023 09:26
src/Instance.ts Outdated
import { _Renderer } from './Renderer.js';
import { _Tokenizer } from './Tokenizer.js';
import { _TextRenderer } from './TextRenderer.js';
import { _Slugger } from './Slugger.js';
Copy link
Member

Choose a reason for hiding this comment

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

Why are these prefixed with underscores?

Is there precedence for this convention?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As I mentioned already in the description, I had to rename most internal symbols i.e. Tokenizer to _Tokenizer to fix an issue in the generated .d.ts file, which would otherwise look like

namespace marked {
   var Tokenizer: typeof Tokenizer; // Cyclic reference while the second Tokenizer actually did not mean the variable, but the import within the file. Also aliasing the classes on import did not fix the issue.
} 

Do you have any better idea on how to overcome this issue?
You should be able to revert that once you get rid of the marked export and it's extension fields.

Copy link
Contributor

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 solution, but I'll second that I don't like the underscores.

Copy link
Member

@UziTech UziTech left a comment

Choose a reason for hiding this comment

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

This looks like a good start. We can fix the anys and ignores in future PRs.

tsconfig-type-test.json Outdated Show resolved Hide resolved
tsconfig.json Outdated Show resolved Hide resolved
@UziTech
Copy link
Member

UziTech commented Jul 29, 2023

@calculuschild do you have any other concerns? I would like to get this merged soon.

@calculuschild
Copy link
Contributor

I guess we can merge. I feel like there are probably some more tweaks we can make to clear up the typing syntax in a few places but I think that can be pushed to a later PR.

@UziTech
Copy link
Member

UziTech commented Jul 29, 2023

Ya I think we should merge this and get a couple other breaking changes in before releasing v6.

@DreierF if you can rebase this I can merge it.

@DreierF
Copy link
Contributor Author

DreierF commented Jul 29, 2023

Great to hear! Rebase is done ✅

@UziTech UziTech changed the title Migrate to Typescript fix: Migrate to Typescript Jul 29, 2023
@UziTech UziTech merged commit cb54906 into markedjs:master Jul 29, 2023
@DreierF DreierF deleted the typescript branch July 29, 2023 06:45
github-actions bot pushed a commit that referenced this pull request Jul 31, 2023
# [6.0.0](v5.1.2...v6.0.0) (2023-07-31)

### Bug Fixes

* Migrate to Typescript ([#2805](#2805)) ([cb54906](cb54906))

### BREAKING CHANGES

* Migrate to Typescript
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 Declaration Files
4 participants