Skip to content

Commit

Permalink
fix: handle all props getting ignored by shouldInclude
Browse files Browse the repository at this point in the history
  • Loading branch information
merceyz committed Mar 28, 2020
1 parent bfbe85a commit b69112e
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 10 deletions.
14 changes: 10 additions & 4 deletions src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ export function generate(node: t.Node | t.PropTypeNode[], options: GenerateOptio
filteredNodes = filteredNodes.filter((x) => shouldInclude(x));
}

if (filteredNodes.length === 0) {
return '';
}

return filteredNodes
.map((prop) => generate(prop, options))
.reduce((prev, curr) => `${prev}\n${curr}`);
Expand All @@ -82,14 +86,16 @@ export function generate(node: t.Node | t.PropTypeNode[], options: GenerateOptio
}

if (t.isComponentNode(node)) {
const generated = generate(node.types, options);
if (generated.length === 0) {
return '';
}

const comment =
options.comment &&
`// ${options.comment.split(/\r?\n/gm).reduce((prev, curr) => `${prev}\n// ${curr}`)}\n`;

return `${node.name}.propTypes = {\n${comment ? comment : ''}${generate(
node.types,
options
)}\n}`;
return `${node.name}.propTypes = {\n${comment ? comment : ''}${generated}\n}`;
}

if (t.isPropTypeNode(node)) {
Expand Down
12 changes: 6 additions & 6 deletions src/injector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,6 @@ function plugin(

if (usedProps.length === 0 && !includeUnusedProps) return;

needImport = true;

// Prevent visiting again
(node as any).hasBeenVisited = true;
path.skip();
Expand Down Expand Up @@ -216,8 +214,6 @@ function plugin(

if (usedProps.length === 0 && !includeUnusedProps) return;

needImport = true;

// Prevent visiting again
(node as any).hasBeenVisited = true;
path.skip();
Expand All @@ -244,8 +240,6 @@ function plugin(

if (usedProps.length === 0 && !includeUnusedProps) return;

needImport = true;

// Prevent visiting again
(node as any).hasBeenVisited = true;
path.skip();
Expand Down Expand Up @@ -274,6 +268,12 @@ function plugin(
shouldInclude: (prop) => shouldInclude!({ prop, usedProps }),
});

if (source.length === 0) {
return;
}

needImport = true;

const placeholder = `const a${uuid().replace(/\-/g, '_')} = null;`;

mapOfPropTypes.set(placeholder, source);
Expand Down
3 changes: 3 additions & 0 deletions test/injector/all-props-ignored/input.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function Foo(props: { className: string }) {
return <div className={props.className}></div>;
}
11 changes: 11 additions & 0 deletions test/injector/all-props-ignored/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 false;
},
},
};

export default options;
3 changes: 3 additions & 0 deletions test/injector/all-props-ignored/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function Foo(props) {
return <div className={props.className}></div>;
}
12 changes: 12 additions & 0 deletions test/injector/all-props-ignored/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 b69112e

Please sign in to comment.