@atlaskit/eslint-plugin-design-system
Version:
The essential plugin for use with the Atlassian Design System.
98 lines (94 loc) • 2.64 kB
JavaScript
/* eslint-disable @repo/internal/react/require-jsdoc */
import { isNodeOfType } from 'eslint-codemod-utils';
import { getSourceCode } from '@atlaskit/eslint-utils/context-compat';
import { Import } from '../../../../ast-nodes/import';
import { ObjectEntry as ObjectEntryHelper } from '../../../../ast-nodes/object-entry';
import { Root } from '../../../../ast-nodes/root';
import { styleMap } from './style-map';
import supported from './supported';
var messageId = 'noRawSpacingValues';
export var 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 (!isNodeOfType(node, 'Property')) {
return {
success: false,
ref: undefined
};
}
var importDeclarations = Root.findImportsByModule(getSourceCode(context).ast.body, '@atlaskit/primitives');
var isXcssImported = importDeclarations.some(function (importDeclaration) {
return 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 (!isNodeOfType(node.value, 'Literal')) {
return {
success: false,
ref: undefined
};
}
var _ObjectEntryHelper$ge = ObjectEntryHelper.getProperty(node),
property = _ObjectEntryHelper$ge.value;
// Bail if the property is not `padding`, `margin`, etc
if (!property || !styleMap.includes(property)) {
return {
success: false,
ref: undefined
};
}
var value = ObjectEntryHelper.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.values.ignore.includes(value)) {
return {
success: false,
ref: undefined
};
}
return {
success: true,
ref: node
};
}
};