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

Disallow top-level await before defineExpose #1872

Closed
AndreasNasman opened this issue Apr 27, 2022 · 4 comments · Fixed by #1885
Closed

Disallow top-level await before defineExpose #1872

AndreasNasman opened this issue Apr 27, 2022 · 4 comments · Fixed by #1885

Comments

@AndreasNasman
Copy link

AndreasNasman commented Apr 27, 2022

Please describe what the rule should do:
The rule should notify when defineExpose is used after any top-level await. Doing so causes problems as described in this core Vue.js issue:
vuejs/core#4930

What category should the rule belong to?
[x] Warns about a potential error (problem)

Provide 2-3 code examples that this rule should warn about:

// Example 1
...
<script setup>
  ...
  const foo = { bar: 'baz' };
  await asyncOperation();
  defineExpose({ foo }); // `defineExpose` should be called before any top-level `await`.
  ...
</script>
...

// Example 2
...
<script setup>
  ...
  await asyncOperation();
  const foo = { bar: 'baz' };
  defineExpose({ foo }); // `defineExpose` should be called before any top-level `await`.
  ...
</script>
...

Additional context
The current workaround is to manually remember to place all awaits after defineExpose. Forgetting to do so leads to problems that are hard to debug. In the context of my examples, a JavaScript error would be thrown when foo is used (foo is not defined or similar) in some other component and it's unclear the source of the problem is actually in the component defining foo.

The problem seems challenging to fix in Vue.js itself:
vuejs/core#4930 (comment)

@AndreasNasman AndreasNasman changed the title Disallow defineExpose before any top-level await Disallow top-level await before defineExpose Apr 27, 2022
@AndreasNasman
Copy link
Author

To clarify, this only happened in a production build – the development build worked fine – and was therefore extra hard to debug.

@FloEdelmann
Copy link
Member

The rule vue/no-expose-after-await should already catch this case. It is currently uncategorized and has to be enabled manually. It will be added to all Vue 3 preset configs in the next major version: #1848

Could you please enable it manually and check whether it reports all problematic cases?

@AndreasNasman
Copy link
Author

AndreasNasman commented Apr 28, 2022

I scaffolded a new Vue project with npm init vue@latest and it seems to only work for regular script components taking expose as a setup parameter, not script setup that uses defineExpose.

image

I upgraded the ESLint and eslint-plugin-vue versions to the latest stable releases, 8.14.0 and 8.7.1 respectively, so it's present in the latest stable versions.

@FloEdelmann
Copy link
Member

Thanks for testing! So the vue/no-expose-after-await rule should be adapted to also catch cases in <script setup>.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants