-
-
Notifications
You must be signed in to change notification settings - Fork 670
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
static-class-names-order rule (#886)
* class-order rule * support ascii whitespace, add tests * polish readme * polish the rule file * update rule parser usage * alphabetize * npm run update * fix typo
- Loading branch information
1 parent
e802d9e
commit a2865e4
Showing
5 changed files
with
171 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,37 @@ | ||
--- | ||
pageClass: rule-details | ||
sidebarDepth: 0 | ||
title: vue/static-class-names-order | ||
description: enforce static class names order | ||
--- | ||
# vue/static-class-names-order | ||
> enforce static class names order | ||
- :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. | ||
|
||
### Example | ||
|
||
<eslint-code-block fix :rules="{'vue/static-class-names-order': ['error']}"> | ||
|
||
```vue | ||
<template> | ||
<!-- ✓ GOOD --> | ||
<div class="a b"></div> | ||
<!-- ✗ BAD --> | ||
<div class="b a"></div> | ||
</template> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
## :books: Further reading | ||
|
||
- [static-class-names-order] | ||
|
||
[static-class-names-order]: https://eslint.org/docs/rules/static-class-names-order | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/static-class-names-order.js) | ||
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/static-class-names-order.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,55 @@ | ||
/** | ||
* @fileoverview Alphabetizes static class names. | ||
* @author Maciej Chmurski | ||
*/ | ||
'use strict' | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
const { defineTemplateBodyVisitor } = require('../utils') | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
module.exports = { | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
url: 'https://eslint.vuejs.org/rules/static-class-names-order.html', | ||
description: 'enforce static class names order', | ||
category: undefined | ||
}, | ||
fixable: 'code', | ||
schema: [] | ||
}, | ||
create: context => { | ||
return defineTemplateBodyVisitor(context, { | ||
"VAttribute[directive=false][key.name='class']" (node) { | ||
const classList = node.value.value | ||
const classListWithWhitespace = classList.split(/(\s+)/) | ||
|
||
// Detect and reuse any type of whitespace. | ||
let divider = '' | ||
if (classListWithWhitespace.length > 1) { | ||
divider = classListWithWhitespace[1] | ||
} | ||
|
||
const classListNoWhitespace = classListWithWhitespace.filter(className => className.trim() !== '') | ||
const classListSorted = classListNoWhitespace.sort().join(divider) | ||
|
||
if (classList !== classListSorted) { | ||
context.report({ | ||
node, | ||
loc: node.loc, | ||
message: 'Classes should be ordered alphabetically.', | ||
fix: (fixer) => fixer.replaceTextRange( | ||
[node.value.range[0], node.value.range[1]], `"${classListSorted}"` | ||
) | ||
}) | ||
} | ||
} | ||
}) | ||
} | ||
} |
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,77 @@ | ||
/** | ||
* @fileoverview enforce ordering of classes | ||
* @author Maciej Chmurski | ||
*/ | ||
'use strict' | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
var rule = require('../../../lib/rules/static-class-names-order') | ||
var RuleTester = require('eslint').RuleTester | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Tests | ||
// ------------------------------------------------------------------------------ | ||
|
||
var tester = new RuleTester({ | ||
parser: require.resolve('vue-eslint-parser'), | ||
parserOptions: { ecmaVersion: 2015 } | ||
}) | ||
tester.run('static-class-names-order', rule, { | ||
|
||
valid: [ | ||
{ | ||
filename: 'no-classes.vue', | ||
code: '<template><div></div></template>' | ||
}, | ||
{ | ||
filename: 'one-class.vue', | ||
code: '<template><div class="a"></div></template>' | ||
}, | ||
{ | ||
filename: 'single-space.vue', | ||
code: '<template><div class="a b c"></div></template>' | ||
}, | ||
{ | ||
filename: 'multiple-spaces.vue', | ||
code: '<template><div class="a b c"></div></template>' | ||
}, | ||
{ | ||
filename: 'tabs.vue', | ||
code: '<template><div class="a b c"></div></template>' | ||
} | ||
], | ||
|
||
invalid: [ | ||
{ | ||
filename: 'two-classes.vue', | ||
code: '<template><div class="b a"></div></template>', | ||
output: '<template><div class="a b"></div></template>', | ||
errors: [{ | ||
message: 'Classes should be ordered alphabetically.', | ||
type: 'VAttribute' | ||
}] | ||
}, | ||
{ | ||
filename: 'three-classes.vue', | ||
code: | ||
`<template> | ||
<div class="c b a"> | ||
</div> | ||
</template>`, | ||
output: | ||
`<template> | ||
<div class="a b c"> | ||
</div> | ||
</template>`, | ||
errors: [ | ||
{ | ||
message: 'Classes should be ordered alphabetically.', | ||
type: 'VAttribute' | ||
} | ||
] | ||
} | ||
] | ||
}) |