var assert = require('assert');

module.exports = function() {};

module.exports.prototype = {
  configure: function(options) {
    assert(
      typeof options === 'object',
      'requireSpacesAfterClosingParenthesisInFunctionDeclaration option must be the object'
    );

    assert(
      options.beforeOpeningCurlyBrace || options.beforeOpeningRoundBrace,
      'requireSpacesAfterClosingParenthesisInFunctionDeclaration must have beforeOpeningCurlyBrace or beforeOpeningRoundBrace property'
    );

    this._beforeOpeningRoundBrace = Boolean(options.beforeOpeningRoundBrace);
    this._beforeOpeningCurlyBrace = Boolean(options.beforeOpeningCurlyBrace);
  },

  getOptionName: function() {
    return 'requireSpacesAfterClosingParenthesisInFunctionDeclaration';
  },

  check: function(file, errors) {
    var beforeOpeningRoundBrace = this._beforeOpeningRoundBrace;
    var beforeOpeningCurlyBrace = this._beforeOpeningCurlyBrace;
    var tokens = file.getTokens();

    file.iterateNodesByType(['FunctionDeclaration'], function(node) {
      var nodeBeforeRoundBrace = node;
      // named function
      if (node.id) {
        nodeBeforeRoundBrace = node.id;
      }

      var functionTokenPos = file.getTokenPosByRangeStart(nodeBeforeRoundBrace.range[0]);
      var functionToken = tokens[functionTokenPos];

      var nextTokenPos = file.getTokenPosByRangeStart(functionToken.range[1]);
      var nextToken = tokens[nextTokenPos];

      if (beforeOpeningRoundBrace) {
        if (nextToken) {
          errors.add(
            'Missing space before opening round brace',
            nextToken.loc.start
          );
        }
      } else {
        if (!nextToken) {
          errors.add(
            'Illegal space before opening round brace',
            functionToken.loc.end
          );
        }
      }

      var tokenBeforeBodyPos = file.getTokenPosByRangeStart(node.body.range[0] - 1);
      var tokenBeforeBody = tokens[tokenBeforeBodyPos];

      if (beforeOpeningCurlyBrace) {
        if (tokenBeforeBody) {
          errors.add(
            'Missing space before opening curly brace',
            tokenBeforeBody.loc.start
          );
        }
      } else {
        if (!tokenBeforeBody) {
          errors.add(
            'Illegal space before opening curly brace',
            node.body.loc.end
          );
        }
      }
    });
  }
};

@@ -1190,27 +1190,24 @@
 unctionToken
-Pos
  = file.getT
@@ -1209,113 +1209,40 @@
 .get
-TokenPosByRangeStart(nodeBeforeRoundBrace.range%5B0%5D);%0A      var functionToken = tokens%5BfunctionTokenPos%5D;%0A
+FirstNodeToken(node.id %7C%7C node);
 %0A   
@@ -1257,19 +1257,16 @@
 extToken
-Pos
  = file.
@@ -1272,96 +1272,32 @@
 .get
+Next
 Token
-PosByRangeStart(functionToken.range%5B1%5D);%0A      var nextToken = tokens%5BnextTokenPos%5D
+(functionToken)
 ;%0A%0A 
@@ -1685,141 +1685,157 @@
     
-var tokenBeforeBodyPos = file.getTokenPosByRangeStart(node.body.range%5B0%5D - 1);%0A      var tokenBeforeBody = tokens%5BtokenBeforeBodyPos%5D
+// errors if no token is found unless %60includeComments%60 is passed%0A      var tokenBeforeBody = file.getPrevToken(node.body, %7B includeComments: true %7D)
 ;%0A%0A 

Fix JSCS rule (getTokenPosByRangeStart was removed upstream).