| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 43956e9 commit 4f4bfbe
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,31 +7,46 @@ const rule = require('../../tools/eslint-rules/prefer-assert-methods'); | |||
| 7 | 7 | ||
| 8 | 8 | new RuleTester().run('prefer-assert-methods', rule, { | |
| 9 | 9 | valid: [ | |
| 10 | - 'assert.strictEqual(foo, bar)', | ||
| 11 | - 'assert(foo === bar && baz)' | ||
| 10 | + 'assert.strictEqual(foo, bar);', | ||
| 11 | + 'assert(foo === bar && baz);', | ||
| 12 | + 'assert.notStrictEqual(foo, bar);', | ||
| 13 | + 'assert(foo !== bar && baz);', | ||
| 14 | + 'assert.equal(foo, bar);', | ||
| 15 | + 'assert(foo == bar && baz);', | ||
| 16 | + 'assert.notEqual(foo, bar);', | ||
| 17 | + 'assert(foo != bar && baz);', | ||
| 18 | + 'assert.ok(foo);', | ||
| 19 | + 'assert.ok(foo != bar);', | ||
| 20 | + 'assert.ok(foo === bar && baz);' | ||
| 12 | 21 | ], | |
| 13 | 22 | invalid: [ | |
| 14 | 23 | { | |
| 15 | - code: 'assert(foo == bar)', | ||
| 16 | - errors: [{ message: "'assert.equal' should be used instead of '=='" }] | ||
| 24 | + code: 'assert(foo == bar);', | ||
| 25 | + errors: [{ | ||
| 26 | + message: "'assert.equal' should be used instead of '=='" | ||
| 27 | + }], | ||
| 28 | + output: 'assert.equal(foo, bar);' | ||
| 17 | 29 | }, | |
| 18 | 30 | { | |
| 19 | - code: 'assert(foo === bar)', | ||
| 31 | + code: 'assert(foo === bar);', | ||
| 20 | 32 | errors: [{ | |
| 21 | 33 | message: "'assert.strictEqual' should be used instead of '==='" | |
| 22 | - }] | ||
| 34 | + }], | ||
| 35 | + output: 'assert.strictEqual(foo, bar);' | ||
| 23 | 36 | }, | |
| 24 | 37 | { | |
| 25 | - code: 'assert(foo != bar)', | ||
| 38 | + code: 'assert(foo != bar);', | ||
| 26 | 39 | errors: [{ | |
| 27 | 40 | message: "'assert.notEqual' should be used instead of '!='" | |
| 28 | - }] | ||
| 41 | + }], | ||
| 42 | + output: 'assert.notEqual(foo, bar);' | ||
| 29 | 43 | }, | |
| 30 | 44 | { | |
| 31 | - code: 'assert(foo !== bar)', | ||
| 45 | + code: 'assert(foo !== bar);', | ||
| 32 | 46 | errors: [{ | |
| 33 | 47 | message: "'assert.notStrictEqual' should be used instead of '!=='" | |
| 34 | - }] | ||
| 35 | - }, | ||
| 48 | + }], | ||
| 49 | + output: 'assert.notStrictEqual(foo, bar);' | ||
| 50 | + } | ||
| 36 | 51 | ] | |
| 37 | 52 | }); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,3 +1,7 @@ | |||
| 1 | + /** | ||
| 2 | + * @fileoverview Prohibit the use of assert operators ( ===, !==, ==, != ) | ||
| 3 | + */ | ||
| 4 | + | ||
| 1 | 5 | 'use strict'; | |
| 2 | 6 | ||
| 3 | 7 | const astSelector = 'ExpressionStatement[expression.type="CallExpression"]' + | |
@@ -21,7 +25,19 @@ module.exports = function(context) { | |||
| 21 | 25 | const arg = node.expression.arguments[0]; | |
| 22 | 26 | const assertMethod = preferedAssertMethod[arg.operator]; | |
| 23 | 27 | if (assertMethod) { | |
| 24 | - context.report(node, parseError(assertMethod, arg.operator)); | ||
| 28 | + context.report({ | ||
| 29 | + node, | ||
| 30 | + message: parseError(assertMethod, arg.operator), | ||
| 31 | + fix: (fixer) => { | ||
| 32 | + const sourceCode = context.getSourceCode(); | ||
| 33 | + const left = sourceCode.getText(arg.left); | ||
| 34 | + const right = sourceCode.getText(arg.right); | ||
| 35 | + return fixer.replaceText( | ||
| 36 | + node, | ||
| 37 | + `assert.${assertMethod}(${left}, ${right});` | ||
| 38 | + ); | ||
| 39 | + } | ||
| 40 | + }); | ||
| 25 | 41 | } | |
| 26 | 42 | } | |
| 27 | 43 | }; | |
| Back | FazBrowse Home | New Git URL |
0 commit comments