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:

  1. $ node
  2. > var esprima = require('esprima')
  3. > esprima.tokenize('/* answer */ 42')
  4. [ { type: 'Numeric', value: '42' } ]

with the following situation where the token array also contains the block comment:

  1. $ node
  2. > var esprima = require('esprima')
  3. > esprima.tokenize('/* answer */ 42', { comment: true })
  4. [ { type: 'BlockComment', value: ' answer ' },
  5. { 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):

  1. $ node
  2. > var esprima = require('esprima')
  3. > esprima.tokenize('/* answer */ 42', { comment: true, range: true })
  4. [ { type: 'BlockComment', value: ' answer ', range: [ 0, 12 ] },
  5. { type: 'Numeric', value: '42', range: [ 13, 15 ] } ]