Galooshi / import-js

A tool to simplify importing JS modules

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Object rest falsely not registering as defined

rivertam opened this issue · comments

Test case:

// ok.js

export function test({ ...props }) {
  return console.log(props);
}
// .importjs.js

const _ = require('lodash/fp');

module.exports = {
  namedExports: {
    'lodash/fp': Object.keys(_),
  },
};

on running importjs fix ok.js, the following output appears:

{"messages":["Imported { props } from 'lodash/fp'"],"fileContent":"import { props } from 'lodash/fp';\n\nexport function test({ ...props }) {\n  return console.log(props);\n}\n","unresolvedImports":{}}

I've been looking through the code and I don't really understand it but I think the issue is probably in visitIdentifierNodes.

Why would you use object rest with destructured arguments when you aren't picking out other keys?

Isn't test({ ...props }) { equivalent to test(props) {?

Does it work when you name other keys to destructure and use rest?

It is probably a bug in import-js that it can't tell this is object rest instead of spread though.
I wonder if the update to babel7 and @babel/parser in #509 will affect/fix this?

@coagmano this was a minimal example. I discovered it with other properties, but this is sufficiently a bug in that it's incorrect, but yes it's a general issue with object rest not just when it's alone.

Thanks for the report and the minimal repro case. I have a fix on the way.