diff --git a/plugins/js/.gitignore b/plugins/js/.gitignore new file mode 100644 index 0000000..c2658d7 --- /dev/null +++ b/plugins/js/.gitignore @@ -0,0 +1 @@ +node_modules/ diff --git a/plugins/js/README.md b/plugins/js/README.md new file mode 100644 index 0000000..38c5db2 --- /dev/null +++ b/plugins/js/README.md @@ -0,0 +1,5 @@ +```bash +cd plugins/js +./validate.js success.js +./validate.js failure.js +``` diff --git a/plugins/js/failure.js b/plugins/js/failure.js new file mode 100644 index 0000000..da01a3b --- /dev/null +++ b/plugins/js/failure.js @@ -0,0 +1,6 @@ +return 42; // should be inside a function +function f() { + 'use strict'; + var x = 042; + with (z) { } +} \ No newline at end of file diff --git a/plugins/js/package-lock.json b/plugins/js/package-lock.json new file mode 100644 index 0000000..cec88bf --- /dev/null +++ b/plugins/js/package-lock.json @@ -0,0 +1,11 @@ +{ + "requires": true, + "lockfileVersion": 1, + "dependencies": { + "esprima": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/esprima/-/esprima-4.0.1.tgz", + "integrity": "sha512-eGuFFw7Upda+g4p+QHvnW0RyTX/SVeJBDM/gCtMARO0cLuT2HcEKnTPvhjV6aGeqrCB/sbNop0Kszm0jsaWU4A==" + } + } +} diff --git a/plugins/js/success.js b/plugins/js/success.js new file mode 100644 index 0000000..de88829 --- /dev/null +++ b/plugins/js/success.js @@ -0,0 +1 @@ +console.log("Hello World!"); \ No newline at end of file diff --git a/plugins/js/validate.js b/plugins/js/validate.js new file mode 100755 index 0000000..132206e --- /dev/null +++ b/plugins/js/validate.js @@ -0,0 +1,23 @@ +#!/usr/local/bin/node +const fs = require('fs'); +const esprima = require('esprima'); + +const file = process.argv[2]; +const code = fs.readFileSync(file).toString(); + +const res = esprima.parseScript(code, { tolerant: true }); +if (res.errors.length != 0) { + const split = code.split('\n'); + for (const error of res.errors) { + console.error(split[error.lineNumber - 1]); + for (let i = 1; i < error.column; i++) { + process.stderr.write(' '); + } + console.error('^'); + console.error(`${error.toString()} + at (${file}:${error.lineNumber}:${error.column})`); + console.error('--------------------------------------------------------------------------'); + } + process.exit(1); +} +process.exit(0); diff --git a/src/judger.rs b/src/judger.rs index 25f2414..53e0a34 100644 --- a/src/judger.rs +++ b/src/judger.rs @@ -35,6 +35,7 @@ pub async fn judger( path: &Path, ) -> Result { let mut resp = JudgeResponse::new(); + // TODO: 使用配置文件 let cmd = match Language::from_i32(request.language) { Some(Language::C) => "./a.out", Some(Language::Cpp) => "./a.out", @@ -120,9 +121,8 @@ pub async fn compile( Some(Language::Cpp) => "/usr/bin/g++ main.cpp -O2 -Wall --static -o a.out --std=gnu++17", Some(Language::Python) => "/usr/bin/python3 -m compileall main.py", Some(Language::Rust) => "/usr/bin/rustc main.rs -o a.out -C opt-level=2", - // TODO: eslint...... - Some(Language::Node) => "/bin/echo hello", - Some(Language::TypeScript) => "/usr/bin/tsc", + Some(Language::Node) => "validate.js main.js", + Some(Language::TypeScript) => "/usr/bin/tsc main.ts", Some(Language::Go) => "/usr/bin/go build -ldflags \"-s -w\" main.go", None => return Err(Error::LanguageNotFound(request.language)), };