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 for no-unused-modules to handle the export { default } from syntax #1640

Conversation

richardxia
Copy link
Contributor

Relates to #1631.

At the moment, I only have a failing test case, since it took me a while to understand how imports and exports worked with the testing system. If my current understanding is correct, the filename property refers to the file that this test "replaces", and the code property is the text contents of the replacement.

The way I've proven to myself that this demonstrates the bug is that if I replace the contents of the code property with

`import o from './file-o'
export default o`

then the test passes. The export { default } from './file-o' syntax should be equivalent to the above, so if we get that test passing, then the bug should be fixed.

I'm going to make an attempt at understanding the ExportMap code enough to try to fix the bug, but I've checked the "Allow edits from maintainers" box on this PR, so feel free to push up commits to this.

@coveralls
Copy link

Coverage Status

Coverage decreased (-4.1%) to 93.717% when pulling c538e69 on richardxia:1631-no-unused-modules-handle-export-default-from into bbd166b on benmosher:master.

3 similar comments
@coveralls
Copy link

Coverage Status

Coverage decreased (-4.1%) to 93.717% when pulling c538e69 on richardxia:1631-no-unused-modules-handle-export-default-from into bbd166b on benmosher:master.

@coveralls
Copy link

Coverage Status

Coverage decreased (-4.1%) to 93.717% when pulling c538e69 on richardxia:1631-no-unused-modules-handle-export-default-from into bbd166b on benmosher:master.

@coveralls
Copy link

Coverage Status

Coverage decreased (-4.1%) to 93.717% when pulling c538e69 on richardxia:1631-no-unused-modules-handle-export-default-from into bbd166b on benmosher:master.

@coveralls
Copy link

coveralls commented Feb 2, 2020

Coverage Status

Coverage increased (+0.001%) to 97.797% when pulling efd6be1 on richardxia:1631-no-unused-modules-handle-export-default-from into adbced7 on benmosher:master.

@ljharb
Copy link
Member

ljharb commented Feb 2, 2020

Test looks good; looking forward to the fix :-)

@richardxia
Copy link
Contributor Author

I have a fix, but I'm not sure if it's correct or complete.

What bothers me is that I was struggling to get the tests passed until I realized that running my test in isolation would work, but running it with the other tests would cause it to fail. I don't currently really understand how the updateExportUsage and updateImportUsage work, nor do I understand how they interact with the test suite, where we sometimes replace a real test file with slightly different textual contents.

I also had to modify the one other test that I identified as interacting with my own test. This is the one where we basically have a copy of file-0.js embedded into the test itself. It appears that once per Mocha run, we parse all of the test files and construct the dependency graphs, but for each individual test case, we sometimes modify that dependency graph. Running the other test was causing the dependency between file-0.js and my new file-s.js to get removed, and it would never get restored. It seems to me that we should be purging pieces of the dependency graph that get stubbed out for testing pieces so that earlier tests don't interfere with later ones.

One general suggestion I have about the code within no-unused-modules.js is to better state what the invariants are before and after any of the important functions run. I had trouble understanding what the expected state of importList and exportList were after each of the functions, especially with respect to things like the handling of special-case behavior, such as mapping the exported name 'default' to 'ImportDefaultSpecifier'.

@richardxia
Copy link
Contributor Author

Hi @ljharb, I was wondering if you had the chance to take a look at this.

I'm also up for refactoring or improving code comments/documentation of this plugin so that it is easier for future contributors to make changes to.

@maniator
Copy link

Is there any update on this PR?

This issue is blocking our team from using this eslint rule.

@richardxia
Copy link
Contributor Author

I think this is ready for review. Let me update the PR title to indicate that it's more than just a failing test case, since there's a fix included as well, and that it's not WIP (at least from my perspective).

@richardxia richardxia changed the title WIP: Add failing test case for no-unused-modules and export { default } from. Fix for no-unused-modules to handle the export { default } from syntax Feb 21, 2020
@richardxia richardxia force-pushed the 1631-no-unused-modules-handle-export-default-from branch 2 times, most recently from 826c39f to 66a6a43 Compare February 24, 2020 05:40
@richardxia
Copy link
Contributor Author

I did a bit of cleaning up here including:

  • Rebasing against the latest master
  • Adding much more detailed comments describing the importList and exportList top-level variables: 9bd07e7. Please let me know if you think I've gone a bit overboard here, and I'd be happy to trim it down or even just revert that change.
  • Adding a line to the CHANGELOG: 66a6a43

@ljharb, please let me know if there's anything else I can do here. Also, feel free to make any changes you want directly to this branch, since I've checked the "Allow edits from maintainers." box on this PR.

@maniator
Copy link

maniator commented Mar 3, 2020

What is blocking this change?

This would help out our team immensely!

Great job on this @richardxia 😄!

@ljharb ljharb force-pushed the 1631-no-unused-modules-handle-export-default-from branch from 66a6a43 to efd6be1 Compare March 3, 2020 22:05
@ljharb ljharb merged commit efd6be1 into import-js:master Mar 3, 2020
@richardxia
Copy link
Contributor Author

Thanks for merging this in, and I'm excited to use this lint rule on my codebase soon!

@richardxia richardxia deleted the 1631-no-unused-modules-handle-export-default-from branch March 4, 2020 16:14
@maniator
Copy link

@ljharb -- is it possible to make a release of the plugin?

My team would really appreciate this fix :-)

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

Successfully merging this pull request may close these issues.

None yet

4 participants