-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy patheslint-code-block.vue
244 lines (228 loc) · 5.66 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<template>
<div class="eslint-code-container">
<eslint-editor
ref="editor"
v-model="code"
:linter="linter"
:config="config"
:style="{ height }"
class="eslint-code-block"
:filename="'/path/' + resplvedFilename"
:language="language"
dark
:format="format"
:fix="fix"
/>
</div>
</template>
<script>
import EslintEditor from 'vue-eslint-editor'
import { rules } from '../../../'
import { setTimeouts } from '../../../dist/utils/default-timeouts'
import { setFileContents } from '../shim/fs/fake-fs'
setTimeouts({ CACHE_LOADER: -1, MTIME_MS_CHECK: -1 })
export default {
name: 'ESLintCodeBlock',
components: { EslintEditor },
inject: {
$resourceGroup: {
from: '$resourceGroup',
default: null
}
},
props: {
fix: {
type: Boolean,
default: false
},
rules: {
type: Object,
default() {
return {}
}
},
filename: {
type: String,
default: undefined
},
language: {
type: String,
default: 'html'
},
localeKey: {
type: String,
default: 'file'
},
messageSyntaxVersion: {
type: String,
default: '^11'
}
},
data() {
return {
linter: null,
format: {
insertSpaces: true,
tabSize: 2
},
code: ''
}
},
computed: {
isResource() {
return this.language === 'json' || this.language === 'yaml'
},
resplvedFilename() {
return (
this.filename ||
(this.language === 'json'
? 'example.json'
: this.language === 'yaml'
? 'example.yaml'
: this.language === 'javascript'
? 'example.js'
: 'example.vue')
)
},
config() {
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:
this.language === 'json'
? 'jsonc-eslint-parser'
: this.language === 'yaml'
? 'yaml-eslint-parser'
: 'vue-eslint-parser',
parserOptions: {
ecmaVersion: 2020,
sourceType: 'module',
ecmaFeatures: {
jsx: true
}
},
settings: {
'vue-i18n': {
localeDir: (this.$resourceGroup
? this.$resourceGroup
.getFiles()
.filter(file => /\.(?:json5?|ya?ml)$/i.test(file))
: this.isResource
? [this.resplvedFilename]
: []
).map(pattern => ({ pattern, localeKey: this.localeKey })),
messageSyntaxVersion: this.messageSyntaxVersion
}
}
}
},
height() {
const lines = this.code.split('\n').length
return `${Math.max(120, 19 * lines)}px`
}
},
watch: {
code(newCode) {
if (this.$resourceGroup) {
this.$resourceGroup.set(this.resplvedFilename, newCode)
}
}
},
async mounted() {
if (this.$resourceGroup) {
this.$resourceGroup.addEditor(this)
}
this.code = `${this.computeCodeFromSlot(this.$slots.default).trim()}\n`
this.$refs.editor.$watch('monaco', monaco => {
monaco.languages.register({ id: 'yaml' })
monaco.languages.setMonarchTokensProvider(
'yaml',
require('monaco-editor/esm/vs/basic-languages/yaml/yaml').language
)
})
// Load linter.
const [{ Linter }, vueESLintParser, jsoncESLintParser, yamlESLintParser] =
await Promise.all([
import('eslint'),
import('espree').then(() => import('vue-eslint-parser')),
import('espree').then(() => import('jsonc-eslint-parser')),
import('yaml-eslint-parser')
])
const linter = (this.linter = new Linter({ cwd: '/path' }))
for (const ruleId of Object.keys(rules)) {
linter.defineRule(`@intlify/vue-i18n/${ruleId}`, rules[ruleId])
}
linter.defineParser('vue-eslint-parser', vueESLintParser)
linter.defineParser('jsonc-eslint-parser', jsoncESLintParser)
linter.defineParser('yaml-eslint-parser', yamlESLintParser)
const verifyHook = this.verifyHook
const verify = linter.verify
linter.verify = function (...args) {
verifyHook()
return verify.apply(this, args)
}
const verifyAndFix = linter.verifyAndFix
linter.verifyAndFix = function (...args) {
verifyHook()
return verifyAndFix.apply(this, args)
}
},
methods: {
computeCodeFromSlot(nodes) {
if (!Array.isArray(nodes)) {
return ''
}
return nodes
.map(node => node.text || this.computeCodeFromSlot(node.children))
.join('')
},
verifyHook() {
setFileContents(
this.$resourceGroup ? this.$resourceGroup.getFileContents() : {}
)
},
lint() {
this.$refs.editor.lint()
}
}
}
</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>