@builder.io/eslint-plugin-mitosis
Version:
A Mitosis plugin containing rules that help you write valid and idiomatic Mitosis code
100 lines (99 loc) • 4.07 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var types = require("@babel/types");
var ts_pattern_1 = require("ts-pattern");
var isMitosisPath_1 = require("../helpers/isMitosisPath");
var noOp_1 = require("../helpers/noOp");
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
var rule = {
meta: {
type: 'problem',
docs: {
description: 'disallow variables as a value for the css attribute.',
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 = {
JSXAttribute: function (node) {
(0, ts_pattern_1.match)(node)
.with({
name: {
name: 'css',
value: {
expression: {
type: 'ObjectExpression',
properties: [],
},
},
},
}, noOp_1.default)
.with({
name: {
name: 'css',
},
value: {
expression: {
type: 'ObjectExpression',
},
},
}, function (_a) {
var expression = _a.value.expression;
var properties = expression.properties;
for (var _i = 0, properties_1 = properties; _i < properties_1.length; _i++) {
var prop = properties_1[_i];
if (prop.value && types.isIdentifier(prop.value)) {
context.report({
node: prop,
message: "Css properties can't be a variable",
});
}
else if (prop.value && types.isConditionalExpression(prop.value)) {
context.report({
node: prop,
message: "Css properties can't be a ternary expression",
});
}
else if (prop.value && types.isMemberExpression(prop.value)) {
context.report({
node: prop,
message: "Css properties can't be a member expression",
});
}
}
})
.with({
name: {
name: 'css',
},
value: {
expression: (0, ts_pattern_1.not)((0, ts_pattern_1.when)(types.isObjectExpression)),
},
}, function (_a) {
var expression = _a.value.expression;
context.report({
node: expression,
message: 'Css attribute value must be an object',
});
})
.otherwise(noOp_1.default);
},
};
return listener;
},
};
exports.default = rule;