eslint-plugin-import
eslint-plugin-import copied to clipboard
Add relaxed option for no-relative-parent-imports
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
strictis used, nothing will change, the current behavior stays intact.strictis 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
relaxedis 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.
Indeed; I think that rule you actually want is a different one - more like enforcing import boundaries on specific directories.
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.
Any information on this?
Bumping up.