Created
October 19, 2023 13:40
-
-
Save yosiat/15cd0933188aa69f993a8e9e0301987a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Document</title> | |
</head> | |
<body> | |
<div id="editor"></div> | |
<script src="https://codemirror.net/codemirror.js"></script> | |
<script> | |
(function () { | |
'use strict'; | |
//!regexpLint | |
const { syntaxTree } = CM["@codemirror/language"]; | |
const { linter, } = CM["@codemirror/lint"]; | |
const regexpLinter = linter(view => { | |
let diagnostics = []; | |
syntaxTree(view.state).cursor().iterate(node => { | |
if (node.name == "RegExp") diagnostics.push({ | |
from: node.from, | |
to: node.to, | |
severity: "error", | |
message: "Regular expressions are FORBIDDEN", | |
actions: [{ | |
name: "Remove", | |
apply(view, from, to) { view.dispatch({ changes: { from, to } }); } | |
}] | |
}); | |
}); | |
return diagnostics | |
}); | |
const { basicSetup, EditorView } = CM["codemirror"]; | |
const { javascript } = CM["@codemirror/lang-javascript"]; | |
const { lintGutter, diagnosticCount } = CM["@codemirror/lint"]; | |
const { EditorState } = CM["@codemirror/state"]; | |
new EditorView({ | |
doc: `function isNumber(string) { | |
return /^\\d+(\\.\\d*)?$/.test(string) | |
}`, | |
extensions: [ | |
basicSetup, | |
javascript(), | |
lintGutter(), | |
regexpLinter, | |
EditorState.transactionExtender.of((tr) => { | |
let state = tr._state || tr.startState; | |
console.log("DiagnosticCount", diagnosticCount(state)); | |
}) | |
], | |
parent: document.querySelector("#editor") | |
}); | |
})(); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment