@atlaskit/eslint-plugin-design-system
Version:
The essential plugin for use with the Atlassian Design System.
72 lines (68 loc) • 4.27 kB
JavaScript
function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t.return || t.return(); } finally { if (u) throw o; } } }; }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
/* eslint-disable @repo/internal/react/require-jsdoc */
import { isNodeOfType } from 'eslint-codemod-utils';
import { isPropertyName } from './common';
export var RestrictedProperty = {
lint: function lint(node, _ref) {
var context = _ref.context,
config = _ref.config;
if (RestrictedProperty._check(node, {
context: context,
config: config
})) {
var property = 'fontSize, lineHeight, fontWeight or letterSpacing';
if (isNodeOfType(node, 'Identifier')) {
property = node.name;
} else if (isNodeOfType(node, 'Literal')) {
property = String(node.value);
}
context.report({
node: node,
messageId: property === 'fontWeight' ? 'noRestrictedTypographyPropertiesHeading' : 'noRestrictedTypographyProperties',
data: {
property: property
}
});
}
},
_check: function _check(node, _ref2) {
var config = _ref2.config;
if (!config.patterns.includes('restricted-property')) {
return false;
}
// Prevent font weight being used in combination with heading tokens
if (isPropertyName(node, 'fontWeight')) {
if (isNodeOfType(node.parent.parent, 'ObjectExpression')) {
var _iterator = _createForOfIteratorHelper(node.parent.parent.properties),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var _property$value$value;
var property = _step.value;
// Only looking for heading token on `font` property
var isFontProperty = isNodeOfType(property, 'Property') && (isNodeOfType(property.key, 'Literal') && property.key.value === 'font' || isNodeOfType(property.key, 'Identifier') && property.key.name === 'font');
if (!isFontProperty) {
continue;
}
// Checking for heading token string, for example xcss({ font: 'font.heading.medium' })
if (isNodeOfType(property.value, 'Literal') && typeof property.value.value === 'string' && (_property$value$value = property.value.value) !== null && _property$value$value !== void 0 && _property$value$value.startsWith('font.heading')) {
return true;
}
// Checking for wrapped heading token, for example xcss({ font: token('font.heading.medium') })
if (isNodeOfType(property.value, 'CallExpression') && isNodeOfType(property.value.callee, 'Identifier') && property.value.callee.name === 'token' && isNodeOfType(property.value.arguments[0], 'Literal') && typeof property.value.arguments[0].value === 'string' && property.value.arguments[0].value.startsWith('font.heading')) {
return true;
}
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
}
return false;
}
return true;
}
};