-
Notifications
You must be signed in to change notification settings - Fork 609
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
138 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. | ||
// See LICENSE in the project root for license information. | ||
|
||
// This is a workaround for ESLint's requirement to consume shareable configurations from package names prefixed | ||
// with "eslint-config". | ||
// | ||
// To remove this requirement, add this line to the top of your project's .eslintrc.js file: | ||
// | ||
// require("@rushstack/eslint-patch/custom-config-package-names"); | ||
// | ||
import { ConfigArrayFactory, ModuleResolver, Naming } from './_patch-base'; | ||
|
||
if (!ConfigArrayFactory.__loadExtendedShareableConfigPatched) { | ||
ConfigArrayFactory.__loadExtendedShareableConfigPatched = true; | ||
const originalLoadExtendedShareableConfig = ConfigArrayFactory.prototype._loadExtendedShareableConfig; | ||
|
||
// Common between ESLint versions | ||
// https://github.com/eslint/eslintrc/blob/242d569020dfe4f561e4503787b99ec016337457/lib/config-array-factory.js#L910 | ||
ConfigArrayFactory.prototype._loadExtendedShareableConfig = function (extendName: string) { | ||
const originalResolve = ModuleResolver.resolve; | ||
try { | ||
ModuleResolver.resolve = function (moduleName: string, relativeToPath: string) { | ||
try { | ||
return originalResolve.call(this, moduleName, relativeToPath); | ||
} catch (e) { | ||
// Only change the name we resolve if we cannot find the normalized module, since it is | ||
// valid to rely on the normalized package name. Use the originally provided module path | ||
// instead of the normalized module path. | ||
if ( | ||
(e as NodeJS.ErrnoException)?.code === 'MODULE_NOT_FOUND' && | ||
moduleName !== extendName && | ||
moduleName === Naming.normalizePackageName(extendName, 'eslint-config') | ||
) { | ||
return originalResolve.call(this, extendName, relativeToPath); | ||
} else { | ||
throw e; | ||
} | ||
} | ||
}; | ||
return originalLoadExtendedShareableConfig.apply(this, arguments); | ||
} finally { | ||
ModuleResolver.resolve = originalResolve; | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. | ||
// See LICENSE in the project root for license information. | ||
// This is a workaround for https://github.com/eslint/eslint/issues/3458 | ||
// | ||
// To correct how ESLint searches for plugin packages, add this line to the top of your project's .eslintrc.js file: | ||
// | ||
// require("@rushstack/eslint-patch/modern-module-resolution"); | ||
// | ||
|
||
import { | ||
EslintMajorVersion, | ||
ConfigArrayFactory, | ||
ModuleResolver, | ||
isModuleResolutionError | ||
} from './_patch-base'; | ||
|
||
// error: "The argument 'filename' must be a file URL object, file URL string, or absolute path string. Received ''" | ||
const isInvalidImporterPath: (ex: unknown) => boolean = (ex) => | ||
(ex as { code: unknown } | undefined)?.code === 'ERR_INVALID_ARG_VALUE'; | ||
|
||
if (!ConfigArrayFactory.__loadPluginPatched) { | ||
ConfigArrayFactory.__loadPluginPatched = true; | ||
const originalLoadPlugin = ConfigArrayFactory.prototype._loadPlugin; | ||
|
||
if (EslintMajorVersion === 6) { | ||
// ESLint 6.x | ||
// https://github.com/eslint/eslint/blob/9738f8cc864d769988ccf42bb70f524444df1349/lib/cli-engine/config-array-factory.js#L915 | ||
ConfigArrayFactory.prototype._loadPlugin = function ( | ||
name: string, | ||
importerPath: string, | ||
importerName: string | ||
) { | ||
const originalResolve = ModuleResolver.resolve; | ||
try { | ||
ModuleResolver.resolve = function (moduleName: string, relativeToPath: string) { | ||
try { | ||
// resolve using importerPath instead of relativeToPath | ||
return originalResolve.call(this, moduleName, importerPath); | ||
} catch (e) { | ||
if (isModuleResolutionError(e) || isInvalidImporterPath(e)) { | ||
return originalResolve.call(this, moduleName, relativeToPath); | ||
} | ||
throw e; | ||
} | ||
}; | ||
return originalLoadPlugin.apply(this, arguments); | ||
} finally { | ||
ModuleResolver.resolve = originalResolve; | ||
} | ||
}; | ||
} else { | ||
// ESLint 7.x || 8.x | ||
// https://github.com/eslint/eslintrc/blob/242d569020dfe4f561e4503787b99ec016337457/lib/config-array-factory.js#L1023 | ||
ConfigArrayFactory.prototype._loadPlugin = function (name: string, ctx: Record<string, unknown>) { | ||
const originalResolve = ModuleResolver.resolve; | ||
try { | ||
ModuleResolver.resolve = function (moduleName: string, relativeToPath: string) { | ||
try { | ||
// resolve using ctx.filePath instead of relativeToPath | ||
return originalResolve.call(this, moduleName, ctx.filePath); | ||
} catch (e) { | ||
if (isModuleResolutionError(e) || isInvalidImporterPath(e)) { | ||
return originalResolve.call(this, moduleName, relativeToPath); | ||
} | ||
throw e; | ||
} | ||
}; | ||
return originalLoadPlugin.apply(this, arguments); | ||
} finally { | ||
ModuleResolver.resolve = originalResolve; | ||
} | ||
}; | ||
} | ||
} |