2019-03-25 17:56:48 +01:00
|
|
|
/*
|
2021-01-20 02:52:56 +01:00
|
|
|
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
|
|
|
* or more contributor license agreements. Licensed under the Elastic License
|
|
|
|
* and the Server Side Public License, v 1; you may not use this file except in
|
|
|
|
* compliance with, at your election, the Elastic License or the Server Side
|
|
|
|
* Public License, v 1.
|
2019-03-25 17:56:48 +01:00
|
|
|
*/
|
|
|
|
|
2019-04-05 18:45:23 +02:00
|
|
|
const path = require('path');
|
|
|
|
const KIBANA_ROOT = path.resolve(__dirname, '../../..');
|
|
|
|
|
2019-03-25 17:56:48 +01:00
|
|
|
function checkModuleNameNode(context, mappings, node) {
|
|
|
|
const mapping = mappings.find(
|
2020-05-22 09:08:58 +02:00
|
|
|
(mapping) => mapping.from === node.value || node.value.startsWith(`${mapping.from}/`)
|
2019-03-25 17:56:48 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
if (!mapping) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-05 18:45:23 +02:00
|
|
|
let newSource;
|
|
|
|
|
2019-10-04 17:15:06 +02:00
|
|
|
if (mapping.to === false) {
|
|
|
|
context.report({
|
|
|
|
message: mapping.disallowedMessage || `Importing "${mapping.from}" is not allowed`,
|
|
|
|
loc: node.loc,
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-05 18:45:23 +02:00
|
|
|
// support for toRelative added to migrate away from X-Pack being bundled
|
|
|
|
// within node modules. after that migration, this can be removed.
|
|
|
|
if (mapping.toRelative) {
|
|
|
|
const sourceDirectory = path.dirname(context.getFilename());
|
|
|
|
const localModulePath = node.value.replace(new RegExp(`^${mapping.from}\/`), '');
|
|
|
|
const modulePath = path.resolve(KIBANA_ROOT, mapping.toRelative, localModulePath);
|
|
|
|
const relativePath = path.relative(sourceDirectory, modulePath);
|
|
|
|
|
|
|
|
newSource = relativePath.startsWith('.') ? relativePath : `./${relativePath}`;
|
|
|
|
} else {
|
|
|
|
newSource = node.value.replace(mapping.from, mapping.to);
|
|
|
|
}
|
|
|
|
|
2019-03-25 17:56:48 +01:00
|
|
|
context.report({
|
|
|
|
message: `Imported module "${node.value}" should be "${newSource}"`,
|
|
|
|
loc: node.loc,
|
|
|
|
fix(fixer) {
|
|
|
|
return fixer.replaceText(node, `'${newSource}'`);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
meta: {
|
|
|
|
fixable: 'code',
|
|
|
|
schema: [
|
|
|
|
{
|
|
|
|
type: 'array',
|
|
|
|
items: {
|
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
from: {
|
|
|
|
type: 'string',
|
|
|
|
},
|
|
|
|
to: {
|
2019-10-04 17:15:06 +02:00
|
|
|
anyOf: [
|
|
|
|
{
|
|
|
|
type: 'string',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
const: false,
|
|
|
|
},
|
|
|
|
],
|
2019-03-25 17:56:48 +01:00
|
|
|
},
|
2019-04-05 18:45:23 +02:00
|
|
|
toRelative: {
|
|
|
|
type: 'string',
|
|
|
|
},
|
2019-10-04 17:15:06 +02:00
|
|
|
disallowedMessage: {
|
|
|
|
type: 'string',
|
|
|
|
},
|
2019-03-25 17:56:48 +01:00
|
|
|
},
|
2019-04-05 18:45:23 +02:00
|
|
|
anyOf: [
|
|
|
|
{
|
|
|
|
required: ['from', 'to'],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
required: ['from', 'toRelative'],
|
|
|
|
},
|
|
|
|
],
|
2019-03-25 17:56:48 +01:00
|
|
|
additionalProperties: false,
|
|
|
|
},
|
|
|
|
default: [],
|
|
|
|
minItems: 1,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2020-05-22 09:08:58 +02:00
|
|
|
create: (context) => {
|
2019-03-25 17:56:48 +01:00
|
|
|
const mappings = context.options[0];
|
|
|
|
|
|
|
|
return {
|
|
|
|
ImportDeclaration(node) {
|
|
|
|
checkModuleNameNode(context, mappings, node.source);
|
|
|
|
},
|
|
|
|
CallExpression(node) {
|
|
|
|
if (
|
|
|
|
node.callee.type === 'Identifier' &&
|
|
|
|
node.callee.name === 'require' &&
|
|
|
|
node.arguments.length === 1 &&
|
|
|
|
node.arguments[0].type === 'Literal'
|
|
|
|
) {
|
|
|
|
checkModuleNameNode(context, mappings, node.arguments[0]);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
},
|
|
|
|
};
|