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
1 parent
c3111fe
commit 473d5bf
Showing
5 changed files
with
146 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,21 @@ | ||
--- | ||
pageClass: rule-details | ||
sidebarDepth: 0 | ||
title: vue/class-order | ||
description: enforce classnames order | ||
--- | ||
# vue/class-order | ||
> enforce classnames 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. | ||
|
||
## :books: Further reading | ||
|
||
- [class-order] | ||
|
||
[class-order]: https://eslint.org/docs/rules/class-order | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/class-order.js) | ||
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/class-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,50 @@ | ||
/** | ||
* @fileoverview Alphabetizes classnames. | ||
* @author Maciej Chmurski | ||
*/ | ||
'use strict' | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
const utils = require('../utils') | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Helpers | ||
// ------------------------------------------------------------------------------ | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
module.exports = { | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
url: 'https://eslint.vuejs.org/rules/class-order.html', | ||
description: 'enforce classnames order', | ||
category: undefined | ||
}, | ||
fixable: 'code', | ||
schema: [] | ||
}, | ||
create: context => { | ||
return utils.defineTemplateBodyVisitor(context, { | ||
"VAttribute[directive=false][key.name='class']" (node) { | ||
const classList = node.value.value | ||
const classListSorted = classList.split(' ').sort().join(' ') | ||
|
||
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,69 @@ | ||
/** | ||
* @fileoverview enforce ordering of classes | ||
* @author Maciej Chmurski | ||
*/ | ||
'use strict' | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
var rule = require('../../../lib/rules/class-order') | ||
var RuleTester = require('eslint').RuleTester | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Tests | ||
// ------------------------------------------------------------------------------ | ||
|
||
var tester = new RuleTester({ | ||
parser: 'vue-eslint-parser', | ||
parserOptions: { ecmaVersion: 2015 } | ||
}) | ||
tester.run('class-order', rule, { | ||
|
||
valid: [ | ||
{ | ||
filename: 'test.vue', | ||
code: '<template><div></div></template>' | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: '<template><div class="a b"></div></template>' | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: '<template><div class="a"></div></template>' | ||
} | ||
], | ||
|
||
invalid: [ | ||
{ | ||
filename: 'test.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: 'test.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' | ||
} | ||
] | ||
} | ||
] | ||
}) |