-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
Copy pathmodule_migration.js
148 lines (135 loc) · 4.04 KB
/
module_migration.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
const path = require('path');
const findKibanaRoot = require('../helpers/find_kibana_root');
const KIBANA_ROOT = findKibanaRoot();
function checkModuleNameNode(context, mappings, node, desc = 'Imported') {
const mapping = mappings.find(
(mapping) =>
mapping.from === node.value || (!mapping.exact && node.value.startsWith(`${mapping.from}/`))
);
if (!mapping) {
return;
}
let newSource;
if (mapping.to === false) {
context.report({
message: mapping.disallowedMessage || `Importing "${mapping.from}" is not allowed`,
loc: node.loc,
});
return;
}
// 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);
}
context.report({
message: `${desc} 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: {
anyOf: [
{
type: 'string',
},
{
const: false,
},
],
},
toRelative: {
type: 'string',
},
disallowedMessage: {
type: 'string',
},
include: {
type: 'array',
},
exclude: {
type: 'array',
},
exact: {
type: 'boolean',
default: false,
},
},
anyOf: [
{
required: ['from', 'to'],
},
{
required: ['from', 'toRelative'],
},
],
additionalProperties: false,
},
default: [],
minItems: 1,
},
],
},
create: (context) => {
const filename = path.relative(KIBANA_ROOT, context.getFilename());
const mappings = context.options[0].filter((mapping) => {
// exclude mapping rule if it is explicitly excluded from this file
if (mapping.exclude && mapping.exclude.some((p) => p.test(filename))) {
return false;
}
// if this mapping rule is only included in specific files, optionally include it
if (mapping.include) {
return mapping.include.some((p) => p.test(filename));
}
// include all mapping rules by default
return true;
});
return {
ImportDeclaration(node) {
checkModuleNameNode(context, mappings, node.source);
},
ExportNamedDeclaration(node) {
if (node.source) {
checkModuleNameNode(context, mappings, node.source, 'Re-exported');
}
},
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]);
}
},
};
},
};