123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- /**
- * @fileoverview Enforce style prop value is an object
- * @author David Petersen
- */
- 'use strict';
- // ------------------------------------------------------------------------------
- // Requirements
- // ------------------------------------------------------------------------------
- const rule = require('../../../lib/rules/style-prop-object');
- const RuleTester = require('eslint').RuleTester;
- const parserOptions = {
- ecmaVersion: 2018,
- sourceType: 'module',
- ecmaFeatures: {
- jsx: true
- }
- };
- // ------------------------------------------------------------------------------
- // Tests
- // ------------------------------------------------------------------------------
- const ruleTester = new RuleTester({parserOptions});
- ruleTester.run('style-prop-object', rule, {
- valid: [
- {
- code: '<div style={{ color: "red" }} />'
- },
- {
- code: '<Hello style={{ color: "red" }} />'
- },
- {
- code: [
- 'function redDiv() {',
- ' const styles = { color: "red" };',
- ' return <div style={styles} />;',
- '}'
- ].join('\n')
- },
- {
- code: [
- 'function redDiv() {',
- ' const styles = { color: "red" };',
- ' return <Hello style={styles} />;',
- '}'
- ].join('\n')
- },
- {
- code: [
- 'const styles = { color: "red" };',
- 'function redDiv() {',
- ' return <div style={styles} />;',
- '}'
- ].join('\n')
- },
- {
- code: [
- 'function redDiv(props) {',
- ' return <div style={props.styles} />;',
- '}'
- ].join('\n')
- },
- {
- code: [
- 'import styles from \'./styles\';',
- 'function redDiv() {',
- ' return <div style={styles} />;',
- '}'
- ].join('\n')
- },
- {
- code: [
- 'import mystyles from \'./styles\';',
- 'const styles = Object.assign({ color: \'red\' }, mystyles);',
- 'function redDiv() {',
- ' return <div style={styles} />;',
- '}'
- ].join('\n'),
- parserOptions: {
- ecmaVersion: 2018,
- sourceType: 'module',
- ecmaFeatures: {
- jsx: true
- }
- }
- },
- {
- code: [
- 'const otherProps = { style: { color: "red" } };',
- 'const { a, b, ...props } = otherProps;',
- '<div {...props} />'
- ].join('\n')
- },
- {
- code: [
- 'const styles = Object.assign({ color: \'red\' }, mystyles);',
- 'React.createElement("div", { style: styles });'
- ].join('\n'),
- parserOptions: Object.assign({sourceType: 'module'}, parserOptions)
- },
- {
- code: '<div style></div>'
- },
- {
- code: [
- 'React.createElement(MyCustomElem, {',
- ' [style]: true',
- '}, \'My custom Elem\')'
- ].join('\n')
- },
- {
- code: [
- 'let style;',
- '<div style={style}></div>'
- ].join('\n')
- },
- {
- code: [
- 'let style = null;',
- '<div style={style}></div>'
- ].join('\n')
- },
- {
- code: [
- 'let style = undefined;',
- '<div style={style}></div>'
- ].join('\n')
- },
- {
- code: '<div style={undefined}></div>'
- },
- {
- code: [
- 'const props = { style: undefined };',
- '<div {...props} />'
- ].join('\n')
- },
- {
- code: [
- 'const otherProps = { style: undefined };',
- 'const { a, b, ...props } = otherProps;',
- '<div {...props} />'
- ].join('\n')
- },
- {
- code: [
- 'React.createElement("div", {',
- ' style: undefined',
- '})'
- ].join('\n')
- },
- {
- code: [
- 'let style;',
- 'React.createElement("div", {',
- ' style',
- '})'
- ].join('\n')
- },
- {
- code: '<div style={null}></div>'
- },
- {
- code: [
- 'const props = { style: null };',
- '<div {...props} />'
- ].join('\n')
- },
- {
- code: [
- 'const otherProps = { style: null };',
- 'const { a, b, ...props } = otherProps;',
- '<div {...props} />'
- ].join('\n')
- },
- {
- code: [
- 'React.createElement("div", {',
- ' style: null',
- '})'
- ].join('\n')
- },
- {
- code: [
- 'const MyComponent = (props) => {',
- ' React.createElement(MyCustomElem, {',
- ' ...props',
- ' });',
- '};'
- ].join('\n')
- }
- ],
- invalid: [
- {
- code: '<div style="color: \'red\'" />',
- errors: [{
- message: 'Style prop value must be an object',
- line: 1,
- column: 6,
- type: 'JSXAttribute'
- }]
- },
- {
- code: '<Hello style="color: \'red\'" />',
- errors: [{
- message: 'Style prop value must be an object',
- line: 1,
- column: 8,
- type: 'JSXAttribute'
- }]
- },
- {
- code: '<div style={true} />',
- errors: [{
- message: 'Style prop value must be an object',
- line: 1,
- column: 6,
- type: 'JSXAttribute'
- }]
- },
- {
- code: [
- 'const styles = \'color: "red"\';',
- 'function redDiv2() {',
- ' return <div style={styles} />;',
- '}'
- ].join('\n'),
- errors: [{
- message: 'Style prop value must be an object',
- line: 3,
- column: 22,
- type: 'Identifier'
- }]
- },
- {
- code: [
- 'const styles = \'color: "red"\';',
- 'function redDiv2() {',
- ' return <Hello style={styles} />;',
- '}'
- ].join('\n'),
- errors: [{
- message: 'Style prop value must be an object',
- line: 3,
- column: 24,
- type: 'Identifier'
- }]
- },
- {
- code: [
- 'const styles = true;',
- 'function redDiv() {',
- ' return <div style={styles} />;',
- '}'
- ].join('\n'),
- errors: [{
- message: 'Style prop value must be an object',
- line: 3,
- column: 22,
- type: 'Identifier'
- }]
- }
- ]
- });
|