Skip to content

Add relaxed option for no-relative-parent-imports #1243

@ninjaprox

Description

@ninjaprox

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions