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

Update: Add resolver option to ESLint and CLIEngine #15099

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/developer-guide/nodejs-api.md
Expand Up @@ -136,6 +136,8 @@ The `ESLint` constructor takes an `options` object. If you omit the `options` ob
Default is `null`. The severity to report unused eslint-disable directives. If this option is a severity, it overrides the `reportUnusedDisableDirectives` setting in your configurations.
* `options.resolvePluginsRelativeTo` (`string` | `null`)<br>
Default is `null`. The path to a directory where plugins should be resolved from. If `null` is present, ESLint loads plugins from the location of the configuration file that contains the plugin setting. If a path is present, ESLint loads all plugins from there.
* `options.resolver` (`ModuleResolver` | `null`)<br>
Default is `null`. You can use an alternate `ModuleResolver` to change how ESLint resolves plugins.
* `options.rulePaths` (`string[]`)<br>
Default is `[]`. An array of paths to directories to load custom rules from.
* `options.useEslintrc` (`boolean`)<br>
Expand Down
8 changes: 7 additions & 1 deletion lib/cli-engine/cli-engine.js
Expand Up @@ -84,6 +84,7 @@ const validFixTypes = new Set(["directive", "problem", "suggestion", "layout"]);
* @property {boolean} [reportUnusedDisableDirectives] `true` adds reports for unused eslint-disable directives
* @property {boolean} [globInputPaths] Set to false to skip glob resolution of input file paths to lint (default: true). If false, each input file paths is assumed to be a non-glob path to an existing file.
* @property {string} [resolvePluginsRelativeTo] The folder where plugins should be resolved from, defaulting to the CWD
* @property {string} [resolver] An instance of a ModuleResolver with a resolve(moduleName, relativeToPath) method.
*/

/**
Expand Down Expand Up @@ -595,6 +596,7 @@ class CLIEngine {
cwd: options.cwd,
ignorePath: options.ignorePath,
resolvePluginsRelativeTo: options.resolvePluginsRelativeTo,
resolve: options.resolve,
rulePaths: options.rulePaths,
specificConfigPath: options.configFile,
useEslintrc: options.useEslintrc,
Expand Down Expand Up @@ -1008,6 +1010,10 @@ class CLIEngine {
*/
getFormatter(format) {

// slot.options might be null, we can't destructure
const slot = internalSlotsMap.get(this);
const resolver = slot && slot.options && slot.options.resolver;

// default is stylish
const resolvedFormatName = format || "stylish";

Expand All @@ -1030,7 +1036,7 @@ class CLIEngine {
try {
const npmFormat = naming.normalizePackageName(normalizedFormatName, "eslint-formatter");

formatterPath = ModuleResolver.resolve(npmFormat, path.join(cwd, "__placeholder__.js"));
formatterPath = (resolver ? resolver : ModuleResolver).resolve(npmFormat, path.join(cwd, "__placeholder__.js"));
} catch {
formatterPath = path.resolve(__dirname, "formatters", normalizedFormatName);
}
Expand Down
9 changes: 9 additions & 0 deletions lib/eslint/eslint.js
Expand Up @@ -57,6 +57,7 @@ const { version } = require("../../package.json");
* @property {Record<string,Plugin>} [plugins] An array of plugin implementations.
* @property {"error" | "warn" | "off"} [reportUnusedDisableDirectives] the severity to report unused eslint-disable directives.
* @property {string} [resolvePluginsRelativeTo] The folder where plugins should be resolved from, defaulting to the CWD.
* @property {string} [resolver] An instance of a ModuleResolver with a resolve(moduleName, relativeToPath) method.
* @property {string[]} [rulePaths] An array of directories to load custom rules from.
* @property {boolean} [useEslintrc] False disables looking for .eslintrc.* files.
*/
Expand Down Expand Up @@ -173,6 +174,7 @@ function processOptions({
plugins = {},
reportUnusedDisableDirectives = null, // ← should be null by default because if it's a string then it overrides the 'reportUnusedDisableDirectives' setting in config files. And we cannot use `overrideConfig.reportUnusedDisableDirectives` instead because we cannot configure the `error` severity with that.
resolvePluginsRelativeTo = null, // ← should be null by default because if it's a string then it suppresses RFC47 feature.
resolver = null,
rulePaths = [],
useEslintrc = true,
...unknownOptions
Expand Down Expand Up @@ -277,6 +279,12 @@ function processOptions({
) {
errors.push("'resolvePluginsRelativeTo' must be a non-empty string or null.");
}
if (
!(resolver === null ||
typeof resolver === "object" && typeof resolver.resolve === "function")
) {
errors.push("'resolver' must be an object with a 'resolver' method or null.");
}
if (!isArrayOfNonEmptyString(rulePaths)) {
errors.push("'rulePaths' must be an array of non-empty strings.");
}
Expand Down Expand Up @@ -305,6 +313,7 @@ function processOptions({
ignorePath,
reportUnusedDisableDirectives,
resolvePluginsRelativeTo,
resolver,
rulePaths,
useEslintrc
};
Expand Down