jscs-dev / node-jscs

:arrow_heading_up: JavaScript Code Style checker (unmaintained)
https://jscs-dev.github.io
MIT License
4.96k stars 513 forks source link

requireNewlineBeforeSingleStatementsInIf throws an error when running fix #2174

Closed jamesdixon closed 8 years ago

jamesdixon commented 8 years ago

Hi,

I'm getting the following error when running --fix on one of my files:

Error running rule requireNewlineBeforeSingleStatementsInIf: This is an issue with JSCS and not your codebase.
Please file an issue (with the stack trace below) at: https://github.com/jscs-dev/node-jscs/issues/new
TypeError: Cannot read property 'type' of null
    at isExpressionStatement (/Users/jamesdixon/Projects/scout/platform/api/node_modules/jscs/lib/rules/require-newline-before-single-statements-in-if.js:61:26)
    at Object.<anonymous> (/Users/jamesdixon/Projects/scout/platform/api/node_modules/jscs/lib/rules/require-newline-before-single-statements-in-if.js:86:17)
    at Array.forEach (native)
    at Object.JsFile.iterateNodesByType (/Users/jamesdixon/Projects/scout/platform/api/node_modules/jscs/lib/js-file.js:595:42)
    at Object.module.exports.check (/Users/jamesdixon/Projects/scout/platform/api/node_modules/jscs/lib/rules/require-newline-before-single-statements-in-if.js:72:14)
    at null.<anonymous> (/Users/jamesdixon/Projects/scout/platform/api/node_modules/jscs/lib/string-checker.js:165:22)
    at Array.forEach (native)
    at StringChecker._checkJsFile (/Users/jamesdixon/Projects/scout/platform/api/node_modules/jscs/lib/string-checker.js:161:31)
    at StringChecker.fixString (/Users/jamesdixon/Projects/scout/platform/api/node_modules/jscs/lib/string-checker.js:261:22)
    at null.<anonymous> (/Users/jamesdixon/Projects/scout/platform/api/node_modules/jscs/lib/checker.js:72:27) at test/appointment.js :
     1 |const Boom = require('boom');
--------^
     2 |const Code = require('code');
     3 |const Hapi = require('..');

My .jscsrc file is pretty simple:

{
    "preset": "airbnb",
    "requireNewlineBeforeSingleStatementsInIf": true
}

Thanks in advance!

markelog commented 8 years ago

Do you have code example?

jamesdixon commented 8 years ago

Yes, I ran JSCS against this file:

'use strict';

// Load modules

const Boom = require('boom');
const Code = require('code');
const Hapi = require('hapi');
const Hoek = require('hoek');
const Lab = require('lab');

// Test shortcuts

const lab = exports.lab = Lab.script();
const before = lab.before;
const describe = lab.describe;
const it = lab.it;
const expect = Code.expect;

describe('appointments', () => {

  let server;

  before((done) => {

    require('../server')((err, srv) => {
      if (err) throw err;

      server = srv;
      done();
    });
  });

  describe('GET /appointments/{id}', () => {

    const url = '/appointments/1';

    describe('with no/invalid credentials', () => {

      const payload = {
        method: 'GET',
        url: url,
      };

      it('CANNOT retrieve an appointment', (done) => {

        server.inject(payload, (res) => {
          expect(res.statusCode).to.equal(401);
          done();
        });

      });

    });
  });
});

Running JSCS 2.11.0.

markelog commented 8 years ago

it seems requireNewlineBeforeSingleStatementsInIf rule doesn't work with single if branches, i added a guard for it, but it seems it should also warn for that case too -

if (err) throw err;

/cc @schempy