Skip to content

Commit fe691d3

Browse files
committed
Lint rules major refactor (start of V2)
1 parent b7c32c3 commit fe691d3

23 files changed

+1424
-479
lines changed
Lines changed: 35 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,31 @@
11
const _ = require('lodash');
22
const utils = require('../utils');
3+
const {
4+
organizeDeprecations,
5+
OrganizationType,
6+
getLocalizedFix,
7+
addToImports,
8+
getComponentLocalName,
9+
getComponentName
10+
} = utils;
311

412
const MAP_SCHEMA = {
513
type: 'object',
6-
additionalProperties: true,
14+
additionalProperties: true
715
};
816

917
module.exports = {
1018
meta: {
1119
docs: {
1220
description: 'asset is deprecated',
1321
category: 'Best Practices',
14-
recommended: true,
22+
recommended: true
1523
},
1624
messages: {
17-
uiLib: 'This asset is deprecated.',
25+
uiLib: 'This asset is deprecated.'
1826
},
1927
fixable: 'code',
20-
schema: [MAP_SCHEMA],
28+
schema: [MAP_SCHEMA]
2129
},
2230
create(context) {
2331
function reportDeprecatedAssets(node, options) {
@@ -32,91 +40,43 @@ module.exports = {
3240
if (options.fix) {
3341
return fixer.replaceText(node, options.fix);
3442
}
35-
},
43+
}
3644
});
3745
} catch (err) {
3846
console.log('Found error in: ', context.getFilename());
3947
}
4048
}
4149

42-
const defaultImportName = 'Assets';
4350
const {deprecations, source} = context.options[0];
44-
let localImportSpecifier;
51+
const organizedDeprecations = organizeDeprecations(deprecations, OrganizationType.SOURCE, source);
52+
const imports = [];
4553

46-
function setLocalImportSpecifierFromImport(node) {
47-
localImportSpecifier = utils.getLocalImportSpecifier(node, source, defaultImportName);
48-
}
54+
function deprecationCheck(node) {
55+
imports.forEach(currentImport => {
56+
const source = Object.keys(currentImport)[0];
57+
const componentLocalName = getComponentLocalName(node);
58+
if (componentLocalName) {
59+
const deprecationSource = organizedDeprecations[source];
60+
if (deprecationSource) {
61+
// There are deprecations from this source
62+
const componentName = getComponentName(componentLocalName, imports);
63+
const foundDeprecations = deprecationSource.filter(
64+
currentDeprecationSource => currentDeprecationSource.path === componentName
65+
);
4966

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-
66-
function getAssetString(node, pathString = '') {
67-
if (node) {
68-
if (node.object) {
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);
67+
if (foundDeprecations.length > 0) {
68+
const localizedFix = getLocalizedFix(foundDeprecations[0].fix, currentImport);
69+
reportDeprecatedAssets(node, {...foundDeprecations[0], fix: localizedFix});
8270
}
8371
}
84-
} else if (node.name === localImportSpecifier) {
85-
pathString = `${node.name}.${pathString}`;
86-
return pathString;
8772
}
88-
}
89-
90-
return undefined;
91-
}
92-
93-
function findAndReportDeprecation(node, possibleDeprecation) {
94-
possibleDeprecation = possibleDeprecation.replace(localImportSpecifier, defaultImportName);
95-
const deprecatedObject = _.find(deprecations, {path: possibleDeprecation});
96-
if (deprecatedObject) {
97-
reportDeprecatedAssets(node, deprecatedObject);
98-
}
99-
}
100-
101-
function testMemberDeprecation(node) {
102-
const assetString = getAssetString(node);
103-
if (assetString) {
104-
findAndReportDeprecation(node, assetString);
105-
}
73+
});
10674
}
10775

10876
return {
109-
ImportDeclaration: node => !localImportSpecifier && setLocalImportSpecifierFromImport(node),
110-
VariableDeclarator: node => !localImportSpecifier && setLocalImportSpecifierFromRequire(node),
111-
MemberExpression: node => localImportSpecifier && testMemberDeprecation(node),
112-
113-
// ExpressionStatement: node => testExpressionStatement(node),
114-
// AssignmentExpression: node => testAssignmentExpression(node),
115-
// JSXAttribute: node => testJSXAttribute(node),
116-
// JSXOpeningElement: node => testJSXOpeningElement(node),
117-
// JSXSpreadAttribute: node => testJSXSpreadAttribute(node),
118-
// ObjectExpression: node => testObjectExpression(node),
119-
// Property: node => testProperty(node),
77+
ImportDeclaration: node => addToImports(node, imports),
78+
VariableDeclarator: node => addToImports(node, imports),
79+
MemberExpression: node => deprecationCheck(node)
12080
};
121-
},
81+
}
12282
};

0 commit comments

Comments
 (0)