Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Caught between two no-restricted-syntax violations

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?

like image 720
Anthony Kong Avatar asked Nov 09 '17 23:11

Anthony Kong


1 Answers

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.

like image 139
loganfsmyth Avatar answered Oct 17 '22 02:10

loganfsmyth