Last active
February 28, 2023 03:22
-
-
Save jonchurch/ecf040b1543c1b2423ccb2f4913d076b to your computer and use it in GitHub Desktop.
Yoda style eslint rule example code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*eslint yoda: "error"*/ | |
const youveBecome = "powerful"; | |
if ("powerful" === youveBecome) { | |
// yoda style | |
// value comes before variable | |
} | |
if (youveBecome === "powerful") { | |
// non-yoda | |
// variable comes before value | |
} |
Yoda speak is better. If there's a typo, compiler will catch it.
if ("powerful" = youvebcome) { // compiler error
if (youvebecome = "powerful") { // no error
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is an example of the eslint yoda rule.
"Yoda" style refers to putting the value before the variable you're comparing. It would read like "powerful equals what youveBecome", which sounds like how yoda speaks. Instead of "youveBecome equals powerful", which reads more naturally.