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

[import/no-unused-modules] not correctly handling export { default } from 'foo' syntax #1631

Closed
richardxia opened this issue Jan 26, 2020 · 5 comments

Comments

@richardxia
Copy link
Contributor

I've been really interested in using the import/no-unused-modules rule in one of my React projects, but it seems to be incorrectly reporting an error when it sees the export { default } from 'foo' syntax:

/usr/src/app/app/pages/About/index.js
  1:1  error  exported declaration 'default' not used within other modules  import/no-unused-modules

It's been a bit tricky for me to reproduce with a smaller example, as it seems to be somewhat reliant on the Babel and Webpack plugins, but I have been able to produce a branch of my project with 95% of the files removed and still demonstrate the issue. I think I've gotten the problem narrowed to the following four files in that branch:

//==============================================================================
// app/init.jsx
import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter } from 'react-router-dom';
import Routes from './routes';

ReactDOM.render((
  <BrowserRouter>
    <Routes />
  </BrowserRouter>
), document.getElementById('root'));


//==============================================================================
// app/routes.jsx
import React from 'react';
import { Route, Switch } from 'react-router-dom';

import About from './pages/About';

export default () => (
  <Switch>
    <Route path="/about" component={About} />
  </Switch>
);


//==============================================================================
// app/pages/About/index.js
export { default } from './About';


//==============================================================================
// app/pages/About/About.jsx
import React from 'react';

export default class About extends React.Component {
  render() {
    return (
      <article>
        <header>
          About the SF Service Guide
        </header>
      </article>
    );
  }
}

Any help I could get with this would be greatly appreciated. If there's a way for me to dump out extra logging information, I'd be willing to attach it here.


Here are the versions of relevant packages, and you can check out the package.json in the branch I linked to for the full details:

  • eslint-plugin-import@2.20.0
  • @babel/core@7.4.4
  • webpack@4.17.4

On the branch I linked to, you should be able to reproduce the problem with the following commands:

git clone https://github.com/ShelterTechSF/askdarcel-web.git -b test-unused-modules
cd askdarcel-web
npm install
npm run lint
@ljharb
Copy link
Member

ljharb commented Jan 26, 2020

It's possible the ExportMap needs a fix to handle export { default } from.

@richardxia
Copy link
Contributor Author

Thanks for pointing that out. I've started adding console.log() statements to attempt to dump out some of the intermediate data, and I'm seeing the following ExportMap data for each file:

ExportMap {
  path: '/usr/src/app/askdarcel-web/app/init.jsx',
  namespace: Map {},
  reexports: Map {},
  dependencies: Set {},
  imports:
   Map {
     '/usr/src/app/askdarcel-web/node_modules/react/index.js' => { getter: [Function],
       source: [Object],
       importedSpecifiers: [Set] },
     '/usr/src/app/askdarcel-web/node_modules/react-dom/index.js' => { getter: [Function],
       source: [Object],
       importedSpecifiers: [Set] },
     '/usr/src/app/askdarcel-web/node_modules/react-router-dom/esm/react-router-dom.js' => { getter: [Function],
       source: [Object],
       importedSpecifiers: [Set] },
     '/usr/src/app/askdarcel-web/app/routes.jsx' => { getter: [Function],
       source: [Object],
       importedSpecifiers: [Set] } },
  errors: [],
  mtime: 2020-01-27T01:06:00.000Z }

ExportMap {
  path: '/usr/src/app/askdarcel-web/app/routes.jsx',
  namespace: Map { 'default' => {} },
  reexports: Map {},
  dependencies: Set {},
  imports:
   Map {
     '/usr/src/app/askdarcel-web/node_modules/react/index.js' => { getter: [Function],
       source: [Object],
       importedSpecifiers: [Set] },
     '/usr/src/app/askdarcel-web/node_modules/react-router-dom/esm/react-router-dom.js' => { getter: [Function],
       source: [Object],
       importedSpecifiers: [Set] },
     '/usr/src/app/askdarcel-web/app/pages/About/index.js' => { getter: [Function],
       source: [Object],
       importedSpecifiers: [Set] } },
  errors: [],
  mtime: 2020-01-27T01:06:00.000Z }

ExportMap {
  path: '/usr/src/app/askdarcel-web/app/pages/About/index.js',
  namespace: Map {},
  reexports:
   Map {
     'default' => { local: 'default', getImport: [Function: getImport] } },
  dependencies: Set {},
  imports: Map {},
  errors: [],
  mtime: 2020-01-26T19:46:48.000Z }

ExportMap {
  path: '/usr/src/app/askdarcel-web/app/pages/About/About.jsx',
  namespace: Map { 'default' => {} },
  reexports: Map {},
  dependencies: Set {},
  imports:
   Map {
     '/usr/src/app/askdarcel-web/node_modules/react/index.js' => { getter: [Function],
       source: [Object],
       importedSpecifiers: [Set] } },
  errors: [],
  mtime: 2020-01-27T01:06:00.000Z }

I'm having a little bit of difficulty trying to tell if the above output is correct, since I don't know what the precise definition of each attribute of ExportMap should be, but I do at least see each imported/exported name appear at least once.

I'm willing to attempt to submit a fix, but I think I need help determining if the bug is in the ExportMap or if the bug is in no-unused-modules.js.

@ljharb
Copy link
Member

ljharb commented Jan 27, 2020

I'm not really sure :-) maybe start with PR with a failing test, and we can iterate on it together?

@richardxia
Copy link
Contributor Author

Sounds good to me. I'm a bit busy the next couple of days, but I'll have some time to work on this this weekend, if not earlier.

@maniator
Copy link

maniator commented Mar 3, 2020

@ljharb -- any updates on getting what @richardxia worked on into the next release? 😄

@ljharb ljharb closed this as completed in efd6be1 Mar 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

3 participants