@rushstack/eslint-plugin
Version:
An ESLint plugin providing supplementary rules for use with the @rushstack/eslint-config package
86 lines • 4.02 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.noExternalLocalImportsRule = exports.MESSAGE_ID = void 0;
const path = __importStar(require("path"));
const LintUtilities_1 = require("./LintUtilities");
exports.MESSAGE_ID = 'error-external-local-imports';
const _relativePathRegex = /^[.\/\\]+$/;
exports.noExternalLocalImportsRule = {
defaultOptions: [],
meta: {
type: 'problem',
messages: {
[exports.MESSAGE_ID]: 'The specified import target is not under the root directory. Ensure that ' +
'all local import targets are either under the "rootDir" specified in your tsconfig.json (if one ' +
'exists) or under the package directory.'
},
schema: [],
docs: {
description: 'Prevents referencing relative imports that are either not under the "rootDir" specified in ' +
'the tsconfig.json (if one exists) or not under the package directory.',
url: 'https://www.npmjs.com/package/@rushstack/eslint-plugin'
}
},
create: (context) => {
const rootDirectory = (0, LintUtilities_1.getRootDirectoryFromContext)(context);
const checkImportExpression = (importExpression) => {
if (!importExpression || !rootDirectory) {
// Can't validate, return
return;
}
// Get the relative path between the target and the root. If the target is under the root, then the resulting
// relative path should be a series of "../" segments.
const importAbsolutePath = (0, LintUtilities_1.getImportAbsolutePathFromExpression)(context, importExpression);
if (!importAbsolutePath) {
// Can't validate, return
return;
}
const relativePathToRoot = path.relative(importAbsolutePath, rootDirectory);
if (!_relativePathRegex.test(relativePathToRoot)) {
context.report({ node: importExpression, messageId: exports.MESSAGE_ID });
}
};
return {
ImportDeclaration: (node) => checkImportExpression(node.source),
ImportExpression: (node) => checkImportExpression(node.source),
ExportAllDeclaration: (node) => checkImportExpression(node.source),
ExportNamedDeclaration: (node) => checkImportExpression(node.source)
};
}
};
//# sourceMappingURL=no-external-local-imports.js.map