2019-03-29 08:48:32 +01:00
|
|
|
/* eslint-disable-line @kbn/eslint/require-license-header */
|
|
|
|
/*
|
|
|
|
* This product uses import/no-restricted-paths which is available under a
|
|
|
|
* "MIT" license.
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2015-present, Ben Mosher
|
|
|
|
* https://github.com/benmosher/eslint-plugin-import
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
const path = require('path');
|
|
|
|
const resolve = require('eslint-module-utils/resolve').default;
|
|
|
|
const mm = require('micromatch');
|
|
|
|
|
|
|
|
function isStaticRequire(node) {
|
|
|
|
return (
|
|
|
|
node &&
|
|
|
|
node.callee &&
|
|
|
|
node.callee.type === 'Identifier' &&
|
|
|
|
node.callee.name === 'require' &&
|
|
|
|
node.arguments.length === 1 &&
|
|
|
|
node.arguments[0].type === 'Literal' &&
|
|
|
|
typeof node.arguments[0].value === 'string'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function traverseToTopFolder(src, pattern) {
|
|
|
|
while (mm([src], pattern).length > 0) {
|
|
|
|
const srcIdx = src.lastIndexOf(path.sep);
|
|
|
|
src = src.slice(0, srcIdx);
|
|
|
|
}
|
|
|
|
return src;
|
|
|
|
}
|
|
|
|
|
|
|
|
function isSameFolderOrDescendent(src, imported, pattern) {
|
2019-04-10 14:56:09 +02:00
|
|
|
// to allow to exclude file by name in pattern (e.g., !**/index*) we start with file dirname and then traverse
|
|
|
|
const srcFileFolderRoot = traverseToTopFolder(path.dirname(src), pattern);
|
|
|
|
const importedFileFolderRoot = traverseToTopFolder(path.dirname(imported), pattern);
|
|
|
|
|
2019-03-29 08:48:32 +01:00
|
|
|
return srcFileFolderRoot === importedFileFolderRoot;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
meta: {
|
|
|
|
schema: [
|
|
|
|
{
|
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
zones: {
|
|
|
|
type: 'array',
|
|
|
|
minItems: 1,
|
|
|
|
items: {
|
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
target: {
|
|
|
|
anyOf: [{ type: 'string' }, { type: 'array', items: { type: 'string' } }],
|
|
|
|
},
|
|
|
|
from: {
|
|
|
|
anyOf: [{ type: 'string' }, { type: 'array', items: { type: 'string' } }],
|
|
|
|
},
|
|
|
|
allowSameFolder: { type: 'boolean' },
|
2019-10-22 01:32:31 +02:00
|
|
|
errorMessage: { type: 'string' },
|
2019-03-29 08:48:32 +01:00
|
|
|
},
|
|
|
|
additionalProperties: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
basePath: { type: 'string' },
|
|
|
|
},
|
|
|
|
additionalProperties: false,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
|
|
|
|
create(context) {
|
|
|
|
const options = context.options[0] || {};
|
|
|
|
const zones = options.zones || [];
|
|
|
|
const basePath = options.basePath;
|
|
|
|
if (!basePath || !path.isAbsolute(basePath)) {
|
|
|
|
throw new Error('basePath option must be specified and must be absolute');
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkForRestrictedImportPath(importPath, node) {
|
2019-07-18 22:52:13 +02:00
|
|
|
const absoluteImportPath = importPath[0] === '.' ? resolve(importPath, context) : undefined;
|
2019-03-29 08:48:32 +01:00
|
|
|
|
|
|
|
const currentFilename = context.getFilename();
|
2019-07-18 22:52:13 +02:00
|
|
|
for (const { target, from, allowSameFolder, errorMessage = '' } of zones) {
|
2019-03-29 08:48:32 +01:00
|
|
|
const srcFilePath = resolve(currentFilename, context);
|
|
|
|
|
|
|
|
const relativeSrcFile = path.relative(basePath, srcFilePath);
|
2019-07-18 22:52:13 +02:00
|
|
|
const relativeImportFile = absoluteImportPath
|
|
|
|
? path.relative(basePath, absoluteImportPath)
|
|
|
|
: importPath;
|
2019-03-29 08:48:32 +01:00
|
|
|
|
|
|
|
if (
|
|
|
|
!mm([relativeSrcFile], target).length ||
|
|
|
|
!mm([relativeImportFile], from).length ||
|
|
|
|
(allowSameFolder && isSameFolderOrDescendent(relativeSrcFile, relativeImportFile, from))
|
|
|
|
)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
context.report({
|
|
|
|
node,
|
2019-07-18 22:52:13 +02:00
|
|
|
message: `Unexpected path "${importPath}" imported in restricted zone.${
|
|
|
|
errorMessage ? ' ' + errorMessage : ''
|
|
|
|
}`,
|
2019-03-29 08:48:32 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2020-05-15 17:21:42 +02:00
|
|
|
ExportNamedDeclaration(node) {
|
|
|
|
if (!node.source) return;
|
|
|
|
checkForRestrictedImportPath(node.source.value, node.source);
|
|
|
|
},
|
2019-03-29 08:48:32 +01:00
|
|
|
ImportDeclaration(node) {
|
|
|
|
checkForRestrictedImportPath(node.source.value, node.source);
|
|
|
|
},
|
|
|
|
CallExpression(node) {
|
|
|
|
if (isStaticRequire(node)) {
|
|
|
|
const [firstArgument] = node.arguments;
|
|
|
|
|
|
|
|
checkForRestrictedImportPath(firstArgument.value, firstArgument);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
},
|
|
|
|
};
|