Lint Rules

useLiteralKeys (since v12.1.0)

Enforce the usage of a literal access to properties over computed property access.

Examples

Invalid

a.b["c"];
nursery/useLiteralKeys.js:1:5 lint/nursery/useLiteralKeys  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

   The computed expression can be simplified without the use of a string literal.
  
  > 1 │ a.b["c"];
       ^^^
    2 │ 
  
   Suggested fix: Replace it with a static expression.
  
    1  - a.b["c"];
      1+ a.b.c;
    2 2  
  
a.c[`d`]
nursery/useLiteralKeys.js:1:5 lint/nursery/useLiteralKeys  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

   The computed expression can be simplified without the use of a string literal.
  
  > 1 │ a.c[`d`]
       ^^^
    2 │ 
  
   Suggested fix: Replace it with a static expression.
  
    1  - a.c[`d`]
      1+ a.c.d
    2 2  
  
a.c[`d`] = "something"
nursery/useLiteralKeys.js:1:5 lint/nursery/useLiteralKeys  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

   The computed expression can be simplified without the use of a string literal.
  
  > 1 │ a.c[`d`] = "something"
       ^^^
    2 │ 
  
   Suggested fix: Replace it with a static expression.
  
    1  - a.c[`d`]·=·"something"
      1+ a.c.d·=·"something"
    2 2  
  
a = {
	['b']: d
}
nursery/useLiteralKeys.js:2:3 lint/nursery/useLiteralKeys  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

   The computed expression can be simplified without the use of a string literal.
  
    1 │ a = {
  > 2 │ 	['b']: d
   	 ^^^
    3 │ }
    4 │ 
  
   Suggested fix: Replace it with a static expression.
  
    2 │ ['b']:·d
    -- --   

Valid

a["c" + "d"];
a[d.c];