Skip to content
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

Add allowModifiers option to valid-v-slot #1330

Merged
merged 5 commits into from Oct 18, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 37 additions & 1 deletion docs/rules/valid-v-slot.md
Expand Up @@ -100,7 +100,43 @@ This rule does not check syntax errors in directives because it's checked by [vu

## :wrench: Options

Nothing.
```json
{
"vue/valid-v-slot": ["error", {
"allowModifiers": false
}]
}
```

- `allowModifiers` (`boolean`) ... allows having modifiers in the argument of `v-slot` directives. Modifiers just after `v-slot` are still disallowed. E.g. `<template v-slot.foo>` default `false`.

### `allowModifiers: true`

<eslint-code-block :rules="{'vue/valid-v-slot': ['error', {allowModifiers: true}]}">

```vue
<template>
<!-- ignore -->
<MyComponent>
<template v-slot:foo></template>
<template v-slot:foo.bar></template>
<template v-slot:foo.baz></template>
<template v-slot:foo.bar.baz></template>
</MyComponent>

<!-- ✗ BAD */ -->
<MyComponent v-slot.foo="{data}">
{{ data }}
</MyComponent>
<MyComponent>
<template v-slot.foo>
bar
</template>
</MyComponent>
</template>
```

</eslint-code-block>

## :couple: Related Rules

Expand Down
42 changes: 36 additions & 6 deletions lib/rules/valid-v-slot.js
Expand Up @@ -75,15 +75,17 @@ function getSlotDirectivesOnChildren(node) {
}

/**
* Get the normalized name of a given `v-slot` directive node.
* Get the normalized name of a given `v-slot` directive node with modifiers after `v-slot:` directive.
* @param {VDirective} node The `v-slot` directive node.
* @param {SourceCode} sourceCode The source code.
* @returns {string} The normalized name.
*/
function getNormalizedName(node, sourceCode) {
return node.key.argument == null
? 'default'
: sourceCode.getText(node.key.argument)
if (node.key.argument == null) {
return 'default'
}
const modifierNames = node.key.modifiers.map((modifier) => modifier.name)
return [sourceCode.getText(node.key.argument), ...modifierNames].join('.')
ota-meshi marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down Expand Up @@ -150,6 +152,22 @@ function isUsingScopeVar(vSlot) {
return false
}

/**
* Check whether a given argument node has invalid modifiers like `v-slot.foo`.
* @param {VDirective} vSlot The `v-slot` directive to check.
* @param {SourceCode} sourceCode The source code.
* @param {boolean} allowModifiers `allowModifiers` option in context.
* @return {boolean} `true` if that argument node has invalid modifiers like `v-slot.foo`.
*/
function hasInvalidModifiers(vSlot, sourceCode, allowModifiers) {
if (!allowModifiers) {
return vSlot.key.modifiers.length >= 1
}
// E.g., `v-slot` extracted from <template v-slot.foo.bar>
const directiveKey = sourceCode.getText(vSlot).split('.')[0]
return directiveKey === 'v-slot'
ota-meshi marked this conversation as resolved.
Show resolved Hide resolved
}

module.exports = {
meta: {
type: 'problem',
Expand All @@ -159,7 +177,16 @@ module.exports = {
url: 'https://eslint.vuejs.org/rules/valid-v-slot.html'
},
fixable: null,
schema: [],
schema: [
{
type: 'object',
properties: {
allowModifiers: {
type: 'boolean'
}
}
}
],
messages: {
ownerMustBeCustomElement:
"'v-slot' directive must be owned by a custom element, but '{{name}}' is not.",
Expand All @@ -181,6 +208,8 @@ module.exports = {
/** @param {RuleContext} context */
create(context) {
const sourceCode = context.getSourceCode()
const options = context.options[0] || {}
const allowModifiers = options.allowModifiers === true

return utils.defineTemplateBodyVisitor(context, {
/** @param {VDirective} node */
Expand Down Expand Up @@ -264,7 +293,8 @@ module.exports = {
}

// Verify modifiers.
if (node.key.modifiers.length >= 1) {
if (hasInvalidModifiers(node, sourceCode, allowModifiers)) {
// E.g., <template v-slot.foo>
context.report({
node,
messageId: 'disallowAnyModifier'
Expand Down
54 changes: 54 additions & 0 deletions tests/lib/rules/valid-v-slot.js
Expand Up @@ -84,6 +84,29 @@ tester.run('valid-v-slot', rule, {
<template v-for="(key, value) in xxxx" #[key]>{{value}}</template>
</MyComponent>
</template>`,
{
code: `
<template>
<MyComponent>
<template v-slot:foo.bar></template>
</MyComponent>
</template>
`,
options: [{ allowModifiers: true }]
},
{
code: `
<template>
<MyComponent>
<template v-slot:foo></template>
<template v-slot:foo.bar></template>
<template v-slot:foo.baz></template>
<template v-slot:foo.bar.baz></template>
</MyComponent>
</template>
`,
options: [{ allowModifiers: true }]
},
// parsing error
{
filename: 'parsing-error.vue',
Expand Down Expand Up @@ -279,6 +302,7 @@ tester.run('valid-v-slot', rule, {
<MyComponent v-slot.foo="{data}">{{data}}</MyComponent>
</template>
`,
options: [{ allowModifiers: true }],
ota-meshi marked this conversation as resolved.
Show resolved Hide resolved
errors: [{ messageId: 'disallowAnyModifier' }]
},
{
Expand All @@ -289,8 +313,38 @@ tester.run('valid-v-slot', rule, {
</MyComponent>
</template>
`,
options: [{ allowModifiers: true }],
ota-meshi marked this conversation as resolved.
Show resolved Hide resolved
errors: [{ messageId: 'disallowAnyModifier' }]
},
{
code: `
<template>
<MyComponent>
<template v-slot:foo.bar></template>
</MyComponent>
</template>
`,
options: [{ allowModifiers: false }],
errors: [{ messageId: 'disallowAnyModifier' }]
},
{
code: `
<template>
<MyComponent>
<template v-slot:foo></template>
<template v-slot:foo.bar></template>
<template v-slot:foo.baz></template>
<template v-slot:foo.bar.baz></template>
</MyComponent>
</template>
`,
options: [{ allowModifiers: false }],
errors: [
{ messageId: 'disallowAnyModifier' },
{ messageId: 'disallowAnyModifier' },
{ messageId: 'disallowAnyModifier' }
]
},
ota-meshi marked this conversation as resolved.
Show resolved Hide resolved

// Verify value.
{
Expand Down