-
Notifications
You must be signed in to change notification settings - Fork 889
JSX does not mark React
import as used
#689
Comments
Oops, just noticed #589 was a closed ticket. I'm not sure I like the suggested solutions though (either manually exclude React from this rule in every single JSX file, or make React a global variable) |
@dallonf - agree that turning off the rule for every time you The second suggestion however is not to make React a global variable; rather it is to use the global typings definition. However, you need to be slightly careful. Because, and this is the case for us (I work with @priyatham-aps), we rely on the import of React in order to ensure modules are loaded when required (we use /// <reference path="../../tsd.d.ts" />
// the above d.ts contains a reference to the global React definition
// this next import ensures that SystemJS loads the module if it hasn't already been loaded
import "react";
class MyComponent extends React.Component<{}, {}> {
render(): JSX.Element {
return <span>Hello</span>;
}
} This satisfies the linter, however there is one dangerous side effect: there could be a So I'm minded to agree that either:
@adidahiya - thoughts? |
It seems like giving Another solution would be to add support for "excludes" to |
Hmm, I don't think it actually matters what mode we are in: I think we're actually on safe ground making an exception under the following conditions:
import * as React from "react"; Thoughts? |
Sorry, I ignored your suggestion:
Agreed, not keen on this - we only have a |
If there are jsx syntax in a tsx file React is used so the warning is erroneous. As the walker supports tsx it's already implicitly React specific so I would say the cleanest solution is to consider React used if the walker visits at least one jsx element. |
If we go with that implementation ("if the ... perhaps just to be safe we should add a |
@adidahiya - great point, I hadn't considered other such frameworks at all.
And just to confirm, by "enables this functionality" are you specifically referring to these two conditions?
import * as React from "react"; |
Yep, sorry if that wasn't clear -- "enable this functionality" means to fix the false positive we currently report where usage of JSX syntax (only possible in |
Sorry, it's probably my misunderstanding! I'm just looking to confirm that, under your proposal, with a config that contains: "no-unused-variable": [true, "react"], the following will not report an error: /// <reference path="tsd.d.ts" />
import * as React from "react";
let x = <span></span>;
console.log(x); where it currently does:
Reason being the use-case I described earlier |
@myitcv yep, we are in sync, you understand my proposal correctly |
fixed as of |
FYI - I'm seeing some issues with this rule in our code base... for some reason we're not getting 'visit' callbacks for every JSX element. I'm investigating. |
@myitcv I think you need to also visit |
Should this work? {
"rules": {
"no-unused-variable": [
true,
"react"
]
}
} It still throws here: import React from 'react';
import { render } from 'react-dom';
import { Router, Route, IndexRedirect, hashHistory } from 'react-router';
import Page from './page/component';
import GistsContainer from './gists/container';
import RepositoriesContainer from './repositories/container';
const DEFAULT_USER = 'octocat';
render(
<Router history={hashHistory}>
<Route path="/">
<IndexRedirect to={`/users/${DEFAULT_USER}/gists`} />
<Route path="users/:user" component={Page}>
<Route path="gists" component={GistsContainer} />
<Route path="repositories" component={RepositoriesContainer} />
</Route>
</Route>
</Router>,
document.getElementById('app')
); I use |
@donaldpipowitch It should, but it's currently a bug: #893 |
Okay, thank you. I'll follow the other bug. |
For input:
with the
no-unused-variable
rule enabled, throws the warning:unused variable: 'React'
. Since JSX compiles toReact.createElement
, this is a false positive.The text was updated successfully, but these errors were encountered: