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.

Change `prefer-const` to permit the author to use `let` in the case of declaring an object that is then mutated.

See original GitHub issue

Version v2.9.0

Problem you want to solve As a developer, I might want to choose to use let when declaring an object I later mutate, even though JavaScript will allow me to use const. This would be to make the status of the variable clearer to future readers and myself. At the moment, it is not possible to do this while using prefer-const, which is an important rule in other situations.

My take on the solution The parser should look to see if a variable is mutated in the scope and, if so, permit the use of let for that variable. prefer-const should have an option that enables this behaviour.

What you did With prefer-const enabled, write

function example(value) {
  let object = {};
  object.property = value;
  return object;
}

What you would like to happen No errors to be triggered

What happened The second line is highlighted by prefer-const.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:7
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

7reactions
mysticateacommented, May 13, 2016

Thank you for this issue.

Hmm, personally I want not add this option. const of JavaScript doesn’t mean that object’s properties are not modified. If people rely on const keyword to check whether properties are modified or not, it’s wrong. So this option would mislead people, I think.

2reactions
gausiecommented, May 13, 2016

I understand that JavaScript will not stop you from modifying an object’s properties if you use const. This is more to support a developer/team’s decision that they might want to indicate (for code readability) that they intend for an object’s properties to be changed by using let instead of const.

I definitely don’t think this should be the default behaviour, but it seems like a reasonable case to be controlled by an option.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using let on mutating variable - swift - Stack Overflow
Think of a let reference variable as: the reference (memory address) that the variable refers to cannot change, but the contents of that...
Read more >
Airbnb JavaScript Style Guide()
They allow you to define all the properties of an object in one place. ... 7.7 Use default parameter syntax rather than mutating...
Read more >
A Writer's Guide to Fair Use and Permissions + Sample ...
Permissions is all about seeking permission to quote or excerpt other people's copyrighted work within your own. Here's when you need to ...
Read more >
rollup.js
Importing. Imported values cannot be reassigned, though imported objects and arrays can be mutated (and the exporting module, and any other importers, will...
Read more >
deno_lint docs
Bans the use of primitive wrapper objects (e.g. String the object is a wrapper ... Disallows using the same case clause in a...
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 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