Skip to content

newline-per-chained-call and PropTypes #748

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
armandabric opened this issue Feb 21, 2016 · 11 comments
Closed

newline-per-chained-call and PropTypes #748

armandabric opened this issue Feb 21, 2016 · 11 comments

Comments

@armandabric
Copy link

Hi everyone,

I'm facing a some trouble with one rule: how do you define PropTypes with the newline-per-chained-call rule activated?

/* @flow */

import React from 'react';

class Baz extends React.Component {
    static propTypes = {
        foo: React.PropTypes.oneOf(['nice', 'bad']),  // This trigger an error
        bar: React.PropTypes.instanceOf(Date).isRequired, // Here too
    };

    // ...
}
@lencioni
Copy link
Contributor

If I understand the rule correctly, and the configuration we have here, I would expect your foo example to not trigger an error but your bar example to trigger an error, since we have set ignoreChainWithDepth to 3.

In which case, you should be able to resolve this by first destructuring PropTypes from React, and then using it directly:

/* @flow */

import React from 'react';

const { PropTypes } = React;

class Baz extends React.Component {
    static propTypes = {
        foo: PropTypes.oneOf(['nice', 'bad']),
        bar: PropTypes.instanceOf(Date).isRequired,
    };

    // ...
}

@armandabric
Copy link
Author

Ho you right! I've build this example from actual code, and only the bar prop types trigger error. My bad.

If the only solution is to play with the PropTypes object, I guess this is the trick:

import { default as React, PropTypes } from 'react';

@kylehodgetts
Copy link

@Spy-Seth What does default as React achieve?
Does it just save you having to do:

import React from 'react';
import { PropTypes } from React;

?

@lencioni
Copy link
Contributor

I believe that this syntax might happen work:

import React, { PropTypes } from 'react';

However, since PropTypes is not (yet?) an actual named export (since React isn't using ES2015 modules), I think that this is not supposed work according to spec (even though it may happen to work with your environment). I think it is probably best to destructure separately for now, to avoid potential problems when upgrading your transpiler or module bundler.

I'd like to throw in a plug for import-js, an editor plugin I've been working on that helps you add imports to your files. Check it out! :)

@ljharb
Copy link
Collaborator

ljharb commented Feb 21, 2016

I'm starting to think this particular rule might be a bit odd - it's called "newline per chained call" but it's counting property access (which is not a "call"). imo none of those examples should be throwing. @yannickcr, what do you think?

@dustinmoorenet
Copy link

There is an issue opened on eslint for that rule. It appears to be a bug. eslint/eslint#5289

IMO, this rule should be turned off until that issue is resolved.

@ljharb
Copy link
Collaborator

ljharb commented Feb 21, 2016

Agreed.

@ljharb ljharb closed this as completed in 12fb486 Feb 21, 2016
@ljharb
Copy link
Collaborator

ljharb commented Feb 21, 2016

Published as v6.0.1.

We'll re-enable this rule once eslint has fixed the bug.

@armandabric
Copy link
Author

That's solution resolve my issue 😄 👍

gilbox pushed a commit to gilbox/javascript that referenced this issue Mar 21, 2016
@taion
Copy link
Contributor

taion commented May 3, 2016

This got fixed in 2.3.0, BTW.

@taion
Copy link
Contributor

taion commented May 3, 2016

eslint/eslint@9b54ed3

jaylaw81 pushed a commit to appirio-digital/ads-best-practices that referenced this issue Sep 19, 2017
jaylaw81 pushed a commit to appirio-digital/ads-best-practices that referenced this issue Sep 19, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants