|
|
"use strict"; |
|
|
|
|
|
|
|
|
var __importDefault = (this && this.__importDefault) || function (mod) { |
|
|
return (mod && mod.__esModule) ? mod : { "default": mod }; |
|
|
}; |
|
|
Object.defineProperty(exports, "__esModule", { value: true }); |
|
|
exports.isModuleResolutionError = exports.ESLINT_MAJOR_VERSION = exports.Naming = exports.ModuleResolver = exports.configArrayFactory = exports.eslintFolder = exports.eslintPackageVersion = void 0; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const path_1 = __importDefault(require("path")); |
|
|
const isModuleResolutionError = (ex) => typeof ex === 'object' && !!ex && 'code' in ex && ex.code === 'MODULE_NOT_FOUND'; |
|
|
exports.isModuleResolutionError = isModuleResolutionError; |
|
|
const FLAT_CONFIG_REGEX = /eslint\.config\.(cjs|mjs|js)$/i; |
|
|
|
|
|
|
|
|
const NODE_STACK_REGEX = /^\s*at (?:((?:\[object object\])?[^\\/]+(?: \[as \S+\])?) )?\(?(.*?)(?::(\d+)| (\d+))(?::(\d+))?\)?\s*$/i; |
|
|
function parseNodeStack(stack) { |
|
|
const stackTraceMatch = NODE_STACK_REGEX.exec(stack); |
|
|
if (!stackTraceMatch) { |
|
|
return undefined; |
|
|
} |
|
|
return { |
|
|
file: stackTraceMatch[2], |
|
|
method: stackTraceMatch[1], |
|
|
lineNumber: parseInt(stackTraceMatch[3], 10), |
|
|
column: stackTraceMatch[4] ? parseInt(stackTraceMatch[4], 10) : undefined |
|
|
}; |
|
|
} |
|
|
function getStackTrace() { |
|
|
const stackObj = {}; |
|
|
const originalStackTraceLimit = Error.stackTraceLimit; |
|
|
Error.stackTraceLimit = Infinity; |
|
|
Error.captureStackTrace(stackObj, getStackTrace); |
|
|
Error.stackTraceLimit = originalStackTraceLimit; |
|
|
if (!stackObj.stack) { |
|
|
throw new Error('Unable to capture stack trace'); |
|
|
} |
|
|
const { stack } = stackObj; |
|
|
const stackLines = stack.split('\n'); |
|
|
const frames = []; |
|
|
for (const line of stackLines) { |
|
|
const frame = parseNodeStack(line); |
|
|
if (frame) { |
|
|
frames.push(frame); |
|
|
} |
|
|
} |
|
|
return frames; |
|
|
} |
|
|
|
|
|
|
|
|
let eslintrcBundlePath = undefined; |
|
|
|
|
|
|
|
|
let configArrayFactoryPath = undefined; |
|
|
|
|
|
|
|
|
let moduleResolverPath = undefined; |
|
|
|
|
|
|
|
|
let namingPath = undefined; |
|
|
|
|
|
|
|
|
let eslintFolder = undefined; |
|
|
exports.eslintFolder = eslintFolder; |
|
|
|
|
|
for (let currentModule = module;;) { |
|
|
if (FLAT_CONFIG_REGEX.test(currentModule.filename)) { |
|
|
|
|
|
|
|
|
|
|
|
const stackTrace = getStackTrace(); |
|
|
const targetFrame = stackTrace.find((frame) => frame.file && frame.file.endsWith('eslint.js')); |
|
|
if (targetFrame) { |
|
|
|
|
|
let currentPath = targetFrame.file; |
|
|
while (currentPath) { |
|
|
const potentialPath = path_1.default.dirname(currentPath); |
|
|
if (potentialPath === currentPath) { |
|
|
break; |
|
|
} |
|
|
currentPath = potentialPath; |
|
|
try { |
|
|
exports.eslintFolder = eslintFolder = path_1.default.dirname(require.resolve('eslint/package.json', { paths: [currentPath] })); |
|
|
break; |
|
|
} |
|
|
catch (ex) { |
|
|
if (!isModuleResolutionError(ex)) { |
|
|
throw ex; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
if (eslintFolder) { |
|
|
const eslintrcFolderPath = path_1.default.dirname(require.resolve('@eslint/eslintrc/package.json', { paths: [eslintFolder] })); |
|
|
eslintrcBundlePath = path_1.default.join(eslintrcFolderPath, 'dist/eslintrc.cjs'); |
|
|
} |
|
|
break; |
|
|
} |
|
|
if (!currentModule.parent) { |
|
|
break; |
|
|
} |
|
|
currentModule = currentModule.parent; |
|
|
} |
|
|
if (!eslintFolder) { |
|
|
|
|
|
for (let currentModule = module;;) { |
|
|
if (!eslintrcBundlePath) { |
|
|
if (currentModule.filename.endsWith('eslintrc.cjs')) { |
|
|
|
|
|
|
|
|
try { |
|
|
const eslintrcFolderPath = path_1.default.dirname(require.resolve('@eslint/eslintrc/package.json', { paths: [currentModule.path] })); |
|
|
|
|
|
|
|
|
const resolvedEslintrcBundlePath = path_1.default.join(eslintrcFolderPath, 'dist/eslintrc.cjs'); |
|
|
if (resolvedEslintrcBundlePath === currentModule.filename) { |
|
|
eslintrcBundlePath = resolvedEslintrcBundlePath; |
|
|
} |
|
|
} |
|
|
catch (ex) { |
|
|
|
|
|
|
|
|
|
|
|
if (!isModuleResolutionError(ex)) { |
|
|
throw ex; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
else { |
|
|
|
|
|
|
|
|
try { |
|
|
const eslintCandidateFolder = path_1.default.dirname(require.resolve('eslint/package.json', { |
|
|
paths: [currentModule.path] |
|
|
})); |
|
|
|
|
|
|
|
|
if (currentModule.filename.startsWith(eslintCandidateFolder + path_1.default.sep)) { |
|
|
exports.eslintFolder = eslintFolder = eslintCandidateFolder; |
|
|
break; |
|
|
} |
|
|
} |
|
|
catch (ex) { |
|
|
|
|
|
|
|
|
|
|
|
if (!isModuleResolutionError(ex)) { |
|
|
throw ex; |
|
|
} |
|
|
} |
|
|
} |
|
|
if (!currentModule.parent) { |
|
|
break; |
|
|
} |
|
|
currentModule = currentModule.parent; |
|
|
} |
|
|
} |
|
|
if (!eslintFolder) { |
|
|
|
|
|
for (let currentModule = module;;) { |
|
|
if (!configArrayFactoryPath) { |
|
|
|
|
|
|
|
|
try { |
|
|
const eslintrcFolder = path_1.default.dirname(require.resolve('@eslint/eslintrc/package.json', { |
|
|
paths: [currentModule.path] |
|
|
})); |
|
|
const resolvedConfigArrayFactoryPath = path_1.default.join(eslintrcFolder, '/lib/config-array-factory.js'); |
|
|
if (resolvedConfigArrayFactoryPath === currentModule.filename) { |
|
|
configArrayFactoryPath = resolvedConfigArrayFactoryPath; |
|
|
moduleResolverPath = `${eslintrcFolder}/lib/shared/relative-module-resolver`; |
|
|
namingPath = `${eslintrcFolder}/lib/shared/naming`; |
|
|
} |
|
|
} |
|
|
catch (ex) { |
|
|
|
|
|
|
|
|
|
|
|
if (!isModuleResolutionError(ex)) { |
|
|
throw ex; |
|
|
} |
|
|
} |
|
|
} |
|
|
else if (currentModule.filename.endsWith('cli-engine.js')) { |
|
|
|
|
|
|
|
|
try { |
|
|
const eslintCandidateFolder = path_1.default.dirname(require.resolve('eslint/package.json', { |
|
|
paths: [currentModule.path] |
|
|
})); |
|
|
if (path_1.default.join(eslintCandidateFolder, 'lib/cli-engine/cli-engine.js') === currentModule.filename) { |
|
|
exports.eslintFolder = eslintFolder = eslintCandidateFolder; |
|
|
break; |
|
|
} |
|
|
} |
|
|
catch (ex) { |
|
|
|
|
|
|
|
|
|
|
|
if (!isModuleResolutionError(ex)) { |
|
|
throw ex; |
|
|
} |
|
|
} |
|
|
} |
|
|
if (!currentModule.parent) { |
|
|
break; |
|
|
} |
|
|
currentModule = currentModule.parent; |
|
|
} |
|
|
} |
|
|
if (!eslintFolder) { |
|
|
|
|
|
for (let currentModule = module;;) { |
|
|
|
|
|
|
|
|
if (/[\\/]eslint[\\/]lib[\\/]cli-engine[\\/]config-array-factory\.js$/i.test(currentModule.filename)) { |
|
|
exports.eslintFolder = eslintFolder = path_1.default.join(path_1.default.dirname(currentModule.filename), '../..'); |
|
|
configArrayFactoryPath = `${eslintFolder}/lib/cli-engine/config-array-factory`; |
|
|
moduleResolverPath = `${eslintFolder}/lib/shared/relative-module-resolver`; |
|
|
|
|
|
|
|
|
let eslintrcFolder; |
|
|
try { |
|
|
eslintrcFolder = path_1.default.dirname(require.resolve('@eslint/eslintrc/package.json', { |
|
|
paths: [currentModule.path] |
|
|
})); |
|
|
} |
|
|
catch (ex) { |
|
|
if (!isModuleResolutionError(ex)) { |
|
|
throw ex; |
|
|
} |
|
|
} |
|
|
namingPath = `${eslintrcFolder !== null && eslintrcFolder !== void 0 ? eslintrcFolder : eslintFolder}/lib/shared/naming`; |
|
|
break; |
|
|
} |
|
|
if (!currentModule.parent) { |
|
|
|
|
|
throw new Error('Failed to patch ESLint because the calling module was not recognized.\n' + |
|
|
'If you are using a newer ESLint version that may be unsupported, please create a GitHub issue:\n' + |
|
|
'https://github.com/microsoft/rushstack/issues'); |
|
|
} |
|
|
currentModule = currentModule.parent; |
|
|
} |
|
|
} |
|
|
|
|
|
const eslintPackageJsonPath = `${eslintFolder}/package.json`; |
|
|
const eslintPackageObject = require(eslintPackageJsonPath); |
|
|
exports.eslintPackageVersion = eslintPackageObject.version; |
|
|
const ESLINT_MAJOR_VERSION = parseInt(exports.eslintPackageVersion, 10); |
|
|
exports.ESLINT_MAJOR_VERSION = ESLINT_MAJOR_VERSION; |
|
|
if (isNaN(ESLINT_MAJOR_VERSION)) { |
|
|
throw new Error(`Unable to parse ESLint version "${exports.eslintPackageVersion}" in file "${eslintPackageJsonPath}"`); |
|
|
} |
|
|
if (!(ESLINT_MAJOR_VERSION >= 6 && ESLINT_MAJOR_VERSION <= 9)) { |
|
|
throw new Error('The ESLint patch script has only been tested with ESLint version 6.x, 7.x, 8.x, and 9.x.' + |
|
|
` (Your version: ${exports.eslintPackageVersion})\n` + |
|
|
'Consider reporting a GitHub issue:\n' + |
|
|
'https://github.com/microsoft/rushstack/issues'); |
|
|
} |
|
|
|
|
|
let configArrayFactory; |
|
|
if (ESLINT_MAJOR_VERSION >= 8 && eslintrcBundlePath) { |
|
|
exports.configArrayFactory = configArrayFactory = require(eslintrcBundlePath).Legacy.ConfigArrayFactory; |
|
|
} |
|
|
else if (configArrayFactoryPath) { |
|
|
exports.configArrayFactory = configArrayFactory = require(configArrayFactoryPath).ConfigArrayFactory; |
|
|
} |
|
|
|
|
|
let ModuleResolver; |
|
|
|
|
|
let Naming; |
|
|
if (ESLINT_MAJOR_VERSION >= 8 && eslintrcBundlePath) { |
|
|
exports.ModuleResolver = ModuleResolver = require(eslintrcBundlePath).Legacy.ModuleResolver; |
|
|
exports.Naming = Naming = require(eslintrcBundlePath).Legacy.naming; |
|
|
} |
|
|
else if (moduleResolverPath && namingPath) { |
|
|
exports.ModuleResolver = ModuleResolver = require(moduleResolverPath); |
|
|
exports.Naming = Naming = require(namingPath); |
|
|
} |
|
|
|