diff --git a/docs/rules/eqeqeq.md b/docs/rules/eqeqeq.md index 7846a007b38..32c95902c2e 100644 --- a/docs/rules/eqeqeq.md +++ b/docs/rules/eqeqeq.md @@ -29,6 +29,44 @@ if (obj.getStuff() != undefined) { } ## Options +### always + +The `"always"` option (default) enforces the use of `===` and `!==` in every situation. + +Examples of **incorrect** code for the `"always"` option: + +```js +/*eslint eqeqeq: ["error", "always"]*/ + +a == b +foo == true +bananas != 1 +value == undefined +typeof foo == 'undefined' +'hello' != 'world' +0 == 0 +true == true +foo == null + +``` + +Examples of **correct** code for the `"always"` option: + +```js +/*eslint eqeqeq: ["error", "always"]*/ + +a === b +foo === true +bananas !== 1 +value === undefined +typeof foo === 'undefined' +'hello' !== 'world' +0 === 0 +true === true +foo === null + +``` + ### smart The `"smart"` option enforces the use of `===` and `!==` except for these cases: diff --git a/lib/rules/eqeqeq.js b/lib/rules/eqeqeq.js index 35f018e6792..441f5b751cf 100644 --- a/lib/rules/eqeqeq.js +++ b/lib/rules/eqeqeq.js @@ -19,7 +19,7 @@ module.exports = { schema: [ { - enum: ["smart", "allow-null"] + enum: ["always", "smart", "allow-null"] } ] }, diff --git a/tests/lib/rules/eqeqeq.js b/tests/lib/rules/eqeqeq.js index c8bce75f312..10c35064986 100644 --- a/tests/lib/rules/eqeqeq.js +++ b/tests/lib/rules/eqeqeq.js @@ -22,6 +22,7 @@ ruleTester.run("eqeqeq", rule, { valid: [ "a === b", "a !== b", + { code: "a === b", options: ["always"] }, { code: "typeof a == 'number'", options: ["smart"] }, { code: "'string' != typeof a", options: ["smart"] }, { code: "'hello' != 'world'", options: ["smart"] }, @@ -36,11 +37,15 @@ ruleTester.run("eqeqeq", rule, { { code: "a == b", errors: [{ message: "Expected '===' and instead saw '=='.", type: "BinaryExpression"}] }, { code: "a != b", errors: [{ message: "Expected '!==' and instead saw '!='.", type: "BinaryExpression"}] }, { code: "typeof a == 'number'", errors: [{ message: "Expected '===' and instead saw '=='.", type: "BinaryExpression"}] }, + { code: "typeof a == 'number'", options: ["always"], errors: [{ message: "Expected '===' and instead saw '=='.", type: "BinaryExpression"}] }, { code: "'string' != typeof a", errors: [{ message: "Expected '!==' and instead saw '!='.", type: "BinaryExpression"}] }, { code: "true == true", errors: [{ message: "Expected '===' and instead saw '=='.", type: "BinaryExpression"}] }, { code: "2 == 3", errors: [{ message: "Expected '===' and instead saw '=='.", type: "BinaryExpression"}] }, + { code: "2 == 3", options: ["always"], errors: [{ message: "Expected '===' and instead saw '=='.", type: "BinaryExpression"}] }, { code: "'hello' != 'world'", errors: [{ message: "Expected '!==' and instead saw '!='.", type: "BinaryExpression"}] }, + { code: "'hello' != 'world'", options: ["always"], errors: [{ message: "Expected '!==' and instead saw '!='.", type: "BinaryExpression"}] }, { code: "a == null", errors: [{ message: "Expected '===' and instead saw '=='.", type: "BinaryExpression"}] }, + { code: "a == null", options: ["always"], errors: [{ message: "Expected '===' and instead saw '=='.", type: "BinaryExpression"}] }, { code: "null != a", errors: [{ message: "Expected '!==' and instead saw '!='.", type: "BinaryExpression"}] }, { code: "true == 1", options: ["smart"], errors: [{ message: "Expected '===' and instead saw '=='.", type: "BinaryExpression"}] }, { code: "0 != '1'", options: ["smart"], errors: [{ message: "Expected '!==' and instead saw '!='.", type: "BinaryExpression"}] },