Skip to content

Commit d3576c7

Browse files
authored
prop-deprecation - required prop - support method and member (#1510)
* prop-deprecation - required prop - support method and member * Prettify * indent
1 parent 719d61e commit d3576c7

File tree

3 files changed

+43
-13
lines changed

3 files changed

+43
-13
lines changed

eslint-rules/lib/rules/component-prop-deprecation.js

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,28 @@ module.exports = {
115115
function testAttributeForDeprecation(attribute, deprecatedPropList, componentName) {
116116
let wasFound = false;
117117
if (attribute.type === 'JSXAttribute') {
118-
wasFound = checkPropDeprecation(attribute, attribute.name, attribute.name.name, deprecatedPropList, componentName);
118+
wasFound = checkPropDeprecation(
119+
attribute,
120+
attribute.name,
121+
attribute.name.name,
122+
deprecatedPropList,
123+
componentName
124+
);
119125
} else if (attribute.type === 'JSXSpreadAttribute') {
120-
const spreadSource = findValueNodeOfIdentifier(attribute.argument.name, context.getScope());
126+
const identifierName =
127+
_.get(attribute, 'argument.name') ||
128+
_.get(attribute, 'argument.callee.name') ||
129+
_.get(attribute, 'argument.property.name');
130+
const spreadSource = findValueNodeOfIdentifier(identifierName, context.getScope());
121131
if (spreadSource) {
122-
_.forEach(spreadSource.properties, property => {
123-
const key = _.get(property, 'key');
124-
const propName = _.get(property, 'key.name');
125-
wasFound = checkPropDeprecation(key, key, propName, deprecatedPropList, componentName);
126-
});
132+
const properties = _.get(spreadSource, 'properties') || _.get(spreadSource, 'body.properties');
133+
if (properties) {
134+
_.forEach(properties, property => {
135+
const key = _.get(property, 'key');
136+
const propName = _.get(property, 'key.name');
137+
wasFound = checkPropDeprecation(key, key, propName, deprecatedPropList, componentName);
138+
});
139+
}
127140
}
128141
}
129142
return wasFound;
@@ -160,12 +173,13 @@ module.exports = {
160173
/* handle required props */
161174
let foundAttribute = false;
162175
attributes.forEach(attribute => {
163-
foundAttribute = foundAttribute || testAttributeForDeprecation(attribute, requiredPropList, componentName);
176+
foundAttribute =
177+
foundAttribute || testAttributeForDeprecation(attribute, requiredPropList, componentName);
164178
});
165-
166-
if (!foundAttribute && requiredPropList[0]) {
179+
180+
if (!foundAttribute && requiredPropList[0]) {
167181
const prop = requiredPropList[0];
168-
reportRequiredProps({node, name: componentName, prop: prop.prop, message: prop.message})
182+
reportRequiredProps({node, name: componentName, prop: prop.prop, message: prop.message});
169183
}
170184
});
171185
}

eslint-rules/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "eslint-plugin-uilib",
3-
"version": "2.1.3",
3+
"version": "2.1.4",
44
"description": "uilib set of eslint rules",
55
"keywords": [
66
"eslint",

eslint-rules/tests/lib/rules/component-prop-deprecation.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,22 @@ ruleTester.run('component-prop-deprecation', rule, {
9797
options: ruleOptions,
9898
code: 'import {Picker} from \'module-with-deprecations\'; pickerProps = {t: "title", s:"subtitle", migrate: true}; <Picker {...pickerProps}/>'
9999
},
100+
{
101+
options: ruleOptions,
102+
code: `
103+
import {Picker} from 'module-with-deprecations';
104+
const getPickerProps = () => ({t: "title", s:"subtitle", migrate: true});
105+
<Picker {...getPickerProps()}/>
106+
`
107+
},
108+
{
109+
options: ruleOptions,
110+
code: `
111+
import {Picker} from 'module-with-deprecations';
112+
pickerProps = {t: "title", s:"subtitle", migrate: true};
113+
<Picker {...this.pickerProps}/>
114+
`
115+
}
100116
],
101117
invalid: [
102118
{
@@ -367,7 +383,7 @@ ruleTester.run('component-prop-deprecation', rule, {
367383
options: ruleOptions,
368384
code: 'import {Picker} from \'module-with-deprecations\'; <Picker t="title" s="subtitle"/>',
369385
errors: [
370-
{message: "The 'Picker' component's prop 'migrate' is required. Please make sure to pass the 'migrate' prop."},
386+
{message: "The 'Picker' component's prop 'migrate' is required. Please make sure to pass the 'migrate' prop."}
371387
]
372388
}
373389
]

0 commit comments

Comments
 (0)