/
eslint-code-block.vue
209 lines (192 loc) · 4.62 KB
/
eslint-code-block.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<template>
<div class="eslint-code-container">
<eslint-editor
:linter="linter"
:config="config"
v-model="code"
:style="{ height }"
class="eslint-code-block"
:filename="filename"
:language="language"
:preprocess="preprocess"
:postprocess="postprocess"
dark
:format="format"
:fix="fix"
/>
</div>
</template>
<script>
import EslintEditor from 'vue-eslint-editor'
import { rules, processors } from '../../../'
export default {
name: 'ESLintCodeBlock',
components: { EslintEditor },
props: {
fix: {
type: Boolean,
default: false
},
rules: {
type: Object,
default() {
return {}
}
},
filename: {
type: String,
default: 'example.vue'
},
language: {
type: String,
default: 'html'
},
/**
* If enabled, `@typescript-eslint/parser` will be used.
* This must be enabled when used for `ts` code blocks.
*/
typescript: {
type: Boolean,
default: false
}
},
data() {
const code = this.computeCodeFromSlot()
// The height is determined in the initial processing.
// This is because later code changes do not change the height.
const lines = code.split('\n').length
const height = `${Math.max(120, 19 * lines)}px`
return {
code,
height,
linter: null,
preprocess: processors['.vue'].preprocess,
postprocess: processors['.vue'].postprocess,
format: {
insertSpaces: true,
tabSize: 2
},
tsEslintParser: null
}
},
computed: {
config() {
let parser = null // Use default parser (`espree`)
if (this.typescript) {
// Use `@typescript-eslint/parser`.
parser = this.tsEslintParser
} else if (this.langTs) {
// Use `@typescript-eslint/parser` only when `<script lang="ts">` or `<script lang="typescript">`.
parser = {
ts: this.tsEslintParser,
typescript: this.tsEslintParser
}
}
return {
globals: {
console: false,
// ES2015 globals
ArrayBuffer: false,
DataView: false,
Float32Array: false,
Float64Array: false,
Int16Array: false,
Int32Array: false,
Int8Array: false,
Map: false,
Promise: false,
Proxy: false,
Reflect: false,
Set: false,
Symbol: false,
Uint16Array: false,
Uint32Array: false,
Uint8Array: false,
Uint8ClampedArray: false,
WeakMap: false,
WeakSet: false,
// ES2017 globals
Atomics: false,
SharedArrayBuffer: false
},
rules: this.rules,
parser: 'vue-eslint-parser',
parserOptions: {
parser,
ecmaVersion: 'latest',
sourceType: 'module',
ecmaFeatures: {
jsx: true
}
}
}
},
/**
* Checks whether code may be using lang="ts" or lang="typescript".
* @returns {boolean} If `true`, may be using lang="ts" or lang="typescript".
*/
langTs() {
return /lang\s*=\s*(?:"ts"|ts|'ts'|"typescript"|typescript|'typescript')/u.test(
this.code
)
}
},
watch: {
typescript(value) {
if (value) {
this.loadTypescriptESLint()
}
},
langTs(value) {
if (value) {
this.loadTypescriptESLint()
}
}
},
methods: {
computeCodeFromSlot() {
return `${computeCodeFromSlot(this.$slots.default).trim()}\n`
},
async loadTypescriptESLint() {
this.tsEslintParser = await import('@typescript-eslint/parser')
}
},
async mounted() {
// Load linter.
const [{ Linter }, { parseForESLint }] = await Promise.all([
import('eslint/lib/linter'),
import('espree').then(() => import('vue-eslint-parser'))
])
if (this.langTs || this.typescript) {
await this.loadTypescriptESLint()
}
const linter = (this.linter = new Linter())
for (const ruleId of Object.keys(rules)) {
linter.defineRule(`vue/${ruleId}`, rules[ruleId])
}
linter.defineParser('vue-eslint-parser', { parseForESLint })
}
}
function computeCodeFromSlot(nodes) {
if (!Array.isArray(nodes)) {
return ''
}
return nodes
.map((node) => node.text || computeCodeFromSlot(node.children))
.join('')
}
</script>
<style>
.eslint-code-container {
border-radius: 6px;
padding: 1.25rem 0;
margin: 1em 0;
background-color: #1e1e1e;
}
.eslint-code-block {
width: 100%;
}
.eslint-editor-actions {
bottom: -0.9rem;
}
</style>