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

feat: support spaces before cut comments #33

Merged
merged 1 commit into from Mar 28, 2024

Conversation

sapphi-red
Copy link
Contributor

Currently twoslash requires the cut comment to be at the beginning of the line.
If I wanted to cut out the type comment for a variable in a function, I have to write it like below.

function foo() {
  const x = "123"
// ---cut-start---
  /** @type {"345"} */
// ---cut-end---
  const b = "345"
}

But if that project uses prettier, it will be formatted and the comments will have a space before the // like below.

function foo() {
  const x = "123"
  // ---cut-start---
  /** @type {"345"} */
  // ---cut-end---
  const b = "345"
}

So I'll have to disable prettier for this part of the code (https://github.com/vitejs/vite/blob/48a2bbb9994f9cd67e3d7455a57f6579f64e784d/docs/config/build-options.md?plain=1#L51-L66).

This PR allows the cut comment to have space characters before.

Copy link

netlify bot commented Mar 27, 2024

Deploy Preview for twoslash ready!

Name Link
🔨 Latest commit 7a53d31
🔍 Latest deploy log https://app.netlify.com/sites/twoslash/deploys/660420bbee7fee0008d5e11a
😎 Deploy Preview https://deploy-preview-33--twoslash.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

export const reCutAfter = /^\/\/\s?---cut-after---$/mg
export const reCutStart = /^\/\/\s?---cut-start---$/mg
export const reCutEnd = /^\/\/\s?---cut-end---\r?\n/mg
export const reCutBefore = /^[\t\v\f ]*\/\/\s?---cut(-before)?---\r?\n/mg
Copy link
Contributor Author

Choose a reason for hiding this comment

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

export const reCutBefore =/^[\t\v\f\uFEFF\p{Zs}]*\/\/\s?---cut(-before)?---\r?\n/mgu

Accurately, the regex needs to be like this to include other characters that are treated as white space. https://developer.mozilla.org/en-US/docs/Glossary/Whitespace#in_javascript
But I guess the u flag has a perf impact and people won't use those characters.

Copy link

codecov bot commented Mar 28, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.25%. Comparing base (249c9d7) to head (7a53d31).

Additional details and impacted files
@@           Coverage Diff           @@
##             main      #33   +/-   ##
=======================================
  Coverage   95.25%   95.25%           
=======================================
  Files          16       16           
  Lines        1982     1982           
  Branches      347      347           
=======================================
  Hits         1888     1888           
  Misses         94       94           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@antfu antfu merged commit d23618a into twoslashes:main Mar 28, 2024
11 checks passed
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.

None yet

2 participants