Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: only export custom prop-type functions #254

Merged
merged 13 commits into from
Sep 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,13 @@ const { config } = require('@dhis2/cli-style')

module.exports = {
...require(config.prettier),
overrides: [
{
files: '**/__testfixtures__/*.js',
options: {
// Codemod does not support spreading imports over multiple lines
printWidth: 10000,
},
},
],
}
11 changes: 11 additions & 0 deletions codemods/__testfixtures__/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const { config } = require('@dhis2/cli-style')

module.exports = {
extends: [config.eslintReact],
rules: {
'no-unused-vars': 'off',
'no-undef': 'off',
'react/no-unused-prop-types': 'off',
'react/sort-prop-types': 'off',
},
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import PropTypes from '@dhis2/prop-types'
import React from 'react'

const Child = () => <span>child</span>

const MyComponent = ({ child }) => <div>{child}</div>

const statusProp = PropTypes.mutuallyExclusive(['success', 'warning', 'error'], PropTypes.bool)

MyComponent.propTypes = {
arr: PropTypes.arrayWithLength(1, 2, PropTypes.number),
cond: PropTypes.conditional(props => (props.multiple ? PropTypes.arrayOf(PropTypes.number) : PropTypes.number)),
multiple: PropTypes.bool,
success: statusProp,
warning: statusProp,
error: statusProp,
child: PropTypes.instanceOfComponent(Child),
errorMsg: PropTypes.requiredIf(props => props.error, PropTypes.string),
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { arrayWithLength, conditional, instanceOfComponent, mutuallyExclusive, requiredIf } from '@dhis2/prop-types'
import PropTypes from 'prop-types'
import React from 'react'

const Child = () => <span>child</span>

const MyComponent = ({ child }) => <div>{child}</div>

const statusProp = mutuallyExclusive(['success', 'warning', 'error'], PropTypes.bool)

MyComponent.propTypes = {
arr: arrayWithLength(1, 2, PropTypes.number),
cond: conditional(props => (props.multiple ? PropTypes.arrayOf(PropTypes.number) : PropTypes.number)),
multiple: PropTypes.bool,
success: statusProp,
warning: statusProp,
error: statusProp,
child: instanceOfComponent(Child),
errorMsg: requiredIf(props => props.error, PropTypes.string),
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import PropTypes from '@dhis2/prop-types'
import React from 'react'

const statusProp = PropTypes.mutuallyExclusive(['success', 'warning', 'error'], PropTypes.bool)

const Child = () => <span>child</span>

export class MyComponent extends React.Component {
static propTypes = {
arr: PropTypes.arrayWithLength(1, 2, PropTypes.number),
cond: PropTypes.conditional(props => (props.multiple ? PropTypes.arrayOf(PropTypes.number) : PropTypes.number)),
multiple: PropTypes.bool,
success: statusProp,
warning: statusProp,
error: statusProp,
child: PropTypes.instanceOfComponent(Child),
errorMsg: PropTypes.requiredIf(props => props.error, PropTypes.string),
}

render() {
return <div>{this.props.child}</div>
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { arrayWithLength, conditional, instanceOfComponent, mutuallyExclusive, requiredIf } from '@dhis2/prop-types'
import PropTypes from 'prop-types'
import React from 'react'

const statusProp = mutuallyExclusive(['success', 'warning', 'error'], PropTypes.bool)

const Child = () => <span>child</span>

export class MyComponent extends React.Component {
static propTypes = {
arr: arrayWithLength(1, 2, PropTypes.number),
cond: conditional(props => (props.multiple ? PropTypes.arrayOf(PropTypes.number) : PropTypes.number)),
multiple: PropTypes.bool,
success: statusProp,
warning: statusProp,
error: statusProp,
child: instanceOfComponent(Child),
errorMsg: requiredIf(props => props.error, PropTypes.string),
}

render() {
return <div>{this.props.child}</div>
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import React from 'react'
import { any, array, arrayOf, bool, element, elementType, exact, func, instanceOf, node, number, object, objectOf, oneOf, oneOfType, shape, string, symbol } from 'prop-types'

const MyComponent = () => {}

const specialProp = shape({
flag: bool,
id: number,
})

MyComponent.propTypes = {
optionalArray: array,
optionalBool: bool,
optionalFunc: func,
optionalNumber: number,
optionalObject: object,
optionalString: string,
optionalSymbol: symbol,
optionalNode: node,
optionalElement: element,
optionalElementType: elementType,
optionalMessage: instanceOf(Message),
optionalEnum: oneOf(['News', 'Photos']),
optionalUnion: oneOfType([string, number, instanceOf(Message)]),
optionalArrayOf: arrayOf(number),
optionalObjectOf: objectOf(number),
optionalObjectWithShape: shape({
optionalProperty: string,
requiredProperty: number,
}),
optionalObjectWithStrictShape: exact({
optionalProperty: string,
requiredProperty: number,
}),
requiredFunc: func,
requiredAny: any,
aDeeplyNestedThing: arrayOf(
shape({
subList: arrayOf(
shape({
id: number,
content: oneOfType([bool, number, string]),
})
),
id: oneOf([1, 2, 3]),
})
),
aSpecialProp: specialProp,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import React from 'react'
import PropTypes from 'prop-types'

const MyComponent = () => {}

const specialProp = PropTypes.shape({
flag: PropTypes.bool,
id: PropTypes.number,
})

MyComponent.propTypes = {
optionalArray: PropTypes.array,
optionalBool: PropTypes.bool,
optionalFunc: PropTypes.func,
optionalNumber: PropTypes.number,
optionalObject: PropTypes.object,
optionalString: PropTypes.string,
optionalSymbol: PropTypes.symbol,
optionalNode: PropTypes.node,
optionalElement: PropTypes.element,
optionalElementType: PropTypes.elementType,
optionalMessage: PropTypes.instanceOf(Message),
optionalEnum: PropTypes.oneOf(['News', 'Photos']),
optionalUnion: PropTypes.oneOfType([PropTypes.string, PropTypes.number, PropTypes.instanceOf(Message)]),
optionalArrayOf: PropTypes.arrayOf(PropTypes.number),
optionalObjectOf: PropTypes.objectOf(PropTypes.number),
optionalObjectWithShape: PropTypes.shape({
optionalProperty: PropTypes.string,
requiredProperty: PropTypes.number,
}),
optionalObjectWithStrictShape: PropTypes.exact({
optionalProperty: PropTypes.string,
requiredProperty: PropTypes.number,
}),
requiredFunc: PropTypes.func,
requiredAny: PropTypes.any,
aDeeplyNestedThing: PropTypes.arrayOf(
PropTypes.shape({
subList: PropTypes.arrayOf(
PropTypes.shape({
id: PropTypes.number,
content: PropTypes.oneOfType([PropTypes.bool, PropTypes.number, PropTypes.string]),
})
),
id: PropTypes.oneOf([1, 2, 3]),
})
),
aSpecialProp: specialProp,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import React from 'react'
import { any, array, arrayOf, bool, element, elementType, exact, func, instanceOf, node, number, object, objectOf, oneOf, oneOfType, shape, string, symbol } from 'prop-types'

const specialProp = shape({
flag: bool,
id: number,
})

export class MyComponent extends React.Component {
static propTypes = {
optionalArray: array,
optionalBool: bool,
optionalFunc: func,
optionalNumber: number,
optionalObject: object,
optionalString: string,
optionalSymbol: symbol,
optionalNode: node,
optionalElement: element,
optionalElementType: elementType,
optionalMessage: instanceOf(Message),
optionalEnum: oneOf(['News', 'Photos']),
optionalUnion: oneOfType([string, number, instanceOf(Message)]),
optionalArrayOf: arrayOf(number),
optionalObjectOf: objectOf(number),
optionalObjectWithShape: shape({
optionalProperty: string,
requiredProperty: number,
}),
optionalObjectWithStrictShape: exact({
optionalProperty: string,
requiredProperty: number,
}),
requiredFunc: func,
requiredAny: any,
aDeeplyNestedThing: arrayOf(
shape({
subList: arrayOf(
shape({
id: number,
content: oneOfType([bool, number, string]),
})
),
id: oneOf([1, 2, 3]),
})
),
aSpecialProp: specialProp,
}

render() {
return <h1>Hello</h1>
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import React from 'react'
import PropTypes from 'prop-types'

const specialProp = PropTypes.shape({
flag: PropTypes.bool,
id: PropTypes.number,
})

export class MyComponent extends React.Component {
static propTypes = {
optionalArray: PropTypes.array,
optionalBool: PropTypes.bool,
optionalFunc: PropTypes.func,
optionalNumber: PropTypes.number,
optionalObject: PropTypes.object,
optionalString: PropTypes.string,
optionalSymbol: PropTypes.symbol,
optionalNode: PropTypes.node,
optionalElement: PropTypes.element,
optionalElementType: PropTypes.elementType,
optionalMessage: PropTypes.instanceOf(Message),
optionalEnum: PropTypes.oneOf(['News', 'Photos']),
optionalUnion: PropTypes.oneOfType([PropTypes.string, PropTypes.number, PropTypes.instanceOf(Message)]),
optionalArrayOf: PropTypes.arrayOf(PropTypes.number),
optionalObjectOf: PropTypes.objectOf(PropTypes.number),
optionalObjectWithShape: PropTypes.shape({
optionalProperty: PropTypes.string,
requiredProperty: PropTypes.number,
}),
optionalObjectWithStrictShape: PropTypes.exact({
optionalProperty: PropTypes.string,
requiredProperty: PropTypes.number,
}),
requiredFunc: PropTypes.func,
requiredAny: PropTypes.any,
aDeeplyNestedThing: PropTypes.arrayOf(
PropTypes.shape({
subList: PropTypes.arrayOf(
PropTypes.shape({
id: PropTypes.number,
content: PropTypes.oneOfType([PropTypes.bool, PropTypes.number, PropTypes.string]),
})
),
id: PropTypes.oneOf([1, 2, 3]),
})
),
aSpecialProp: specialProp,
}

render() {
return <h1>Hello</h1>
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import PropTypes from '@dhis2/prop-types'
import React from 'react'

const Child = () => <span>child</span>

const MyComponent = ({ child }) => <div>{child}</div>

const statusProp = PropTypes.mutuallyExclusive(['success', 'warning', 'error'], PropTypes.bool)

MyComponent.propTypes = {
arr: PropTypes.arrayWithLength(1, 2, PropTypes.number).isRequired,
cond: PropTypes.conditional(props => (props.multiple ? PropTypes.arrayOf(PropTypes.number) : PropTypes.number)).isRequired,
multiple: PropTypes.bool.isRequired,
success: statusProp.isRequired,
warning: statusProp.isRequired,
error: statusProp.isRequired,
child: PropTypes.instanceOfComponent(Child).isRequired,
errorMsg: PropTypes.requiredIf(props => props.error, PropTypes.string).isRequired,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { arrayWithLength, conditional, instanceOfComponent, mutuallyExclusive, requiredIf } from '@dhis2/prop-types'
import PropTypes from 'prop-types'
import React from 'react'

const Child = () => <span>child</span>

const MyComponent = ({ child }) => <div>{child}</div>

const statusProp = mutuallyExclusive(['success', 'warning', 'error'], PropTypes.bool)

MyComponent.propTypes = {
arr: arrayWithLength(1, 2, PropTypes.number).isRequired,
cond: conditional(props => (props.multiple ? PropTypes.arrayOf(PropTypes.number) : PropTypes.number)).isRequired,
multiple: PropTypes.bool.isRequired,
success: statusProp.isRequired,
warning: statusProp.isRequired,
error: statusProp.isRequired,
child: instanceOfComponent(Child).isRequired,
errorMsg: requiredIf(props => props.error, PropTypes.string).isRequired,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import PropTypes from '@dhis2/prop-types'
import React from 'react'

const statusProp = PropTypes.mutuallyExclusive(['success', 'warning', 'error'], PropTypes.bool).isRequired

const Child = () => <span>child</span>

export class MyComponent extends React.Component {
static propTypes = {
arr: PropTypes.arrayWithLength(1, 2, PropTypes.number).isRequired,
cond: PropTypes.conditional(props => (props.multiple ? PropTypes.arrayOf(PropTypes.number) : PropTypes.number)).isRequired,
multiple: PropTypes.bool.isRequired,
success: statusProp,
warning: statusProp,
error: statusProp,
child: PropTypes.instanceOfComponent(Child).isRequired,
errorMsg: PropTypes.requiredIf(props => props.error, PropTypes.string).isRequired,
}

render() {
return <div>{this.props.child}</div>
}
}
Loading