-
-
Notifications
You must be signed in to change notification settings - Fork 689
⭐️New: Add vue/no-deprecated-slot-attribute rule #839
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f28bdaa
⭐️New: Add vue/no-deprecated-slot-attribute rule
ota-meshi 1ce47a1
Made the following changes
ota-meshi bf5f03f
Changed not to autofix if `v-bind:slot` contains non Latin characters.
ota-meshi 014e6b6
Merge branch 'master' into add-rule/no-deprecated-slot-attribute
mysticatea 6bbc928
Changed it to autofix only when `slot` is attached to `<template>`
ota-meshi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
--- | ||
pageClass: rule-details | ||
sidebarDepth: 0 | ||
title: vue/no-deprecated-slot-attribute | ||
description: disallow deprecated `slot` attribute (in Vue.js 2.6.0+) | ||
--- | ||
# vue/no-deprecated-slot-attribute | ||
> disallow deprecated `slot` attribute (in Vue.js 2.6.0+) | ||
|
||
- :wrench: The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) can automatically fix some of the problems reported by this rule. | ||
|
||
## :book: Rule Details | ||
|
||
This rule reports deprecated `slot` attribute in Vue.js v2.6.0+. | ||
|
||
<eslint-code-block fix :rules="{'vue/no-deprecated-slot-attribute': ['error']}"> | ||
|
||
```vue | ||
<template> | ||
<ListComponent> | ||
<!-- ✓ GOOD --> | ||
<template v-slot:name> | ||
{{ props.title }} | ||
</template> | ||
</ListComponent> | ||
<ListComponent> | ||
<!-- ✗ BAD --> | ||
<template slot="name"> | ||
{{ props.title }} | ||
</template> | ||
</ListComponent> | ||
</template> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
## :books: Further reading | ||
|
||
- [API - slot](https://vuejs.org/v2/api/#slot-deprecated) | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/no-deprecated-slot-attribute.js) | ||
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/no-deprecated-slot-attribute.js) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* @author Yosuke Ota | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
'use strict' | ||
|
||
const utils = require('../utils') | ||
const slotAttribute = require('./syntaxes/slot-attribute') | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: 'disallow deprecated `slot` attribute (in Vue.js 2.6.0+)', | ||
category: undefined, | ||
url: 'https://eslint.vuejs.org/rules/no-deprecated-slot-attribute.html' | ||
}, | ||
fixable: 'code', | ||
schema: [], | ||
messages: { | ||
forbiddenSlotAttribute: '`slot` attributes are deprecated.' | ||
} | ||
}, | ||
create (context) { | ||
const templateBodyVisitor = slotAttribute.createTemplateBodyVisitor(context) | ||
return utils.defineTemplateBodyVisitor(context, templateBodyVisitor) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/** | ||
* @author Yosuke Ota | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
'use strict' | ||
module.exports = { | ||
deprecated: '2.6.0', | ||
createTemplateBodyVisitor (context) { | ||
const sourceCode = context.getSourceCode() | ||
|
||
/** | ||
* Checks whether the given node can convert to the `v-slot`. | ||
* @param {VAttribute} slotAttr node of `slot` | ||
* @returns {boolean} `true` if the given node can convert to the `v-slot` | ||
*/ | ||
function canConvertFromSlotToVSlot (slotAttr) { | ||
if (!slotAttr.value) { | ||
return true | ||
} | ||
const slotName = slotAttr.value.value | ||
// If non-Latin characters are included it can not be converted. | ||
return !/[^a-z]/i.test(slotName) | ||
} | ||
|
||
/** | ||
* Checks whether the given node can convert to the `v-slot`. | ||
* @param {VAttribute} slotAttr node of `v-bind:slot` | ||
* @returns {boolean} `true` if the given node can convert to the `v-slot` | ||
*/ | ||
function canConvertFromVBindSlotToVSlot (slotAttr) { | ||
if (!slotAttr.value) { | ||
return true | ||
} | ||
|
||
if (!slotAttr.value.expression) { | ||
// parse error or empty expression | ||
return false | ||
} | ||
const slotName = sourceCode.getText(slotAttr.value.expression).trim() | ||
// If space is included it can not be converted. | ||
return !/\s/.test(slotName) | ||
} | ||
|
||
/** | ||
* Convert to `v-slot`. | ||
* @param {object} fixer fixer | ||
* @param {VAttribute} slotAttr node of `slot` | ||
* @param {string | null} slotName name of `slot` | ||
* @param {boolean} vBind `true` if `slotAttr` is `v-bind:slot` | ||
* @returns {*} fix data | ||
*/ | ||
function fixSlotToVSlot (fixer, slotAttr, slotName, vBind) { | ||
const element = slotAttr.parent | ||
const scopeAttr = element.attributes | ||
.find(attr => attr.directive === true && attr.key.name && ( | ||
attr.key.name.name === 'slot-scope' || | ||
attr.key.name.name === 'scope' | ||
)) | ||
const nameArgument = slotName ? (vBind ? `:[${slotName}]` : `:${slotName}`) : '' | ||
const scopeValue = scopeAttr && scopeAttr.value | ||
? `=${sourceCode.getText(scopeAttr.value)}` | ||
: '' | ||
|
||
const replaceText = `v-slot${nameArgument}${scopeValue}` | ||
const fixers = [ | ||
fixer.replaceText(slotAttr || scopeAttr, replaceText) | ||
] | ||
if (slotAttr && scopeAttr) { | ||
fixers.push(fixer.remove(scopeAttr)) | ||
} | ||
return fixers | ||
} | ||
/** | ||
* Reports `slot` node | ||
* @param {VAttribute} slotAttr node of `slot` | ||
* @returns {void} | ||
*/ | ||
function reportSlot (slotAttr) { | ||
context.report({ | ||
node: slotAttr.key, | ||
messageId: 'forbiddenSlotAttribute', | ||
// fix to use `v-slot` | ||
fix (fixer) { | ||
if (!canConvertFromSlotToVSlot(slotAttr)) { | ||
return null | ||
} | ||
const slotName = slotAttr.value && | ||
slotAttr.value.value | ||
return fixSlotToVSlot(fixer, slotAttr, slotName, false) | ||
} | ||
}) | ||
} | ||
/** | ||
* Reports `v-bind:slot` node | ||
* @param {VAttribute} slotAttr node of `v-bind:slot` | ||
* @returns {void} | ||
*/ | ||
function reportVBindSlot (slotAttr) { | ||
context.report({ | ||
node: slotAttr.key, | ||
messageId: 'forbiddenSlotAttribute', | ||
// fix to use `v-slot` | ||
fix (fixer) { | ||
if (!canConvertFromVBindSlotToVSlot(slotAttr)) { | ||
return null | ||
} | ||
const slotName = slotAttr.value && | ||
slotAttr.value.expression && | ||
sourceCode.getText(slotAttr.value.expression).trim() | ||
return fixSlotToVSlot(fixer, slotAttr, slotName, true) | ||
} | ||
}) | ||
} | ||
|
||
return { | ||
"VAttribute[directive=false][key.name='slot']": reportSlot, | ||
"VAttribute[directive=true][key.name.name='bind'][key.argument.name='slot']": reportVBindSlot | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.