forked from babel/babel
/
plugin-name.cjs
50 lines (40 loc) · 1.23 KB
/
plugin-name.cjs
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
const isBabelPluginFactory = require("../utils/is-babel-plugin-factory.cjs");
function getReturnValue(node) {
const { body } = node;
if (body.type === "BlockStatement") {
const returnNode = body.body.find(n => n.type === "ReturnStatement");
return returnNode && returnNode.argument;
}
// Arrow functions with implicit return
return body;
}
module.exports = {
meta: {
schema: [],
},
create(context /*: Context */) {
let pluginFound = false;
return {
FunctionDeclaration: functionVisitor,
FunctionExpression: functionVisitor,
ArrowFunctionExpression: functionVisitor,
"Program:exit"(node) {
if (!pluginFound) {
context.report(node, "This file does not export a Babel plugin.");
}
},
};
function functionVisitor(node) {
if (!isBabelPluginFactory(node, context.getScope())) return;
const returnValue = getReturnValue(node);
if (!returnValue || returnValue.type !== "ObjectExpression") return;
pluginFound = true;
if (!returnValue.properties.some(p => p.key.name === "name")) {
context.report(
returnValue,
"This Babel plugin doesn't have a 'name' property.",
);
}
}
},
};