@builder.io/eslint-plugin-mitosis
Version:
A Mitosis plugin containing rules that help you write valid and idiomatic Mitosis code
68 lines (67 loc) • 2.9 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var types = require("@babel/types");
var hooks_1 = require("../constants/hooks");
var isMitosisPath_1 = require("../helpers/isMitosisPath");
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
var rule = {
meta: {
type: 'problem',
docs: {
description: 'disallow defining async methods as a state property',
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 = {
CallExpression: function (node) {
var program = context.getAncestors()[0];
if (!types.isProgram(program))
return;
var importSpecifiers = program.body.find(function (n) { return types.isImportDeclaration(n); });
if (!types.isImportDeclaration(importSpecifiers))
return;
var useState = importSpecifiers.specifiers.find(function (n) {
if (types.isImportSpecifier(n) &&
(n.imported.name === hooks_1.HOOKS.STATE || n.imported.name === hooks_1.HOOKS.STORE)) {
return true;
}
});
if (!useState ||
!types.isIdentifier(node.callee) ||
!types.isObjectExpression(node.arguments[0]))
return;
for (var _i = 0, _a = node.arguments[0].properties; _i < _a.length; _i++) {
var prop = _a[_i];
if (!types.isProperty(prop) ||
!types.isIdentifier(prop.key) ||
!types.isFunctionExpression(prop.value))
continue;
var async = prop.value.async;
if (async) {
context.report({
node: prop,
message: 'async methods can\'t be defined on "state"',
});
}
}
},
};
return listener;
},
};
exports.default = rule;