Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Implement a "react" option for the no-unused-variable rule #725

Merged
merged 1 commit into from
Nov 3, 2015
Merged

Implement a "react" option for the no-unused-variable rule #725

merged 1 commit into from
Nov 3, 2015

Conversation

myitcv
Copy link
Contributor

@myitcv myitcv commented Oct 10, 2015

Addresses #698

"no-unused-variable", [true, "react"],

With such a valid option, the following is not an error:

import * as React from "react";

let x = <span></span>;
console.log(x);

Thoughts/comments?

@myitcv
Copy link
Contributor Author

myitcv commented Oct 14, 2015

Rebased against master following merge of #726

@myitcv
Copy link
Contributor Author

myitcv commented Oct 20, 2015

@jkillian @adidahiya just rebased this

@jkillian
Copy link
Contributor

Ideally, it would be nicer to just accept one option named 'react' and not worry about prefixes. Especially since React 0.14 moved addons to their own npm packages (and hopefully bower packages?). Are there any other legit use cases other than 'react' and 'react/addons'?

@myitcv
Copy link
Contributor Author

myitcv commented Oct 21, 2015

Are there any other legit use cases other than 'react' and 'react/addons'?

Not sure, hence why I coded a bit more defensively here... but on reflection it's much simpler to have the modules "react" and "react/addons" as constants which makes the option just "react".

I'll push up another commit that makes that change... then we can choose between the two (and then squash commits)

misak113 added a commit to misak113/app-sandbox that referenced this pull request Oct 21, 2015
* use it as npm test
* waits for jsx react commit: palantir/tslint#725
misak113 added a commit to misak113/app-sandbox that referenced this pull request Oct 21, 2015
* not fully waiting for PR merge to master: palantir/tslint#725
@adidahiya
Copy link
Contributor

I'd prefer only supporting "react".

@jkillian
Copy link
Contributor

Why not support both? I assume using "react/addons" is pretty common

@adidahiya
Copy link
Contributor

Sorry, I should've been more clear --

I would like the rule option to be named "react". When looking for imports, we should look for ... from "react" and ... from "react/*".

@jkillian
Copy link
Contributor

Oh gotchya. I was thinking have the option be named "react" only and when looking for imports only look for "react" and "react/addons".

@majo-javorka
Copy link

hi folks. I just found this discussion as I saw the same thing in my project. I also have "unused variable: 'defaultProps'", do you think it would make sense to 'whitelist' that as well when "react" option is set?

@jkillian
Copy link
Contributor

@Jazzik Can you post an example of your code? I believe as long as it's not private, you shouldn't get that error. Something like this shouldn't give a lint error:

    interface Props { foo: number; }
    export class ReactComponent extends React.Component<Props, {}> {
        public static defaultProps = {
            foo: 12
        };
        ...
    }

@majo-javorka
Copy link

@jkillian oh right, I had it as private 😊

misak113 added a commit to misak113/app-sandbox that referenced this pull request Oct 24, 2015
* use it as npm test
* waits for jsx react commit: palantir/tslint#725
misak113 added a commit to misak113/app-sandbox that referenced this pull request Oct 24, 2015
* not fully waiting for PR merge to master: palantir/tslint#725
@myitcv
Copy link
Contributor Author

myitcv commented Oct 25, 2015

@jkillian @adidahiya pushed up a second commit with a single "react" option. The list of valid React modules is now maintained as a const in the rule.

@myitcv
Copy link
Contributor Author

myitcv commented Oct 29, 2015

@adidahiya @jkillian - any thoughts on this? Only reason for me pushing is that we've got a whole load of /* tslint:disable:no-unused-variable */ lines kicking around our code which I'm keen to remove 😄

@@ -16,8 +16,13 @@
import * as Lint from "../lint";
import * as ts from "typescript";

const OPTION_REACT = "react";
const OPTION_REACT_MODULES = ["react", "react/addons"];
const OPTION_REACT_NAMESPACE_IMPORT_NAME = "React";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can these two consts not start with OPTION_? I think that should be reserved only for actual options which are provided to the rule

// a) seen another usage of React and/or
// b) seen a JSX identifier
//
// otherwise a usage error will be reported
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change this to "... a variable usage failure will ..."

"error" is ambiguous in JS, so we should stick to the "failure" terminology.

@myitcv
Copy link
Contributor Author

myitcv commented Oct 31, 2015

@adidahiya - all points addressed in most recent push. Apologies, a number of the whitespace changes crept in because my editor automatically runs tsfmt on save. Look forward to #732 landing and being enforced in the build 😄

adidahiya added a commit that referenced this pull request Nov 3, 2015
Implement a "react" option for the no-unused-variable rule
@adidahiya adidahiya merged commit 2d1501f into palantir:master Nov 3, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants