-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
109 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,31 @@ | ||
# Ensure icon buttons have a variant defined (icon-button-variant) | ||
|
||
:wrench: This rule is fixable with `eslint --fix` | ||
|
||
## Rule Details | ||
|
||
Buttons in Vuetify 3 no longer have a different variant applied automatically. | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```html | ||
<v-btn icon /> | ||
<v-btn icon="search" /> | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```js | ||
<v-btn icon variant="text" /> | ||
<v-btn icon="search" variant="text" /> | ||
``` | ||
|
||
### Options | ||
|
||
A different variant other than `text` can be assigned: | ||
|
||
```js | ||
{ | ||
'vuetify/icon-button-variant': ['error', 'plain'] | ||
} | ||
``` |
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,48 @@ | ||
'use strict' | ||
|
||
const { classify, getAttributes } = require('../util/helpers') | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: 'Ensure icon buttons have a variant defined.', | ||
category: 'recommended', | ||
}, | ||
fixable: 'code', | ||
schema: [ | ||
{ type: 'string' }, | ||
], | ||
messages: { | ||
needsVariant: 'Icon buttons should have {{ a }} defined.', | ||
}, | ||
}, | ||
|
||
create (context) { | ||
return context.parserServices.defineTemplateBodyVisitor({ | ||
VElement (element) { | ||
const tag = classify(element.rawName) | ||
if (tag !== 'VBtn') return | ||
|
||
const attributes = getAttributes(element) | ||
const iconAttribute = attributes.find(attr => attr.name === 'icon') | ||
if (!iconAttribute) return | ||
if (attributes.some(attr => attr.name === 'variant')) return | ||
|
||
const variant = `variant="${context.options[0] || 'text'}"` | ||
|
||
context.report({ | ||
node: iconAttribute.node, | ||
messageId: 'needsVariant', | ||
data: { a: variant }, | ||
fix (fixer) { | ||
return fixer.insertTextAfter(iconAttribute.node, ' ' + variant) | ||
}, | ||
}) | ||
}, | ||
}) | ||
}, | ||
} |
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 @@ | ||
const RuleTester = require('eslint').RuleTester | ||
const rule = require('../../src/rules/icon-button-variant') | ||
|
||
const tester = new RuleTester({ | ||
parser: require.resolve('vue-eslint-parser'), | ||
parserOptions: { ecmaVersion: 2015 }, | ||
}) | ||
|
||
tester.run('icon-button-variant', rule, { | ||
valid: [ | ||
'<template><v-btn /></template>', | ||
'<template><v-btn variant="text" /></template>', | ||
'<template><v-btn icon variant="text" /></template>', | ||
'<template><v-btn icon="foo" variant="text" /></template>', | ||
], | ||
invalid: [ | ||
{ | ||
code: '<template><v-btn icon /></template>', | ||
output: '<template><v-btn icon variant="text" /></template>', | ||
errors: [{ messageId: 'needsVariant' }], | ||
}, | ||
{ | ||
code: '<template><v-btn icon="foo" /></template>', | ||
output: '<template><v-btn icon="foo" variant="text" /></template>', | ||
errors: [{ messageId: 'needsVariant' }], | ||
}, | ||
], | ||
}) |