From 9736e0361cdfe6be353a4c0cc1533044486515f9 Mon Sep 17 00:00:00 2001 From: nokazn Date: Sun, 11 Oct 2020 18:21:36 +0900 Subject: [PATCH 1/5] add allowModifiers option to valid-v-slot --- lib/rules/valid-v-slot.js | 42 +++++++++++++++++++++---- tests/lib/rules/valid-v-slot.js | 54 +++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 6 deletions(-) diff --git a/lib/rules/valid-v-slot.js b/lib/rules/valid-v-slot.js index a94d8c9dc..06536a184 100644 --- a/lib/rules/valid-v-slot.js +++ b/lib/rules/valid-v-slot.js @@ -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('.') } /** @@ -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 `, + options: [{ allowModifiers: true }], errors: [{ messageId: 'disallowAnyModifier' }] }, { @@ -289,8 +313,38 @@ tester.run('valid-v-slot', rule, { `, + options: [{ allowModifiers: true }], errors: [{ messageId: 'disallowAnyModifier' }] }, + { + code: ` + + `, + options: [{ allowModifiers: false }], + errors: [{ messageId: 'disallowAnyModifier' }] + }, + { + code: ` + + `, + options: [{ allowModifiers: false }], + errors: [ + { messageId: 'disallowAnyModifier' }, + { messageId: 'disallowAnyModifier' }, + { messageId: 'disallowAnyModifier' } + ] + }, // Verify value. { From 168dddb709ebe702caae2ea09790c3ec8e31e761 Mon Sep 17 00:00:00 2001 From: nokazn Date: Sun, 11 Oct 2020 20:12:23 +0900 Subject: [PATCH 2/5] update doc --- docs/rules/valid-v-slot.md | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/docs/rules/valid-v-slot.md b/docs/rules/valid-v-slot.md index 2a9183926..f0810cc75 100644 --- a/docs/rules/valid-v-slot.md +++ b/docs/rules/valid-v-slot.md @@ -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. `