Skip to content

Commit

Permalink
fix(injector): always call injectPropTypes to allow shouldInclude to run
Browse files Browse the repository at this point in the history
  • Loading branch information
merceyz committed Mar 28, 2020
1 parent 2ab6f43 commit 277258d
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 26 deletions.
41 changes: 15 additions & 26 deletions src/injector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand Down Expand Up @@ -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) {
Expand All @@ -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,
});
Expand Down
3 changes: 3 additions & 0 deletions test/injector/whitelisted-props/input.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Foo(props: { className: string }) {
return <div {...props}></div>;
}
11 changes: 11 additions & 0 deletions test/injector/whitelisted-props/options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { TestOptions } from '../../types';

const options: TestOptions = {
injector: {
shouldInclude() {
return true;
},
},
};

export default options;
10 changes: 10 additions & 0 deletions test/injector/whitelisted-props/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import PropTypes from 'prop-types';
function Foo(props) {
return <div {...props}></div>;
}

Foo.propTypes = {
className: PropTypes.string.isRequired,
};

export default Foo;
12 changes: 12 additions & 0 deletions test/injector/whitelisted-props/output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"type": "ProgramNode",
"body": [
{
"type": "ComponentNode",
"name": "Foo",
"types": [
{ "type": "PropTypeNode", "name": "className", "propType": { "type": "StringNode" } }
]
}
]
}

0 comments on commit 277258d

Please sign in to comment.