Skip to content

Commit 5b13328

Browse files
authored
Lint - fix undefined bug when importing all (*) (#769)
* Lint - fix undefined bug when importing all (*) * Move shared code to utils * Bump eslint version
1 parent 70b1006 commit 5b13328

File tree

6 files changed

+174
-23
lines changed

6 files changed

+174
-23
lines changed

eslint-rules/lib/rules/assets-deprecation.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,7 @@ module.exports = {
4444
let localImportSpecifier;
4545

4646
function setLocalImportSpecifier(node) {
47-
const importSource = node.source.value;
48-
if (source === importSource) {
49-
const specifiers = node.specifiers;
50-
if (specifiers) {
51-
localImportSpecifier = _.find(specifiers, specifier => specifier.imported.name === defaultImportName);
52-
if (localImportSpecifier) {
53-
localImportSpecifier = localImportSpecifier.local.name;
54-
}
55-
}
56-
}
47+
localImportSpecifier = utils.getLocalImportSpecifier(node, source, defaultImportName);
5748
}
5849

5950
function getAssetString(node, pathString = '') {
@@ -81,7 +72,7 @@ module.exports = {
8172
}
8273

8374
function testMemberDeprecation(node) {
84-
let assetString = getAssetString(node);
75+
const assetString = getAssetString(node);
8576
if (assetString) {
8677
findAndReportDeprecation(node, assetString);
8778
}

eslint-rules/lib/rules/typography-deprecation.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const _ = require('lodash');
2+
const utils = require('../utils');
23

34
const MAP_SCHEMA = {
45
type: 'object',
@@ -43,16 +44,7 @@ module.exports = {
4344
let localImportSpecifier;
4445

4546
function setLocalImportSpecifier(node) {
46-
const importSource = node.source.value;
47-
if (source === importSource) {
48-
const specifiers = node.specifiers;
49-
if (specifiers) {
50-
localImportSpecifier = _.find(specifiers, specifier => specifier.imported.name === defaultImportName);
51-
if (localImportSpecifier) {
52-
localImportSpecifier = localImportSpecifier.local.name;
53-
}
54-
}
55-
}
47+
localImportSpecifier = utils.getLocalImportSpecifier(node, source, defaultImportName);
5648
}
5749

5850
function findAndReportDeprecation(node, possibleDeprecation) {

eslint-rules/lib/utils.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const _ = require('lodash');
2+
13
// no-hard-coded-color
24
const colorProps = [
35
'color', 'backgroundColor', 'borderColor', 'borderRightColor',
@@ -57,11 +59,32 @@ function findValueNodeOfIdentifier(identifierName, scope) {
5759
return valueNode;
5860
}
5961

62+
function getLocalImportSpecifier(node, source, defaultImportName) {
63+
let localImportSpecifier;
64+
const importSource = node.source.value;
65+
if (source === importSource) {
66+
const specifiers = node.specifiers;
67+
if (specifiers) {
68+
localImportSpecifier = _.find(specifiers, specifier => specifier.imported && specifier.imported.name === defaultImportName);
69+
if (localImportSpecifier) {
70+
localImportSpecifier = localImportSpecifier.local.name;
71+
}
72+
}
73+
74+
if (!localImportSpecifier) { // someone is importing everything (*)
75+
localImportSpecifier = defaultImportName;
76+
}
77+
}
78+
79+
return localImportSpecifier;
80+
}
81+
6082
module.exports = {
6183
isPropFont,
6284
findAndReportHardCodedValues,
6385
propIsColor,
6486
isColorException,
6587
isLiteral,
6688
findValueNodeOfIdentifier,
89+
getLocalImportSpecifier,
6790
};

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": "1.0.20",
3+
"version": "1.0.21",
44
"description": "uilib set of eslint rules",
55
"keywords": [
66
"eslint",

eslint-rules/tests/lib/rules/assets-deprecation.js

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,51 @@ class Example extends React.Component {
142142
prop7={this.props.prop7}
143143
prop8={this.props.prop8}
144144
prop9={this.props.prop9}
145-
icon={validIcon(Assets.icons.deprecated)}
145+
icon={validIcon(Assets.icons.deprecated)}
146+
/>
147+
</ScrollView>
148+
<Component3
149+
prop10={this.props.prop10}
150+
prop11={this.props.prop11}
151+
prop12={this.props.prop12}
152+
/>
153+
</Component2>
154+
);
155+
}
156+
}
157+
158+
export default Example;`;
159+
160+
const fullClassTest2 = `
161+
import * as LetsImportEverything from '${ourSource}';
162+
163+
const {Assets}: typeof LetsImportEverything = require('${ourSource}');
164+
165+
const validIcon = (icon) => (typeof icon === 'number' ? icon : undefined);
166+
167+
class Example extends React.Component {
168+
renderComponent1() {
169+
return this.props.list.map((item) => (
170+
<Component1
171+
prop1={item.data1}
172+
prop2={item.data2}
173+
prop3={item.data3}
174+
prop4={item.data4}
175+
/>
176+
));
177+
}
178+
179+
render() {
180+
return (
181+
<Component2 prop5={this.props.prop5}>
182+
<ScrollView contentContainerStyle={{paddingBottom: 10}}>
183+
{this.renderComponent1()}
184+
<List.Item
185+
prop6={this.props.prop6}
186+
prop7={this.props.prop7}
187+
prop8={this.props.prop8}
188+
prop9={this.props.prop9}
189+
icon={validIcon(Assets.icons.deprecated)}
146190
/>
147191
</ScrollView>
148192
<Component3
@@ -308,6 +352,11 @@ ruleTester.run('assets-deprecation', rule, {
308352
options: options,
309353
code: `${fullClassTest1}`,
310354
errors: [{message: error}]
355+
},
356+
{
357+
options: options,
358+
code: `${fullClassTest2}`,
359+
errors: [{message: error}]
311360
}
312361
],
313362
});

eslint-rules/tests/lib/rules/typography-deprecation.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,92 @@ class Example extends Component {
123123
}
124124
}`;
125125

126+
const fullClassTest1 = `
127+
${ourImport}
128+
129+
const validTypography = (typography) => (myLogic(typography) ? typography : undefined);
130+
131+
class Example extends React.Component {
132+
renderComponent1() {
133+
return this.props.list.map((item) => (
134+
<Component1
135+
prop1={item.data1}
136+
prop2={item.data2}
137+
prop3={item.data3}
138+
prop4={item.data4}
139+
/>
140+
));
141+
}
142+
143+
render() {
144+
return (
145+
<Component2 prop5={this.props.prop5}>
146+
<ScrollView contentContainerStyle={{paddingBottom: 10}}>
147+
{this.renderComponent1()}
148+
<List.Item
149+
prop6={this.props.prop6}
150+
prop7={this.props.prop7}
151+
prop8={this.props.prop8}
152+
prop9={this.props.prop9}
153+
labelStyle={validTypography(Typography.deprecated)}
154+
/>
155+
</ScrollView>
156+
<Component3
157+
prop10={this.props.prop10}
158+
prop11={this.props.prop11}
159+
prop12={this.props.prop12}
160+
/>
161+
</Component2>
162+
);
163+
}
164+
}
165+
166+
export default Example;`;
167+
168+
const fullClassTest2 = `
169+
import * as LetsImportEverything from '${ourSource}';
170+
171+
const {Typography}: typeof LetsImportEverything = require('${ourSource}');
172+
173+
const validTypography = (typography) => (myLogic(typography) ? typography : undefined);
174+
175+
class Example extends React.Component {
176+
renderComponent1() {
177+
return this.props.list.map((item) => (
178+
<Component1
179+
prop1={item.data1}
180+
prop2={item.data2}
181+
prop3={item.data3}
182+
prop4={item.data4}
183+
/>
184+
));
185+
}
186+
187+
render() {
188+
return (
189+
<Component2 prop5={this.props.prop5}>
190+
<ScrollView contentContainerStyle={{paddingBottom: 10}}>
191+
{this.renderComponent1()}
192+
<List.Item
193+
prop6={this.props.prop6}
194+
prop7={this.props.prop7}
195+
prop8={this.props.prop8}
196+
prop9={this.props.prop9}
197+
labelStyle={validTypography(Typography.deprecated)}
198+
/>
199+
</ScrollView>
200+
<Component3
201+
prop10={this.props.prop10}
202+
prop11={this.props.prop11}
203+
prop12={this.props.prop12}
204+
/>
205+
</Component2>
206+
);
207+
}
208+
}
209+
210+
export default Example;`;
211+
126212
const error = "'Typography.deprecated' is deprecated. Please use 'Typography.valid' instead (fix is available).";
127213
const errorDate = ' Please fix this issue by 2 November, Friday!';
128214

@@ -340,6 +426,16 @@ ruleTester.run('typography-deprecation', rule, {
340426
options: options,
341427
code: `${fullClassDeprecatedRenamed}`,
342428
errors: [{message: error}, {message: error}, {message: error}, {message: error}]
429+
},
430+
{
431+
options: options,
432+
code: `${fullClassTest1}`,
433+
errors: [{message: error}]
434+
},
435+
{
436+
options: options,
437+
code: `${fullClassTest2}`,
438+
errors: [{message: error}]
343439
}
344440
],
345441
});

0 commit comments

Comments
 (0)