123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- 'use strict'
- const canConvertToVSlotForElement = require('./utils/can-convert-to-v-slot')
- module.exports = {
- deprecated: '2.6.0',
- supported: '>=2.5.0 <3.0.0',
-
- createTemplateBodyVisitor(context, { fixToUpgrade } = {}) {
- const sourceCode = context.getSourceCode()
- const tokenStore =
- context.parserServices.getTemplateBodyTokenStore &&
- context.parserServices.getTemplateBodyTokenStore()
-
- function canConvertToVSlot(startTag) {
- if (
- !canConvertToVSlotForElement(startTag.parent, sourceCode, tokenStore)
- ) {
- return false
- }
- const slotAttr = startTag.attributes.find(
- (attr) => attr.directive === false && attr.key.name === 'slot'
- )
- if (slotAttr) {
-
-
- return false
- }
- const vBindSlotAttr = startTag.attributes.find(
- (attr) =>
- attr.directive === true &&
- attr.key.name.name === 'bind' &&
- attr.key.argument &&
- attr.key.argument.type === 'VIdentifier' &&
- attr.key.argument.name === 'slot'
- )
- if (vBindSlotAttr) {
-
-
- return false
- }
- return true
- }
-
- function fixSlotScopeToVSlot(fixer, scopeAttr) {
- const scopeValue =
- scopeAttr && scopeAttr.value
- ? `=${sourceCode.getText(scopeAttr.value)}`
- : ''
- const replaceText = `v-slot${scopeValue}`
- return fixer.replaceText(scopeAttr, replaceText)
- }
-
- function reportSlotScope(scopeAttr) {
- context.report({
- node: scopeAttr.key,
- messageId: 'forbiddenSlotScopeAttribute',
- fix(fixer) {
- if (!fixToUpgrade) {
- return null
- }
-
- const startTag = scopeAttr.parent
- if (!canConvertToVSlot(startTag)) {
- return null
- }
- return fixSlotScopeToVSlot(fixer, scopeAttr)
- }
- })
- }
- return {
- "VAttribute[directive=true][key.name.name='slot-scope']": reportSlotScope
- }
- }
- }
|