Skip to content

Lint - add missed cases for assets-deprecation (and a TODO) #776

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
May 18, 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
42 changes: 35 additions & 7 deletions eslint-rules/lib/rules/assets-deprecation.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,43 @@ module.exports = {
const {deprecations, source} = context.options[0];
let localImportSpecifier;

function setLocalImportSpecifier(node) {
function setLocalImportSpecifierFromImport(node) {
localImportSpecifier = utils.getLocalImportSpecifier(node, source, defaultImportName);
}

function setLocalImportSpecifierFromRequire(node) {
if (node.init && node.init.callee && node.init.callee.name === 'require') {
if (node.id && node.id.properties) {
_.map(node.id.properties, property => {
if (property.key && property.key.name === defaultImportName) {
if (property.value && property.value.name) {
localImportSpecifier = property.value.name;
} else {
localImportSpecifier = property.key.name;
}
}
});
}
}
}

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 getAssetString(node.object, pathString);
if (node.property) {
let name;
if (node.property.type === 'Identifier' && node.property.name) {
name = node.property.name;
} else if (node.property.type === 'Literal' && node.property.value) {
name = node.property.value;
} else if (node.property.type === 'CallExpression' && node.property.callee && node.property.callee.name) {
// TODO: ¯\_(ツ)_/¯
}

if (name) {
pathString = (pathString === '') ? `${name}` : `${name}.${pathString}`;
return getAssetString(node.object, pathString);
}
}
} else if (node.name === localImportSpecifier) {
pathString = `${node.name}.${pathString}`;
Expand All @@ -79,11 +106,12 @@ module.exports = {
}

return {
ImportDeclaration: node => !localImportSpecifier && setLocalImportSpecifier(node),
ImportDeclaration: node => !localImportSpecifier && setLocalImportSpecifierFromImport(node),
VariableDeclarator: node => !localImportSpecifier && setLocalImportSpecifierFromRequire(node),
MemberExpression: node => localImportSpecifier && testMemberDeprecation(node),


// VariableDeclarator: node => testVariableDeclarator(node),
// ExpressionStatement: node => testExpressionStatement(node),
// AssignmentExpression: node => testAssignmentExpression(node),
// JSXAttribute: node => testJSXAttribute(node),
// JSXOpeningElement: node => testJSXOpeningElement(node),
// JSXSpreadAttribute: node => testJSXSpreadAttribute(node),
Expand Down
77 changes: 76 additions & 1 deletion eslint-rules/tests/lib/rules/assets-deprecation.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,81 @@ ruleTester.run('assets-deprecation', rule, {
options: options,
code: `${fullClassTest2}`,
errors: [{message: error}]
}
},
{
options: options,
code: `
function createImages() {
const IDs1 = require('my-ids-1');
const {Assets} = require('${ourSource}');
const IDs2 = require('my-ids-2');

const images = {};
images[IDs1.ID1] = Assets.icons.valid;
images[IDs1.ID2] = Assets.icons.deprecated;
images[IDs1.ID3] = Assets.icons.general.valid;
images[IDs1.ID4] = require('../../images/image1.png');
images[IDs2.ID5] = require('../../images/image2.png');
images[IDs2.ID6] = Assets.icons.general.valid2;
images[IDs2.ID7] = Assets.icons.valid2;
return images;
}`,
errors: [{message: error}]
},
{
options: options,
code: `
${ourImport}

const props = {
title: 'title',
subtitle: 'subtitle',
icon: Assets.icons.deprecated
};`,
errors: [{message: error}]
},
{
options: options,
code: `
${ourImport}

const props = {
title: 'title',
subtitle: 'subtitle',
icon: Assets.icons['deprecated']
};`,
errors: [{message: error}]
},
// TODO: ¯\_(ツ)_/¯
// {
// options: options,
// code: `
// ${ourImport}

// const data = 'deprecated';
// const props = {
// title: 'title',
// subtitle: 'subtitle',
// icon: Assets.icons[data]
// };`,
// errors: [{message: error}]
// },
// {
// options: options,
// code: `
// ${ourImport}

// function getAsset() {
// const result = 'deprecated';
// return result;
// }

// const props = {
// title: 'title',
// subtitle: 'subtitle',
// icon: Assets.icons[getAsset()]
// };`,
// errors: [{message: error}]
// }
],
});