Line and Block Comments
By default, Esprima tokenizer ignores every line and block comment. If each comment needs to be included in the output, then the property comment
in the configuration object needs to be set to true. To illustrate this, compare the following simple tokenization:
- $ node
- > var esprima = require('esprima')
- > esprima.tokenize('/* answer */ 42')
- [ { type: 'Numeric', value: '42' } ]
with the following situation where the token array also contains the block comment:
- $ node
- > var esprima = require('esprima')
- > esprima.tokenize('/* answer */ 42', { comment: true })
- [ { type: 'BlockComment', value: ' answer ' },
- { type: 'Numeric', value: '42' } ]
If the location of each comment is needed, enable the location information using range
and/or loc
(as explained in the previous section):
- $ node
- > var esprima = require('esprima')
- > esprima.tokenize('/* answer */ 42', { comment: true, range: true })
- [ { type: 'BlockComment', value: ' answer ', range: [ 0, 12 ] },
- { type: 'Numeric', value: '42', range: [ 13, 15 ] } ]