Skip to content

Instantly share code, notes, and snippets.

@amazzalel-habib
Created May 18, 2020 17:29
Show Gist options
  • Save amazzalel-habib/1c71ed9631a3f83572d942017cecc4da to your computer and use it in GitHub Desktop.
Save amazzalel-habib/1c71ed9631a3f83572d942017cecc4da to your computer and use it in GitHub Desktop.
format(code: string): string{
// if the code contains errors, no need to format, because this way of formating the code, will remove some of the code
// to make things simple, we only allow formatting a valide code
if(this.validate(code).length > 0)
return code;
let formattedCode = "";
const ast: TodoExpressionsContext = parseAndGetASTRoot(code);
ast.children.forEach(node => {
if (node instanceof AddExpressionContext) {
// if a Add expression : ADD TODO "STRING"
const todo = node.STRING().text;
formattedCode += `ADD TODO ${todo}\n`;
}else if(node instanceof CompleteExpressionContext) {
// If a Complete expression: COMPLETE TODO "STRING"
const todoToComplete = node.STRING().text;
formattedCode += `COMPLETE TODO ${todoToComplete}\n`;
}
});
return formattedCode;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment