File tree 5 files changed +142
-0
lines changed
5 files changed +142
-0
lines changed Original file line number Diff line number Diff line change @@ -144,6 +144,7 @@ For example:
144
144
| [ vue/block-spacing] ( ./block-spacing.md ) | disallow or enforce spaces inside of blocks after opening block and before closing block | :wrench : |
145
145
| [ vue/brace-style] ( ./brace-style.md ) | enforce consistent brace style for blocks | :wrench : |
146
146
| [ vue/camelcase] ( ./camelcase.md ) | enforce camelcase naming convention | |
147
+ | [ vue/class-order] ( ./class-order.md ) | enforce classnames order | :wrench : |
147
148
| [ vue/comma-dangle] ( ./comma-dangle.md ) | require or disallow trailing commas | :wrench : |
148
149
| [ vue/component-name-in-template-casing] ( ./component-name-in-template-casing.md ) | enforce specific casing for the component naming style in template | :wrench : |
149
150
| [ vue/dot-location] ( ./dot-location.md ) | enforce consistent newlines before and after dots | :wrench : |
Original file line number Diff line number Diff line change
1
+ ---
2
+ pageClass : rule-details
3
+ sidebarDepth : 0
4
+ title : vue/class-order
5
+ description : enforce classnames order
6
+ ---
7
+ # vue/class-order
8
+ > enforce classnames order
9
+
10
+ - :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.
11
+
12
+ ## :books : Further reading
13
+
14
+ - [ class-order]
15
+
16
+ [ class-order ] : https://eslint.org/docs/rules/class-order
17
+
18
+ ## :mag : Implementation
19
+
20
+ - [ Rule source] ( https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/class-order.js )
21
+ - [ Test source] ( https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/class-order.js )
Original file line number Diff line number Diff line change @@ -14,6 +14,7 @@ module.exports = {
14
14
'block-spacing' : require ( './rules/block-spacing' ) ,
15
15
'brace-style' : require ( './rules/brace-style' ) ,
16
16
'camelcase' : require ( './rules/camelcase' ) ,
17
+ 'class-order' : require ( './rules/class-order' ) ,
17
18
'comma-dangle' : require ( './rules/comma-dangle' ) ,
18
19
'comment-directive' : require ( './rules/comment-directive' ) ,
19
20
'component-name-in-template-casing' : require ( './rules/component-name-in-template-casing' ) ,
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @fileoverview Alphabetizes classnames.
3
+ * @author Maciej Chmurski
4
+ */
5
+ 'use strict'
6
+
7
+ // ------------------------------------------------------------------------------
8
+ // Requirements
9
+ // ------------------------------------------------------------------------------
10
+
11
+ const utils = require ( '../utils' )
12
+
13
+ // ------------------------------------------------------------------------------
14
+ // Helpers
15
+ // ------------------------------------------------------------------------------
16
+
17
+ // ------------------------------------------------------------------------------
18
+ // Rule Definition
19
+ // ------------------------------------------------------------------------------
20
+ module . exports = {
21
+ meta : {
22
+ type : 'suggestion' ,
23
+ docs : {
24
+ url : 'https://eslint.vuejs.org/rules/class-order.html' ,
25
+ description : 'enforce classnames order' ,
26
+ category : undefined
27
+ } ,
28
+ fixable : 'code' ,
29
+ schema : [ ]
30
+ } ,
31
+ create : context => {
32
+ return utils . defineTemplateBodyVisitor ( context , {
33
+ "VAttribute[directive=false][key.name='class']" ( node ) {
34
+ const classList = node . value . value
35
+ const classListSorted = classList . split ( ' ' ) . sort ( ) . join ( ' ' )
36
+
37
+ if ( classList !== classListSorted ) {
38
+ context . report ( {
39
+ node,
40
+ loc : node . loc ,
41
+ message : 'Classes should be ordered alphabetically.' ,
42
+ fix : ( fixer ) => fixer . replaceTextRange (
43
+ [ node . value . range [ 0 ] , node . value . range [ 1 ] ] , `"${ classListSorted } "`
44
+ )
45
+ } )
46
+ }
47
+ }
48
+ } )
49
+ }
50
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @fileoverview enforce ordering of classes
3
+ * @author Maciej Chmurski
4
+ */
5
+ 'use strict'
6
+
7
+ // ------------------------------------------------------------------------------
8
+ // Requirements
9
+ // ------------------------------------------------------------------------------
10
+
11
+ var rule = require ( '../../../lib/rules/class-order' )
12
+ var RuleTester = require ( 'eslint' ) . RuleTester
13
+
14
+ // ------------------------------------------------------------------------------
15
+ // Tests
16
+ // ------------------------------------------------------------------------------
17
+
18
+ var tester = new RuleTester ( {
19
+ parser : 'vue-eslint-parser' ,
20
+ parserOptions : { ecmaVersion : 2015 }
21
+ } )
22
+ tester . run ( 'class-order' , rule , {
23
+
24
+ valid : [
25
+ {
26
+ filename : 'test.vue' ,
27
+ code : '<template><div></div></template>'
28
+ } ,
29
+ {
30
+ filename : 'test.vue' ,
31
+ code : '<template><div class="a b"></div></template>'
32
+ } ,
33
+ {
34
+ filename : 'test.vue' ,
35
+ code : '<template><div class="a"></div></template>'
36
+ }
37
+ ] ,
38
+
39
+ invalid : [
40
+ {
41
+ filename : 'test.vue' ,
42
+ code : '<template><div class="b a"></div></template>' ,
43
+ output : '<template><div class="a b"></div></template>' ,
44
+ errors : [ {
45
+ message : 'Classes should be ordered alphabetically.' ,
46
+ type : 'VAttribute'
47
+ } ]
48
+ } ,
49
+ {
50
+ filename : 'test.vue' ,
51
+ code :
52
+ `<template>
53
+ <div class="c b a">
54
+ </div>
55
+ </template>` ,
56
+ output :
57
+ `<template>
58
+ <div class="a b c">
59
+ </div>
60
+ </template>` ,
61
+ errors : [
62
+ {
63
+ message : 'Classes should be ordered alphabetically.' ,
64
+ type : 'VAttribute'
65
+ }
66
+ ]
67
+ }
68
+ ]
69
+ } )
You can’t perform that action at this time.
0 commit comments