Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ninjaprox opened this issue Dec 11, 2018 · 4 comments
Open

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

ninjaprox opened this issue Dec 11, 2018 · 4 comments

Comments

@ninjaprox
Copy link

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.

@ljharb
Copy link
Member

ljharb commented Dec 11, 2018

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

@ninjaprox
Copy link
Author

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.

@sharikovvladislav
Copy link

Any information on this?

@graphan
Copy link

graphan commented Dec 15, 2020

Bumping up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants