airbnb / eslint-plugin-react-with-styles

ESLint plugin for react-with-styles
MIT License
49 stars 22 forks source link

Rule proposal: css argument types #4

Open lencioni opened 8 years ago

lencioni commented 8 years ago

The css() function can only take arguments that are properties of the styles prop object, plain JavaScript objects, falsey values, or arrays of those types.

Bad:

function MyComponent({ styles }) {
  return (
    <div {...css(styles)} />
  );
}
function MyComponent({ styles }) {
  return (
    <div {...css('foo')} />
  );
}
function MyComponent({ styles }) {
  return (
    <div {...css(1)} />
  );
}
function MyComponent({ styles }) {
  return (
    <div {...css(true)} />
  );
}
function MyComponent({ styles, foo }) {
  return (
    <div {...css(foo)} />
  );
}
function MyComponent({ styles, foo }) {
  return (
    <div {...css(foo.bar)} />
  );
}

Good:

function MyComponent({ styles }) {
  return (
    <div {...css(styles.foo, styles.bar, { color: 'red' })} />
  );
}

It might also make sense for this rule to validate the shape of the JavaScript objects passed to css() to ensure that they are flat objects appropriate for inline styles.

ljharb commented 8 years ago

Good:

function MyComponent({ styles: { foo, bar } }) {
  return (
    <div {...css(foo, bar, { color: 'red' })} />
  );
}