@builder.io/eslint-plugin-mitosis
Version:
A Mitosis plugin containing rules that help you write valid and idiomatic Mitosis code
65 lines (64 loc) • 3.32 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var types = require("@babel/types");
var isMitosisPath_1 = require("../helpers/isMitosisPath");
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
var rule = {
meta: {
type: 'problem',
docs: {
description: 'disallow defining setters with the same name 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 = {
Property: function (node) {
var _a, _b, _c, _d, _e, _f;
if (types.isIdentifier(node.key) &&
types.isCallExpression((_a = node.parent) === null || _a === void 0 ? void 0 : _a.parent) &&
types.isIdentifier((_c = (_b = node.parent) === null || _b === void 0 ? void 0 : _b.parent) === null || _c === void 0 ? void 0 : _c.callee) &&
((_f = (_e = (_d = node.parent) === null || _d === void 0 ? void 0 : _d.parent) === null || _e === void 0 ? void 0 : _e.callee) === null || _f === void 0 ? void 0 : _f.name) === 'useStore') {
var stateKeys = types.isObjectExpression(node.parent)
? node.parent.properties.map(function (property) {
if (property.type === 'Property' && types.isIdentifier(property.key)) {
return property.key.name;
}
return undefined;
})
: [];
var propName = node.key.name;
var isSetter = propName.startsWith('set') &&
propName.length > 3 &&
propName[3] === propName[3].toUpperCase();
if (!isSetter)
return;
var strippedStateName = propName.replace(/^set/, '');
var lowercasedStateName = strippedStateName.charAt(0).toLowerCase() + strippedStateName.slice(1);
if (stateKeys.includes(lowercasedStateName)) {
context.report({
node: node,
message: "Cannot name a state property `".concat(propName, "` because of a collision with Mitosis-generated code for the state property `").concat(lowercasedStateName, "`. Please use a different name."),
});
}
}
},
};
return listener;
},
};
exports.default = rule;