Skip to content
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

no-undef mark JSX elements as not defined #19

Closed
BerkeleyTrue opened this issue Mar 15, 2015 · 18 comments
Closed

no-undef mark JSX elements as not defined #19

BerkeleyTrue opened this issue Mar 15, 2015 · 18 comments

Comments

@BerkeleyTrue
Copy link

With

var React =require('react'),
    Router = require('react-router'),
    Route = Router.Route,
    DefaultRoute = Router.DefaultRoute;

at the top and using [email protected] and [email protected] with
"react/jsx-uses-react": 1, "react/jsx-uses-vars": 1

This works fine.

var routes = React.createClass({
  render: function() {
    return (
      <Route
        name='app'
        path='/'
        handler={ App }>

        <Route
          name='bonfires'
          path='/bonfires/?:bonfires?'
          handler={ Bonfires } />

        <DefaultRoute
          handler={ Bonfires } />
      </Route>
    );
  }
});

But this barphs all over the place

var routes = (
  <Route 
    name='app'
    path='/'
    handler={ App }>

    <Route
      name='bonfires'
      path='/bonfires/?:bonfires?'
      handler={ Bonfires } />

    <DefaultRoute
      handler={ Bonfires } />
  </Route>
);

1__tmux

@yannickcr
Copy link
Member

Are you using the globalReturn or modules feature ? There is an issue in ESLint related to variable used in global scope with these features

I described the issue here eslint/eslint#2040

@BerkeleyTrue
Copy link
Author

Here is the eslintrc file we are using. We are not using either of those, although we will need modules soon.

@yannickcr
Copy link
Member

Using the node environment set globalReturn to true https://github.com/eslint/eslint/blob/master/conf/environments.js#L17-L19 so your issue seems related.

You can force globalReturn to false by adding:

"ecmaFeatures": {
    "globalReturn": false
}

But this is just a workaround for now. Hope this issue will be resolved soon.

@BerkeleyTrue
Copy link
Author

Yup that workaround works. I'll keep an eye on the issue in eslint use this workaround for now. Should this issue be closed since this an eslint bug?

BerkeleyTrue pushed a commit to freeCodeCamp/freeCodeCamp that referenced this issue Mar 16, 2015
@Cellule
Copy link
Contributor

Cellule commented Mar 16, 2015

I tried the workaround and it didn't work for me.
However, I see that wrapping the jsx in a function seems to fix the problem.
for instance

var routes = (function(){
  return <Route 
    name='app'
    path='/'
    handler={ App }>

    <Route
      name='bonfires'
      path='/bonfires/?:bonfires?'
      handler={ Bonfires } />

    <DefaultRoute
      handler={ Bonfires } />
  </Route>
})();

is correctly recognised by the linter.
It seems that any jsx not in a function is incorrect

@ericclemmons
Copy link

@Cellule's fix worked for me as well. Thanks!

@yannickcr
Copy link
Member

I think the problem occur only on variables used in global scope.

@ericclemmons
Copy link

I'm not sure what's considered "global scope", but the OP's example (where you use <Route />) is all it took for me.

@Cellule
Copy link
Contributor

Cellule commented Mar 17, 2015

I also saw the problem elsewhere (not really a normal use case) and it might pinpoint the issue.

var Panel = require("react-bootstrap/Panel");
var Application = React.createClass({
  statics: {
    componentX: (
      <Panel> <p> test </p> </Panel>
    )
  },

I get the errors

   4:4   error    Panel is defined but never used          no-unused-vars
  22:6   warning  'React' must be in scope when using JSX  react/react-in-jsx-scope
  22:7   error    'Panel' is not defined                   no-undef
  22:14  warning  'React' must be in scope when using JSX  react/react-in-jsx-scope

However

var Panel = require("react-bootstrap/Panel");
var Application = React.createClass({
  statics: {
    getComponentX: function() {
      return (
        <Panel> <p> test </p> </Panel>
      );
    }
  },
});

is fine

@yannickcr
Copy link
Member

Fix submited to ESLint eslint/eslint/pull/2090

@JedWatson
Copy link

I just came across this in the current published version (1.5.0 with eslint 0.17.1) - haven't had a chance to test against the PR submitted to ESLint yet but thought I'd leave this here for reference.

I get an odd result listing react-tappable

With the following config:

{
    "parser": "babel-eslint",
    "env": {
        "browser": true,
        "node": true
    },
    "plugins": [
        "react"
    ],
    "rules": {
        "curly": [2, "multi-line"],
        "no-shadow": 0,
        "no-underscore-dangle": 0,
        "no-unused-expressions": 0,
        "quotes": [2, "single", "avoid-escape"],
        "react/jsx-uses-react": 1,
        "react/jsx-uses-vars": 1,
        "react/react-in-jsx-scope": 1,
        "semi": 2,
        "strict": 0
    }
}

I get the following results:

example/src/app.js
   4:4   error     App is defined but never used           no-unused-vars
  67:13  warning  'React' must be in scope when using JSX  react/react-in-jsx-scope
  67:14  error    'App' is not defined                     no-undef

3 problems (2 errors, 1 warning)

It's kind of ironic that it throws both the App is defined but never used and App is not defined issues; also React is in scope at the top of the file.

@yannickcr
Copy link
Member

Will be fixed when eslint/eslint#2121 (for no-unused-vars) and eslint/eslint#2122 (for no-undef) gets merged.

@JedWatson
Copy link

👍 great, thanks!

@BerkeleyTrue
Copy link
Author

👍 Woot!

@yannickcr yannickcr changed the title Bug: jsx-uses-vars and react-in-jsx-scope does not work properly outside React.createClass no-undef mark JSX elements as not defined Mar 24, 2015
@BerkeleyTrue
Copy link
Author

Looks like both of these have been merge. Will wait to test next release of eslint to close this.

@yannickcr
Copy link
Member

Fixed in ESLint 0.18.0

@r01010010
Copy link

r01010010 commented Jan 11, 2017

adding these rules fixed it for me:

"react/jsx-uses-react": 1,
"react/jsx-uses-vars": 1

@cookcc
Copy link

cookcc commented Aug 2, 2021

Official answer is here https://github.com/typescript-eslint/typescript-eslint/blob/master/docs/getting-started/linting/FAQ.md#i-get-errors-from-the-no-undef-rule-about-global-variables-not-being-defined-even-though-there-are-no-typescript-errors and it says indeed to add them to globals or to disable the no-undef rule because typescript has already its own checks

overrides: [
    {
      files: ['*.ts', '*.tsx'],
      rules: {
        'no-undef': 0,
      },
    },
  ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

7 participants