eslint-plugin-vue
Version:
Official ESLint plugin for Vue.js
85 lines (84 loc) • 2.84 kB
JavaScript
const require_index = require("../utils/index.js");
// lib/rules/require-direct-export.ts
/**
* @fileoverview require the component to be directly exported
* @author Hiroki Osame <hiroki.osame@gmail.com>
*/
var require_direct_export_default = {
meta: {
type: "suggestion",
docs: {
description: "require the component to be directly exported",
categories: void 0,
url: "https://eslint.vuejs.org/rules/require-direct-export.html"
},
fixable: null,
schema: [{
type: "object",
properties: { disallowFunctionalComponentFunction: { type: "boolean" } },
additionalProperties: false
}],
messages: { expectedDirectExport: "Expected the component literal to be directly exported." }
},
create(context) {
const filePath = context.filename;
if (!require_index.default.isVueFile(filePath)) return {};
const disallowFunctional = (context.options[0] || {}).disallowFunctionalComponentFunction;
let maybeVue3Functional;
let scopeStack = null;
return {
"ExportDefaultDeclaration > *"(node) {
if (node.type === "ObjectExpression") return;
if (node.type === "CallExpression") {
const { callee } = node;
const [firstArg] = node.arguments;
if (firstArg && firstArg.type === "ObjectExpression" && (callee.type === "Identifier" && callee.name === "defineComponent" || callee.type === "MemberExpression" && callee.object.type === "Identifier" && callee.object.name === "Vue" && callee.property.type === "Identifier" && callee.property.name === "extend")) return;
}
if (!disallowFunctional) {
if (node.type === "ArrowFunctionExpression") {
if (node.body.type !== "BlockStatement") return;
maybeVue3Functional = {
body: node.body,
hasReturnArgument: false
};
return;
}
if (node.type === "FunctionExpression" || node.type === "FunctionDeclaration") {
maybeVue3Functional = {
body: node.body,
hasReturnArgument: false
};
return;
}
}
context.report({
node: node.parent,
messageId: "expectedDirectExport"
});
},
...disallowFunctional ? {} : {
":function > BlockStatement"(node) {
if (!maybeVue3Functional) return;
scopeStack = {
upper: scopeStack,
withinVue3FunctionalBody: maybeVue3Functional.body === node
};
},
ReturnStatement(node) {
if (scopeStack && scopeStack.withinVue3FunctionalBody && node.argument) maybeVue3Functional.hasReturnArgument = true;
},
":function > BlockStatement:exit"() {
scopeStack &&= scopeStack.upper;
},
"ExportDefaultDeclaration:exit"(node) {
if (!maybeVue3Functional) return;
if (!maybeVue3Functional.hasReturnArgument) context.report({
node,
messageId: "expectedDirectExport"
});
}
}
};
}
};
exports.default = require_direct_export_default;