I'm doing static analysis of JavaScript code using the esprima and estraverse modules. As JavaScript doesn't enforce the use of semicolons, then no error is thrown when analyzing code with no semicolons, but I would like it to.
The only solution I've come up with is to look for expressions that could require a semicolon and check if there's any, but the esprima.parse function doesn't remember the presence of any semicolon. They appear to be ignored after tokenization.
> esprima.tokenize("a = 3;")
[
{ type: 'Identifier', value: 'a' },
{ type: 'Punctuator', value: '=' },
{ type: 'Numeric', value: '3' },
{ type: 'Punctuator', value: ';' } // <-- Present after tokenization
]
> console.log(JSON.stringify(esprima.parse("a = 3;"), null, 2))
{
"type": "Program",
"body": [
{
"type": "ExpressionStatement",
"expression": {
"type": "AssignmentExpression",
"operator": "=",
"left": {
"type": "Identifier",
"name": "a"
},
"right": {
"type": "Literal",
"value": 3,
"raw": "3"
}
}
}
],
"sourceType": "script"
}
// Not present anywhere
So, in short: Is there a way to detect missing semicolons while making an static analysis of javascript code with esprima?