UNPKG

@builder.io/eslint-plugin-mitosis

Version:

A Mitosis plugin containing rules that help you write valid and idiomatic Mitosis code

64 lines (63 loc) 3.23 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.onlyDefaultFunctionAndImportsMessage = void 0; var types = require("@babel/types"); var hooks_1 = require("../constants/hooks"); var isMitosisPath_1 = require("../helpers/isMitosisPath"); // ------------------------------------------------------------------------------ // Rule Definition // ------------------------------------------------------------------------------ exports.onlyDefaultFunctionAndImportsMessage = 'Mitosis component files should only contain import declarations, the component itself (in a default export), module-scope hooks, and type declarations'; var rule = { meta: { type: 'problem', docs: { description: 'disallow anything other than import declarations, the component itself (in a default export), module-scope hooks, and type declarations inside the component file.', recommended: true, }, }, create: function (context) { // variables should be defined here var filename = context.getFilename(); if (!(0, isMitosisPath_1.default)(filename)) return {}; // ---------------------------------------------------------------------- // Helpers // ---------------------------------------------------------------------- // any helper functions should go here or else delete this section // ---------------------------------------------------------------------- // Public // ---------------------------------------------------------------------- // var listener = { Program: function (node) { var body = node.body; for (var _i = 0, body_1 = body; _i < body_1.length; _i++) { var child = body_1[_i]; if (!types.isImportDeclaration(child) && !types.isExportDefaultDeclaration(child) && !types.isTypeAlias(child) && !types.isInterfaceDeclaration(child) && !types.isTSInterfaceDeclaration(child) && !types.isTSTypeAliasDeclaration(child) && !(types.isExportNamedDeclaration(child) && types.isTSInterfaceDeclaration(child.declaration)) && !(types.isExportNamedDeclaration(child) && types.isTSTypeAliasDeclaration(child.declaration)) && (!types.isExpressionStatement(child) || !types.isCallExpression(child.expression) || !types.isIdentifier(child.expression.callee) || (child.expression.callee.name !== hooks_1.HOOKS.META_DATA && child.expression.callee.name !== hooks_1.HOOKS.DEFAULT_PROPS))) { context.report({ node: child, message: exports.onlyDefaultFunctionAndImportsMessage, }); } } }, }; return listener; }, }; exports.default = rule;