Caught between two no-restricted-syntax violations

Anthony Kong picture Anthony Kong · Nov 10, 2017 · Viewed 25.8k times · Source

This is my original code:

const buildTableContent = (settings) => {
  const entries = [];
  for (const key in settings) {
    for (const subkey in env[key]) {

settings is basically a dictionary of dictionary

  {  
    'env': {'name': 'prod'}, 
    'sass: {'app-id': 'a123445', 'app-key': 'xxyyzz'}
  }

It triggered the following AirBnb style guide error:

35:3 error for..in loops iterate over the entire prototype chain, which is virtually never what you want. Use Object.{keys,values,entries}, and iterate over the resulting array no-restricted-syntax

So I change the code to

const buildTableContent = (settings) => {
  const entries = [];
  for (const key of Object.keys(settings)) {
    for (const subkey of Object.keys(env[key])) {

as suggested.

Now when I run lint, I got this:

35:3 error iterators/generators require regenerator-runtime, which is too heavyweight for this guide to allow them. Separately, loops should be avoided in favor of array iterations no-restricted-syntax

So it looks to me either way they are violating some lint style.

How can I fix this issue?

Answer

loganfsmyth picture loganfsmyth · Nov 10, 2017

You'd want to use

Object.keys(settings).forEach(key => {
  Object.keys(env[key]).forEach(subkey => {

or potentially Object.entries or Object.values depending on if you actually want the keys.