Skip to content

Commit 0780c42

Browse files
authored
Lint - add missed cases for assets-deprecation (and a TODO) (#776)
* Lint - add missed cases for assets-deprecation (and a TODO) * Add another TODO
1 parent b25761a commit 0780c42

File tree

2 files changed

+111
-8
lines changed

2 files changed

+111
-8
lines changed

eslint-rules/lib/rules/assets-deprecation.js

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,43 @@ module.exports = {
4343
const {deprecations, source} = context.options[0];
4444
let localImportSpecifier;
4545

46-
function setLocalImportSpecifier(node) {
46+
function setLocalImportSpecifierFromImport(node) {
4747
localImportSpecifier = utils.getLocalImportSpecifier(node, source, defaultImportName);
4848
}
4949

50+
function setLocalImportSpecifierFromRequire(node) {
51+
if (node.init && node.init.callee && node.init.callee.name === 'require') {
52+
if (node.id && node.id.properties) {
53+
_.map(node.id.properties, property => {
54+
if (property.key && property.key.name === defaultImportName) {
55+
if (property.value && property.value.name) {
56+
localImportSpecifier = property.value.name;
57+
} else {
58+
localImportSpecifier = property.key.name;
59+
}
60+
}
61+
});
62+
}
63+
}
64+
}
65+
5066
function getAssetString(node, pathString = '') {
5167
if (node) {
5268
if (node.object) {
53-
if (node.property && node.property.name) {
54-
pathString = (pathString === '') ? `${node.property.name}` : `${node.property.name}.${pathString}`;
55-
return getAssetString(node.object, pathString);
69+
if (node.property) {
70+
let name;
71+
if (node.property.type === 'Identifier' && node.property.name) {
72+
name = node.property.name;
73+
} else if (node.property.type === 'Literal' && node.property.value) {
74+
name = node.property.value;
75+
} else if (node.property.type === 'CallExpression' && node.property.callee && node.property.callee.name) {
76+
// TODO: ¯\_(ツ)_/¯
77+
}
78+
79+
if (name) {
80+
pathString = (pathString === '') ? `${name}` : `${name}.${pathString}`;
81+
return getAssetString(node.object, pathString);
82+
}
5683
}
5784
} else if (node.name === localImportSpecifier) {
5885
pathString = `${node.name}.${pathString}`;
@@ -79,11 +106,12 @@ module.exports = {
79106
}
80107

81108
return {
82-
ImportDeclaration: node => !localImportSpecifier && setLocalImportSpecifier(node),
109+
ImportDeclaration: node => !localImportSpecifier && setLocalImportSpecifierFromImport(node),
110+
VariableDeclarator: node => !localImportSpecifier && setLocalImportSpecifierFromRequire(node),
83111
MemberExpression: node => localImportSpecifier && testMemberDeprecation(node),
84112

85-
86-
// VariableDeclarator: node => testVariableDeclarator(node),
113+
// ExpressionStatement: node => testExpressionStatement(node),
114+
// AssignmentExpression: node => testAssignmentExpression(node),
87115
// JSXAttribute: node => testJSXAttribute(node),
88116
// JSXOpeningElement: node => testJSXOpeningElement(node),
89117
// JSXSpreadAttribute: node => testJSXSpreadAttribute(node),

eslint-rules/tests/lib/rules/assets-deprecation.js

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,81 @@ ruleTester.run('assets-deprecation', rule, {
360360
options: options,
361361
code: `${fullClassTest2}`,
362362
errors: [{message: error}]
363-
}
363+
},
364+
{
365+
options: options,
366+
code: `
367+
function createImages() {
368+
const IDs1 = require('my-ids-1');
369+
const {Assets} = require('${ourSource}');
370+
const IDs2 = require('my-ids-2');
371+
372+
const images = {};
373+
images[IDs1.ID1] = Assets.icons.valid;
374+
images[IDs1.ID2] = Assets.icons.deprecated;
375+
images[IDs1.ID3] = Assets.icons.general.valid;
376+
images[IDs1.ID4] = require('../../images/image1.png');
377+
images[IDs2.ID5] = require('../../images/image2.png');
378+
images[IDs2.ID6] = Assets.icons.general.valid2;
379+
images[IDs2.ID7] = Assets.icons.valid2;
380+
return images;
381+
}`,
382+
errors: [{message: error}]
383+
},
384+
{
385+
options: options,
386+
code: `
387+
${ourImport}
388+
389+
const props = {
390+
title: 'title',
391+
subtitle: 'subtitle',
392+
icon: Assets.icons.deprecated
393+
};`,
394+
errors: [{message: error}]
395+
},
396+
{
397+
options: options,
398+
code: `
399+
${ourImport}
400+
401+
const props = {
402+
title: 'title',
403+
subtitle: 'subtitle',
404+
icon: Assets.icons['deprecated']
405+
};`,
406+
errors: [{message: error}]
407+
},
408+
// TODO: ¯\_(ツ)_/¯
409+
// {
410+
// options: options,
411+
// code: `
412+
// ${ourImport}
413+
414+
// const data = 'deprecated';
415+
// const props = {
416+
// title: 'title',
417+
// subtitle: 'subtitle',
418+
// icon: Assets.icons[data]
419+
// };`,
420+
// errors: [{message: error}]
421+
// },
422+
// {
423+
// options: options,
424+
// code: `
425+
// ${ourImport}
426+
427+
// function getAsset() {
428+
// const result = 'deprecated';
429+
// return result;
430+
// }
431+
432+
// const props = {
433+
// title: 'title',
434+
// subtitle: 'subtitle',
435+
// icon: Assets.icons[getAsset()]
436+
// };`,
437+
// errors: [{message: error}]
438+
// }
364439
],
365440
});

0 commit comments

Comments
 (0)