Skip to content

Fix/eslint findValueNodeOfIdentifier util #1268

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 2 commits into from
Apr 22, 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
12 changes: 12 additions & 0 deletions eslint-rules/lib/utils/generalUtils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const _ = require('lodash');

function getPrefix(str) {
const indexOfDot = str.indexOf('.');
return indexOfDot === -1 ? str : str.substring(0, indexOfDot);
Expand All @@ -18,6 +20,16 @@ function findValueNodeOfIdentifier(identifierName, scope) {
}
}
});
if (valueNode === null || valueNode.value !== undefined) {
scope.block.body.forEach(scopeNode => {
if (scopeNode.type === 'ExpressionStatement') {
const variableName = _.get(scopeNode, 'expression.left.name');
if (variableName === identifierName) {
valueNode = scopeNode.expression.right;
}
}
});
}
if (scope.upper === null) {
return valueNode;
}
Expand Down
2 changes: 1 addition & 1 deletion eslint-rules/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eslint-plugin-uilib",
"version": "2.0.2",
"version": "2.0.3",
"description": "uilib set of eslint rules",
"keywords": [
"eslint",
Expand Down
38 changes: 35 additions & 3 deletions eslint-rules/tests/lib/rules/no-hard-coded-color.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ const validStyleSheetExample = `StyleSheet.create({
})
`;

const invalidConditionalExpression = `const test = <Text style = {{ color: true ? '${Colors.dark10}' : '${
Colors.dark20
}'}}> </Text>;`;
const invalidConditionalExpression = `const test = <Text style = {{ color: true ? '${Colors.dark10}' : '${Colors.dark20}'}}> </Text>;`;
const validConditionalExpression =
'const test = <Text style = {{ color: true ? Colors.dark10 : Colors.dark20}}> </Text>;';

Expand All @@ -60,6 +58,40 @@ ruleTester.run('no-hard-coded-color', rule, {
{code: 'const goodUsage = <View style={{backgroundColor: Constants.blue20}}/>;'}
],
invalid: [
{
options: ruleOptions,
code: `
let x;
let y;
x = '#20303C';
y = 3;
const test = <Text style={{color: x}}>text</Text>;
`,
output: `
let x;
let y;
x = Colors.dark10;
y = 3;
const test = <Text style={{color: x}}>text</Text>;
`,
errors: [{message: "Found '#20303C'. Use UILib colors instead of hardcoded colors."}]
},
{
options: ruleOptions,
code: `
let x = 0;
x = 1;
x = '#20303C';
const test = <Text style={{color: x}}>text</Text>;
`,
output: `
let x = 0;
x = 1;
x = Colors.dark10;
const test = <Text style={{color: x}}>text</Text>;
`,
errors: [{message: "Found '#20303C'. Use UILib colors instead of hardcoded colors."}]
},
{
options: ruleOptions,
code: invalidStyleSheetExample,
Expand Down