|
| 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 | + createTemplateBodyVisitor (context) { |
| 9 | + const sourceCode = context.getSourceCode() |
| 10 | + |
| 11 | + /** |
| 12 | + * Checks whether the given node can convert to the `v-slot`. |
| 13 | + * @param {VAttribute} slotAttr node of `slot` |
| 14 | + * @returns {boolean} `true` if the given node can convert to the `v-slot` |
| 15 | + */ |
| 16 | + function canConvertFromSlotToVSlot (slotAttr) { |
| 17 | + if (slotAttr.parent.parent.name !== 'template') { |
| 18 | + return false |
| 19 | + } |
| 20 | + if (!slotAttr.value) { |
| 21 | + return true |
| 22 | + } |
| 23 | + const slotName = slotAttr.value.value |
| 24 | + // If non-Latin characters are included it can not be converted. |
| 25 | + return !/[^a-z]/i.test(slotName) |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Checks whether the given node can convert to the `v-slot`. |
| 30 | + * @param {VAttribute} slotAttr node of `v-bind:slot` |
| 31 | + * @returns {boolean} `true` if the given node can convert to the `v-slot` |
| 32 | + */ |
| 33 | + function canConvertFromVBindSlotToVSlot (slotAttr) { |
| 34 | + if (slotAttr.parent.parent.name !== 'template') { |
| 35 | + return false |
| 36 | + } |
| 37 | + |
| 38 | + if (!slotAttr.value) { |
| 39 | + return true |
| 40 | + } |
| 41 | + |
| 42 | + if (!slotAttr.value.expression) { |
| 43 | + // parse error or empty expression |
| 44 | + return false |
| 45 | + } |
| 46 | + const slotName = sourceCode.getText(slotAttr.value.expression).trim() |
| 47 | + // If non-Latin characters are included it can not be converted. |
| 48 | + // It does not check the space only because `a>b?c:d` should be rejected. |
| 49 | + return !/[^a-z]/i.test(slotName) |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Convert to `v-slot`. |
| 54 | + * @param {object} fixer fixer |
| 55 | + * @param {VAttribute} slotAttr node of `slot` |
| 56 | + * @param {string | null} slotName name of `slot` |
| 57 | + * @param {boolean} vBind `true` if `slotAttr` is `v-bind:slot` |
| 58 | + * @returns {*} fix data |
| 59 | + */ |
| 60 | + function fixSlotToVSlot (fixer, slotAttr, slotName, vBind) { |
| 61 | + const element = slotAttr.parent |
| 62 | + const scopeAttr = element.attributes |
| 63 | + .find(attr => attr.directive === true && attr.key.name && ( |
| 64 | + attr.key.name.name === 'slot-scope' || |
| 65 | + attr.key.name.name === 'scope' |
| 66 | + )) |
| 67 | + const nameArgument = slotName ? (vBind ? `:[${slotName}]` : `:${slotName}`) : '' |
| 68 | + const scopeValue = scopeAttr && scopeAttr.value |
| 69 | + ? `=${sourceCode.getText(scopeAttr.value)}` |
| 70 | + : '' |
| 71 | + |
| 72 | + const replaceText = `v-slot${nameArgument}${scopeValue}` |
| 73 | + const fixers = [ |
| 74 | + fixer.replaceText(slotAttr || scopeAttr, replaceText) |
| 75 | + ] |
| 76 | + if (slotAttr && scopeAttr) { |
| 77 | + fixers.push(fixer.remove(scopeAttr)) |
| 78 | + } |
| 79 | + return fixers |
| 80 | + } |
| 81 | + /** |
| 82 | + * Reports `slot` node |
| 83 | + * @param {VAttribute} slotAttr node of `slot` |
| 84 | + * @returns {void} |
| 85 | + */ |
| 86 | + function reportSlot (slotAttr) { |
| 87 | + context.report({ |
| 88 | + node: slotAttr.key, |
| 89 | + messageId: 'forbiddenSlotAttribute', |
| 90 | + // fix to use `v-slot` |
| 91 | + fix (fixer) { |
| 92 | + if (!canConvertFromSlotToVSlot(slotAttr)) { |
| 93 | + return null |
| 94 | + } |
| 95 | + const slotName = slotAttr.value && |
| 96 | + slotAttr.value.value |
| 97 | + return fixSlotToVSlot(fixer, slotAttr, slotName, false) |
| 98 | + } |
| 99 | + }) |
| 100 | + } |
| 101 | + /** |
| 102 | + * Reports `v-bind:slot` node |
| 103 | + * @param {VAttribute} slotAttr node of `v-bind:slot` |
| 104 | + * @returns {void} |
| 105 | + */ |
| 106 | + function reportVBindSlot (slotAttr) { |
| 107 | + context.report({ |
| 108 | + node: slotAttr.key, |
| 109 | + messageId: 'forbiddenSlotAttribute', |
| 110 | + // fix to use `v-slot` |
| 111 | + fix (fixer) { |
| 112 | + if (!canConvertFromVBindSlotToVSlot(slotAttr)) { |
| 113 | + return null |
| 114 | + } |
| 115 | + const slotName = slotAttr.value && |
| 116 | + slotAttr.value.expression && |
| 117 | + sourceCode.getText(slotAttr.value.expression).trim() |
| 118 | + return fixSlotToVSlot(fixer, slotAttr, slotName, true) |
| 119 | + } |
| 120 | + }) |
| 121 | + } |
| 122 | + |
| 123 | + return { |
| 124 | + "VAttribute[directive=false][key.name='slot']": reportSlot, |
| 125 | + "VAttribute[directive=true][key.name.name='bind'][key.argument.name='slot']": reportVBindSlot |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments