| 1 |
const isBabelPluginFactory = require("../utils/is-babel-plugin-factory.cjs"); |
| 2 |
|
| 3 |
function getReturnValue(node) { |
| 4 |
const { body } = node; |
| 5 |
|
| 6 |
if (body.type === "BlockStatement") { |
| 7 |
const returnNode = body.body.find(n => n.type === "ReturnStatement"); |
| 8 |
return returnNode && returnNode.argument; |
| 9 |
} |
| 10 |
|
| 11 |
|
| 12 |
return body; |
| 13 |
} |
| 14 |
|
| 15 |
module.exports = { |
| 16 |
meta: { |
| 17 |
schema: [], |
| 18 |
}, |
| 19 |
create(context ) { |
| 20 |
let pluginFound = false; |
| 21 |
|
| 22 |
return { |
| 23 |
FunctionDeclaration: functionVisitor, |
| 24 |
FunctionExpression: functionVisitor, |
| 25 |
ArrowFunctionExpression: functionVisitor, |
| 26 |
|
| 27 |
"Program:exit"(node) { |
| 28 |
if (!pluginFound) { |
| 29 |
context.report(node, "This file does not export a Babel plugin."); |
| 30 |
} |
| 31 |
}, |
| 32 |
}; |
| 33 |
|
| 34 |
function functionVisitor(node) { |
| 35 |
if (!isBabelPluginFactory(node, context.getScope())) return; |
| 36 |
|
| 37 |
const returnValue = getReturnValue(node); |
| 38 |
if (!returnValue || returnValue.type !== "ObjectExpression") return; |
| 39 |
|
| 40 |
pluginFound = true; |
| 41 |
|
| 42 |
if (!returnValue.properties.some(p => p.key.name === "name")) { |
| 43 |
context.report( |
| 44 |
returnValue, |
| 45 |
"This Babel plugin doesn't have a 'name' property.", |
| 46 |
); |
| 47 |
} |
| 48 |
} |
| 49 |
}, |
| 50 |
}; |