moroshko / react-scanner

Extract React components and props usage from code.
MIT License
563 stars 40 forks source link

Custom processors are not working #36

Closed filipdanisko closed 3 years ago

filipdanisko commented 3 years ago

Hello, first of all wanted to say, thank you for creating this! 🚀

I wanted to create my own processor as menitoned in the readme. But I get an error:

when processor is a tuple, the second item is options and should be an object

I was looking what can be the cause, and I believe this condition is not correct, as it prevents passing a function in to the config tuple. https://github.com/moroshko/react-scanner/blob/e87a9ee4de31feb9c6e6cc362941635e54431fa1/src/utils.js#L133-L137

I tested it out without the condition and worked as expected, I can try to contribute this if you can give me more context to what would you consider a best fix. 🙏

moroshko commented 3 years ago

Hey @filipdanisko, Could you share a config that is causing the issue?

filipdanisko commented 3 years ago

Hey @moroshko sure I just used the one from readme so something like this:

module.exports = {
  crawlFrom: '../../../../../',
  includeSubComponents: true,
  exclude: [
    'node_modules',
  ],
  importedFrom: /(my-lib)/,
  globs: ['**/!(*.test|*.spec).@(js|ts)?(x)'],
  processors: [
    ['raw-report',   ({ prevResult }) => {
    ...
  }],
  ],
};
moroshko commented 3 years ago

@filipdanisko I just tried the following config and it worked fine:

module.exports = {
  crawlFrom: "/path/to/src",
  processors: ["raw-report", ({ prevResult }) => {
    console.log(prevResult);
  }]
};

I ran the latest react-scanner 0.7.0.

npx react-scanner -c ./react-scanner.config.js

Could you provide a repro?

drewbrend commented 3 years ago

I stumbled across this and found when processors are provided as a tuple array with a function I get the same error as originally reported, when I switch it to a single tuple it works fine.

This explains why @filipdanisko's config doesn't work while yours does @moroshko.

This gives me the error reported in this issue:

  processors: [
    [
      'raw-report',
      ({ prevResult }) => {
        console.log(prevResult);
      },
    ],
  ],

While this works fine:

  processors: [
    'raw-report',
    ({ prevResult }) => {
      console.log(prevResult);
    },
  ],
moroshko commented 3 years ago

According to readme:

built-in processors come in the form of a string or a tuple

When it's a tuple, the second argument is expected to be an options object.

Whereas:

custom processors are functions

So, trying to treat ['raw-report', ({ prevResult }) => { ... } ] as a single processor is not valid because it's not one of the formats above.

This array, however, has 2 processors in it: the first one is 'raw-report' and the second one is the custom processor. That's why processors: ['raw-report', ({ prevResult }) => { ... } ] works as expected.

Does this make sense?

filipdanisko commented 3 years ago

@moroshko yup makes sense! Thanks for clarification!