|
| 1 | +/** |
| 2 | + * @author Yosuke Ota |
| 3 | + * See LICENSE file in root directory for full license. |
| 4 | + */ |
| 5 | +'use strict' |
| 6 | +module.exports = { |
| 7 | + deprecated: '2.6.0', |
| 8 | + supported: '2.5.0', |
| 9 | + createTemplateBodyVisitor (context, { fixToUpgrade } = {}) { |
| 10 | + const sourceCode = context.getSourceCode() |
| 11 | + |
| 12 | + /** |
| 13 | + * Checks whether the given node can convert to the `v-slot`. |
| 14 | + * @param {VStartTag} startTag node of `<element v-slot ... >` |
| 15 | + * @returns {boolean} `true` if the given node can convert to the `v-slot` |
| 16 | + */ |
| 17 | + function canConvertToVSlot (startTag) { |
| 18 | + if (startTag.parent.name !== 'template') { |
| 19 | + return false |
| 20 | + } |
| 21 | + |
| 22 | + const slotAttr = startTag.attributes |
| 23 | + .find(attr => attr.directive === false && attr.key.name === 'slot') |
| 24 | + if (slotAttr) { |
| 25 | + // if the element have `slot` it can not be converted. |
| 26 | + // Conversion of `slot` is done with `vue/no-deprecated-slot-attribute`. |
| 27 | + return false |
| 28 | + } |
| 29 | + |
| 30 | + const vBindSlotAttr = startTag.attributes |
| 31 | + .find(attr => |
| 32 | + attr.directive === true && |
| 33 | + attr.key.name.name === 'bind' && |
| 34 | + attr.key.argument && |
| 35 | + attr.key.argument.name === 'slot') |
| 36 | + if (vBindSlotAttr) { |
| 37 | + // if the element have `v-bind:slot` it can not be converted. |
| 38 | + // Conversion of `v-bind:slot` is done with `vue/no-deprecated-slot-attribute`. |
| 39 | + return false |
| 40 | + } |
| 41 | + return true |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Convert to `v-slot`. |
| 46 | + * @param {object} fixer fixer |
| 47 | + * @param {VAttribute | null} scopeAttr node of `slot-scope` |
| 48 | + * @returns {*} fix data |
| 49 | + */ |
| 50 | + function fixSlotScopeToVSlot (fixer, scopeAttr) { |
| 51 | + const scopeValue = scopeAttr && scopeAttr.value |
| 52 | + ? `=${sourceCode.getText(scopeAttr.value)}` |
| 53 | + : '' |
| 54 | + |
| 55 | + const replaceText = `v-slot${scopeValue}` |
| 56 | + return fixer.replaceText(scopeAttr, replaceText) |
| 57 | + } |
| 58 | + /** |
| 59 | + * Reports `slot-scope` node |
| 60 | + * @param {VAttribute} scopeAttr node of `slot-scope` |
| 61 | + * @returns {void} |
| 62 | + */ |
| 63 | + function reportSlotScope (scopeAttr) { |
| 64 | + context.report({ |
| 65 | + node: scopeAttr.key, |
| 66 | + messageId: 'forbiddenSlotScopeAttribute', |
| 67 | + fix: fixToUpgrade |
| 68 | + // fix to use `v-slot` |
| 69 | + ? (fixer) => { |
| 70 | + const startTag = scopeAttr.parent |
| 71 | + if (!canConvertToVSlot(startTag)) { |
| 72 | + return null |
| 73 | + } |
| 74 | + return fixSlotScopeToVSlot(fixer, scopeAttr) |
| 75 | + } |
| 76 | + : null |
| 77 | + }) |
| 78 | + } |
| 79 | + |
| 80 | + return { |
| 81 | + "VAttribute[directive=true][key.name.name='slot-scope']": reportSlotScope |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments