3
3
<eslint-editor
4
4
:linter =" linter"
5
5
:config =" config"
6
- :code =" code"
6
+ v-model =" code"
7
7
:style =" { height }"
8
8
class =" eslint-code-block"
9
9
:filename =" filename"
@@ -43,23 +43,50 @@ export default {
43
43
language: {
44
44
type: String ,
45
45
default: ' html'
46
+ },
47
+ /**
48
+ * If enabled, `@typescript-eslint/parser` will be used.
49
+ * This must be enabled when used for `ts` code blocks.
50
+ */
51
+ typescript: {
52
+ type: Boolean ,
53
+ default: false
46
54
}
47
55
},
48
56
49
57
data () {
58
+ const code = this .computeCodeFromSlot ()
59
+ // The height is determined in the initial processing.
60
+ // This is because later code changes do not change the height.
61
+ const lines = code .split (' \n ' ).length
62
+ const height = ` ${ Math .max (120 , 19 * lines)} px`
50
63
return {
64
+ code,
65
+ height,
51
66
linter: null ,
52
67
preprocess: processors[' .vue' ].preprocess ,
53
68
postprocess: processors[' .vue' ].postprocess ,
54
69
format: {
55
70
insertSpaces: true ,
56
71
tabSize: 2
57
- }
72
+ },
73
+ tsEslintParser: null
58
74
}
59
75
},
60
76
61
77
computed: {
62
78
config () {
79
+ let parser = null // Use default parser (`espree`)
80
+ if (this .typescript ) {
81
+ // Use `@typescript-eslint/parser`.
82
+ parser = this .tsEslintParser
83
+ } else if (this .langTs ) {
84
+ // Use `@typescript-eslint/parser` only when `<script lang="ts">` or `<script lang="typescript">`.
85
+ parser = {
86
+ ts: this .tsEslintParser ,
87
+ typescript: this .tsEslintParser
88
+ }
89
+ }
63
90
return {
64
91
globals: {
65
92
console: false ,
@@ -90,6 +117,7 @@ export default {
90
117
rules: this .rules ,
91
118
parser: ' vue-eslint-parser' ,
92
119
parserOptions: {
120
+ parser,
93
121
ecmaVersion: ' latest' ,
94
122
sourceType: ' module' ,
95
123
ecmaFeatures: {
@@ -99,24 +127,37 @@ export default {
99
127
}
100
128
},
101
129
102
- code () {
103
- return ` ${ this .computeCodeFromSlot (this .$slots .default ).trim ()} \n `
104
- },
130
+ /**
131
+ * Checks whether code may be using lang="ts" or lang="typescript".
132
+ * @returns {boolean} If `true`, may be using lang="ts" or lang="typescript".
133
+ */
134
+ langTs () {
135
+ return / lang\s * =\s * (?:"ts"| ts| 'ts'| "typescript"| typescript| 'typescript')/ u .test (
136
+ this .code
137
+ )
138
+ }
139
+ },
105
140
106
- height () {
107
- const lines = this .code .split (' \n ' ).length
108
- return ` ${ Math .max (120 , 19 * lines)} px`
141
+ watch: {
142
+ typescript (value ) {
143
+ if (value) {
144
+ this .loadTypescriptESLint ()
145
+ }
146
+ },
147
+ langTs (value ) {
148
+ if (value) {
149
+ this .loadTypescriptESLint ()
150
+ }
109
151
}
110
152
},
111
153
112
154
methods: {
113
- computeCodeFromSlot (nodes ) {
114
- if (! Array .isArray (nodes)) {
115
- return ' '
116
- }
117
- return nodes
118
- .map ((node ) => node .text || this .computeCodeFromSlot (node .children ))
119
- .join (' ' )
155
+ computeCodeFromSlot () {
156
+ return ` ${ computeCodeFromSlot (this .$slots .default ).trim ()} \n `
157
+ },
158
+
159
+ async loadTypescriptESLint () {
160
+ this .tsEslintParser = await import (' @typescript-eslint/parser' )
120
161
}
121
162
},
122
163
@@ -126,6 +167,9 @@ export default {
126
167
import (' eslint/lib/linter' ),
127
168
import (' espree' ).then (() => import (' vue-eslint-parser' ))
128
169
])
170
+ if (this .langTs || this .typescript ) {
171
+ await this .loadTypescriptESLint ()
172
+ }
129
173
130
174
const linter = (this .linter = new Linter ())
131
175
@@ -136,6 +180,15 @@ export default {
136
180
linter .defineParser (' vue-eslint-parser' , { parseForESLint })
137
181
}
138
182
}
183
+
184
+ function computeCodeFromSlot (nodes ) {
185
+ if (! Array .isArray (nodes)) {
186
+ return ' '
187
+ }
188
+ return nodes
189
+ .map ((node ) => node .text || computeCodeFromSlot (node .children ))
190
+ .join (' ' )
191
+ }
139
192
</script >
140
193
141
194
<style >
0 commit comments