@atlaskit/eslint-plugin-design-system
Version:
The essential plugin for use with the Atlassian Design System.
105 lines (100 loc) • 2.99 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.StyleProperty = void 0;
var _eslintCodemodUtils = require("eslint-codemod-utils");
var _contextCompat = require("@atlaskit/eslint-utils/context-compat");
var _import = require("../../../../ast-nodes/import");
var _objectEntry = require("../../../../ast-nodes/object-entry");
var _root = require("../../../../ast-nodes/root");
var _styleMap = require("./style-map");
var _supported = _interopRequireDefault(require("./supported"));
/* eslint-disable @repo/internal/react/require-jsdoc */
var messageId = 'noRawSpacingValues';
var StyleProperty = exports.StyleProperty = {
lint: function lint(node, _ref) {
var context = _ref.context;
var _StyleProperty$_check = StyleProperty._check(node, {
context: context
}),
success = _StyleProperty$_check.success,
ref = _StyleProperty$_check.ref;
if (!success) {
return;
}
context.report({
node: ref,
messageId: messageId
});
},
_check: function _check(node, _ref2) {
var context = _ref2.context;
if (!(0, _eslintCodemodUtils.isNodeOfType)(node, 'Property')) {
return {
success: false,
ref: undefined
};
}
var importDeclarations = _root.Root.findImportsByModule((0, _contextCompat.getSourceCode)(context).ast.body, '@atlaskit/primitives');
var isXcssImported = importDeclarations.some(function (importDeclaration) {
return _import.Import.containsNamedSpecifier(importDeclaration, 'xcss');
});
if (!isXcssImported) {
return {
success: false,
ref: undefined
};
}
/**
* Currently, we support values like:
* ```
* xcss({
* margin: '8px', // value.type is Literal
* })
* ```
*
* More complex code, like:
* ```
* xcss({
* margin: condition ? 'space.100' : 'space.200',
* })
* ```
* is too difficult to lint
*/
if (!(0, _eslintCodemodUtils.isNodeOfType)(node.value, 'Literal')) {
return {
success: false,
ref: undefined
};
}
var _ObjectEntryHelper$ge = _objectEntry.ObjectEntry.getProperty(node),
property = _ObjectEntryHelper$ge.value;
// Bail if the property is not `padding`, `margin`, etc
if (!property || !_styleMap.styleMap.includes(property)) {
return {
success: false,
ref: undefined
};
}
var value = _objectEntry.ObjectEntry.getValue(node);
if (typeof value !== 'string') {
return {
success: false,
ref: undefined
};
}
// There are valid values to ignore, such as tokens, or `margin: auto`
if (_supported.default.values.ignore.includes(value)) {
return {
success: false,
ref: undefined
};
}
return {
success: true,
ref: node
};
}
};