Skip to content

Commit 54b7cca

Browse files
authored
Infra/eslint v2 (#1193)
* Add more tests for component deprecation (commented for now) * Lint rules major refactor (start of V2) * Remove OrganizationType * Split for readability * Remove extra line * Mark as internal function * Add a TODO to remove value * Review fixes
1 parent 116b151 commit 54b7cca

23 files changed

+1455
-416
lines changed
Lines changed: 34 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
11
const _ = require('lodash');
2-
const utils = require('../utils');
2+
const {
3+
organizeDeprecations,
4+
getLocalizedFix,
5+
addToImports,
6+
getComponentLocalName,
7+
getComponentName
8+
} = require('../utils');
39

410
const MAP_SCHEMA = {
511
type: 'object',
6-
additionalProperties: true,
12+
additionalProperties: true
713
};
814

915
module.exports = {
1016
meta: {
1117
docs: {
1218
description: 'asset is deprecated',
1319
category: 'Best Practices',
14-
recommended: true,
20+
recommended: true
1521
},
1622
messages: {
17-
uiLib: 'This asset is deprecated.',
23+
uiLib: 'This asset is deprecated.'
1824
},
1925
fixable: 'code',
20-
schema: [MAP_SCHEMA],
26+
schema: [MAP_SCHEMA]
2127
},
2228
create(context) {
2329
function reportDeprecatedAssets(node, options) {
@@ -32,91 +38,43 @@ module.exports = {
3238
if (options.fix) {
3339
return fixer.replaceText(node, options.fix);
3440
}
35-
},
41+
}
3642
});
3743
} catch (err) {
3844
console.log('Found error in: ', context.getFilename());
3945
}
4046
}
4147

42-
const defaultImportName = 'Assets';
4348
const {deprecations, source} = context.options[0];
44-
let localImportSpecifier;
49+
const organizedDeprecations = organizeDeprecations(deprecations, source);
50+
const imports = [];
4551

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

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);
65+
if (foundDeprecations.length > 0) {
66+
const localizedFix = getLocalizedFix(foundDeprecations[0].fix, currentImport);
67+
reportDeprecatedAssets(node, {...foundDeprecations[0], fix: localizedFix});
8268
}
8369
}
84-
} else if (node.name === localImportSpecifier) {
85-
pathString = `${node.name}.${pathString}`;
86-
return pathString;
8770
}
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-
}
71+
});
10672
}
10773

10874
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),
75+
ImportDeclaration: node => addToImports(node, imports),
76+
VariableDeclarator: node => addToImports(node, imports),
77+
MemberExpression: node => deprecationCheck(node)
12078
};
121-
},
79+
}
12280
};

0 commit comments

Comments
 (0)