UNPKG

eslint-plugin-styled-components-a11y

Version:

This plugin adds the ability to lint styled components according to the rules outlined in eslint-plugin-jsx-a11y.

50 lines (48 loc) 2.11 kB
"use strict"; var mergeStyledAttrsWithNodeAttrs = require('../mergeStyledAttrsWithNodeAttrs'); var getAsProp = require('../getAsProp'); var _require = require('util'), inspect = _require.inspect; module.exports = function (context, styledComponents, rule, name) { return { JSXElement: function JSXElement(node) { var func = function func(inspectee) { return name.includes('scope') && context.report(node, inspect(inspectee || node)); }; try { var elementName = node.openingElement.name.name; if (!elementName && node.openingElement.name.type === 'JSXMemberExpression') { elementName = "".concat(node.openingElement.name.object.name, ".").concat(node.openingElement.name.property.name); } var styledComponent = styledComponents[elementName]; if (styledComponent) { var tag = styledComponent.tag, attrs = styledComponent.attrs; var originalNodeAttr = node.openingElement.attributes; var originalNodeName = node.openingElement.name; try { var allAttrs = mergeStyledAttrsWithNodeAttrs(attrs, originalNodeAttr); var asProp = getAsProp(allAttrs); node.openingElement.attributes = allAttrs; // Convert JSXMemberExpression to JSXIdentifier, so it'll be properly handled by eslint-plugin-jsx-a11y plugin node.openingElement.name = { type: 'JSXIdentifier', name: asProp || tag, start: originalNodeName.start, end: originalNodeName.end, loc: originalNodeName.loc, range: originalNodeName.range, parent: originalNodeName.parent }; // if we haven't discovered what type of tag the component is based off, bail if (!(asProp || tag)) return; rule.create(context).JSXElement(node); } finally { node.openingElement.name = originalNodeName; node.openingElement.attributes = originalNodeAttr; } } } catch (_unused) {} } }; };