Skip to content

Commit f137b9f

Browse files
committed
Refactor demo code
1 parent d47dbd3 commit f137b9f

File tree

6 files changed

+90
-73
lines changed

6 files changed

+90
-73
lines changed

explorer-v2/src/lib/AstExplorer.svelte

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,18 +173,18 @@
173173
</script>
174174

175175
<div class="ast-explorer-root">
176-
<div class="ast-tools">{time}<AstOptions bind:modelValue={options} /></div>
176+
<div class="ast-tools">{time}<AstOptions bind:options /></div>
177177
<div class="ast-explorer">
178178
<MonacoEditor
179179
bind:this={sourceEditor}
180-
bind:modelValue={svelteValue}
180+
bind:code={svelteValue}
181181
language="html"
182182
on:focusEditorText={() => handleFocus('source')}
183183
on:changeCursorPosition={(e) => handleCursor(e.detail, 'source')}
184184
/>
185185
<MonacoEditor
186186
bind:this={jsonEditor}
187-
modelValue={astJson.json}
187+
code={astJson.json}
188188
language="json"
189189
readOnly
190190
on:focusEditorText={() => handleFocus('json')}

explorer-v2/src/lib/AstOptions.svelte

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<script>
2-
export let modelValue = {};
2+
export let options = {};
33
function updateShowLocations(newValue) {
4-
modelValue = {
5-
...modelValue,
4+
options = {
5+
...options,
66
showLocations: newValue
77
};
88
}
@@ -11,7 +11,7 @@
1111
<label class="ast-options"
1212
><input
1313
type="checkbox"
14-
checked={modelValue.showLocations}
14+
checked={options.showLocations}
1515
on:change={(e) => updateShowLocations(e.target.checked)}
1616
/>Locations</label
1717
>

explorer-v2/src/lib/ESLintEditor.svelte

Lines changed: 26 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,54 @@
11
<script>
2-
import Linter from 'eslint4b';
32
import MonacoEditor from './MonacoEditor.svelte';
4-
import * as svelteEslintParser from 'svelte-eslint-parser';
53
import { monacoEditorLoad } from './scripts/monaco-loader';
64
import { createEventDispatcher, onMount } from 'svelte';
75
86
const dispatch = createEventDispatcher();
97
10-
const linter = new Linter();
11-
linter.defineParser('svelte-eslint-parser', svelteEslintParser);
8+
export let linter = null;
9+
export let code = '';
10+
export let config = {};
11+
export let options = {};
1212
13-
export let modelValue = '';
14-
export let rules = {};
15-
export let useEslintPluginSvelte3 = false;
16-
17-
let fixedValue = modelValue;
13+
let fixedValue = code;
1814
let leftMarkers = [];
1915
let rightMarkers = [];
2016
2117
$: {
22-
lint(modelValue, useEslintPluginSvelte3, rules);
18+
lint(linter, code, config, options);
2319
}
2420
2521
onMount(() => {
26-
lint(modelValue, useEslintPluginSvelte3, rules);
22+
lint(linter, code, config, options);
2723
});
2824
29-
async function getEslintPluginSvelte3Options() {
30-
const pluginSvelte3 = await import('eslint-plugin-svelte3');
31-
return {
32-
preprocess: pluginSvelte3.processors.svelte3.preprocess,
33-
postprocess: pluginSvelte3.processors.svelte3.postprocess
34-
};
35-
}
36-
37-
async function lint(code, useEslintPluginSvelte3, rules) {
38-
const config = {
39-
parser: useEslintPluginSvelte3 ? undefined : 'svelte-eslint-parser',
40-
parserOptions: {
41-
ecmaVersion: 2020,
42-
sourceType: 'module'
43-
},
44-
rules,
45-
env: {
46-
browser: true,
47-
es2021: true
48-
}
49-
};
50-
const options = useEslintPluginSvelte3 ? await getEslintPluginSvelte3Options() : {};
25+
async function lint(linter, code, config, options) {
26+
/* eslint-disable no-param-reassign -- ignore */
27+
linter = await linter;
28+
if (!linter) {
29+
return;
30+
}
31+
code = await code;
32+
config = await config;
33+
options = await options;
34+
/* eslint-enable no-param-reassign -- ignore */
5135
5236
const start = Date.now();
5337
const messages = linter.verify(code, config, options);
5438
const time = Date.now() - start;
5539
5640
dispatch('time', time);
5741
58-
dispatch('updateMessages', messages);
59-
6042
const fixResult = linter.verifyAndFix(code, config, options);
6143
fixedValue = fixResult.output;
6244
45+
dispatch('result', {
46+
messages,
47+
time,
48+
output: fixResult.output,
49+
fixedMessages: fixResult.messages
50+
});
51+
6352
leftMarkers = await Promise.all(messages.map(messageToMarker));
6453
rightMarkers = await Promise.all(fixResult.messages.map(messageToMarker));
6554
}
@@ -100,8 +89,8 @@
10089
</script>
10190

10291
<MonacoEditor
103-
bind:modelValue
104-
bind:rightValue={fixedValue}
92+
bind:code
93+
bind:rightCode={fixedValue}
10594
language="html"
10695
diffEditor
10796
markers={leftMarkers}

explorer-v2/src/lib/ESLintPlayground.svelte

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
<script>
22
import { onDestroy, onMount } from 'svelte';
3-
3+
import Linter from 'eslint4b';
4+
import * as svelteEslintParser from 'svelte-eslint-parser';
45
import ESLintEditor from './ESLintEditor.svelte';
56
import RulesSettings from './RulesSettings.svelte';
67
import { deserializeState, serializeState } from './scripts/state';
78
import { DEFAULT_RULES_CONFIG, getURL } from './scripts/rules.js';
89
10+
const linter = new Linter();
11+
linter.defineParser('svelte-eslint-parser', svelteEslintParser);
12+
913
const DEFAULT_CODE =
1014
`<script>
1115
let a = 1;
@@ -28,6 +32,18 @@
2832
let messages = [];
2933
let useEslintPluginSvelte3 = Boolean(state.useEslintPluginSvelte3);
3034
let time = '';
35+
let options = {};
36+
37+
$: {
38+
options = useEslintPluginSvelte3 ? getEslintPluginSvelte3Options() : {};
39+
}
40+
async function getEslintPluginSvelte3Options() {
41+
const pluginSvelte3 = await import('eslint-plugin-svelte3');
42+
return {
43+
preprocess: pluginSvelte3.processors.svelte3.preprocess,
44+
postprocess: pluginSvelte3.processors.svelte3.postprocess
45+
};
46+
}
3147
3248
// eslint-disable-next-line no-use-before-define -- false positive
3349
$: serializedString = (() => {
@@ -56,8 +72,9 @@
5672
window.removeEventListener('hashchange', onUrlHashChange);
5773
}
5874
});
59-
function onUpdateMessages(msgs) {
60-
messages = msgs.detail;
75+
function onLintedResult(evt) {
76+
messages = evt.detail.messages;
77+
time = `${evt.detail.time}ms`;
6178
}
6279
function onUrlHashChange() {
6380
const newSerializedString =
@@ -103,12 +120,23 @@
103120
<RulesSettings bind:rules class="rules-settings" />
104121
<div class="editor-content">
105122
<ESLintEditor
106-
bind:modelValue={code}
107-
bind:rules
108-
bind:useEslintPluginSvelte3
123+
{linter}
124+
bind:code
125+
config={{
126+
parser: useEslintPluginSvelte3 ? undefined : 'svelte-eslint-parser',
127+
parserOptions: {
128+
ecmaVersion: 2020,
129+
sourceType: 'module'
130+
},
131+
rules,
132+
env: {
133+
browser: true,
134+
es2021: true
135+
}
136+
}}
137+
{options}
109138
class="eslint-playground"
110-
on:updateMessages={onUpdateMessages}
111-
on:time={(t) => (time = `${t.detail}ms`)}
139+
on:result={onLintedResult}
112140
/>
113141
<div class="messages">
114142
<ol>

explorer-v2/src/lib/MonacoEditor.svelte

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
const dispatch = createEventDispatcher();
55
66
import { monacoEditorLoad } from './scripts/monaco-loader';
7-
export let modelValue = '';
8-
export let rightValue = '';
7+
export let code = '';
8+
export let rightCode = '';
99
export let language = 'json';
1010
export let readOnly = false;
1111
export let diffEditor = false;
@@ -15,12 +15,12 @@
1515
let rootElement, editor, setLeftValue, setRightValue, setLeftMarkers, setRightMarkers;
1616
$: {
1717
if (setLeftValue) {
18-
setLeftValue(modelValue);
18+
setLeftValue(code);
1919
}
2020
}
2121
$: {
2222
if (setRightValue) {
23-
setRightValue(rightValue);
23+
setRightValue(rightCode);
2424
}
2525
}
2626
$: {
@@ -37,7 +37,7 @@
3737
onMount(async () => {
3838
const monaco = await monacoEditorLoad;
3939
const options = {
40-
value: modelValue,
40+
value: code,
4141
readOnly,
4242
theme: 'vs-dark',
4343
language,
@@ -59,27 +59,27 @@
5959
originalEditable: true,
6060
...options
6161
});
62-
const original = monaco.editor.createModel(modelValue, language);
63-
const modified = monaco.editor.createModel(rightValue, language);
62+
const original = monaco.editor.createModel(code, language);
63+
const modified = monaco.editor.createModel(rightCode, language);
6464
const leftEditor = editor.getOriginalEditor();
6565
const rightEditor = editor.getModifiedEditor();
6666
rightEditor.updateOptions({ readOnly: true });
6767
editor.setModel({ original, modified });
6868
original.onDidChangeContent(() => {
6969
const value = original.getValue();
70-
modelValue = value;
70+
code = value;
7171
});
7272
73-
setLeftValue = (modelValue) => {
73+
setLeftValue = (code) => {
7474
const value = original.getValue();
75-
if (modelValue !== value) {
76-
original.setValue(modelValue);
75+
if (code !== value) {
76+
original.setValue(code);
7777
}
7878
};
79-
setRightValue = (modelValue) => {
79+
setRightValue = (code) => {
8080
const value = modified.getValue();
81-
if (modelValue !== value) {
82-
modified.setValue(modelValue);
81+
if (code !== value) {
82+
modified.setValue(code);
8383
}
8484
};
8585
setLeftMarkers = (markers) => {
@@ -95,18 +95,18 @@
9595
editor = monaco.editor.create(rootElement, options);
9696
editor.onDidChangeModelContent(() => {
9797
const value = editor.getValue();
98-
modelValue = value;
98+
code = value;
9999
});
100100
editor.onDidChangeCursorPosition((evt) => {
101101
dispatch('changeCursorPosition', evt);
102102
});
103103
editor.onDidFocusEditorText((evt) => {
104104
dispatch('focusEditorText', evt);
105105
});
106-
setLeftValue = (modelValue) => {
106+
setLeftValue = (code) => {
107107
const value = editor.getValue();
108-
if (modelValue !== value) {
109-
editor.setValue(modelValue);
108+
if (code !== value) {
109+
editor.setValue(code);
110110
}
111111
};
112112
setRightValue = () => {

explorer-v2/src/lib/ScopeExplorer.svelte

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,18 +285,18 @@
285285
</script>
286286
287287
<div class="ast-explorer-root">
288-
<div class="ast-tools">{time}<AstOptions bind:modelValue={options} /></div>
288+
<div class="ast-tools">{time}<AstOptions bind:options /></div>
289289
<div class="ast-explorer">
290290
<MonacoEditor
291291
bind:this={sourceEditor}
292-
bind:modelValue={svelteValue}
292+
bind:code={svelteValue}
293293
language="html"
294294
on:focusEditorText={() => handleFocus('source')}
295295
on:changeCursorPosition={(e) => handleCursor(e.detail, 'source')}
296296
/>
297297
<MonacoEditor
298298
bind:this={jsonEditor}
299-
modelValue={scopeJson.json}
299+
code={scopeJson.json}
300300
language="json"
301301
readOnly
302302
on:focusEditorText={() => handleFocus('json')}

0 commit comments

Comments
 (0)