From ec99b486052c2045ccfaac9de09c1f87c53c04e4 Mon Sep 17 00:00:00 2001 From: Andrew Bastin Date: Fri, 24 Jul 2020 00:38:14 -0400 Subject: [PATCH] Added JS linting to Ace Editor --- components/ui/ace-editor.vue | 37 ++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/components/ui/ace-editor.vue b/components/ui/ace-editor.vue index ca77e09bc..7525c1c36 100644 --- a/components/ui/ace-editor.vue +++ b/components/ui/ace-editor.vue @@ -26,6 +26,8 @@ import "ace-builds/webpack-resolver" import jsonParse from "~/helpers/jsonParse" import debounce from "~/helpers/utils/debounce" +import * as esprima from "esprima" + export default { props: { value: { @@ -136,6 +138,41 @@ export default { }, ]) } + } else if (this.lang === "javascript") { + try { + const res = esprima.parseScript(code, { tolerant: true }) + console.log(res) + if (res.errors && res.errors.length > 0) { + this.editor.session.setAnnotations( + res.errors.map((err) => { + const pos = this.editor.session.getDocument().indexToPosition(err.index, 0) + + console.log({ + row: pos.row, + column: pos.column, + text: err.description, + type: "error", + }) + return { + row: pos.row, + column: pos.column, + text: err.description, + type: "error", + } + }) + ) + } + } catch (e) { + const pos = this.editor.session.getDocument().indexToPosition(e.index, 0) + this.editor.session.setAnnotations([ + { + row: pos.row, + column: pos.column, + text: e.description, + type: "error", + }, + ]) + } } }, 2000), },