Skip to content

Commit

Permalink
feat(injector): add component to shouldInclude (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon authored Mar 31, 2020
1 parent 14bb785 commit 18a7fce
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/injector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ export type InjectOptions = {
* use the default behaviour
* @default includeUnusedProps ? true : data.usedProps.includes(data.prop.name)
*/
shouldInclude?(data: { prop: t.PropTypeNode; usedProps: string[] }): boolean | undefined;
shouldInclude?(data: {
component: t.ComponentNode;
prop: t.PropTypeNode;
usedProps: string[];
}): boolean | undefined;

/**
* Options passed to babel.transformSync
Expand Down Expand Up @@ -82,7 +86,7 @@ function plugin(
): babel.PluginObj {
const { includeUnusedProps = false, removeExistingPropTypes = false, ...otherOptions } = options;

const shouldInclude: InjectOptions['shouldInclude'] = (data) => {
const shouldInclude: Exclude<InjectOptions['shouldInclude'], undefined> = (data) => {
if (options.shouldInclude) {
const result = options.shouldInclude(data);
if (result !== undefined) {
Expand Down Expand Up @@ -263,7 +267,7 @@ function plugin(
const source = generate(props, {
...otherOptions,
importedName: importName,
shouldInclude: (prop) => shouldInclude!({ prop, usedProps }),
shouldInclude: (prop) => shouldInclude({ component: props, prop, usedProps }),
});

if (source.length === 0) {
Expand Down
20 changes: 20 additions & 0 deletions test/injector/should-include-component-based/input.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import * as React from 'react';

export interface SnackBarProps {
/**
* Some hints about why this is useful
*/
id?: string;
/**
* some prop that is inherited which we don't care about here
*/
onChange?: () => void;
}

export function Snackbar(props: SnackBarProps) {
return <div {...props} />;
}

export function SomeOtherComponent(props: { id?: string }) {
return <div {...props} />;
}
13 changes: 13 additions & 0 deletions test/injector/should-include-component-based/options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { TestOptions } from '../../types';

const options: TestOptions = {
injector: {
shouldInclude({ component, prop }) {
if (component.name === 'Snackbar' && prop.name === 'id') {
return true;
}
},
},
};

export default options;
17 changes: 17 additions & 0 deletions test/injector/should-include-component-based/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as React from 'react';
import PropTypes from 'prop-types';
function Snackbar(props) {
return <div {...props} />;
}

Snackbar.propTypes = {
/**
* Some hints about why this is useful
*/
id: PropTypes.string,
};

export { Snackbar };
export function SomeOtherComponent(props) {
return <div {...props} />;
}
43 changes: 43 additions & 0 deletions test/injector/should-include-component-based/output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"type": "ProgramNode",
"body": [
{
"type": "ComponentNode",
"name": "Snackbar",
"types": [
{
"type": "PropTypeNode",
"name": "id",
"jsDoc": "Some hints about why this is useful",
"propType": {
"type": "UnionNode",
"types": [{ "type": "UndefinedNode" }, { "type": "StringNode" }]
}
},
{
"type": "PropTypeNode",
"name": "onChange",
"jsDoc": "some prop that is inherited which we don't care about here",
"propType": {
"type": "UnionNode",
"types": [{ "type": "UndefinedNode" }, { "type": "FunctionNode" }]
}
}
]
},
{
"type": "ComponentNode",
"name": "SomeOtherComponent",
"types": [
{
"type": "PropTypeNode",
"name": "id",
"propType": {
"type": "UnionNode",
"types": [{ "type": "UndefinedNode" }, { "type": "StringNode" }]
}
}
]
}
]
}

0 comments on commit 18a7fce

Please sign in to comment.