diff --git a/packages/website/src/components/playground.html b/packages/website/src/components/playground.html index c0718d4d..20cc64ce 100644 --- a/packages/website/src/components/playground.html +++ b/packages/website/src/components/playground.html @@ -40,13 +40,11 @@

JavaScript

-
+
- HERE
- +
diff --git a/packages/website/src/scripts/playground/app.js b/packages/website/src/scripts/playground/app.js index 169c1a21..c12b77e2 100644 --- a/packages/website/src/scripts/playground/app.js +++ b/packages/website/src/scripts/playground/app.js @@ -20,6 +20,10 @@ class App { "changeLanguage", () => this.handleLanguageChange() ); + this.model.on( + "autofixed", + () => this.view.codeEditor.setValue(this.model.getCode()) + ); this.view.codeEditor.on( "change", (editor) => this.handleCodeChange(editor) @@ -37,6 +41,19 @@ class App { this.model.setLanguage($tab.dataset.language); } ); + this.view.$errors.addEventListener('click', (event) => { + const $button = event.target.closest("button"); + if ($button) { + const $li = $button.closest("li"); + const index = Array.from($li.parentNode.children).indexOf($li); + const message = this.model.messages[index]; + if ($button.textContent.includes("fix")) { + this.model.applyFix(message); + } else if ($button.textContent.includes("suggestion")) { + this.model.applySuggestion(message); + } + } + }); } start() { diff --git a/packages/website/src/scripts/playground/model.js b/packages/website/src/scripts/playground/model.js index cff610fa..629b9883 100644 --- a/packages/website/src/scripts/playground/model.js +++ b/packages/website/src/scripts/playground/model.js @@ -2,7 +2,7 @@ * @import eslint from "eslint"; * @import {Language} from "./language"; * @import {RulesRecord, ParserOptions} from "./linter"; - * @typedef {"lint" | "changeLanguage"} EventType + * @typedef {"lint" | "changeLanguage" | "autofixed"} EventType */ import { @@ -74,7 +74,8 @@ export class Model { */ this.observers = { lint: new Set(), - changeLanguage: new Set() + changeLanguage: new Set(), + autofixed: new Set() }; } @@ -157,6 +158,44 @@ export class Model { this.observers[type].forEach((observer) => observer()); } + /** + * + * @param {LintMessage} message + */ + applyFix(message) { + const { + fix + } = message; + if (fix) { + const code = this.getCode(); + const [start, end] = fix.range; + const fixed = code.slice(0, start) + + fix.text + + code.slice(end); + this.setCode(fixed); + this.lint(); + this.notify('autofixed'); + } + } + + applySuggestion(message) { + const { + suggestions + } = message; + if (suggestions && suggestions.length > 0) { + const suggestion = suggestions[0]; + if (suggestion.fix) { + const code = this.getCode(); + const [start, end] = suggestion.fix.range; + const fixed = code.slice(0, start) + + suggestion.fix.text + + code.slice(end); + this.setCode(fixed); + this.lint(); + this.notify('autofixed'); + } + } + } /** * @returns {string} */ diff --git a/packages/website/src/scripts/playground/view.js b/packages/website/src/scripts/playground/view.js index e00e3552..e525f4d2 100644 --- a/packages/website/src/scripts/playground/view.js +++ b/packages/website/src/scripts/playground/view.js @@ -43,6 +43,7 @@ export class View { ); this.$languageTabs = document.getElementById("language-tabs"); + this.$errors = document.getElementById("errors"); } /** @@ -72,12 +73,10 @@ export class View { * @param {LintMessage[]} messages */ renderErrors(messages) { - const $errors = document.getElementById("errors"); - const children = messages. map((message) => this.lintMessageHTML(message)); - $errors.innerHTML = html`