Skip to content

Commit

Permalink
Merge pull request #16 from klee214/issue-13
Browse files Browse the repository at this point in the history
Fix #13: Is parentheses or bracket closed properly?
  • Loading branch information
PrivateGER authored Oct 19, 2020
2 parents 9d1393b + 0e47e40 commit 5bfe3b7
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
1 change: 1 addition & 0 deletions greenlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ if(transpiler.runChecks()) {
console.log(colors.green + "[OK] Syntax checker finished, no issues found!" + colors.reset);
} else {
console.log(colors.yellow + "[WARN] Syntax checker found at least one issue in your code!" + colors.reset);
return -1;
}

if(process.argv[2] === "c") {
Expand Down
48 changes: 43 additions & 5 deletions lib/syntax_checker.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,56 @@ let colors = require(__dirname + "/colors");

function checkSyntax(compiledCode) {
let checkPassed = true;
let splitCode = compiledCode.split(" ");

if(!checkAsyncFunctionCalls(splitCode)) checkPassed = false;
let splitCode = compiledCode.split(";");

if (!checkParenthesesBracket(splitCode)) checkPassed = false;

splitCode = compiledCode.split(" ");
if (!checkAsyncFunctionCalls(splitCode)) checkPassed = false;

return checkPassed;
}

function checkParenthesesBracket(code) {
let passed = true;

for (let i = 0; i < code.length; i++) {
// Case 1: '(' is not properly closed
if (code[i].includes("(")) {
let flag = true; // check '(', ')' is a pair
code[i].split("").forEach(char => {
if (flag && char === "(") flag = false; // needs ')'
if (!flag && char === ")") flag = true; // needs '('
});
// '(', ')' is not a pair
if (!flag) {
console.log(colors.yellow + "[WARN] '(' is not closed. This could cause syntax errors: " + code[i] + colors.reset);
passed = false;
}
}

// Case 2: '[' is not properly closed
if (code[i].includes("[")) {
let flag = true; // check '[', ']' is a pair
code[i].split("").forEach(char => {
if (flag && char === "[") flag = false; // needs ']'
if (!flag && char === "]") flag = true; // needs '['
});
// '[', ']' is not a pair
if (!flag) {
console.log(colors.yellow + "[WARN] Array '[' is not closed. This could cause syntax errors: " + code[i] + colors.reset);
passed = false;
}
}
}
return passed;
}

function checkAsyncFunctionCalls(code) {
let passed = true;
let withinString = false;
for(let i = 1; i < code.length; i++) {
if(code[i].endsWith("(") && code[i-1] !== "await") {
for (let i = 1; i < code.length; i++) {
if (code[i].endsWith("(") && code[i - 1] !== "await") {
console.log(colors.yellows + "[WARN] Function call made without call statement. This could cause serious logic errors: " + code[i] + colors.reset);
passed = false;
}
Expand Down

0 comments on commit 5bfe3b7

Please sign in to comment.