'use strict';

var peg = require('pegjs');

class PegJsPlugin {

  constructor(config) {
    this.config = config.plugins.pegjs;
    // The output of the below peg.generate() function must be a string, not an
    // object
    this.config.output = 'source';
  }

  compile(file) {
    var parser;
    try {
      parser = peg.generate(file.data, this.config);
    } catch(error) {
      if (error instanceof peg.parser.SyntaxError) {
        error.message = `${error.message} at ${error.location.start.line}:${error.location.start.column}`;
      }
      return Promise.reject(error);
    }
    return Promise.resolve({data: parser});
  }

}

// brunchPlugin must be set to true for all Brunch plugins
PegJsPlugin.prototype.brunchPlugin = true;
// The type of file to generate
PegJsPlugin.prototype.type = 'javascript';
// The extension for files to process
PegJsPlugin.prototype.extension = 'pegjs';
// The extension for processed files (this allows for compiler chaining)
PegJsPlugin.prototype.targetExtension = 'js';

module.exports = PegJsPlugin;

'use strict';

const peg = require('pegjs');

class PegJsPlugin {

  constructor(config) {
    this.config = config.plugins.pegjs;
    // The output of the below peg.generate() function must be a string, not an
    // object
    this.config.output = 'source';
  }

  compile(file) {
    let parser;
    try {
      parser = peg.generate(file.data, this.config);
    } catch(error) {
      if (error instanceof peg.parser.SyntaxError) {
        error.message = `${error.message} at ${error.location.start.line}:${error.location.start.column}`;
      }
      return Promise.reject(error);
    }
    return Promise.resolve({data: parser});
  }

}

// brunchPlugin must be set to true for all Brunch plugins
PegJsPlugin.prototype.brunchPlugin = true;
// The type of file to generate
PegJsPlugin.prototype.type = 'javascript';
// The extension for files to process
PegJsPlugin.prototype.extension = 'pegjs';
// The extension for processed files (this allows for compiler chaining)
PegJsPlugin.prototype.targetExtension = 'js';

module.exports = PegJsPlugin;

Use const/let instead of var
Use const/let instead of var
