|
| 1 | +const _ = require('lodash'); |
| 2 | + |
| 3 | +const MAP_SCHEMA = { |
| 4 | + type: 'object', |
| 5 | + properties: { |
| 6 | + component: { |
| 7 | + type: 'string' |
| 8 | + }, |
| 9 | + props: { |
| 10 | + type: 'array', |
| 11 | + items: { |
| 12 | + type: 'object', |
| 13 | + required: ['prop', 'value'], |
| 14 | + properties: { |
| 15 | + prop: { |
| 16 | + type: 'string' |
| 17 | + }, |
| 18 | + value: { |
| 19 | + type: 'array', |
| 20 | + items: { |
| 21 | + type: 'object', |
| 22 | + required: ['prop', 'message', 'fix'], |
| 23 | + properties: { |
| 24 | + prop: { |
| 25 | + type: 'string' |
| 26 | + }, |
| 27 | + message: { |
| 28 | + type: 'string' |
| 29 | + }, |
| 30 | + fix: { |
| 31 | + type: 'object', |
| 32 | + required: ['propName'], |
| 33 | + properties: { |
| 34 | + propName: { |
| 35 | + type: 'string' |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + }, |
| 46 | + additionalProperties: true |
| 47 | +}; |
| 48 | + |
| 49 | +module.exports = { |
| 50 | + meta: { |
| 51 | + docs: { |
| 52 | + description: "Detect deprecation of prop's value shape", |
| 53 | + category: 'Best Practices', |
| 54 | + recommended: true |
| 55 | + }, |
| 56 | + messages: { |
| 57 | + uiLib: "This prop's value shape is deprecated." |
| 58 | + }, |
| 59 | + fixable: 'code', |
| 60 | + schema: [MAP_SCHEMA] |
| 61 | + }, |
| 62 | + create(context) { |
| 63 | + function testJSXAttributes(node) { |
| 64 | + try { |
| 65 | + const {deprecations} = _.get(context, 'options[0]'); |
| 66 | + const nodeName = _.get(node, 'name.name'); |
| 67 | + const deprecation = nodeName && deprecations && _.find(deprecations, ['component', nodeName]); |
| 68 | + if (deprecation.component && nodeName === deprecation.component) { |
| 69 | + _.forEach(node.attributes, attribute => { |
| 70 | + const attributeName = _.get(attribute, 'name.name'); |
| 71 | + const deprecationProps = _.get(deprecation, 'props'); |
| 72 | + const deprecationProp = |
| 73 | + attributeName && deprecationProps && _.find(deprecationProps, ['prop', attributeName]); |
| 74 | + const deprecationPropName = _.get(deprecationProp, 'prop'); |
| 75 | + if (deprecationPropName && attributeName === deprecationPropName) { |
| 76 | + const attributeProperties = _.get(attribute, 'value.expression.properties'); |
| 77 | + for (let i = 0; i <= attributeProperties.length; i++) { |
| 78 | + const propertyName = _.get(attributeProperties[i], 'key.name'); |
| 79 | + const origin = |
| 80 | + deprecationProp.value && propertyName && _.find(deprecationProp.value, ['prop', propertyName]); |
| 81 | + if (origin.prop && propertyName === origin.prop) { |
| 82 | + const destination = _.get(origin, 'fix.propName'); |
| 83 | + const message = `The shape of '${deprecationPropName}' prop of '${deprecation.component}' doesn't contain '${origin.prop}' anymore. Please use '${destination}' instead (fix is available).`; |
| 84 | + context.report({ |
| 85 | + node, |
| 86 | + message, |
| 87 | + fix(fixer) { |
| 88 | + if (destination && attributeProperties[i].key) { |
| 89 | + return fixer.replaceText(attributeProperties[i].key, destination); |
| 90 | + } |
| 91 | + } |
| 92 | + }); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + }); |
| 97 | + } |
| 98 | + } catch (err) { |
| 99 | + console.log('Found error in: ', context.getFilename()); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + return { |
| 104 | + JSXOpeningElement: testJSXAttributes |
| 105 | + }; |
| 106 | + } |
| 107 | +}; |
0 commit comments