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

Added eslint-plugin-react to the CLIEngine #88

Merged
merged 16 commits into from
Sep 6, 2018
Merged

Added eslint-plugin-react to the CLIEngine #88

merged 16 commits into from
Sep 6, 2018

Conversation

muuvmuuv
Copy link
Contributor

See: #87

@Glavin001 Glavin001 added the add plugin Add ESLint plugin support label Aug 28, 2018
Copy link
Member

@Glavin001 Glavin001 left a comment

Choose a reason for hiding this comment

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

Looking good so far!

Left to-do:

src/index.ts Outdated
@@ -23,6 +23,11 @@ export const beautifier: Beautifier = {
name: "ESLint",
package: "eslint",
},
{
type: DependencyType.Node,
name: "ESLintPluginReact",
Copy link
Member

Choose a reason for hiding this comment

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

ESLint-plugin-React (as seen in https://www.npmjs.com/package/eslint-plugin-react ) may be a better name.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good approach, but what about on L60, where we assign it to a variable. There I need to call it ESLintPluginReact and then we would have two different styles.

Copy link
Member

Choose a reason for hiding this comment

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

Yup, change line 60, too, so they are the same 👍 .

@muuvmuuv
Copy link
Contributor Author

muuvmuuv commented Aug 29, 2018

@Glavin001 I added JSX like that: JSX: options.JavaScript,

Now I got stuck by testing it. I had to rename the .eslintrc.yml to .json because I couldn't get it running in yml. I really tried adapting the react rules but had no luck. I think the problem below is caused by jest or some directory issue where eslint is not looking in the test rather then in the root of the project for a configuration file.

https://gist.github.com/muuvmuuv/eae245f991d0c6b6e012a00326ee5598

@Glavin001
Copy link
Member

@szeck87 any advice on @muuvmuuv 's problem above in #88 (comment) ? I think you setup most of this beautifier.

@Glavin001
Copy link
Member

@muuvmuuv I see your Gist with the file changes and test report. You can commit your changes to this PR, so we can see what changes you've done, let Travis CI run the tests, and comment on lines of code 👍 .

@stevenzeck
Copy link
Contributor

stevenzeck commented Aug 29, 2018

@muuvmuuv you need to include the filePath when calling the beautify method, otherwise the beautifier won't know where to look for the config file. Adding that allows it to find the config file.

From there ESLint and/or the React plugin errors due to this message: Parsing error: The keyword 'const' is reserved. So it doesn't return an output in the result. I think you need to have "ecmaVersion": 6, in your ESLint config under parserOptions.

Copy link
Contributor

@stevenzeck stevenzeck left a comment

Choose a reason for hiding this comment

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

The core ESLint beautifier should continue to work if the user is not using the React plugin.

package.json Outdated
@@ -37,7 +37,8 @@
"@types/jest": "23.3.1",
"@types/node": "10.9.3",
"@types/read-pkg-up": "3.0.1",
"eslint": "5.4.0",
"eslint": "^5.4.0",
"eslint-plugin-react": "^7.11.1",
Copy link
Member

Choose a reason for hiding this comment

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

@szeck87 pointed out to me on Slack eslint-plugin-react should also be added as a peerDependency:

"eslint": "^4.19.1 || ^5.0.0"
},
"devDependencies": {
"@types/eslint": "4.16.3",
"@types/jest": "23.3.1",
"@types/node": "10.9.3",
"@types/read-pkg-up": "3.0.1",
"eslint": "^5.4.0",

src/index.ts Outdated
type: DependencyType.Node,
name: "ESLint-plugin-React",
package: "eslint-plugin-react",
},
Copy link
Member

Choose a reason for hiding this comment

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

Should be optional. cc @szeck87 from #87 (comment)

Copy link
Contributor

Choose a reason for hiding this comment

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

Yep, please add optional: true

src/index.ts Outdated
@@ -51,6 +57,9 @@ export const beautifier: Beautifier = {
}: BeautifierBeautifyData) {
return new Promise<string>((resolve, reject) => {
const { CLIEngine } = dependencies.get<NodeDependency>("ESLint").package;
const ESLintPluginReact = dependencies.get<NodeDependency>(
Copy link
Member

Choose a reason for hiding this comment

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

To allow for the React plugin to be optional, we should check beautifierConfig (or config after line 63).

Installation instructions from https://www.npmjs.com/package/eslint-plugin-react show:

 "extends": [
    "eslint:recommended",
    "plugin:react/recommended"
  ]

So we should check for plugin:react/ and if it exists in extends then get the React plugin dependency (line 60) and add the plugin (line 79).

cc @szeck87 for #87 (comment)

Copy link
Contributor

Choose a reason for hiding this comment

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

You can try that. If that can't be done, dependencies.get<NodeDependency>("ESLint-plugin-React") will have an error that you can check for.

Copy link
Member

Choose a reason for hiding this comment

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

@szeck87 Good point! Actually, let's just skip the checking of beautifierConfig entirely, that sounds like an unnecessary rabbit hole.

We can use the .has method! https://api.unibeautify.com/classes/_dependencymanager_dependencymanager_.dependencymanager.html#has

Like this:

if (dependencies.has("ESLint-plugin-React")) {
  cli.addPlugin("eslint-plugin-react", dependencies.get<NodeDependency>("ESLint-plugin-React").package);
}

🎉

Copy link
Member

Choose a reason for hiding this comment

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

Whoops. @szeck87 pointed out .has will not work here.

We actually want to use dependency.isInstalled: https://api.unibeautify.com/classes/_dependencymanager_dependency_.dependency.html#isinstalled

So more like this:

const reactPlugin = dependencies.get<NodeDependency>("ESLint-plugin-React");
if (reactPlugin.isInstalled) {
  cli.addPlugin("eslint-plugin-react", reactPlugin.package);
}

Feel free to clean up the above.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

When I do this I'm getting : A Promise was found that appears to not have resolve or reject invoked on all code paths (promise-must-complete) @Glavin001

Copy link
Contributor

Choose a reason for hiding this comment

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

Yea I noticed that too, might have to comment on the line above the if statement:

// tslint:disable-next-line:promise-must-complete

Copy link
Member

Choose a reason for hiding this comment

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

Where is this error coming from @muuvmuuv ? Could you push a commit with this?

Maybe you already did push a commit. I currently see Travis CI failing:

npm ERR! Unexpected token } in JSON at position 994

src/index.ts Outdated
type: DependencyType.Node,
name: "ESLint-plugin-React",
package: "eslint-plugin-react",
},
Copy link
Contributor

Choose a reason for hiding this comment

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

Yep, please add optional: true

package.json Outdated
@@ -30,14 +30,16 @@
"homepage": "https://github.com/Unibeautify/beautifier-eslint#readme",
"peerDependencies": {
"unibeautify": ">= 0.12.1",
"eslint": "^4.19.1 || ^5.0.0"
"eslint": "^4.19.1 || ^5.0.0",
"eslint-plugin-react": "^7.11.1",
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove the comma at the end here

@@ -67,6 +76,10 @@ export const beautifier: Beautifier = {
rules: finalOptions,
};
const cli: CLIEngine = new CLIEngine(cliOptions);
// tslint:disable-next-line:promise-must-complete
Copy link
Member

Choose a reason for hiding this comment

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

This may be a bug with https://github.com/Microsoft/tslint-microsoft-contrib/
If you can create a much smaller test case, you could report an issue 👍 .

Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like any if is a branch, and doesn't bother looking what it actually does to consider whether the promise completes or not (since we are using return new Promise<string>((resolve, reject) => {).

Maybe it's safer to remove that block and change the returns at the bottom to return Promise.resolve.

Copy link
Member

Choose a reason for hiding this comment

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

Let's not make any major changes yet. We can try different approaches in a later PR 👍 .

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Glavin001 would you mind creating a small test case and make an issue there? I think you can way better explain that error case.

Copy link
Member

@Glavin001 Glavin001 left a comment

Choose a reason for hiding this comment

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

@szeck87 had some good comments. Once those are fixed up we can merge 🎉!

src/index.ts Outdated
@@ -21,11 +21,18 @@ export const beautifier: Beautifier = {
{
type: DependencyType.Node,
name: "ESLint",
package: "eslint",
package: "eslint"
Copy link
Contributor

@stevenzeck stevenzeck Aug 31, 2018

Choose a reason for hiding this comment

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

Put a comma at the end of package: "eslint"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can we enable a linter for this? I really don't know when to put a comma and when I shouldn't …

Copy link
Contributor

Choose a reason for hiding this comment

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

https://palantir.github.io/tslint/rules/trailing-comma/ should be catching it, but it's not.

Copy link
Member

Choose a reason for hiding this comment

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

Looks like it is set to ignore for multiline: https://github.com/Unibeautify/beautifier-eslint/blob/master/tslint.json#L294-L300

@muuvmuuv Could you tweak this and make sure TSLint is telling you to add the comma? :)

src/index.ts Outdated
type: DependencyType.Node,
name: "ESLint-plugin-React",
package: "eslint-plugin-react",
optional: true
Copy link
Contributor

@stevenzeck stevenzeck Aug 31, 2018

Choose a reason for hiding this comment

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

Put a comma at the end of optional: true

@stevenzeck
Copy link
Contributor

@muuvmuuv just add in those two commas I mentioned and I'll approve.

@muuvmuuv
Copy link
Contributor Author

muuvmuuv commented Sep 5, 2018

@szeck87 @Glavin001 I've now added this ESLint issue we discussed before. Sorry that the file is beautified that much, but that is what Unibeautify is doing when I save that file.

@stevenzeck
Copy link
Contributor

@muuvmuuv can you rebase again so the CI tests can run?

Copy link
Member

@Glavin001 Glavin001 left a comment

Choose a reason for hiding this comment

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

I think this is good to go!

@Glavin001 Glavin001 merged commit 2ebbe9f into Unibeautify:master Sep 6, 2018
@Glavin001
Copy link
Member

Awesome work, @muuvmuuv ! Thanks for contributing! 🎉

@Glavin001
Copy link
Member

Published to v0.6.0! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
add plugin Add ESLint plugin support
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants