Skip to content

Commit

Permalink
Add a very basic Static code checker
Browse files Browse the repository at this point in the history
* Improvise it as we go
  • Loading branch information
vjspranav committed Nov 24, 2023
1 parent 23fd992 commit a213ec4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/components/MonacoEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { makeStyles } from "@material-ui/core";

import { API_URL } from "../Constants";
import { StagBinContext } from "../App";
import detectLanguage from "../utils/language";

let reqData = {};

Expand All @@ -21,6 +22,7 @@ const getData = async (
setOldEncrypted,
setEncryptedReadOnly,
setOpenPasswordDialog,
setLanguage,
id,
redirect,
base_url,
Expand Down Expand Up @@ -49,6 +51,7 @@ const getData = async (
setOpenPasswordDialog(reqData.isEncrypted || false);
setData(reqData.data);
setLoading(false);
if (id.indexOf(".") === -1) setLanguage(detectLanguage(reqData.data));
}
if (reqData.url && !redirect) {
window.location = reqData.data;
Expand Down Expand Up @@ -135,8 +138,6 @@ export default function MEditor() {
default:
break;
}
} else {
setLanguage("javascript");
}
if (!(!readOnly && edited)) setReadOnly(true);
setUrl(id);
Expand All @@ -147,13 +148,15 @@ export default function MEditor() {
setOldEncrypted,
setEncryptedReadOnly,
setOpenPasswordDialog,
setLanguage,
id,
redirect,
base_url,
setIsSameContentbuid,
setLoading
);
}
} else {
}
}

Expand Down
25 changes: 25 additions & 0 deletions src/utils/language.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const detectLanguage = (code) => {
if (code.includes("import java")) {
return "java";
} else if (code.includes("import ") && code.includes("print(")) {
return "python";
} else if (code.includes("#include") && code.includes("cout")) {
return "cpp";
} else if (code.includes("#include")) {
return "c";
} else if (code.includes("<html>") || code.includes("</html>")) {
return "html";
} else if (code.includes("func main()")) {
return "go";
} else if (code.includes("function") && code.includes(";")) {
return "javascript";
} else if (code.includes("#") && code.includes("-")) {
return "markdown";
} else if (code.includes("{") && code.includes("}") && code.includes(":")) {
return "css";
} else {
return "javascript";
}
};

export default detectLanguage;

0 comments on commit a213ec4

Please sign in to comment.