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.

Validate object literals

See original GitHub issue

Please describe what the rule should do:

I propose to add a new object literal validation. In case the properties on an object are not dynamically manipulated in any way and the object is also not exported it would be possible to validate all accesses to properties of that object. That way it would be possible to check if the static property accesses are all valid. Besides that it would also be possible to validate for unused properties in case property access is only static and the object is never exported. [Update]: There are a couple further things that would have to be though about like referencing the same object with a different variable name and then accessing the properties / setting them and passing the object to a function or using destructuring. The rule could therefore not warn about a lot of things but I believe this could a) be maybe improved later on and b) would still provide benefit for all possible cases. [/Update]

What category of rule is this? (place an “X” next to just one item)

[ ] Enforces code style [x] Warns about a potential error [ ] Suggests an alternate way of doing something [ ] Other (please specify:)

Provide code examples that this rule will warn about:

const kConstants = {
  a: true,
  b: false,
  errorOnNotAccessed: 'yes please' // Should error
}
Object.defineProperty(kConstants, 'c', {
  value: 'works'
}

// ... application code ...
function doThings(bar) {
  const randomAccess = kConstants[bar]
  const abc = kConstants.c
  const accessError = kConstants.d // Should error
  // ...
}

// Object is not exported
module.exports = { doThings }

Why should this rule be included in ESLint (instead of a plugin)?

It could provide early feedback when users access non existing properties on an object.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
not-an-aardvarkcommented, Jul 1, 2018

Personally, I think it would be better to use a dedicated type checker such as TypeScript for this, because it would be able to do a much better job than ESLint and catch many more errors. Only being able to catch errors in a very small subset of cases (when there is no aliasing, and the object isn’t passed to any functions that could modify it, and the object is never passed outside of the file) makes me think that this rule wouldn’t be very useful.

0reactions
eslint-deprecated[bot]commented, Dec 11, 2018

Unfortunately, it looks like there wasn’t enough interest from the team or community to implement this change. While we wish we’d be able to accommodate everyone’s requests, we do need to prioritize. We’ve found that issues failing to reach accepted status after 21 days tend to never be accepted, and as such, we close those issues. This doesn’t mean the idea isn’t interesting or useful, just that it’s not something the team can commit to.

Thanks for contributing to ESLint and we appreciate your understanding.

Read more comments on GitHub >

github_iconTop Results From Across the Web

validate JavaScript object literal in Java - Stack Overflow
Is there a way to check if a given Java String conforms to the format of a JavaScript object literal? For e.g., String...
Read more >
Show love to the object literal | Christian Heilmann
My code in easyFameAndFortune.js will not be executed at all, as the functions init() and validate() have been redefined � or you could...
Read more >
Object Literals - SnapLogic Documentation - Confluence
An object literal is a comma-delimited list of zero or more pairs of property names and values surrounded by curly braces ({}), like...
Read more >
How to Create a JavaScript Object Literal | Kevin Chisholm
The simplest way to create an object literal is to assign an empty object to a variable. For example: var foo = {};...
Read more >
Types - Superstruct
enums structs validate that a value is one of a specific set of literals values. ... record structs validate an object with specific...
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