Skip to content

Commit 1e9c919

Browse files
authored
Feat/lint add typography deprecation (#762)
* Update package.json (reduce security vulnerabilities) * Lint - add Typography deprecation rules
1 parent fec444b commit 1e9c919

File tree

5 files changed

+448
-2
lines changed

5 files changed

+448
-2
lines changed

eslint-rules/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@ module.exports = {
44
'no-direct-import': require('./lib/rules/no-direct-import'),
55
'component-deprecation': require('./lib/rules/component-deprecation'),
66
'assets-deprecation': require('./lib/rules/assets-deprecation'),
7+
'typography-deprecation': require('./lib/rules/typography-deprecation'),
78
// for duplicate rules usage
89
'component-deprecation_warn': require('./lib/rules/component-deprecation'),
910
'assets-deprecation_warn': require('./lib/rules/assets-deprecation'),
11+
'typography-deprecation_warn': require('./lib/rules/typography-deprecation'),
1012
'component-deprecation_error': require('./lib/rules/component-deprecation'),
1113
'assets-deprecation_error': require('./lib/rules/assets-deprecation'),
14+
'typography-deprecation_error': require('./lib/rules/typography-deprecation'),
1215
},
1316
};
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
const _ = require('lodash');
2+
3+
const MAP_SCHEMA = {
4+
type: 'object',
5+
additionalProperties: true,
6+
};
7+
8+
module.exports = {
9+
meta: {
10+
docs: {
11+
description: 'typography is deprecated',
12+
category: 'Best Practices',
13+
recommended: true,
14+
},
15+
messages: {
16+
uiLib: 'This typography is deprecated.',
17+
},
18+
fixable: 'code',
19+
schema: [MAP_SCHEMA],
20+
},
21+
create(context) {
22+
function reportDeprecatedTypography(node, options) {
23+
try {
24+
const {dueDate} = context.options[0];
25+
const dueDateNotice = dueDate ? ` Please fix this issue by ${dueDate}!` : '';
26+
const msg = `'${options.path}' is deprecated. ${options.message}${dueDateNotice}`;
27+
context.report({
28+
node,
29+
message: `${msg}`,
30+
fix(fixer) {
31+
if (options.fix) {
32+
return fixer.replaceText(node, options.fix);
33+
}
34+
},
35+
});
36+
} catch (err) {
37+
console.log('Found error in: ', context.getFilename());
38+
}
39+
}
40+
41+
const defaultImportName = 'Typography';
42+
const {deprecations, source} = context.options[0];
43+
let localImportSpecifier;
44+
45+
function setLocalImportSpecifier(node) {
46+
const importSource = node.source.value;
47+
if (source === importSource) {
48+
const specifiers = node.specifiers;
49+
if (specifiers) {
50+
localImportSpecifier = _.find(specifiers, specifier => specifier.imported.name === defaultImportName);
51+
if (localImportSpecifier) {
52+
localImportSpecifier = localImportSpecifier.local.name;
53+
}
54+
}
55+
}
56+
}
57+
58+
function findAndReportDeprecation(node, possibleDeprecation) {
59+
const path = `${defaultImportName}.${possibleDeprecation}`;
60+
const foundDeprecation = _.find(deprecations, {path});
61+
if (foundDeprecation) {
62+
reportDeprecatedTypography(node, foundDeprecation);
63+
}
64+
}
65+
66+
function testMemberDeprecation(node) {
67+
if (node && node.object && node.property && node.object.name === localImportSpecifier) {
68+
findAndReportDeprecation(node, node.property.name);
69+
}
70+
}
71+
72+
function testJSXAttribute(node) {
73+
if (node && node.name) {
74+
findAndReportDeprecation(node, node.name.name);
75+
}
76+
}
77+
78+
return {
79+
ImportDeclaration: node => setLocalImportSpecifier(node),
80+
MemberExpression: node => localImportSpecifier && testMemberDeprecation(node),
81+
JSXAttribute: node => testJSXAttribute(node),
82+
83+
84+
// JSXOpeningElement: node => testJSXOpeningElement(node),
85+
// ObjectExpression: node => testObjectExpression(node),
86+
// VariableDeclarator: node => testVariableDeclarator(node),
87+
// Property: node => testProperty(node),
88+
// JSXSpreadAttribute: node => testJSXSpreadAttribute(node)
89+
};
90+
},
91+
};

eslint-rules/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"devDependencies": {
2929
"babel-cli": "^6.23.0",
3030
"babel-eslint": "^7.0.0",
31-
"babel-jest": "^20.0.3",
31+
"babel-jest": "^25.5.1",
3232
"babel-preset-es2015": "^6.22.0",
3333
"babel-preset-react": "^6.22.0",
3434
"babel-preset-react-native": "1.9.1",
@@ -38,7 +38,7 @@
3838
"eslint-plugin-node": "^6.0.1",
3939
"eslint-plugin-promise": "^3.7.0",
4040
"eslint-plugin-standard": "^3.0.1",
41-
"mocha": "^3.1.2"
41+
"mocha": "7.1.2"
4242
},
4343
"engines": {
4444
"node": ">=0.10.0"

0 commit comments

Comments
 (0)