import-js / import-js/eslint-plugin-import
Make moduleVisitor extendable by plugins
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 5.9k
- Forks
- 1.5k
- PR merge metrics
- No merged PRs in 30d
Description
Hello,
This is a feature request (kind of a continuation of #906),
Some modules require module paths for some calls (like the `jest.mock()` in Jest). As a developer, I would like `eslint-plugin-import` to apply its rules to paths to modules in other places different than `require()` or `import` calls so that I can quickly catch possible issues with such calls (i.e. the path in some call is pointing to an unresolved module).
Given this requirement is out of the scope of `eslint-plugin-import` (it shouldn't/can't keep track of paths used in calls other different modules), it can instead provide an interface for other modules to extend the scope of the default `moduleVisitor` (like the one for custom resolvers).
For instance, As a module author, I should be able to write an `eslint-import-visitor-jest` module that exports a function such as:
```JavaScript
function checkJestMocks(visitors, checkSourceValue) {
const currentCallExpression = visitors['CallExpression'];
visitors['CallExpression'] = function (call) {
if (currentCallExpression) currentCallExpression(call)
if (call.callee.type !== 'MemberExpression') return
if (call.callee.object.name !== 'jest') return
if (call.callee.property.name !== 'mock') return
if (call.arguments.length !== 1 || call.arguments.length !== 2) return
const modulePath = call.arguments[0]
if (modulePath.type !== 'Literal') return
if (typeof modulePath.value !== 'string') return
checkSourceValue(modulePath, call)
}
}
module.exports.visitor = checkJestMocks;
```
Then a developer can include my module and point `eslint-plugin-import` to use the exported visitor via a setting.
I would like to write this new feature if you consider and accept a PR for this.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by locating the moduleVisitor entry point and reading the existing custom-resolver extension path described in the issue. Define how external visitors would be supplied through settings, how they would receive checkSourceValue, and what the completed plugin interface should support; the issue does not name files or tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100