From 277258ddc73c3da816aba6fccb739c69dfe8e83a Mon Sep 17 00:00:00 2001 From: merceyz Date: Sat, 28 Mar 2020 18:38:34 +0100 Subject: [PATCH] fix(injector): always call injectPropTypes to allow shouldInclude to run --- src/injector.ts | 41 ++++++++------------- test/injector/whitelisted-props/input.tsx | 3 ++ test/injector/whitelisted-props/options.ts | 11 ++++++ test/injector/whitelisted-props/output.js | 10 +++++ test/injector/whitelisted-props/output.json | 12 ++++++ 5 files changed, 51 insertions(+), 26 deletions(-) create mode 100644 test/injector/whitelisted-props/input.tsx create mode 100644 test/injector/whitelisted-props/options.ts create mode 100644 test/injector/whitelisted-props/output.js create mode 100644 test/injector/whitelisted-props/output.json diff --git a/src/injector.ts b/src/injector.ts index 7e9fe50..47487c1 100644 --- a/src/injector.ts +++ b/src/injector.ts @@ -163,24 +163,17 @@ function plugin( const props = propTypes.body.find((prop) => prop.name === node.id!.name); if (!props) return; - let usedProps: string[] = []; - - if (!includeUnusedProps) { - const prop = node.params[0]; - if (babelTypes.isIdentifier(prop) || babelTypes.isObjectPattern(prop)) { - usedProps = getUsedProps(path, prop); - } - } - - if (usedProps.length === 0 && !includeUnusedProps) return; - // Prevent visiting again (node as any).hasBeenVisited = true; path.skip(); + const prop = node.params[0]; injectPropTypes({ nodeName: node.id.name, - usedProps, + usedProps: + babelTypes.isIdentifier(prop) || babelTypes.isObjectPattern(prop) + ? getUsedProps(path, prop) + : [], path, props, }); @@ -215,19 +208,19 @@ function plugin( } function getFromProp(prop: babelTypes.Node) { - const usedProps = - !includeUnusedProps && - (babelTypes.isIdentifier(prop) || babelTypes.isObjectPattern(prop)) - ? getUsedProps(path, prop) - : []; - - if (usedProps.length === 0 && !includeUnusedProps) return; - // Prevent visiting again (node as any).hasBeenVisited = true; path.skip(); - injectPropTypes({ path: path.parentPath, usedProps, props: props!, nodeName }); + injectPropTypes({ + path: path.parentPath, + usedProps: + babelTypes.isIdentifier(prop) || babelTypes.isObjectPattern(prop) + ? getUsedProps(path, prop) + : [], + props: props!, + nodeName, + }); } }, ClassDeclaration(path) { @@ -245,17 +238,13 @@ function plugin( const props = propTypes.body.find((prop) => prop.name === nodeName); if (!props) return; - const usedProps = !includeUnusedProps ? getUsedProps(path, undefined) : []; - - if (usedProps.length === 0 && !includeUnusedProps) return; - // Prevent visiting again (node as any).hasBeenVisited = true; path.skip(); injectPropTypes({ nodeName, - usedProps, + usedProps: getUsedProps(path, undefined), path, props, }); diff --git a/test/injector/whitelisted-props/input.tsx b/test/injector/whitelisted-props/input.tsx new file mode 100644 index 0000000..7693638 --- /dev/null +++ b/test/injector/whitelisted-props/input.tsx @@ -0,0 +1,3 @@ +export default function Foo(props: { className: string }) { + return
; +} diff --git a/test/injector/whitelisted-props/options.ts b/test/injector/whitelisted-props/options.ts new file mode 100644 index 0000000..ef9fdf4 --- /dev/null +++ b/test/injector/whitelisted-props/options.ts @@ -0,0 +1,11 @@ +import { TestOptions } from '../../types'; + +const options: TestOptions = { + injector: { + shouldInclude() { + return true; + }, + }, +}; + +export default options; diff --git a/test/injector/whitelisted-props/output.js b/test/injector/whitelisted-props/output.js new file mode 100644 index 0000000..9c0ae14 --- /dev/null +++ b/test/injector/whitelisted-props/output.js @@ -0,0 +1,10 @@ +import PropTypes from 'prop-types'; +function Foo(props) { + return
; +} + +Foo.propTypes = { + className: PropTypes.string.isRequired, +}; + +export default Foo; diff --git a/test/injector/whitelisted-props/output.json b/test/injector/whitelisted-props/output.json new file mode 100644 index 0000000..e2ca53b --- /dev/null +++ b/test/injector/whitelisted-props/output.json @@ -0,0 +1,12 @@ +{ + "type": "ProgramNode", + "body": [ + { + "type": "ComponentNode", + "name": "Foo", + "types": [ + { "type": "PropTypeNode", "name": "className", "propType": { "type": "StringNode" } } + ] + } + ] +}