@atlaskit/eslint-plugin-design-system
Version:
The essential plugin for use with the Atlassian Design System.
67 lines (64 loc) • 2.17 kB
JavaScript
import { isNodeOfType, literal } from 'eslint-codemod-utils';
var HelperJSXAttribute = {
getName: function getName(node) {
if (!isNodeOfType(node, 'JSXAttribute')) {
throw new Error('Not a JSXAttribute');
}
if (!isNodeOfType(node.name, 'JSXIdentifier')) {
throw new Error('name is not a JSXIdentifier');
}
return node.name.name;
},
updateName: function updateName(node, name, fixer) {
if (!isNodeOfType(node, 'JSXAttribute')) {
throw new Error('Not a JSXAttribute');
}
if (!isNodeOfType(node.name, 'JSXIdentifier')) {
throw new Error('name is not a JSXIdentifier');
}
return fixer.replaceText(node.name, literal(name).toString());
},
/**
* A JSXAttribute value can be many things:
* - css='myStyles'
* - css={myStyles}
* - css={[styles1, styles2]}
* - header={<></>}
* - css={styleMap.header}
* - css={...styles}
*
* Currently, `getValue` has only implemented strategies for when the value is a string, or an ExpressionStatement
* If you need additional functionality add it, and set the correct `type` on the returned object
*/
getValue: function getValue(node) {
if (!isNodeOfType(node, 'JSXAttribute')) {
return;
}
if (!node.value) {
return;
}
// handle `css={myStyles}`
if (isNodeOfType(node.value, 'JSXExpressionContainer') && isNodeOfType(node.value.expression, 'Identifier')) {
return {
type: 'ExpressionStatement',
value: node.value.expression.name
};
}
// handle `css={true}`
if (isNodeOfType(node.value, 'JSXExpressionContainer') && isNodeOfType(node.value.expression, 'Literal')) {
return {
type: 'ExpressionStatement Literal',
value: node.value.expression.value
};
}
// handle `css='myStyles'`
if (isNodeOfType(node.value, 'Literal') && node.value.value) {
var _node$value$value;
return {
type: 'Literal',
value: (_node$value$value = node.value.value) === null || _node$value$value === void 0 ? void 0 : _node$value$value.toString()
};
}
}
};
export { HelperJSXAttribute as JSXAttribute };