Skip to content

Support checking for required props in a component #1320

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

Merged
merged 1 commit into from
May 30, 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
47 changes: 41 additions & 6 deletions eslint-rules/lib/rules/component-prop-deprecation.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ const MAP_SCHEMA = {
message: {
type: 'string'
},
isRequired: {
type: 'boolean'
},
fix: {
type: 'object',
required: ['propName'],
Expand Down Expand Up @@ -80,32 +83,48 @@ module.exports = {
}
}

function reportRequiredProps({node, name, prop, message: customMessage}) {
try {
const message = `The '${name}' component's prop '${prop}' is required. ${customMessage}`;
context.report({
node: node,
message
});
} catch (err) {
console.log('Found error in: ', context.getFilename());
}
}

const {deprecations} = context.options[0];
const organizedDeprecations = organizeDeprecations(deprecations);

const imports = [];

function checkPropDeprecation(node, fixNode, propName, deprecatedPropList, componentName) {
const deprecatedProp = _.find(deprecatedPropList, {prop: propName});
if (deprecatedProp) {
if (deprecatedProp && !deprecatedProp.isRequired) {
const {prop, message, fix} = deprecatedProp;
reportDeprecatedProps({node, name: componentName, prop, message, fixNode, fix});
}

return !!deprecatedProp;
}

function testAttributeForDeprecation(attribute, deprecatedPropList, componentName) {
let wasFound = false;
if (attribute.type === 'JSXAttribute') {
checkPropDeprecation(attribute, attribute.name, attribute.name.name, deprecatedPropList, componentName);
wasFound = checkPropDeprecation(attribute, attribute.name, attribute.name.name, deprecatedPropList, componentName);
} else if (attribute.type === 'JSXSpreadAttribute') {
const spreadSource = findValueNodeOfIdentifier(attribute.argument.name, context.getScope());
if (spreadSource) {
_.forEach(spreadSource.properties, property => {
const key = _.get(property, 'key');
const propName = _.get(property, 'key.name');
checkPropDeprecation(key, key, propName, deprecatedPropList, componentName);
wasFound = checkPropDeprecation(key, key, propName, deprecatedPropList, componentName);
});
}
}
return wasFound;
}

function deprecationCheck(node) {
Expand All @@ -123,13 +142,29 @@ module.exports = {
currentImport,
deprecationSource
);

foundPossibleDeprecations.forEach(foundPossibleDeprecation => {
const deprecatedPropList = foundPossibleDeprecation.props;
const deprecatedPropList = [...foundPossibleDeprecation.props];
const requiredPropList = _.remove(deprecatedPropList, p => !!p.isRequired);
const attributes = node.attributes;

/* handle deprecated props */
if (!_.isEmpty(deprecatedPropList)) {
attributes.forEach(attribute => {
testAttributeForDeprecation(attribute, deprecatedPropList, componentName);
});
}

/* handle required props */
let foundAttribute = false;
attributes.forEach(attribute => {
testAttributeForDeprecation(attribute, deprecatedPropList, componentName);
foundAttribute = foundAttribute || testAttributeForDeprecation(attribute, requiredPropList, componentName);
});

if (!foundAttribute && requiredPropList[0]) {
const prop = requiredPropList[0];
reportRequiredProps({node, name: componentName, prop: prop.prop, message: prop.message})
}
});
}
}
Expand Down
11 changes: 11 additions & 0 deletions eslint-rules/tests/component_prop_deprecation.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@
"fix": {"propName": "subtitle"}
}
]
},
{
"component": "Picker",
"source": "module-with-deprecations",
"props": [
{
"prop": "migrate",
"message": "Please make sure to pass the 'migrate' prop.",
"isRequired": true
}
]
}
]

16 changes: 15 additions & 1 deletion eslint-rules/tests/lib/rules/component-prop-deprecation.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,14 @@ ruleTester.run('component-prop-deprecation', rule, {
import {View as V} from 'module-with-deprecations';
<Button text="my button"/>
`
}
},
{
options: ruleOptions,
code: `
import {Picker} from 'module-with-deprecations';
<Picker value="value" migrate={true}/>
`
},
],
invalid: [
{
Expand Down Expand Up @@ -347,6 +354,13 @@ ruleTester.run('component-prop-deprecation', rule, {
{message: "The 'Text' component's prop 't' is deprecated. Please use the 'title' prop instead."},
{message: "The 'Text' component's prop 's' is deprecated. Please use the 'subtitle' prop instead."}
]
},
{
options: ruleOptions,
code: 'import {Picker} from \'module-with-deprecations\'; <Picker t="title" s="subtitle"/>',
errors: [
{message: "The 'Picker' component's prop 'migrate' is required. Please make sure to pass the 'migrate' prop."},
]
}
]
});