Skip to content

Commit 7e5369c

Browse files
authored
Lint - fix asset deprecation (#768)
1 parent 80d3dac commit 7e5369c

File tree

3 files changed

+323
-86
lines changed

3 files changed

+323
-86
lines changed

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

Lines changed: 35 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -39,76 +39,65 @@ module.exports = {
3939
}
4040
}
4141

42-
// Path
43-
let pathString = '';
42+
const defaultImportName = 'Assets';
43+
const {deprecations, source} = context.options[0];
44+
let localImportSpecifier;
4445

45-
function deprecationCheck(node) {
46-
if (node) {
47-
const check = isAssetsObject(node);
48-
if (check && pathString !== '') {
49-
const deprecatedObject = getDeprecatedObject(pathString);
50-
if (deprecatedObject) {
51-
const {path, message, fix} = deprecatedObject;
52-
reportDeprecatedAssets(node, {path, message, fix});
46+
function setLocalImportSpecifier(node) {
47+
const importSource = node.source.value;
48+
if (source === importSource) {
49+
const specifiers = node.specifiers;
50+
if (specifiers) {
51+
localImportSpecifier = _.find(specifiers, specifier => specifier.imported.name === defaultImportName);
52+
if (localImportSpecifier) {
53+
localImportSpecifier = localImportSpecifier.local.name;
5354
}
5455
}
5556
}
5657
}
5758

58-
function isAssetsObject(node) {
59+
function getAssetString(node, pathString = '') {
5960
if (node) {
6061
if (node.object) {
6162
if (node.property && node.property.name) {
6263
pathString = (pathString === '') ? `${node.property.name}` : `${node.property.name}.${pathString}`;
63-
return isAssetsObject(node.object);
64+
return getAssetString(node.object, pathString);
6465
}
65-
} else if (node.name === assetsName) {
66+
} else if (node.name === localImportSpecifier) {
6667
pathString = `${node.name}.${pathString}`;
67-
return true;
68+
return pathString;
6869
}
6970
}
70-
pathString = '';
71-
return false;
72-
}
7371

74-
const {deprecations} = context.options[0];
75-
76-
function getDeprecatedObject(path) {
77-
return _.find(deprecations, {path});
72+
return undefined;
7873
}
7974

80-
function checkSpreadAttribute(node) {
81-
const spreadSource = utils.findValueNodeOfIdentifier(node.argument.name, context.getScope());
82-
if (spreadSource && spreadSource.properties) {
83-
_.forEach(spreadSource.properties, (property) => {
84-
deprecationCheck(property);
85-
});
75+
function findAndReportDeprecation(node, possibleDeprecation) {
76+
possibleDeprecation = possibleDeprecation.replace(localImportSpecifier, defaultImportName);
77+
const deprecatedObject = _.find(deprecations, {path: possibleDeprecation});
78+
if (deprecatedObject) {
79+
reportDeprecatedAssets(node, deprecatedObject);
8680
}
8781
}
8882

89-
// Import
90-
const {source} = context.options[0];
91-
const assetsName = 'Assets';
92-
let shouldCheckDeprecation = false;
93-
94-
function checkAssetsImport(node) {
95-
const importSource = node.source.value;
96-
97-
if (source === importSource) {
98-
const specifiers = node.specifiers;
99-
if (specifiers) {
100-
shouldCheckDeprecation = _.find(specifiers, e => e.local.name === assetsName);
101-
}
83+
function testMemberDeprecation(node) {
84+
let assetString = getAssetString(node);
85+
if (assetString) {
86+
findAndReportDeprecation(node, assetString);
10287
}
10388
}
10489

10590
return {
106-
ImportDeclaration: node => checkAssetsImport(node),
107-
// MemberExpression: node => shouldCheckDeprecation && deprecationCheck(node),
108-
VariableDeclarator: node => shouldCheckDeprecation && deprecationCheck(node.init),
109-
Property: node => shouldCheckDeprecation && deprecationCheck(node.value),
110-
JSXAttribute: node => shouldCheckDeprecation && node.value && deprecationCheck(node.value.expression),
111-
JSXSpreadAttribute: node => shouldCheckDeprecation && checkSpreadAttribute(node),
91+
ImportDeclaration: node => setLocalImportSpecifier(node),
92+
MemberExpression: node => localImportSpecifier && testMemberDeprecation(node),
93+
94+
95+
// VariableDeclarator: node => testVariableDeclarator(node),
96+
// JSXAttribute: node => testJSXAttribute(node),
97+
// JSXOpeningElement: node => testJSXOpeningElement(node),
98+
// JSXSpreadAttribute: node => testJSXSpreadAttribute(node),
99+
// ObjectExpression: node => testObjectExpression(node),
100+
// Property: node => testProperty(node),
112101
};
113102
},
114103
};
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[
22
{
33
"path": "Assets.icons.deprecated",
4-
"message": "Use 'Assets.icons.general.valid' instead.",
4+
"message": "Please use 'Assets.icons.general.valid' instead (fix is available).",
55
"fix": "Assets.icons.general.valid"
66
}
77
]

0 commit comments

Comments
 (0)