Skip to content

Lint - fix asset deprecation #768

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 1 commit into from
May 11, 2020
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
81 changes: 35 additions & 46 deletions eslint-rules/lib/rules/assets-deprecation.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,76 +39,65 @@ module.exports = {
}
}

// Path
let pathString = '';
const defaultImportName = 'Assets';
const {deprecations, source} = context.options[0];
let localImportSpecifier;

function deprecationCheck(node) {
if (node) {
const check = isAssetsObject(node);
if (check && pathString !== '') {
const deprecatedObject = getDeprecatedObject(pathString);
if (deprecatedObject) {
const {path, message, fix} = deprecatedObject;
reportDeprecatedAssets(node, {path, message, fix});
function setLocalImportSpecifier(node) {
const importSource = node.source.value;
if (source === importSource) {
const specifiers = node.specifiers;
if (specifiers) {
localImportSpecifier = _.find(specifiers, specifier => specifier.imported.name === defaultImportName);
if (localImportSpecifier) {
localImportSpecifier = localImportSpecifier.local.name;
}
}
}
}

function isAssetsObject(node) {
function getAssetString(node, pathString = '') {
if (node) {
if (node.object) {
if (node.property && node.property.name) {
pathString = (pathString === '') ? `${node.property.name}` : `${node.property.name}.${pathString}`;
return isAssetsObject(node.object);
return getAssetString(node.object, pathString);
}
} else if (node.name === assetsName) {
} else if (node.name === localImportSpecifier) {
pathString = `${node.name}.${pathString}`;
return true;
return pathString;
}
}
pathString = '';
return false;
}

const {deprecations} = context.options[0];

function getDeprecatedObject(path) {
return _.find(deprecations, {path});
return undefined;
}

function checkSpreadAttribute(node) {
const spreadSource = utils.findValueNodeOfIdentifier(node.argument.name, context.getScope());
if (spreadSource && spreadSource.properties) {
_.forEach(spreadSource.properties, (property) => {
deprecationCheck(property);
});
function findAndReportDeprecation(node, possibleDeprecation) {
possibleDeprecation = possibleDeprecation.replace(localImportSpecifier, defaultImportName);
const deprecatedObject = _.find(deprecations, {path: possibleDeprecation});
if (deprecatedObject) {
reportDeprecatedAssets(node, deprecatedObject);
}
}

// Import
const {source} = context.options[0];
const assetsName = 'Assets';
let shouldCheckDeprecation = false;

function checkAssetsImport(node) {
const importSource = node.source.value;

if (source === importSource) {
const specifiers = node.specifiers;
if (specifiers) {
shouldCheckDeprecation = _.find(specifiers, e => e.local.name === assetsName);
}
function testMemberDeprecation(node) {
let assetString = getAssetString(node);
if (assetString) {
findAndReportDeprecation(node, assetString);
}
}

return {
ImportDeclaration: node => checkAssetsImport(node),
// MemberExpression: node => shouldCheckDeprecation && deprecationCheck(node),
VariableDeclarator: node => shouldCheckDeprecation && deprecationCheck(node.init),
Property: node => shouldCheckDeprecation && deprecationCheck(node.value),
JSXAttribute: node => shouldCheckDeprecation && node.value && deprecationCheck(node.value.expression),
JSXSpreadAttribute: node => shouldCheckDeprecation && checkSpreadAttribute(node),
ImportDeclaration: node => setLocalImportSpecifier(node),
MemberExpression: node => localImportSpecifier && testMemberDeprecation(node),


// VariableDeclarator: node => testVariableDeclarator(node),
// JSXAttribute: node => testJSXAttribute(node),
// JSXOpeningElement: node => testJSXOpeningElement(node),
// JSXSpreadAttribute: node => testJSXSpreadAttribute(node),
// ObjectExpression: node => testObjectExpression(node),
// Property: node => testProperty(node),
};
},
};
2 changes: 1 addition & 1 deletion eslint-rules/tests/assets_deprecation.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[
{
"path": "Assets.icons.deprecated",
"message": "Use 'Assets.icons.general.valid' instead.",
"message": "Please use 'Assets.icons.general.valid' instead (fix is available).",
"fix": "Assets.icons.general.valid"
}
]
Loading