Skip to content

Commit d487b1a

Browse files
authored
Fix/eslint findValueNodeOfIdentifier util (#1268)
* Fix/findValueNodeOfIdentifier util * update eslint-plugin-uilib version
1 parent b41c92f commit d487b1a

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

eslint-rules/lib/utils/generalUtils.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const _ = require('lodash');
2+
13
function getPrefix(str) {
24
const indexOfDot = str.indexOf('.');
35
return indexOfDot === -1 ? str : str.substring(0, indexOfDot);
@@ -18,6 +20,16 @@ function findValueNodeOfIdentifier(identifierName, scope) {
1820
}
1921
}
2022
});
23+
if (valueNode === null || valueNode.value !== undefined) {
24+
scope.block.body.forEach(scopeNode => {
25+
if (scopeNode.type === 'ExpressionStatement') {
26+
const variableName = _.get(scopeNode, 'expression.left.name');
27+
if (variableName === identifierName) {
28+
valueNode = scopeNode.expression.right;
29+
}
30+
}
31+
});
32+
}
2133
if (scope.upper === null) {
2234
return valueNode;
2335
}

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

eslint-rules/tests/lib/rules/no-hard-coded-color.js

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ const validStyleSheetExample = `StyleSheet.create({
3838
})
3939
`;
4040

41-
const invalidConditionalExpression = `const test = <Text style = {{ color: true ? '${Colors.dark10}' : '${
42-
Colors.dark20
43-
}'}}> </Text>;`;
41+
const invalidConditionalExpression = `const test = <Text style = {{ color: true ? '${Colors.dark10}' : '${Colors.dark20}'}}> </Text>;`;
4442
const validConditionalExpression =
4543
'const test = <Text style = {{ color: true ? Colors.dark10 : Colors.dark20}}> </Text>;';
4644

@@ -60,6 +58,40 @@ ruleTester.run('no-hard-coded-color', rule, {
6058
{code: 'const goodUsage = <View style={{backgroundColor: Constants.blue20}}/>;'}
6159
],
6260
invalid: [
61+
{
62+
options: ruleOptions,
63+
code: `
64+
let x;
65+
let y;
66+
x = '#20303C';
67+
y = 3;
68+
const test = <Text style={{color: x}}>text</Text>;
69+
`,
70+
output: `
71+
let x;
72+
let y;
73+
x = Colors.dark10;
74+
y = 3;
75+
const test = <Text style={{color: x}}>text</Text>;
76+
`,
77+
errors: [{message: "Found '#20303C'. Use UILib colors instead of hardcoded colors."}]
78+
},
79+
{
80+
options: ruleOptions,
81+
code: `
82+
let x = 0;
83+
x = 1;
84+
x = '#20303C';
85+
const test = <Text style={{color: x}}>text</Text>;
86+
`,
87+
output: `
88+
let x = 0;
89+
x = 1;
90+
x = Colors.dark10;
91+
const test = <Text style={{color: x}}>text</Text>;
92+
`,
93+
errors: [{message: "Found '#20303C'. Use UILib colors instead of hardcoded colors."}]
94+
},
6395
{
6496
options: ruleOptions,
6597
code: invalidStyleSheetExample,

0 commit comments

Comments
 (0)