-
-
Notifications
You must be signed in to change notification settings - Fork 680
static-class-names-order rule #886
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7a19f2c
class-order rule
mchmurski-rms 37a9421
support ascii whitespace, add tests
mchmurski-rms 460b379
polish readme
mchmurski-rms 92e466d
polish the rule file
mchmurski-rms d921900
update rule parser usage
mchmurski-rms 638fddd
alphabetize
mchmurski-rms 14bcf74
npm run update
mchmurski-rms 54ab23d
fix typo
mchmurski-rms File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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' | ||
} | ||
] | ||
} | ||
] | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.