@rushstack/eslint-plugin
Version:
An ESLint plugin providing supplementary rules for use with the @rushstack/eslint-config package
121 lines • 5.85 kB
JavaScript
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.getFilePathFromContext = getFilePathFromContext;
exports.getRootDirectoryFromContext = getRootDirectoryFromContext;
exports.parseImportSpecifierFromExpression = parseImportSpecifierFromExpression;
exports.serializeImportSpecifier = serializeImportSpecifier;
exports.getImportPathFromExpression = getImportPathFromExpression;
exports.getImportAbsolutePathFromExpression = getImportAbsolutePathFromExpression;
const path = __importStar(require("path"));
const utils_1 = require("@typescript-eslint/utils");
// Regex to parse out the import target from the specifier. Expected formats are:
// - '<target>'
// - '<loader>!<target>'
// - '<target>?<loader-options>'
// - '<loader>!<target>?<loader-options>'
const LOADER_CAPTURE_GROUP = 'loader';
const IMPORT_TARGET_CAPTURE_GROUP = 'importTarget';
const LOADER_OPTIONS_CAPTURE_GROUP = 'loaderOptions';
// eslint-disable-next-line @rushstack/security/no-unsafe-regexp
const SPECIFIER_REGEX = new RegExp(`^((?<${LOADER_CAPTURE_GROUP}>(!|-!|!!).+)!)?` +
`(?<${IMPORT_TARGET_CAPTURE_GROUP}>[^!?]+)` +
`(\\?(?<${LOADER_OPTIONS_CAPTURE_GROUP}>.*))?$`);
function getFilePathFromContext(context) {
return context.physicalFilename || context.filename;
}
function getRootDirectoryFromContext(context) {
let rootDirectory;
try {
// First attempt to get the root directory from the tsconfig baseUrl, then the program current directory
const program = (context.sourceCode?.parserServices ?? utils_1.ESLintUtils.getParserServices(context)).program;
rootDirectory = program?.getCompilerOptions().baseUrl ?? program?.getCurrentDirectory();
}
catch {
// Ignore the error if we cannot retrieve a TS program
}
// Fall back to the parserOptions.tsconfigRootDir if available, otherwise the eslint working directory
if (!rootDirectory) {
rootDirectory = context.parserOptions?.tsconfigRootDir ?? context.getCwd?.();
}
return rootDirectory;
}
function parseImportSpecifierFromExpression(importExpression) {
if (!importExpression ||
importExpression.type !== utils_1.TSESTree.AST_NODE_TYPES.Literal ||
typeof importExpression.value !== 'string') {
// Can't determine the path of the import target, return
return undefined;
}
// Extract the target of the import, stripping out webpack loaders and query strings. The regex will
// also ensure that the import target is a relative path.
const specifierMatch = importExpression.value.match(SPECIFIER_REGEX);
if (!specifierMatch?.groups) {
// Can't determine the path of the import target, return
return undefined;
}
const loader = specifierMatch.groups[LOADER_CAPTURE_GROUP];
const importTarget = specifierMatch.groups[IMPORT_TARGET_CAPTURE_GROUP];
const loaderOptions = specifierMatch.groups[LOADER_OPTIONS_CAPTURE_GROUP];
return { loader, importTarget, loaderOptions };
}
function serializeImportSpecifier(parsedImportPath) {
const { loader, importTarget, loaderOptions } = parsedImportPath;
return `${loader ? `${loader}!` : ''}${importTarget}${loaderOptions ? `?${loaderOptions}` : ''}`;
}
function getImportPathFromExpression(importExpression, relativeImportsOnly = true) {
const parsedImportSpecifier = parseImportSpecifierFromExpression(importExpression);
if (!parsedImportSpecifier ||
(relativeImportsOnly && !parsedImportSpecifier.importTarget.startsWith('.'))) {
// The import target isn't a path, return
return undefined;
}
return parsedImportSpecifier?.importTarget;
}
function getImportAbsolutePathFromExpression(context, importExpression, relativeImportsOnly = true) {
const importPath = getImportPathFromExpression(importExpression, relativeImportsOnly);
if (importPath === undefined) {
// Can't determine the absolute path of the import target, return
return undefined;
}
const filePath = getFilePathFromContext(context);
const fileDirectory = path.dirname(filePath);
// Combine the import path with the absolute path of the file parent directory to get the
// absolute path of the import target
return path.resolve(fileDirectory, importPath);
}
//# sourceMappingURL=LintUtilities.js.map
;