jscs-dev / node-jscs

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

v3.0.3 throws "Cannot read property 'replace' of undefined" #2219

Closed EvHaus closed 8 years ago

EvHaus commented 8 years ago

After upgrading to 3.0.2, I can't seem to be able to get JSCS to work. Running the jscs CLI returns this error:

TypeError: Cannot read property 'replace' of undefined
    at renderLine (/Users/test/my_project/node_modules/jscs/lib/errors.js:266:16)
    at Object.Errors.explainError (/Users/test/my_project/node_modules/jscs/lib/errors.js:197:13)
    at /Users/test/my_project/node_modules/jscs/lib/reporters/console.js:16:36
    at Array.forEach (native)
    at /Users/test/my_project/node_modules/jscs/lib/reporters/console.js:14:35
    at Array.forEach (native)
    at Object.module.exports [as writer] (/Users/test/my_project/node_modules/jscs/lib/reporters/console.js:9:22)
    at /Users/test/my_project/node_modules/jscs/lib/cli.js:149:18
    at Array.<anonymous> (/Users/test/my_project/node_modules/vow/lib/vow.js:712:56)
    at Immediate.callFns [as _onImmediate] (/Users/test/my_project/node_modules/vow/lib/vow.js:23:35)

My .jscsrc is:

{
    "excludeFiles": [
        "html/build/**",
        "html/js/examples/**",
        "html/test/jasmine-2.4.1/**"
    ],

    "disallowKeywords": ["with"],
    "disallowMultipleLineStrings": true,
    "disallowNamedUnassignedFunctions": true,
    "disallowNewlineBeforeBlockStatements": true,
    "disallowOperatorBeforeLineBreak": ["."],
    "disallowQuotedKeysInObjects": "allButReserved",
    "disallowSpaceAfterPrefixUnaryOperators": ["++", "--", "+", "~"],
    "disallowSpaceBeforeBinaryOperators": ["!"],
    "disallowSpacesInsideArrayBrackets": "all",
    "disallowSpacesInsideBrackets": true,
    "disallowSpacesInsideObjectBrackets": "all",
    "disallowTrailingComma": true,
    "disallowTrailingWhitespace": true,
    "requireAnonymousFunctions": true,
    "requireCapitalizedConstructors": true,
    "requireCommaBeforeLineBreak": true,
    "requireCurlyBraces": ["for", "try", "catch"],
    "requireLineBreakAfterVariableAssignment": true,
    "requireOperatorBeforeLineBreak": ["?", "=", "+", "-", "/", "*", "==", "===", "!=", "!==", ">", ">=", "<", "<="],
    "requirePaddingNewLinesAfterUseStrict": true,
    "requirePaddingNewLinesBeforeExport": true,
    "requireParenthesesAroundIIFE": true,
    "requireSpaceAfterKeywords": ["do", "for", "else", "switch", "case", "void", "while", "with", "typeof", "function"],
    "requireSpaceBeforeKeywords": ["else", "while"],
    "requireSpaceBeforeObjectValues": true,
    "requireSpacesInConditionalExpression": {
        "afterTest": true,
        "beforeConsequent": true,
        "afterConsequent": true,
        "beforeAlternate": true
    },
    "requireSpacesInForStatement": true,
    "requireSpacesInFunctionDeclaration": {
        "beforeOpeningRoundBrace": true,
        "beforeOpeningCurlyBrace": true
    },
    "requireSpacesInNamedFunctionExpression": {
        "beforeOpeningCurlyBrace": true
    },
    "safeContextKeyword": ["self", "_this"],
    "validateAlignedFunctionParameters": {
        "lineBreakAfterOpeningBraces": true,
        "lineBreakBeforeClosingBraces": true
    },
    "validateIndentation": "\t",
    "validateParameterSeparator": ", "
}
hzoo commented 8 years ago

I thought we fixed this one @mdevils?

koistya commented 8 years ago

I have the same issue in react-starter-kit after upgrading to 3.0.2

koistya commented 8 years ago

3.0.3 doesn't fix it, take a look https://travis-ci.org/kriasoft/react-starter-kit/builds/123586977

mdevils commented 8 years ago

@hzoo I though so

Will take a look soon

louh commented 8 years ago

This has been happening for me for any version in the 3.0.x series.

I went through my codebase and ran jscs on each file until I discovered which file was causing the error to occur, and then through each function inside that file until I narrowed down the root cause. It seems that this object destructuring assignment inside of a function parameter causes the error: https://github.com/tangrams/tangram-play/blob/master/src/js/map/map.js#L80

This is valid ES6 and has been addressed before.

I created a very minimal test case so you can duplicate the error:

export function myFunction ({ foo = false, bar = null } = {}) {
    console.log(foo, bar);
}

See here: https://gist.github.com/louh/0b2e74228c141afc4a34b70d3e944254

kaievns commented 8 years ago

still happening in 3.0.3

Yixi commented 8 years ago

I have same problem use with react

Grygir commented 8 years ago

Same error for static property definition (in jscs 3.0.3)

class Bar {
    static test = {
        foo: 'bar'
    };
}

That seems to be valid code.

TypeError: Cannot read property 'replace' of undefined
    at renderLine (/var/www/test/node_modules/jscs/lib/errors.js:266:16)
    at Object.Errors.explainError (/var/www/test/node_modules/jscs/lib/errors.js:197:13)
    at /var/www/test/node_modules/jscs/lib/reporters/console.js:16:36
    at Array.forEach (native)
    at /var/www/test/node_modules/jscs/lib/reporters/console.js:14:35
    at Array.forEach (native)
    at Object.module.exports [as writer] (/var/www/test/node_modules/jscs/lib/reporters/console.js:9:22)
    at /var/www/test/node_modules/jscs/lib/cli.js:149:18
    at Array.<anonymous> (/var/www/test/node_modules/vow/lib/vow.js:712:56)
    at Immediate.callFns [as _onImmediate] (/var/www/test/node_modules/vow/lib/vow.js:23:35)
r-murphy commented 8 years ago

I got the same error when using esnext bind expression, which doesn't seem to be supported in cst used by jscs 3.0. Whenever there is a parse error (error.rule === "parseError"), the error has no line number.

The fixed I applied temporarily to my jscs install was this, which a least prints the underlying reason for the parse error. I'm not sure if that's the best place for the fix or if it should be as soon as the parse error is detected.

explainError: function(error, colorize) {
  if (typeof error.line === 'undefined') {
    //probably a parseError, so include the rule name
    return formatErrorMessage(error.rule + ': ' + error.message, this.getFilename(), colorize);
  }
  //... the rest 
}
markelog commented 8 years ago

There is lot of different errors here and they about different things, like

export function myFunction ({ foo = false, bar = null } = {}) {
    console.log(foo, bar);
}

Is https://github.com/cst/cst/issues/118

static properties is about - https://github.com/cst/cst/issues/112

And so on.

After https://github.com/cst/cst/pull/116 and with https://github.com/jscs-dev/node-jscs/commit/729db94f86902496f3962a4a97bdd91a75ab5514, errors will be outputted in a friendly manner

UnsungHero97 commented 8 years ago

pulling in the latest changes from master fixed this issue for me:

"devDependencies": {
  ...
  "jscs": "git:github.com/jscs-dev/node-jscs.git",
  ...
}
qfox commented 8 years ago

This is enough:

"devDependencies": {
  ...
  "jscs": "jscs-dev/node-jscs",
  ...
}
UnsungHero97 commented 8 years ago

@zxqfox cool, thanks!

szebrowski commented 8 years ago

For me problem still occurs. Even on the jscs-dev/node-jscs version. It's hard for me to paste example code because project is too large.

lf94 commented 8 years ago

Reporting in the same. Version 3.0.3. May move to ESLint as suggested though on the blog. For now I'm using the fix people have suggested.

qfox commented 8 years ago

We gonna publish 3.0.4 in a few days with a fix (we hope). Can you please try master branch?

szebrowski commented 8 years ago

I will try it on Monday (just finished work;) On 20 May 2016 4:06 pm, "Alexej Yaroshevich" notifications@github.com wrote:

We gonna publish 3.0.4 in a few days with a fix (we hope). Can you please try master branch?

— You are receiving this because you commented. Reply to this email directly or view it on GitHub https://github.com/jscs-dev/node-jscs/issues/2219#issuecomment-220615013

szebrowski commented 8 years ago

Unfortunatly, I still get 'replace' of undefined error. :(

On Fri, May 20, 2016 at 4:17 PM Sebastian mergol@gmail.com wrote:

I will try it on Monday (just finished work;) On 20 May 2016 4:06 pm, "Alexej Yaroshevich" notifications@github.com wrote:

We gonna publish 3.0.4 in a few days with a fix (we hope). Can you please try master branch?

— You are receiving this because you commented. Reply to this email directly or view it on GitHub https://github.com/jscs-dev/node-jscs/issues/2219#issuecomment-220615013

szebrowski commented 8 years ago

Most basic example to get the error:

var m = require('mori');

function getTrNid(trNid, saveRes) {
  return trNid < 0
        ? m.getIn(saveRes, [parseInt(trNid), 'nid'])
        : trNid;
};

module.exports = getTrNid;

Results in:

~/delme/node-jscs/bin/jscs -x --preset airbnb src/js/model/FlightListLeg/getTrNid.js 
validateIndentation: Invalid indentation character:      at src/js/model/FlightListLeg/getTrNid.js :
     3 |function getTrNid(trNid, saveRes) {
     4 |  return trNid < 0
     5 |  ? m.getIn(saveRes, [parseInt(trNid), 'nid'])
--------^
     6 |  : trNid;
     7 |};

validateIndentation: Invalid indentation character:      at src/js/model/FlightListLeg/getTrNid.js :
     4 |  return trNid < 0
     5 |  ? m.getIn(saveRes, [parseInt(trNid), 'nid'])
     6 |  : trNid;
--------^
     7 |};
     8 |

TypeError: Cannot read property 'replace' of undefined
    at renderLine (/home/x/delme/node-jscs/lib/errors.js:267:16)
    at Object.Errors.explainError (/home/x/delme/node-jscs/lib/errors.js:198:13)
    at /home/x/delme/node-jscs/lib/reporters/console.js:16:36
    at Array.forEach (native)
    at /home/x/delme/node-jscs/lib/reporters/console.js:14:35
    at Array.forEach (native)
    at Object.module.exports [as writer] (/home/x/delme/node-jscs/lib/reporters/console.js:9:22)
    at /home/x/delme/node-jscs/lib/cli.js:149:18
    at Array.<anonymous> (/home/x/delme/node-jscs/node_modules/vow/lib/vow.js:712:56)
    at Immediate.callFns [as _onImmediate] (/home/x/delme/node-jscs/node_modules/vow/lib/vow.js:23:35)