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

Added filterWarnings option, fixes #85 #108

Merged
merged 5 commits into from
Feb 17, 2022
Merged

Conversation

Prinzhorn
Copy link
Contributor

@Prinzhorn Prinzhorn commented Feb 10, 2022

For context sveltejs/svelte#7240

I have no idea about TypeScript and why it thinks options could be undefined given this:

esbuild-svelte/index.ts

Lines 88 to 90 in 589870f

if (!options) {
options = {};
}

Anyway, if I disable the TypeScript checks then npm run build && npm test succeeds. Could you please clean this up (warnings.filter(options?.filterWarnings) doesn't look correct)? I need a way to filter out false positive warnings. I don't understand why Rollup uses a callback pattern. This PR solves my problem but might have some shortcoming? idk

Verified

This commit was signed with the committer’s verified signature. The key has expired.
levrik Levin Rickert
Copy link

@dummdidumm dummdidumm left a comment

Choose a reason for hiding this comment

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

I think this would be a good addition 👍

dist/index.d.ts Outdated
* A function to filter out warnings
* Defaults to a constant function that returns `true`
*/
filterWarnings?: Function;

Choose a reason for hiding this comment

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

I would change this to onwarn since that's what the option is called in the rollup/webpack/vite plugin

Copy link
Contributor Author

@Prinzhorn Prinzhorn Feb 10, 2022

Choose a reason for hiding this comment

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

The semantics are vastly different. I assume onwarn in the other plugins (I'm looking at Rollup) is executed for each warning in an (async?) event-type scenario (not in batches as it's done here) and that's why it's using a callback (so it can also do async stuff I assume?). Given the architecture here and that I have access to a static array of warnings I don't see a point forcing it into the same structure. Honestly how would that even work? I can't await all onwarn calls if handler is only called when you want to keep the warning and it's simply not called when you don't. How would I know it's done? Like I said, there's probably a reason but the API is awkward and not a pattern you often see. It's neither classic error-first callback nor is it compatible with sth. like async.filter where the iteratee needs to call the callback at all times, either with a truthy or falsy values.

I have the luxury here of doing a super basic Array#filter and I'm down for it.

index.ts Outdated
* A function to filter out warnings
* Defaults to a constant function that returns `true`
*/
filterWarnings?: Function;

Choose a reason for hiding this comment

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

See other comment

index.ts Outdated
@@ -237,7 +248,7 @@ export default function sveltePlugin(options?: esbuildSvelteOptions): Plugin {

const result: OnLoadResult = {
contents,
warnings: warnings.map(convertMessage),
warnings: warnings.filter(options?.filterWarnings).map(convertMessage),

Choose a reason for hiding this comment

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

TS complains here / wants you to add the ? because control flow is reset inside functions. TS can't know for sure that there isn't something else that resets this options to undefined between you setting it to sth and this function getting called.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not questioning TSs decision here, but there is only a single assignment to options in that entire file. I know it will never be undefined 🤷

Choose a reason for hiding this comment

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

You can do options!.filterWarnings in that case

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
@EMH333
Copy link
Owner

EMH333 commented Feb 10, 2022

This addition makes sense to me at first glance, thanks for the PR! I agree that onwarn from the Rollup plugin is a little odd and their implementation wouldn't be clean here. I'm going to try and look into that a little more to make sure I'm not missing something.

I'll do a review and get this merged this weekend

@EMH333 EMH333 merged commit bd64c35 into EMH333:main Feb 17, 2022
@Prinzhorn Prinzhorn deleted the filter-warnings branch February 18, 2022 07:59
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

3 participants