eslint-plugin-import icon indicating copy to clipboard operation
eslint-plugin-import copied to clipboard

Add relaxed option for no-relative-parent-imports

Open ninjaprox opened this issue 7 years ago • 4 comments

This is the proposal for no-relative-parent-imports rule improvement.

Context

When no-relative-parent-imports rule is enabled, it doesn't allow to import from relative parents at all, for example:

// Both will fail ESLint
import component from '../path/to/component' // immediate parent
import component from '../../path/to/component'

However, it's sometimes acceptable and reasonable to import from the immediate parent as depicted above.

Proposal

Give no-relative-parent-imports the option of either strict or relaxed.

  • If strict is used, nothing will change, the current behavior stays intact. strict is by default if not specifying the option.
// Both will be in strict mode
// Explicitly specify the option
{
  `no-relative-parent-imports`: ['warn', 'strict']
}
// `strict` by default
{
  `no-relative-parent-imports`: 'warn'
}
  • If relaxed is used, the rule will pass on immediate parent imports, and still catch the rest.
{
  `no-relative-parent-imports`: ['warn', 'relaxed']
}
import component from '../path/to/component' // passed
import component from '../../path/to/component' // failed

Effect

By using strict by default, this proposal doesn't affect any existing users. However, it gives users more options to tweak the rule if needed.

Alternatives

Give the option to specify how depth of relative parent imports is allowed.

{
  `no-relative-parent-imports`: ['warn', { depth: 2 }]
}
import component from '../path/to/component' // passed
import component from '../../path/to/component' // passed
import component from '../../../path/to/component' // passed

However, I think this isn't a preferred option since it goes against the purpose of no-relative-parent-imports rule.

ninjaprox avatar Dec 11 '18 15:12 ninjaprox

Indeed; I think that rule you actually want is a different one - more like enforcing import boundaries on specific directories.

ljharb avatar Dec 11 '18 22:12 ljharb

That might become a rule as well, and that will give users fine-grained control over how to enforce imports. This proposal is far simpler than that rule in term of configuration and enforcement, yet opens flexibility.

ninjaprox avatar Dec 12 '18 09:12 ninjaprox

Any information on this?

sharikovvladislav avatar May 19 '19 19:05 sharikovvladislav

Bumping up.

graphan avatar Dec 15 '20 14:12 graphan