-
Notifications
You must be signed in to change notification settings - Fork 68
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
Updated style guide for JavaScript #67
Conversation
This updates ESLint to a version compatible with the latest plugins.
This makes the file easier to interpret at a glance.
This commit removes any custom rules that are redundant with the Airbnb rules and reinforces custom rules that conflict with the Airbnb rules.
Overall I think this looks good and is a step in the right direction. I did find some strange warnings that I couldn't figure out, though. For example, running on the
On this block of code
This is in a function and I don't see parameters define previously in the function. The uses appear to be within scope unless I'm overlooking something. |
@smgallo Thanks! I looked into that example and the problem is the rule's description doesn't match up with every case the rule can flag. As a secondary case, // Incorrect
function doIfElse() {
if (true) {
var build = true;
} else {
var build = false;
}
}
// Correct
function doIfElse() {
var build;
if (true) {
build = true;
} else {
build = false;
}
} Once I made a similar change to our code, the error went away. I think the primary case the rule handles is worth keeping it for, but this secondary case should probably be broken out into its own rule. If C-style block scope is what this rule attempts to emulate, then the "incorrect" case is perfectly okay as long as the variables aren't used outside of those conditional blocks. If this case is one some developers want to disallow, then there should be a separate rule to tell the user exactly what they did wrong. |
Do we need corresponding pull request in any of the other repos to make sure we have the same styles across repositories? |
@plessbd We do. I was just waiting until there was consensus on the style before copying the rules over. |
In that case. The only one I would argue with is vars-on-top because of hoisting. However, i don't feel strongly enough to fight that hard for it. |
Description
This pull request updates the style guide for JavaScript code and enforces the changes with ESLint.
The commits in this pull request:
"error"
instead of2
).Motivation and Context
Our current style guide is a good start, but it leaves room for a number of inconsistencies. When ESLint's auto-fixer is run, it can produce results that don't mesh with our implicit, existing style, like the example below.
This pull request aims to tighten up the rules by using a more thorough style guide as a basis for ours. Since there is no official or semi-official style guide for JavaScript along the lines of PHP's PSR-2 or Python's PEP 8, a popular third-party guide was chosen - namely, the ES5 variant of Airbnb's guide.
I've made a handful of tweaks to their guide already based on discussions with various team members, but our style guide should be something the whole team feels comfortable with. If there are any rules you would like to add, modify, or delete, please comment. (I recommend running ESLint on some sample files with the new ruleset to see what it does and doesn't flag.)
The overrides of Airbnb's rules, both preexisting and new, are listed in the table below. If an option for a custom rule in the
.eslintrc.json
file is not described below, it is likely because it was copied from the Airbnb config due to how ESLint handles rule overrides.curly
, 1st optionbrace-style
, 2nd option,allowSingleLine
no-implicit-coercion
this
to any variable.this
to be assigned to variables namedself
.consistent-this
indent
, 1st optionfunc-names
vars-on-top
recordType
in stores).new-cap
, 1st option,properties
max-len
, 1st optioncontinue
keyword.continue
keyword.continue
can significantly reduce indentation in lengthy and/or nested loops, making them easier to read.no-continue
no-plusplus
Object.prototype
methods fromObject
instances.Object.prototype
methods fromObject
instances.no-prototype-builtins
Removals of preexisting custom rules in favor of Airbnb defaults are explained in the table below.
block-scoped-var
no-extra-parens
^[a-z]+(_[a-z]+)+$
.dot-notation
Tests Performed
Ran ESLint with new plugin and custom rules and verified that it worked as expected.
Checklist:
I have added tests to cover my changes.