Skip to content

Commit ac4d625

Browse files
authored
Add eslint-plugin-svelte3 playground (#24)
1 parent a967b37 commit ac4d625

File tree

7 files changed

+80
-7
lines changed

7 files changed

+80
-7
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import Linter from "eslint4b"
2+
export { Linter }

explorer/src/components/ESLintEditor.vue

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ export default {
3333
return {}
3434
},
3535
},
36+
useEslintPluginSvelte3: {
37+
type: Boolean,
38+
},
3639
},
3740
emits: ["update:modelValue", "updateMessages"],
3841
data() {
@@ -49,29 +52,44 @@ export default {
4952
},
5053
deep: true,
5154
},
55+
useEslintPluginSvelte3() {
56+
this.lint(this.modelValue)
57+
},
5258
},
5359
mounted() {
5460
this.lint(this.modelValue)
5561
},
5662
methods: {
63+
async getEslintPluginSvelte3Options() {
64+
const pluginSvelte3 = await import("eslint-plugin-svelte3")
65+
return {
66+
preprocess: pluginSvelte3.processors.svelte3.preprocess,
67+
postprocess: pluginSvelte3.processors.svelte3.postprocess,
68+
}
69+
},
5770
async lint(code) {
58-
const options = {
59-
parser: "svelte-eslint-parser",
71+
const config = {
72+
parser: this.useEslintPluginSvelte3
73+
? undefined
74+
: "svelte-eslint-parser",
6075
parserOptions: {
6176
ecmaVersion: 2020,
77+
sourceType: "module",
6278
},
6379
rules: this.rules,
6480
env: {
6581
browser: true,
6682
es2021: true,
6783
},
6884
}
69-
70-
const messages = linter.verify(code, options)
85+
const options = this.useEslintPluginSvelte3
86+
? await this.getEslintPluginSvelte3Options()
87+
: {}
88+
const messages = linter.verify(code, config, options)
7189
7290
this.$emit("updateMessages", messages)
7391
74-
const fixResult = linter.verifyAndFix(code, options)
92+
const fixResult = linter.verifyAndFix(code, config, options)
7593
this.fixedValue = fixResult.output
7694
7795
this.leftMarkers = await Promise.all(messages.map(messageToMarker))

explorer/src/components/ESLintPlayground.vue

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
<template>
22
<div class="playground-root">
3-
<div class="playground-tools"></div>
3+
<div class="playground-tools">
4+
<label>
5+
<input v-model="useEslintPluginSvelte3" type="checkbox" />
6+
See result of
7+
<a href="https://github.com/sveltejs/eslint-plugin-svelte3"
8+
>eslint-plugin-svelte3</a
9+
>.
10+
</label>
11+
<template v-if="useEslintPluginSvelte3">
12+
<span style="color: red"
13+
>svelte-eslint-parser is not used.</span
14+
>
15+
</template>
16+
</div>
417
<div class="playground-content">
518
<RulesSettings
619
ref="settings"
@@ -12,6 +25,7 @@
1225
v-model="code"
1326
:rules="rules"
1427
class="eslint-playground"
28+
:use-eslint-plugin-svelte3="useEslintPluginSvelte3"
1529
@update-messages="onUpdateMessages"
1630
/>
1731
<div class="messages">
@@ -76,6 +90,7 @@ export default {
7690
code: state.code || DEFAULT_CODE,
7791
rules: state.rules || Object.assign({}, DEFAULT_RULES_CONFIG),
7892
messages: [],
93+
useEslintPluginSvelte3: Boolean(state.useEslintPluginSvelte3),
7994
}
8095
},
8196
computed: {
@@ -89,6 +104,7 @@ export default {
89104
const serializedString = serializeState({
90105
code,
91106
rules,
107+
useEslintPluginSvelte3: this.useEslintPluginSvelte3,
92108
})
93109
return serializedString
94110
},
@@ -127,7 +143,9 @@ export default {
127143
this.code = state.code || DEFAULT_CODE
128144
this.rules =
129145
state.rules || Object.assign({}, DEFAULT_RULES_CONFIG)
130-
this.script = state.script
146+
this.useEslintPluginSvelte3 = Boolean(
147+
state.useEslintPluginSvelte3,
148+
)
131149
}
132150
},
133151
},

explorer/src/components/scripts/state/deserialize.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ export function deserializeState(serializedString) {
2828
if (typeof json.code === "string") {
2929
state.code = json.code
3030
}
31+
if (json.useEslintPluginSvelte3 === true) {
32+
state.useEslintPluginSvelte3 = true
33+
}
34+
3135
if (typeof json.rules === "object" && json.rules != null) {
3236
state.rules = {}
3337
for (const id of Object.keys(json.rules)) {

explorer/src/components/scripts/state/serialize.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export function serializeState(state) {
2323
const saveData = {
2424
code: state.code,
2525
rules: state.rules ? getEnabledRules(state.rules) : undefined,
26+
useEslintPluginSvelte3: state.useEslintPluginSvelte3,
2627
}
2728
const jsonString = JSON.stringify(saveData)
2829
// eslint-disable-next-line node/no-unsupported-features/node-builtins -- ignore

explorer/vue.config.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,34 @@ module.exports = {
1212
module: require.resolve("./shim/module"),
1313
},
1414
},
15+
module: {
16+
rules: [
17+
{
18+
test: /node_modules\/eslint-plugin-svelte3\/index\.js$/u,
19+
loader: "string-replace-loader",
20+
options: {
21+
search: "require\\(linter_path\\)",
22+
replace: (original) =>
23+
`require(${JSON.stringify(
24+
require.resolve(
25+
"./shim/eslint/lib/linter/linter",
26+
),
27+
)}) // ${original}`,
28+
flags: "",
29+
},
30+
},
31+
{
32+
test: /node_modules\/eslint-plugin-svelte3\/index\.js$/u,
33+
loader: "string-replace-loader",
34+
options: {
35+
search:
36+
"throw new Error\\('Could not find ESLint Linter in require cache'\\);",
37+
replace: (original) => ` // ${original}`,
38+
flags: "",
39+
},
40+
},
41+
],
42+
},
1543
}
1644
},
1745
}

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,15 @@
6262
"eslint-plugin-node": "^11.1.0",
6363
"eslint-plugin-prettier": "^3.2.0",
6464
"eslint-plugin-regexp": "^0.9.0",
65+
"eslint-plugin-svelte3": "^3.2.0",
6566
"eslint-plugin-vue": "^7.2.0",
6667
"estree-walker": "^3.0.0",
6768
"locate-character": "^2.0.5",
6869
"magic-string": "^0.25.7",
6970
"mocha": "^8.0.0",
7071
"nyc": "^15.1.0",
7172
"prettier": "^2.0.5",
73+
"string-replace-loader": "^3.0.1",
7274
"svelte": "^3.37.0",
7375
"ts-node": "^9.0.0",
7476
"typescript": "^4.0.0",

0 commit comments

Comments
 (0)