|
| 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 | +}; |
0 commit comments