Skip to content

Commit 91fe059

Browse files
authored
Infra/delete utils_old file (#1236)
* fix screen UI * Infra/ remove utils_old * use getComponentLocalName * use addToImports and getComponentName utils
1 parent 3e76660 commit 91fe059

File tree

6 files changed

+55
-155
lines changed

6 files changed

+55
-155
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ module.exports = {
9696
if (attribute.type === 'JSXAttribute') {
9797
checkPropDeprecation(attribute, attribute.name, attribute.name.name, deprecatedPropList, componentName);
9898
} else if (attribute.type === 'JSXSpreadAttribute') {
99-
const spreadSource = findValueNodeOfIdentifier(attribute.argument.name, context);
99+
const spreadSource = findValueNodeOfIdentifier(attribute.argument.name, context.getScope());
100100
if (spreadSource) {
101101
_.forEach(spreadSource.properties, property => {
102102
const key = _.get(property, 'key');

eslint-rules/lib/rules/prop-value-shape-deprecation.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const _ = require('lodash');
2-
const {findValueNodeOfIdentifier, getComponentName, getPathPrefix, getPathSuffix} = require('../utils_old');
2+
const {findValueNodeOfIdentifier, getComponentLocalName, addToImports, getComponentName} = require('../utils');
33

44
const MAP_SCHEMA = {
55
type: 'object',
@@ -52,7 +52,7 @@ module.exports = {
5252
},
5353
create(context) {
5454
function reportPropValueShapeDeprecation(propKey, prop, deprecation, node) {
55-
const componentName = getComponentName(node);
55+
const componentName = getComponentName(getComponentLocalName(node), imports);
5656
const newProp = _.get(deprecation, 'fix.propName');
5757
const fixMessage = _.get(deprecation, 'message') ? ' ' + _.get(deprecation, 'message') : '';
5858
const message = `The shape of '${prop}' prop of '${componentName}' doesn't contain '${deprecation.prop}' anymore.${fixMessage}`;
@@ -67,10 +67,12 @@ module.exports = {
6767
});
6868
}
6969

70+
const imports = [];
71+
7072
function testJSXAttributes(node) {
7173
try {
7274
const {deprecations} = _.get(context, 'options[0]');
73-
const componentName = getComponentName(node);
75+
const componentName = getComponentName(getComponentLocalName(node), imports);
7476
_.forEach(deprecations, deprecation => {
7577
if (_.includes(deprecation.components, componentName)) {
7678
_.forEach(node.attributes, attribute => {
@@ -130,6 +132,16 @@ module.exports = {
130132
}
131133
}
132134

135+
function getPathPrefix(str) {
136+
const index = str.indexOf('.');
137+
return index === -1 ? str : str.substring(0, index);
138+
}
139+
140+
function getPathSuffix(str) {
141+
const index = str.indexOf('.');
142+
return index === -1 ? undefined : str.substring(index + 1);
143+
}
144+
133145
function checkAttributeProperties(attributeProperties, attributeName, deprecation, node) {
134146
for (let i = 0; i < attributeProperties.length; i++) {
135147
const propertyType = _.get(attributeProperties[i], 'type');
@@ -146,6 +158,8 @@ module.exports = {
146158
}
147159

148160
return {
161+
ImportDeclaration: node => addToImports(node, imports),
162+
VariableDeclarator: node => addToImports(node, imports),
149163
JSXOpeningElement: testJSXAttributes
150164
};
151165
}
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
function findValueNodeOfIdentifier(identifierName, context) {
2-
const scope = context.getScope();
3-
const {variables} = scope;
1+
function findValueNodeOfIdentifier(identifierName, scope) {
2+
const varsInScope = scope.variables;
43
let valueNode = false;
5-
variables.forEach(variable => {
4+
varsInScope.forEach((variable) => {
65
if (variable.name === identifierName) {
76
if (variable.defs) {
87
valueNode = variable.defs[variable.defs.length - 1].node.init;
98
}
109
}
1110
});
12-
13-
return valueNode;
11+
if (scope.upper === null) {
12+
return valueNode;
13+
}
14+
return valueNode || findValueNodeOfIdentifier(identifierName, scope.upper);
1415
}
1516

17+
1618
module.exports = {
1719
findValueNodeOfIdentifier
1820
};

eslint-rules/lib/utils/noHardCodedUtils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function findAndReportHardCodedValues(value, reporter, context, depthOfSearch =
1313
findAndReportHardCodedValues(value.consequent, reporter, context, depthOfSearch - 1);
1414
findAndReportHardCodedValues(value.alternate, reporter, context, depthOfSearch - 1);
1515
} else if (value.type === 'Identifier') {
16-
findAndReportHardCodedValues(findValueNodeOfIdentifier(value.name, context), reporter, context, depthOfSearch - 1);
16+
findAndReportHardCodedValues(findValueNodeOfIdentifier(value.name, context.getScope()), reporter, context, depthOfSearch - 1);
1717
}
1818
}
1919

eslint-rules/lib/utils_old.js

Lines changed: 0 additions & 126 deletions
This file was deleted.

eslint-rules/tests/lib/rules/prop-value-shape-deprecation.js

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,26 @@ const imageSource = 'imageSource: {uri: some_uri}';
1515
const source = 'source: {uri: some_uri}';
1616
const ruleOptions = [{deprecations: deprecationsJson}];
1717
const PassedPropExampleCode = `
18-
const myProps = {
19-
goodProp: goodValue,
20-
${imageSource}
21-
};
18+
import {Label} from 'our-source';
19+
const myProps = {
20+
goodProp: goodValue,
21+
${imageSource}
22+
};
2223
23-
<Label avatar={myProps}/>
24+
<Label avatar={myProps}/>
2425
`;
2526
const PassedPropExampleOutput = `
26-
const myProps = {
27-
goodProp: goodValue,
28-
${source}
29-
};
27+
import {Label} from 'our-source';
28+
const myProps = {
29+
goodProp: goodValue,
30+
${source}
31+
};
3032
31-
<Label avatar={myProps}/>
33+
<Label avatar={myProps}/>
3234
`;
3335

3436
const firstLevelSpreadCode = `
37+
import {Label} from 'our-source';
3538
const myProps = {
3639
avatarProps: {
3740
goodProp: goodValue,
@@ -43,6 +46,7 @@ const myProps = {
4346
`;
4447

4548
const firstLevelSpreadOutput = `
49+
import {Label} from 'our-source';
4650
const myProps = {
4751
avatarProps: {
4852
goodProp: goodValue,
@@ -54,6 +58,7 @@ const myProps = {
5458
`;
5559

5660
const secondLevelSpreadCode = `
61+
import {Label} from 'our-source';
5762
const myProps = {
5863
goodProp: goodValue,
5964
${imageSource}
@@ -63,6 +68,7 @@ const myProps = {
6368
`;
6469

6570
const secondLevelSpreadOutput = `
71+
import {Label} from 'our-source';
6672
const myProps = {
6773
goodProp: goodValue,
6874
${source}
@@ -103,33 +109,33 @@ ruleTester.run('prop-value-shape-deprecation', rule, {
103109
invalid: [
104110
{
105111
options: ruleOptions,
106-
code: `<ListItem avatar={{${imageSource}, someProp: someValue}}/>`,
112+
code: `import {ListItem} from 'our-source'; <ListItem avatar={{${imageSource}, someProp: someValue}}/>`,
107113
errors: [
108114
{
109115
message: `The shape of 'avatar' prop of 'ListItem' doesn't contain 'imageSource' anymore. Please use 'source' instead (fix is available).`
110116
}
111117
],
112-
output: `<ListItem avatar={{${source}, someProp: someValue}}/>`
118+
output: `import {ListItem} from 'our-source'; <ListItem avatar={{${source}, someProp: someValue}}/>`
113119
},
114120
{
115121
options: ruleOptions,
116-
code: `<ListItem avatar={{someProp: someValue, ${imageSource}}}/>`,
122+
code: `import {ListItem} from 'our-source'; <ListItem avatar={{someProp: someValue, ${imageSource}}}/>`,
117123
errors: [
118124
{
119125
message: `The shape of 'avatar' prop of 'ListItem' doesn't contain 'imageSource' anymore. Please use 'source' instead (fix is available).`
120126
}
121127
],
122-
output: `<ListItem avatar={{someProp: someValue, ${source}}}/>`
128+
output: `import {ListItem} from 'our-source'; <ListItem avatar={{someProp: someValue, ${source}}}/>`
123129
},
124130
{
125131
options: ruleOptions,
126-
code: `<ListItem someProp={someValue} avatar={{${imageSource}}}/>`,
132+
code: `import {ListItem} from 'our-source'; <ListItem someProp={someValue} avatar={{${imageSource}}}/>`,
127133
errors: [
128134
{
129135
message: `The shape of 'avatar' prop of 'ListItem' doesn't contain 'imageSource' anymore. Please use 'source' instead (fix is available).`
130136
}
131137
],
132-
output: `<ListItem someProp={someValue} avatar={{${source}}}/>`
138+
output: `import {ListItem} from 'our-source'; <ListItem someProp={someValue} avatar={{${source}}}/>`
133139
},
134140
{
135141
options: ruleOptions,
@@ -202,7 +208,9 @@ ruleTester.run('prop-value-shape-deprecation', rule, {
202208
},
203209
{
204210
options: ruleOptions,
205-
code: `const myProps1 = {
211+
code: `
212+
import {Label} from 'our-source';
213+
const myProps1 = {
206214
avatarProps: {
207215
${imageSource},
208216
goodProp1: goodValue1
@@ -225,7 +233,9 @@ ruleTester.run('prop-value-shape-deprecation', rule, {
225233
},
226234
{
227235
options: ruleOptions,
228-
code: `const myProps1 = {
236+
code: `
237+
import {Label} from 'our-source';
238+
const myProps1 = {
229239
buttonProps: {
230240
goodProp1: goodValue1,
231241
goodProp2: goodValue2,

0 commit comments

Comments
 (0)