eslint-plugin-react: It must be possible for ES6 defaultProps be equal to function execution

In the class below:

class TestComponent extends React.Component {

    static defaultProps = function () {

       const date = new Date();

        return {
            date
        };
    }();
}

the defaultProps are equal to a anonymous function return value.

In this case, the eslint-plugin-react doesn’t run, and throws the following error: TypeError: Cannot read property 'split' of undefined at Object.isPropWrapperFunction (~/project/node_modules/eslint-plugin-react/lib/util/propWrapper.js:12:26) at resolveNodeValue (~/project/node_modules/eslint-plugin-react/lib/util/defaultProps.js:30:23) at Object.ClassProperty (~/project/node_modules/eslint-plugin-react/lib/util/defaultProps.js:222:26) at updatedRuleInstructions.(anonymous function) (~/project/node_modules/eslint-plugin-react/lib/util/Components.js:752:46) at listeners.(anonymous function).forEach.listener (~/project/node_modules/eslint/lib/util/safe-emitter.js:45:58)

I’m using the following versions: “eslint”: “5.10.0” “eslint-plugin-react”: “7.11.1”.

If in the same class I put the defaultProps equal do an object it runs as expected.

Thanks

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 16 (12 by maintainers)

Commits related to this issue

Most upvoted comments

Perfect, I’m able to repro.

@ljharb yeah that’s what I meant there; just thought you might know why that could be unrealistic. I’ll look around their issue tracker. 👍

File with error (i think):

HeroTeaserList.propTypes = Object.assign({
    heroIndex: PropTypes.number,
    preview: PropTypes.bool,
}, componentApi, teaserListProps);

in another file

export const componentConfiguration = {
  childComponentMapping: objectOf(string),
  componentConfig: object,                
  componentInstanceId: string,            
  componentType: string,                  
  dataType: string,                       
  filter: shape(TemplateFilter),          
  filterVersionSetUuid: string,           
  teasers: arrayOf(shape(TeaserContent)), 
  title: string,                          
  titleOverride: node,                    
  uuid: string.isRequired,                
  visibleCount: number,                   
  epgUuid: string,                        
  readOnly: bool,                         
};

export const componentApi = Object.assign({
  getComponent: func.isRequired,
  onNavigation: func,
  onConfigurationChange: func,
}, componentConfiguration);

another file

export const teaserListProps = {
  'componentInstanceId': string,
  'title':               string.isRequired,
  'uuid':                string.isRequired,
  'data':                object,
  'listSize':            number,
  'priority':            number,
  'teasers':             arrayOf(shape(teaserProps)).isRequired,
  'visibleCount':        number,
};

The OP’s test code doesn’t crash on master.

@jethrolarson I understand. if you edit lib/rules/forbid-prop-types.js line 128 to console.log(context.getFilename()) (i think), it should print out the file at least?