11<template >
22 <div class =" app__root" >
33 <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 >
1025 </label >
11- Playground for <a href =" https://github.com/vuejs/eslint-plugin-vue#readme" target =" _blank" >eslint-plugin-vue</a >.
1226 </div >
1327 <div class =" app__body" >
1428 <rule-select
2337 :messages =" messages"
2438 :fixed-code =" fixedCode"
2539 :fixed-messages =" fixedMessages"
40+ :format-options =" formatOptions"
2641 :show-fixed-code =" showFixedCode"
27- @edit =" onEdit "
42+ @edit =" onCodeChange "
2843 @initialize.once =" onEditorInitialize"
2944 />
3045 <message-list class =" app__errors" :messages =" messages" />
@@ -66,70 +81,61 @@ export default {
6681 },
6782
6883 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 ,
8188 }
8289 },
8390
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"
10093 },
10194
102- fixedCode () {
103- return this . fixResult . output
95+ versions () {
96+ return versions
10497 },
98+ },
10599
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 ()
108105 },
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 ()
112116 },
113117 },
114118
115119 mounted () {
120+ this .lint ()
116121 window .addEventListener (" hashchange" , this .onUrlHashChange )
117122 },
118123 beforeDestroey () {
119124 window .removeEventListener (" hashchange" , this .onUrlHashChange )
120125 },
121126
122127 methods: {
123- onEdit (code ) {
124- this .code = code
125- this .applyUrlHash ()
126- },
127-
128128 onEditorInitialize () {
129129 window .MainContent .show ()
130130 },
131131
132+ onCodeChange (code ) {
133+ this .code = code
134+ },
135+
132136 onConfigChange () {
137+ // The inside of `this.config` was changed directly.
138+ this .lint ()
133139 this .applyUrlHash ()
134140 },
135141
@@ -144,6 +150,53 @@ export default {
144150 applyUrlHash () {
145151 window .location .replace (` #${ serializeState (this .$data )} ` )
146152 },
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+ },
147200 },
148201}
149202 </script >
@@ -164,13 +217,30 @@ a:hover {
164217}
165218
166219.app__header {
220+ display : flex ;
221+ flex-wrap : wrap ;
222+ align-items : center ;
223+ justify-content : flex-end ;
167224 flex-shrink : 0 ;
168- padding : 8px ;
169225 background-color : #A5D6A7 ;
170226 border-bottom : 1px solid #4CAF50 ;
171- font-weight : bold ;
172227 font-family : -apple-system , BlinkMacSystemFont, " Segoe UI" , Helvetica , Arial , sans-serif ;
173228}
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+ }
174244
175245.app__body {
176246 display : flex ;
0 commit comments