-
-
Notifications
You must be signed in to change notification settings - Fork 680
feat: add slot-name-casing rule #2620
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
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,88 @@ | ||
--- | ||
pageClass: rule-details | ||
sidebarDepth: 0 | ||
title: vue/slot-name-casing | ||
description: enforce specific casing for the slot name | ||
--- | ||
|
||
# vue/slot-name-casing | ||
|
||
> enforce specific casing for the slot name | ||
|
||
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> _**This rule has not been released yet.**_ </badge> | ||
|
||
## :book: Rule Details | ||
|
||
This rule enforce proper casing of slot names in vue components. | ||
|
||
<eslint-code-block :rules="{'vue/slot-name-casing': ['error']}"> | ||
|
||
```vue | ||
<template> | ||
<!-- ✓ GOOD --> | ||
<slot name="foo" /> | ||
<slot name="fooBar" /> | ||
|
||
<!-- ✗ BAD --> | ||
<slot name="foo-bar" /> | ||
<slot name="foo_bar" /> | ||
<slot name="foo:bar" /> | ||
</template> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
## :wrench: Options | ||
|
||
```json | ||
{ | ||
"vue/slot-name-casing": ["error", "camelCase" | "kebab-case" | "singleword"] | ||
} | ||
``` | ||
|
||
- `"camelCase"` (default) ... Enforce slot name to be in camel case. | ||
- `"kebab-case"` ... Enforce slot name to be in kebab case. | ||
- `"singleword"` ... Enforce slot name to be a single word. | ||
|
||
### `"kebab-case"` | ||
|
||
<eslint-code-block :rules="{'vue/prop-name-casing': ['error', 'kebab-case']}"> | ||
|
||
```vue | ||
<template> | ||
<!-- ✓ GOOD --> | ||
<slot name="foo" /> | ||
<slot name="foo-bar" /> | ||
|
||
<!-- ✗ BAD --> | ||
<slot name="fooBar" /> | ||
<slot name="foo_bar" /> | ||
<slot name="foo:bar" /> | ||
</template> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
### `"singleword"` | ||
|
||
<eslint-code-block :rules="{'vue/prop-name-casing': ['error', 'singleword']}"> | ||
|
||
```vue | ||
<template> | ||
<!-- ✓ GOOD --> | ||
<slot name="foo" /> | ||
|
||
<!-- ✗ BAD --> | ||
<slot name="foo-bar" /> | ||
<slot name="fooBar" /> | ||
<slot name="foo_bar" /> | ||
<slot name="foo:bar" /> | ||
</template> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/slot-name-casing.js) | ||
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/slot-name-casing.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,82 @@ | ||
/** | ||
* @author Wayne Zhang | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
'use strict' | ||
|
||
const utils = require('../utils') | ||
const casing = require('../utils/casing') | ||
|
||
/** | ||
* @typedef { 'camelCase' | 'kebab-case' | 'singleword' } OptionType | ||
* @typedef { (str: string) => boolean } CheckerType | ||
*/ | ||
|
||
/** | ||
* Checks whether the given string is a single word. | ||
* @param {string} str | ||
* @return {boolean} | ||
*/ | ||
function isSingleWord(str) { | ||
return /^[a-z]+$/u.test(str) | ||
} | ||
|
||
/** @type {OptionType[]} */ | ||
const allowedCaseOptions = ['camelCase', 'kebab-case', 'singleword'] | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: 'enforce specific casing for the slot name', | ||
waynzh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
categories: undefined, | ||
url: 'https://eslint.vuejs.org/rules/slot-name-casing.html' | ||
}, | ||
fixable: null, | ||
schema: [ | ||
{ | ||
enum: allowedCaseOptions | ||
} | ||
], | ||
messages: { | ||
invalidCase: 'Slot name "{{name}}" is not {{caseType}}.' | ||
} | ||
}, | ||
/** @param {RuleContext} context */ | ||
create(context) { | ||
const option = context.options[0] | ||
|
||
/** @type {OptionType} */ | ||
const caseType = allowedCaseOptions.includes(option) ? option : 'camelCase' | ||
|
||
/** @type {CheckerType} */ | ||
const checker = | ||
caseType === 'singleword' ? isSingleWord : casing.getChecker(caseType) | ||
|
||
/** @param {VAttribute} node */ | ||
function processSlotNode(node) { | ||
const name = node.value?.value | ||
if (name && !checker(name)) { | ||
context.report({ | ||
node, | ||
loc: node.loc, | ||
messageId: 'invalidCase', | ||
data: { | ||
name, | ||
caseType | ||
} | ||
}) | ||
} | ||
} | ||
|
||
return utils.defineTemplateBodyVisitor(context, { | ||
/** @param {VElement} node */ | ||
"VElement[name='slot']"(node) { | ||
const slotName = utils.getAttribute(node, 'name') | ||
if (slotName) { | ||
processSlotNode(slotName) | ||
} | ||
} | ||
}) | ||
} | ||
} |
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,143 @@ | ||
/** | ||
* @author WayneZhang | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
'use strict' | ||
|
||
const RuleTester = require('../../eslint-compat').RuleTester | ||
const rule = require('../../../lib/rules/slot-name-casing') | ||
|
||
const tester = new RuleTester({ | ||
languageOptions: { | ||
parser: require('vue-eslint-parser'), | ||
ecmaVersion: 2020, | ||
sourceType: 'module' | ||
} | ||
}) | ||
|
||
tester.run('slot-name-casing', rule, { | ||
valid: [ | ||
`<template><slot key="foo" /></template>`, | ||
`<template><slot name /></template>`, | ||
`<template><slot name="foo" /></template>`, | ||
`<template><slot name="fooBar" /></template>`, | ||
`<template><slot :name="fooBar" /></template>`, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
<template> | ||
<slot name="foo" /> | ||
<slot name="foo-bar" /> | ||
<slot :name="fooBar" /> | ||
</template>`, | ||
waynzh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
options: ['kebab-case'] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
<template> | ||
<slot name="foo" /> | ||
<slot :name="fooBar" /> | ||
</template>`, | ||
options: ['singleword'] | ||
waynzh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
], | ||
invalid: [ | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
<template> | ||
<slot name="foo-bar" /> | ||
<slot name="foo-Bar_baz" /> | ||
</template>`, | ||
errors: [ | ||
{ | ||
messageId: 'invalidCase', | ||
data: { | ||
name: 'foo-bar', | ||
caseType: 'camelCase' | ||
}, | ||
line: 3, | ||
column: 13 | ||
}, | ||
{ | ||
messageId: 'invalidCase', | ||
data: { | ||
name: 'foo-Bar_baz', | ||
caseType: 'camelCase' | ||
}, | ||
line: 4, | ||
column: 13 | ||
} | ||
waynzh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
<template> | ||
<slot name="fooBar" /> | ||
<slot name="foo-Bar_baz" /> | ||
</template>`, | ||
options: ['kebab-case'], | ||
errors: [ | ||
{ | ||
messageId: 'invalidCase', | ||
data: { | ||
name: 'fooBar', | ||
caseType: 'kebab-case' | ||
}, | ||
line: 3, | ||
column: 13 | ||
}, | ||
{ | ||
messageId: 'invalidCase', | ||
data: { | ||
name: 'foo-Bar_baz', | ||
caseType: 'kebab-case' | ||
}, | ||
line: 4, | ||
column: 13 | ||
} | ||
waynzh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
<template> | ||
<slot name="foo-bar" /> | ||
<slot name="fooBar" /> | ||
<slot name="foo-Bar_baz" /> | ||
</template>`, | ||
options: ['singleword'], | ||
errors: [ | ||
{ | ||
messageId: 'invalidCase', | ||
data: { | ||
name: 'foo-bar', | ||
caseType: 'singleword' | ||
}, | ||
line: 3, | ||
column: 13 | ||
}, | ||
{ | ||
messageId: 'invalidCase', | ||
data: { | ||
name: 'fooBar', | ||
caseType: 'singleword' | ||
}, | ||
line: 4, | ||
column: 13 | ||
}, | ||
{ | ||
messageId: 'invalidCase', | ||
data: { | ||
name: 'foo-Bar_baz', | ||
caseType: 'singleword' | ||
}, | ||
line: 5, | ||
column: 13 | ||
} | ||
waynzh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
] | ||
} | ||
] | ||
}) |
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.