question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

False-positive on date.setDate() in a loop

See original GitHub issue

What version of ESLint are you using?

"eslint": "^2.10.2",
"eslint-friendly-formatter": "^2.0.5",
"eslint-loader": "^1.3.0",
"eslint-plugin-html": "^1.3.0",
"eslint-config-standard": "^5.1.0",
"eslint-plugin-promise": "^1.0.8",
"eslint-plugin-standard": "^1.3.2",

What parser (default, Babel-ESLint, etc.) are you using?

“babel-eslint”: “^6.1.2”,

Please show your full configuration:

I am using generated vue-webpack project https://github.com/vuejs-templates/webpack

What did you do? Please include the actual source code causing the issue.

while (start <= end) { 
      dates.push(new Date(start))
      start.setDate(start.getDate() + 1)
}

What did you expect to happen?

It should go through eslint checks without any errors.

What actually happened? Please include the actual, raw output from ESLint.

http://eslint.org/docs/rules/no-unmodified-loop-condition ‘start’ is not modified in this loop
…/src/utils/DateUtils.js:163:12 while (start <= end) { ^

http://eslint.org/docs/rules/no-unmodified-loop-condition ‘end’ is not modified in this loop
…/src/utils/DateUtils.js:163:21 while (start <= end) { ^

The solutions suggested:

  1. By @ilyavolodin
while (start <= end) {
  dates.push(new Date(start))
  start = start.getDate() + 1;
}
  1. By @platinumazure
while (start.valueOf() <= end.valueOf())?
  1. By @platinumazure (my favorite one)
while (start <= end) {  // eslint-disable-line no-unmodified-loop-condition
  dates.push(new Date(start))
  start.setDate(start.getDate() + 1)
}

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:14 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
mnpennercommented, Feb 3, 2017

@feross @kaicataldo Here it is. Sorry I didn’t write it up sooner, been busy. Hopefully that’s sufficient 😃

1reaction
kaicataldocommented, Nov 4, 2016

I don’t think we should allow arbitrary information in the eslint-disable comments - having to parse what is config and what isn’t would make it error prone. My recommendation would be to put another comment before the eslint-disable-next-line comment.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Loops should not be infinite (javascript:S2189)
The analyser is complaining that iteratorDate and endDateObject are not modified in the while loop, but iteratorDate is modified.
Read more >
date - Javascript setDate() unexpected behaviour wrong month
I know that JS runs months from a 0 index, however I would have assumed that setDate() would take care of any discrepencies....
Read more >
Demystifying DateTime Manipulation in JavaScript - Toptal
Time and date manipulation is notoriously difficult. Developers encountering time zone rules, leap seconds, differences in locale-specific formatting are ...
Read more >
Date.prototype.setDate() - JavaScript - MDN Web Docs - Mozilla
The setDate() method changes the day of the month of a given Date instance, based on local time.
Read more >
Date and time - The Modern JavaScript Tutorial
To create a new Date object call new Date() with one of the following arguments ... The following methods allow to set date/time...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found