-
-
Notifications
You must be signed in to change notification settings - Fork 666
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
⭐️New: Add vue/no-deprecated-slot-scope-attribute rule (#840)
* ⭐️New: Add vue/no-deprecated-slot-scope-attribute rule * Changed not to autofix, if it becomes an invalid attribute * Changed to not autofix, if if the element have `v-bind:slot` * Change to autofix
- Loading branch information
Showing
6 changed files
with
301 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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-scope-attribute | ||
description: disallow deprecated `slot-scope` attribute (in Vue.js 2.6.0+) | ||
--- | ||
# vue/no-deprecated-slot-scope-attribute | ||
> disallow deprecated `slot-scope` 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-scope` attribute in Vue.js v2.6.0+. | ||
|
||
<eslint-code-block fix :rules="{'vue/no-deprecated-slot-scope-attribute': ['error']}"> | ||
|
||
```vue | ||
<template> | ||
<ListComponent> | ||
<!-- ✓ GOOD --> | ||
<template v-slot="props"> | ||
{{ props.title }} | ||
</template> | ||
</ListComponent> | ||
<ListComponent> | ||
<!-- ✗ BAD --> | ||
<template slot-scope="props"> | ||
{{ props.title }} | ||
</template> | ||
</ListComponent> | ||
</template> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
## :books: Further reading | ||
|
||
- [API - slot-scope](https://vuejs.org/v2/api/#slot-scope-deprecated) | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/no-deprecated-slot-scope-attribute.js) | ||
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/no-deprecated-slot-scope-attribute.js) |
This file contains 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 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 slotScopeAttribute = require('./syntaxes/slot-scope-attribute') | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: 'disallow deprecated `slot-scope` attribute (in Vue.js 2.6.0+)', | ||
category: undefined, | ||
url: 'https://eslint.vuejs.org/rules/no-deprecated-slot-scope-attribute.html' | ||
}, | ||
fixable: 'code', | ||
schema: [], | ||
messages: { | ||
forbiddenSlotScopeAttribute: '`slot-scope` are deprecated.' | ||
} | ||
}, | ||
create (context) { | ||
const templateBodyVisitor = slotScopeAttribute.createTemplateBodyVisitor(context, { fixToUpgrade: true }) | ||
return utils.defineTemplateBodyVisitor(context, templateBodyVisitor) | ||
} | ||
} |
This file contains 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,84 @@ | ||
/** | ||
* @author Yosuke Ota | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
'use strict' | ||
module.exports = { | ||
deprecated: '2.6.0', | ||
supported: '2.5.0', | ||
createTemplateBodyVisitor (context, { fixToUpgrade } = {}) { | ||
const sourceCode = context.getSourceCode() | ||
|
||
/** | ||
* Checks whether the given node can convert to the `v-slot`. | ||
* @param {VStartTag} startTag node of `<element v-slot ... >` | ||
* @returns {boolean} `true` if the given node can convert to the `v-slot` | ||
*/ | ||
function canConvertToVSlot (startTag) { | ||
if (startTag.parent.name !== 'template') { | ||
return false | ||
} | ||
|
||
const slotAttr = startTag.attributes | ||
.find(attr => attr.directive === false && attr.key.name === 'slot') | ||
if (slotAttr) { | ||
// if the element have `slot` it can not be converted. | ||
// Conversion of `slot` is done with `vue/no-deprecated-slot-attribute`. | ||
return false | ||
} | ||
|
||
const vBindSlotAttr = startTag.attributes | ||
.find(attr => | ||
attr.directive === true && | ||
attr.key.name.name === 'bind' && | ||
attr.key.argument && | ||
attr.key.argument.name === 'slot') | ||
if (vBindSlotAttr) { | ||
// if the element have `v-bind:slot` it can not be converted. | ||
// Conversion of `v-bind:slot` is done with `vue/no-deprecated-slot-attribute`. | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
/** | ||
* Convert to `v-slot`. | ||
* @param {object} fixer fixer | ||
* @param {VAttribute | null} scopeAttr node of `slot-scope` | ||
* @returns {*} fix data | ||
*/ | ||
function fixSlotScopeToVSlot (fixer, scopeAttr) { | ||
const scopeValue = scopeAttr && scopeAttr.value | ||
? `=${sourceCode.getText(scopeAttr.value)}` | ||
: '' | ||
|
||
const replaceText = `v-slot${scopeValue}` | ||
return fixer.replaceText(scopeAttr, replaceText) | ||
} | ||
/** | ||
* Reports `slot-scope` node | ||
* @param {VAttribute} scopeAttr node of `slot-scope` | ||
* @returns {void} | ||
*/ | ||
function reportSlotScope (scopeAttr) { | ||
context.report({ | ||
node: scopeAttr.key, | ||
messageId: 'forbiddenSlotScopeAttribute', | ||
fix: fixToUpgrade | ||
// fix to use `v-slot` | ||
? (fixer) => { | ||
const startTag = scopeAttr.parent | ||
if (!canConvertToVSlot(startTag)) { | ||
return null | ||
} | ||
return fixSlotScopeToVSlot(fixer, scopeAttr) | ||
} | ||
: null | ||
}) | ||
} | ||
|
||
return { | ||
"VAttribute[directive=true][key.name.name='slot-scope']": reportSlotScope | ||
} | ||
} | ||
} |
This file contains 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,143 @@ | ||
'use strict' | ||
|
||
const RuleTester = require('eslint').RuleTester | ||
const rule = require('../../../lib/rules/no-deprecated-slot-scope-attribute') | ||
|
||
const tester = new RuleTester({ | ||
parser: 'vue-eslint-parser', | ||
parserOptions: { | ||
ecmaVersion: 2015 | ||
} | ||
}) | ||
|
||
tester.run('no-deprecated-slot-scope-attribute', rule, { | ||
valid: [ | ||
`<template> | ||
<LinkList> | ||
<template v-slot:name><a /></template> | ||
</LinkList> | ||
</template>`, | ||
`<template> | ||
<LinkList> | ||
<template #name><a /></template> | ||
</LinkList> | ||
</template>`, | ||
`<template> | ||
<LinkList> | ||
<template v-slot="{a}"><a /></template> | ||
</LinkList> | ||
</template>`, | ||
`<template> | ||
<LinkList v-slot="{a}"> | ||
<a /> | ||
</LinkList> | ||
</template>`, | ||
`<template> | ||
<LinkList> | ||
<template #default="{a}"><a /></template> | ||
</LinkList> | ||
</template>`, | ||
`<template> | ||
<LinkList> | ||
<a slot="name" /> | ||
</LinkList> | ||
</template>`, | ||
`<template> | ||
<LinkList> | ||
<template><a /></template> | ||
</LinkList> | ||
</template>`, | ||
`<template> | ||
<LinkList> | ||
<a /> | ||
</LinkList>` | ||
], | ||
invalid: [ | ||
{ | ||
code: ` | ||
<template> | ||
<LinkList> | ||
<template slot-scope="{a}"><a /></template> | ||
</LinkList> | ||
</template>`, | ||
output: ` | ||
<template> | ||
<LinkList> | ||
<template v-slot="{a}"><a /></template> | ||
</LinkList> | ||
</template>`, | ||
errors: [ | ||
{ | ||
message: '`slot-scope` are deprecated.', | ||
line: 4 | ||
} | ||
] | ||
}, | ||
{ | ||
code: ` | ||
<template> | ||
<LinkList> | ||
<template slot-scope><a /></template> | ||
</LinkList> | ||
</template>`, | ||
output: ` | ||
<template> | ||
<LinkList> | ||
<template v-slot><a /></template> | ||
</LinkList> | ||
</template>`, | ||
errors: [ | ||
{ | ||
message: '`slot-scope` are deprecated.', | ||
line: 4 | ||
} | ||
] | ||
}, | ||
// cannot fix | ||
{ | ||
code: ` | ||
<template> | ||
<LinkList> | ||
<a slot-scope="{a}" /> | ||
</LinkList> | ||
</template>`, | ||
output: null, | ||
errors: [ | ||
{ | ||
message: '`slot-scope` are deprecated.', | ||
line: 4 | ||
} | ||
] | ||
}, | ||
{ | ||
code: ` | ||
<template> | ||
<LinkList> | ||
<template slot-scope="{a}" slot="foo"><a /></template> | ||
</LinkList> | ||
</template>`, | ||
output: null, | ||
errors: [ | ||
{ | ||
message: '`slot-scope` are deprecated.', | ||
line: 4 | ||
} | ||
] | ||
}, | ||
{ | ||
code: ` | ||
<template> | ||
<LinkList> | ||
<template slot-scope="{a}" :slot="arg"><a /></template> | ||
</LinkList> | ||
</template>`, | ||
output: null, | ||
errors: [ | ||
{ | ||
message: '`slot-scope` are deprecated.', | ||
line: 4 | ||
} | ||
] | ||
} | ||
] | ||
}) |