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(dts): transform vue script setup if no type-only macros #210

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

yusufkandemir
Copy link

πŸ”— Linked issue

Resolves #209

❓ Type of change

  • πŸ“– Documentation (updates to the documentation, readme, or JSdoc annotations)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

Some macros in <script setup> have a type-only variant:

  • defineProps
  • defineEmits
  • defineSlots
  • defineModel

As long as their type-only variant is NOT used, they can safely be transformed into JS. So, this PR improves the exclusion logic to exclude <script setup> only if the file contains some type-only macro usage.

πŸ“ Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

@yusufkandemir yusufkandemir changed the title feat: process <script setup lang="ts> if there is no type-only macros feat: transform Vue script setup if there are no type-only macros Apr 1, 2024
@danielroe
Copy link
Member

Would you be able to look at the failing test? πŸ™

Copy link

codecov bot commented Aug 15, 2024

Codecov Report

All modified and coverable lines are covered by tests βœ…

Project coverage is 74.50%. Comparing base (9000888) to head (a4eb997).
Report is 50 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #210      +/-   ##
==========================================
- Coverage   82.86%   74.50%   -8.37%     
==========================================
  Files          12       12              
  Lines         852      753      -99     
  Branches      133      147      +14     
==========================================
- Hits          706      561     -145     
- Misses        144      190      +46     
  Partials        2        2              

β˜” View full report in Codecov by Sentry.
πŸ“’ Have feedback on the report? Share it here.

return false;
}

const CODE_COMMENT_RE = /\/\/.*\n|\/\*[\S\s]*?\*\//g;
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps we could use strip-literal?

Copy link
Author

Choose a reason for hiding this comment

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

I am not sure if that would increase the accuracy. But, it will be slower as it will have to parse the whole document first.

So, if performance is not really a concern and/or accuracy is more important, then I think using js-tokens would be better. strip-literal uses js-tokens to filter the comment and literal related stuff. We would use it to check for identifiers(the macro names in this case). Since it's iterator-based, we won't have to parse the whole document when there is a macro. So, it will be better in terms of performance compared to strip-literal. It would also simplify the code as we would no longer need regexes.

Please let me know if we should keep it like this or go with the js-tokens approach.

Copy link
Member

Choose a reason for hiding this comment

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

sure - let's use js-tokens then!

Copy link
Author

Choose a reason for hiding this comment

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

Done πŸ‘ Just to give a rough statistic, executing the exclude method went from ~0.1ms to ~1ms on my machine(Apple M1).

@danielroe danielroe changed the title feat: transform Vue script setup if there are no type-only macros feat(dts): transform vue script setup if no type-only macros Sep 2, 2024
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.

Improve Vue <script setup lang="ts"> handling
2 participants