Add a shared ESLint config based on Airbnb's ES5 styleguide, document our customizations#1
Conversation
|
Note to reviewers: following this link allows you to see the |
|
Also CCing @dan-f, I know you were looking forward to seeing this effort pushed forward. |
|
cool, ty @bjacobel! |
| "jquery": true, | ||
| "amd": true | ||
| }, | ||
| "extends": "airbnb-base/legacy", |
There was a problem hiding this comment.
For reviewers: this comes from here:
There was a problem hiding this comment.
put this in an alphabetically order? so and comes first
|
Fantastic stuff @bjacobel. It will be very interesting to see your UI Toolkit PR and see how these rules play out in practice. I'd also like to hear from @pdesjardins about where he sees the various pieces of the documentation living. You should also talk to IT and/or devops about creating a new repo in the edx organization. When you create an npm package for this, can you also add all the usual suspects as collaborators. |
|
@andy-armstrong Cool, I'll work on getting this into the edx github org and doing an npm release after this merges to master. |
| "description": "ESLint config for edX JavaScript code.", | ||
| "main": "index.js", | ||
| "scripts": { | ||
| "test": "eslint ." |
There was a problem hiding this comment.
I was thinking about this line, and how we could test the rules. One idea I had is to include a 'samples' directory with code demonstrating each of the rules we care about. Then we could have CI actually run eslint and fail if any of the samples have errors. What do you think of that?
It would also be good to have some negative tests, but I don't know how you could achieve that. Maybe you could have a separate directory for code with issues, and then run eslint twice. The bad examples would have an expected number of eslint failures.
Are there any good examples of how to write tests for eslint rules?
There was a problem hiding this comment.
Not 100% sure I understand you here - you're saying we should test our lint ruleset by providing samples of code that should fail, and testing that they do? ESLint already does that when they ship, though.
I guess beyond the correctness of ESLint rules in actually doing what they say they do, we could also get into a situation where we create a contradiction (eg, object properties have to be in kebab case, but you can't quote them). I don't know if unit testing helps us catch that though, just running it on our real codebase should be enough.
There was a problem hiding this comment.
My reason for putting a test line here at all was mostly as a joke/easter egg... I thought it was funny that our lint repo could pass its own linter :)
There was a problem hiding this comment.
Sorry that wasn't clear. I was thinking we could provide good representative code samples that would pass the linter. The documentation then could point people at that code to say here's how we expect our code to look. These samples would demonstrate the rules that we have changed from AirBnB.
I was then wondering about negative tests but that seems less important, and is a little complex to make work.
There was a problem hiding this comment.
I actually really like that the lint repo passes it's own linter! It would probably prevent errors from creeping in too. For a while I had a linter running on my gulp config file and it caught some errors early on!
…tent") Fixing all the camelcase violations with properties: always on would have required breaking the UITK's API. So turn properties: to never, which is Airbnb default, so remove the rule. Once you open up the possibility of snake/kebab-case object properties, quoting becomes an issue again. I don't like the Airbnb config's rule here, it leads to having to quote some things and not other things. So use this rule with "consistent" setting, which I've documented in the README.
9f05b1e to
0b41eed
Compare
|
I altered the behavior of both the |
|
There's been some discussion of the That means the following patterns are correct: function foo() {
}
var bar = function () {
};And the following patterns will be a linter error: function foo () {
}
var bar = function() {
};My opinions: |
|
@bjacobel Thanks for the explanation. My preference is to support @cahrens @AlasdairSwan @dsjen @atang, what say you? |
|
I agree-- "never" in both cases. Why make them different? |
|
Here's the airbnb rationale for why they set it up that way (it's only in the ES6 guide, not the ES5 one, which is why I couldn't find it at first): https://github.com/airbnb/javascript#functions--signature-spacing (here's the PR that introduced it: airbnb/javascript#605) I think in their mind |
|
I agree both have --never. It has to be consistent and easy to remember. |
|
Added new commit with: FYI, several people have mentioned our JS package naming convention of |
|
@bjacobel This looks great conceptually, and the changes necessary in your UI Toolkit PR all seem reasonable. Excellent stuff! 👍 |
|
I added a checklist for reviewers up top, I'd like to get wide agreement from people before closing this feedback PR as this is something you'll all have to deal with when writing JS :) If others besides Andy have no more feedback would they mind checking off their boxes (and adding anyone else they think should see this)? |
|
@andy-armstrong do you think we should move the main section of the README into the developers guide before closing this? |
|
@bjacobel Let's hold off on any developer's guide changes until @pdesjardins is available to review it. I think merging it here makes sense for this PR. |
|
👍 +1 |
|
@bjacobel -- thank you for putting this together! I'm going to look at this today. |
| } | ||
|
|
||
| var foo2 = { | ||
| 'bar': 'buzz', |
There was a problem hiding this comment.
Wouldn't 'bar': 'buzz' should throw an error?
There was a problem hiding this comment.
Because the style is set to consistent, quoting is all-or-nothing: either none of them get it, or they all do. Often they all will because one property is not an allowed object identifier, which is what's going on here.
(Although your comment lead me to realize the docs here are actually wrong, pattern I've documented would pass consistent-as-needed, not consistent... hmmmmm)
There was a problem hiding this comment.
I'm going to change our rule to consistent-as-needed because that's what I actually documented here mistakenly so I'll assume that that's what the people who already thumbsedup wanted when they reviewed.
c6718bc to
f06996b
Compare
|
👍 🎆 💃 |
|
|
||
| ## License | ||
|
|
||
| The code in this repository is licensed the Apache 2.0 license unless otherwise |
There was a problem hiding this comment.
The code in this repository is released under the Apache 2.0 license?
There was a problem hiding this comment.
Yeah, thoughts on that? I just added that because the Toolkit is Apache and has the same line in its README. Technically there's only one line of code in this repo, is Apache the right choice for that situation? The Airbnb styleguide / linter config this extends is MIT-licensed.
There was a problem hiding this comment.
Oh, I just realized you're commenting on the fact that this line is grammatically incorrect, not specifically on the Apache part of it.
There was a problem hiding this comment.
I checked with the OSS team and some other members of Docs and verified that Apache is the right license for this project.
|
Looks good, I support merging as is, although I added a few comments. I'd like to capture some of the information in the developer's guide, as described by Andy. 👍 |
|
Looks good @bjacobel 👍 |
|
@bjacobel why did you turn off the 1 var rule? I like it for hammering home the hoisting issue for developers not as familiar with JavaScript. I also worry that the new rule is a little vague (use 1 var if you want, or multiple only specific cases). |
|
@AlasdairSwan Eek, yeah, I probably should have socialized that change a little more. Couple of reasons why:
IMO, having knowledge that strange things can happen if you don't use just one var is good to have and should make it into our developer's guide, but it's not a common enough concern to mandate that style everywhere. Thoughts? |
|
I'm going to make a separate issue for discussing this because I just figured out some new information |
|
I would prefer to discuss it in more detail before making that change. I think that the move to ES2015 is going to take a while and what I like about enforcing the rule is that it highlights how browsers actually process the JS. I would prefer that the whole rule is omitted in this PR and a separate PR is opened to discuss our agreed upon approach. |
Looking for feedback on both the linting rules this would have us adopt in our Javascript, and my efforts at documenting them.