Skip to content

Instantly share code, notes, and snippets.

@yosiat
Created October 19, 2023 13:40
Show Gist options
  • Save yosiat/15cd0933188aa69f993a8e9e0301987a to your computer and use it in GitHub Desktop.
Save yosiat/15cd0933188aa69f993a8e9e0301987a to your computer and use it in GitHub Desktop.
<!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