no-inline-styles.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /**
  2. * @fileoverview Detects inline styles
  3. * @author Aaron Greenwald
  4. */
  5. 'use strict';
  6. const util = require('util');
  7. const Components = require('../util/Components');
  8. const styleSheet = require('../util/stylesheet');
  9. const { StyleSheets } = styleSheet;
  10. const { astHelpers } = styleSheet;
  11. const create = Components.detect((context) => {
  12. const styleSheets = new StyleSheets();
  13. function reportInlineStyles(inlineStyles) {
  14. if (inlineStyles) {
  15. inlineStyles.forEach((style) => {
  16. if (style) {
  17. const expression = util.inspect(style.expression);
  18. context.report({
  19. node: style.node,
  20. message: 'Inline style: {{expression}}',
  21. data: { expression },
  22. });
  23. }
  24. });
  25. }
  26. }
  27. return {
  28. JSXAttribute: (node) => {
  29. if (astHelpers.isStyleAttribute(node)) {
  30. const styles = astHelpers.collectStyleObjectExpressions(node.value, context);
  31. styleSheets.addObjectExpressions(styles);
  32. }
  33. },
  34. 'Program:exit': () => reportInlineStyles(styleSheets.getObjectExpressions()),
  35. };
  36. });
  37. module.exports = {
  38. meta: {
  39. schema: [],
  40. },
  41. create,
  42. };