1
1
<template >
2
2
<div class =" app__root" >
3
3
<div class =" app__header" >
4
- <label class =" app__show-fixed-code-button" >
5
- <input
6
- type =" checkbox"
7
- v-model =" showFixedCode"
8
- >
9
- Show fixed code
4
+ <div class =" app__header-title" >
5
+ Playground for <a href =" https://github.com/vuejs/eslint-plugin-vue#readme" target =" _blank" >eslint-plugin-vue</a >.
6
+ </div >
7
+ <label class =" app__header-option-item" >
8
+ <select v-model.number =" indentSize" >
9
+ <option value =" 2" >TabSize: 2</option >
10
+ <option value =" 4" >TabSize: 4</option >
11
+ <option value =" 8" >TabSize: 8</option >
12
+ </select >
13
+ </label >
14
+ <label class =" app__header-option-item" >
15
+ <select v-model =" indentType" >
16
+ <option value =" space" >Indent: spaces</option >
17
+ <option value =" tab" >Indent: tabs</option >
18
+ </select >
19
+ </label >
20
+ <label class =" app__header-option-item" >
21
+ <select v-model =" editorType" >
22
+ <option value =" codeAndFixedCode" >FixedCode: show</option >
23
+ <option value =" codeOnly" >FixedCode: hide</option >
24
+ </select >
10
25
</label >
11
- Playground for <a href =" https://github.com/vuejs/eslint-plugin-vue#readme" target =" _blank" >eslint-plugin-vue</a >.
12
26
</div >
13
27
<div class =" app__body" >
14
28
<rule-select
23
37
:messages =" messages"
24
38
:fixed-code =" fixedCode"
25
39
:fixed-messages =" fixedMessages"
40
+ :format-options =" formatOptions"
26
41
:show-fixed-code =" showFixedCode"
27
- @edit =" onEdit "
42
+ @edit =" onCodeChange "
28
43
@initialize.once =" onEditorInitialize"
29
44
/>
30
45
<message-list class =" app__errors" :messages =" messages" />
@@ -66,70 +81,61 @@ export default {
66
81
},
67
82
68
83
computed: {
69
- messages () {
70
- try {
71
- return linter .verify (this .code , this .config , " vue-eslint-demo.vue" )
72
- }
73
- catch (err) {
74
- return [{
75
- fatal: true ,
76
- severity: 2 ,
77
- message: err .message ,
78
- line: 1 ,
79
- column: 0 ,
80
- }]
84
+ formatOptions () {
85
+ return {
86
+ insertSpaces: this .indentType === " space" ,
87
+ tabSize: this .indentSize ,
81
88
}
82
89
},
83
90
84
- fixResult () {
85
- try {
86
- return linter .verifyAndFix (this .code , this .config , " vue-eslint-demo.vue" )
87
- }
88
- catch (err) {
89
- return {
90
- output: this .code ,
91
- messages: [{
92
- fatal: true ,
93
- severity: 2 ,
94
- message: err .message ,
95
- line: 1 ,
96
- column: 0 ,
97
- }],
98
- }
99
- }
91
+ showFixedCode () {
92
+ return this .editorType === " codeAndFixedCode"
100
93
},
101
94
102
- fixedCode () {
103
- return this . fixResult . output
95
+ versions () {
96
+ return versions
104
97
},
98
+ },
105
99
106
- fixedMessages () {
107
- return this .fixResult .messages
100
+ // Use `watch` to re-use the internal state of the linter while making `messages` and `fixedCode`.
101
+ watch: {
102
+ code () {
103
+ this .lint ()
104
+ this .applyUrlHash ()
108
105
},
109
-
110
- versions () {
111
- return versions
106
+ indentSize () {
107
+ this .lint ()
108
+ this .applyUrlHash ()
109
+ },
110
+ indentType () {
111
+ this .lint ()
112
+ this .applyUrlHash ()
113
+ },
114
+ editorType () {
115
+ this .applyUrlHash ()
112
116
},
113
117
},
114
118
115
119
mounted () {
120
+ this .lint ()
116
121
window .addEventListener (" hashchange" , this .onUrlHashChange )
117
122
},
118
123
beforeDestroey () {
119
124
window .removeEventListener (" hashchange" , this .onUrlHashChange )
120
125
},
121
126
122
127
methods: {
123
- onEdit (code ) {
124
- this .code = code
125
- this .applyUrlHash ()
126
- },
127
-
128
128
onEditorInitialize () {
129
129
window .MainContent .show ()
130
130
},
131
131
132
+ onCodeChange (code ) {
133
+ this .code = code
134
+ },
135
+
132
136
onConfigChange () {
137
+ // The inside of `this.config` was changed directly.
138
+ this .lint ()
133
139
this .applyUrlHash ()
134
140
},
135
141
@@ -144,6 +150,53 @@ export default {
144
150
applyUrlHash () {
145
151
window .location .replace (` #${ serializeState (this .$data )} ` )
146
152
},
153
+
154
+ lint () {
155
+ // Adjust the indentation options to the editor settings.
156
+ const config = Object .assign ({}, this .config )
157
+ const rules = config .rules = Object .assign ({}, this .config .rules )
158
+ const indentType = (this .indentType === " space" ) ? this .indentSize : " tab"
159
+ rules .indent = [rules .indent , indentType]
160
+ rules[" vue/html-indent" ] = [rules[" vue/html-indent" ], indentType]
161
+
162
+ // Fix
163
+ try {
164
+ // At first, fix because `linter.verifyAndFix` does not accept SourceCode object.
165
+ const ret = linter .verifyAndFix (this .code , config, " vue-eslint-demo.vue" )
166
+ this .fixedCode = ret .output
167
+ this .fixedMessages = ret .messages
168
+ }
169
+ catch (err) {
170
+ this .fixedCode = this .code
171
+ this .fixedMessages = [{
172
+ fatal: true ,
173
+ severity: 2 ,
174
+ message: err .message ,
175
+ line: 1 ,
176
+ column: 0 ,
177
+ }]
178
+ }
179
+
180
+ // Lint
181
+ try {
182
+ this .messages = linter .verify (
183
+ // Cannot reuse until https://github.com/eslint/eslint/pull/8755 is merged.
184
+ // linter.getSourceCode(), // Reuse the AST of the previous `linter.verifyAndFix`.
185
+ this .code ,
186
+ config,
187
+ " vue-eslint-demo.vue"
188
+ )
189
+ }
190
+ catch (err) {
191
+ this .messages = [{
192
+ fatal: true ,
193
+ severity: 2 ,
194
+ message: err .message ,
195
+ line: 1 ,
196
+ column: 0 ,
197
+ }]
198
+ }
199
+ },
147
200
},
148
201
}
149
202
</script >
@@ -164,13 +217,30 @@ a:hover {
164
217
}
165
218
166
219
.app__header {
220
+ display : flex ;
221
+ flex-wrap : wrap ;
222
+ align-items : center ;
223
+ justify-content : flex-end ;
167
224
flex-shrink : 0 ;
168
- padding : 8px ;
169
225
background-color : #A5D6A7 ;
170
226
border-bottom : 1px solid #4CAF50 ;
171
- font-weight : bold ;
172
227
font-family : -apple-system , BlinkMacSystemFont, " Segoe UI" , Helvetica , Arial , sans-serif ;
173
228
}
229
+ .app__header-title {
230
+ flex-grow : 1 ;
231
+ padding : 8px ;
232
+ font-weight : bold ;
233
+ }
234
+ .app__header-option-item {
235
+ flex-shrink : 0 ;
236
+ padding : 2px ;
237
+ }
238
+ .app__header-option-item > select {
239
+ padding : 4px ;
240
+ border : 1px solid #4CAF50 ;
241
+ border-radius : 3px ;
242
+ background-color : #E8F5E9 ;
243
+ }
174
244
175
245
.app__body {
176
246
display : flex ;
0 commit comments