Skip to content

Infra/delete utils_old file #1236

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 6 commits into from
Apr 12, 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
2 changes: 1 addition & 1 deletion eslint-rules/lib/rules/component-prop-deprecation.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ module.exports = {
if (attribute.type === 'JSXAttribute') {
checkPropDeprecation(attribute, attribute.name, attribute.name.name, deprecatedPropList, componentName);
} else if (attribute.type === 'JSXSpreadAttribute') {
const spreadSource = findValueNodeOfIdentifier(attribute.argument.name, context);
const spreadSource = findValueNodeOfIdentifier(attribute.argument.name, context.getScope());
if (spreadSource) {
_.forEach(spreadSource.properties, property => {
const key = _.get(property, 'key');
Expand Down
20 changes: 17 additions & 3 deletions eslint-rules/lib/rules/prop-value-shape-deprecation.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const _ = require('lodash');
const {findValueNodeOfIdentifier, getComponentName, getPathPrefix, getPathSuffix} = require('../utils_old');
const {findValueNodeOfIdentifier, getComponentLocalName, addToImports, getComponentName} = require('../utils');

const MAP_SCHEMA = {
type: 'object',
Expand Down Expand Up @@ -52,7 +52,7 @@ module.exports = {
},
create(context) {
function reportPropValueShapeDeprecation(propKey, prop, deprecation, node) {
const componentName = getComponentName(node);
const componentName = getComponentName(getComponentLocalName(node), imports);
const newProp = _.get(deprecation, 'fix.propName');
const fixMessage = _.get(deprecation, 'message') ? ' ' + _.get(deprecation, 'message') : '';
const message = `The shape of '${prop}' prop of '${componentName}' doesn't contain '${deprecation.prop}' anymore.${fixMessage}`;
Expand All @@ -67,10 +67,12 @@ module.exports = {
});
}

const imports = [];

function testJSXAttributes(node) {
try {
const {deprecations} = _.get(context, 'options[0]');
const componentName = getComponentName(node);
const componentName = getComponentName(getComponentLocalName(node), imports);
_.forEach(deprecations, deprecation => {
if (_.includes(deprecation.components, componentName)) {
_.forEach(node.attributes, attribute => {
Expand Down Expand Up @@ -130,6 +132,16 @@ module.exports = {
}
}

function getPathPrefix(str) {
const index = str.indexOf('.');
return index === -1 ? str : str.substring(0, index);
}

function getPathSuffix(str) {
const index = str.indexOf('.');
return index === -1 ? undefined : str.substring(index + 1);
}

function checkAttributeProperties(attributeProperties, attributeName, deprecation, node) {
for (let i = 0; i < attributeProperties.length; i++) {
const propertyType = _.get(attributeProperties[i], 'type');
Expand All @@ -146,6 +158,8 @@ module.exports = {
}

return {
ImportDeclaration: node => addToImports(node, imports),
VariableDeclarator: node => addToImports(node, imports),
JSXOpeningElement: testJSXAttributes
};
}
Expand Down
14 changes: 8 additions & 6 deletions eslint-rules/lib/utils/generalUtils.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
function findValueNodeOfIdentifier(identifierName, context) {
const scope = context.getScope();
const {variables} = scope;
function findValueNodeOfIdentifier(identifierName, scope) {
const varsInScope = scope.variables;
let valueNode = false;
variables.forEach(variable => {
varsInScope.forEach((variable) => {
if (variable.name === identifierName) {
if (variable.defs) {
valueNode = variable.defs[variable.defs.length - 1].node.init;
}
}
});

return valueNode;
if (scope.upper === null) {
return valueNode;
}
return valueNode || findValueNodeOfIdentifier(identifierName, scope.upper);
}


module.exports = {
findValueNodeOfIdentifier
};
2 changes: 1 addition & 1 deletion eslint-rules/lib/utils/noHardCodedUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function findAndReportHardCodedValues(value, reporter, context, depthOfSearch =
findAndReportHardCodedValues(value.consequent, reporter, context, depthOfSearch - 1);
findAndReportHardCodedValues(value.alternate, reporter, context, depthOfSearch - 1);
} else if (value.type === 'Identifier') {
findAndReportHardCodedValues(findValueNodeOfIdentifier(value.name, context), reporter, context, depthOfSearch - 1);
findAndReportHardCodedValues(findValueNodeOfIdentifier(value.name, context.getScope()), reporter, context, depthOfSearch - 1);
}
}

Expand Down
126 changes: 0 additions & 126 deletions eslint-rules/lib/utils_old.js

This file was deleted.

46 changes: 28 additions & 18 deletions eslint-rules/tests/lib/rules/prop-value-shape-deprecation.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,26 @@ const imageSource = 'imageSource: {uri: some_uri}';
const source = 'source: {uri: some_uri}';
const ruleOptions = [{deprecations: deprecationsJson}];
const PassedPropExampleCode = `
const myProps = {
goodProp: goodValue,
${imageSource}
};
import {Label} from 'our-source';
const myProps = {
goodProp: goodValue,
${imageSource}
};

<Label avatar={myProps}/>
<Label avatar={myProps}/>
`;
const PassedPropExampleOutput = `
const myProps = {
goodProp: goodValue,
${source}
};
import {Label} from 'our-source';
const myProps = {
goodProp: goodValue,
${source}
};

<Label avatar={myProps}/>
<Label avatar={myProps}/>
`;

const firstLevelSpreadCode = `
import {Label} from 'our-source';
const myProps = {
avatarProps: {
goodProp: goodValue,
Expand All @@ -43,6 +46,7 @@ const myProps = {
`;

const firstLevelSpreadOutput = `
import {Label} from 'our-source';
const myProps = {
avatarProps: {
goodProp: goodValue,
Expand All @@ -54,6 +58,7 @@ const myProps = {
`;

const secondLevelSpreadCode = `
import {Label} from 'our-source';
const myProps = {
goodProp: goodValue,
${imageSource}
Expand All @@ -63,6 +68,7 @@ const myProps = {
`;

const secondLevelSpreadOutput = `
import {Label} from 'our-source';
const myProps = {
goodProp: goodValue,
${source}
Expand Down Expand Up @@ -103,33 +109,33 @@ ruleTester.run('prop-value-shape-deprecation', rule, {
invalid: [
{
options: ruleOptions,
code: `<ListItem avatar={{${imageSource}, someProp: someValue}}/>`,
code: `import {ListItem} from 'our-source'; <ListItem avatar={{${imageSource}, someProp: someValue}}/>`,
errors: [
{
message: `The shape of 'avatar' prop of 'ListItem' doesn't contain 'imageSource' anymore. Please use 'source' instead (fix is available).`
}
],
output: `<ListItem avatar={{${source}, someProp: someValue}}/>`
output: `import {ListItem} from 'our-source'; <ListItem avatar={{${source}, someProp: someValue}}/>`
},
{
options: ruleOptions,
code: `<ListItem avatar={{someProp: someValue, ${imageSource}}}/>`,
code: `import {ListItem} from 'our-source'; <ListItem avatar={{someProp: someValue, ${imageSource}}}/>`,
errors: [
{
message: `The shape of 'avatar' prop of 'ListItem' doesn't contain 'imageSource' anymore. Please use 'source' instead (fix is available).`
}
],
output: `<ListItem avatar={{someProp: someValue, ${source}}}/>`
output: `import {ListItem} from 'our-source'; <ListItem avatar={{someProp: someValue, ${source}}}/>`
},
{
options: ruleOptions,
code: `<ListItem someProp={someValue} avatar={{${imageSource}}}/>`,
code: `import {ListItem} from 'our-source'; <ListItem someProp={someValue} avatar={{${imageSource}}}/>`,
errors: [
{
message: `The shape of 'avatar' prop of 'ListItem' doesn't contain 'imageSource' anymore. Please use 'source' instead (fix is available).`
}
],
output: `<ListItem someProp={someValue} avatar={{${source}}}/>`
output: `import {ListItem} from 'our-source'; <ListItem someProp={someValue} avatar={{${source}}}/>`
},
{
options: ruleOptions,
Expand Down Expand Up @@ -202,7 +208,9 @@ ruleTester.run('prop-value-shape-deprecation', rule, {
},
{
options: ruleOptions,
code: `const myProps1 = {
code: `
import {Label} from 'our-source';
const myProps1 = {
avatarProps: {
${imageSource},
goodProp1: goodValue1
Expand All @@ -225,7 +233,9 @@ ruleTester.run('prop-value-shape-deprecation', rule, {
},
{
options: ruleOptions,
code: `const myProps1 = {
code: `
import {Label} from 'our-source';
const myProps1 = {
buttonProps: {
goodProp1: goodValue1,
goodProp2: goodValue2,
Expand Down