-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rules)!: add tap-no-deprecated-aliases rule
BREAKING CHANGE: deprecated `tap-consistent-assertions` rule [email protected] deprecated the use of aliases for assertion methods, which invalidates the concept of a "preferred" alias. This adds a separate rule for enforcing the use of unaliased assertion methods. This rule is auto-fixable.
- Loading branch information
Showing
9 changed files
with
216 additions
and
38 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,18 +28,11 @@ the `eslint-plugin-` prefix. Then you can configure the rules you want to use: | |
"rules": { | ||
"logdna/grouped-require": 2, | ||
"logdna/require-file-extension": 2, | ||
"logdna/tap-no-deprecated-aliases": 2, | ||
"logdna/tap-consistent-assertions": [2, { | ||
"preferredMap": { | ||
"error": "error", | ||
"equal": "strictEqual", | ||
"not": "notStrictEqual", | ||
"same": "deepEqual", | ||
"notSame": "notDeepEqual", | ||
"strictSame": "strictDeepEqual", | ||
"strictNotSame": "strictDeepNotEqual", | ||
"match": "match", | ||
"notMatch": "notMatch", | ||
"type": "type" | ||
} | ||
}] | ||
} | ||
|
@@ -50,7 +43,7 @@ the `eslint-plugin-` prefix. Then you can configure the rules you want to use: | |
|
||
### `logdna/grouped-require` | ||
|
||
Enforce sorted require declarations within modules | ||
> Enforce sorted require declarations within modules | ||
```js | ||
// Bad | ||
|
@@ -73,9 +66,36 @@ const foo = require('./lib/foo.js') //local | |
* `typeOrder` [`<Array>`][] - sort order of require types | ||
(default: `['static', 'builtin', 'contrib', 'scoped', 'local']`) | ||
|
||
### `logdna/tap-consistent-assertions` | ||
### `logdna/tap-no-deprecated-aliases` | ||
|
||
Enforce consistent aliases for tap assertions | ||
> Prevent usage of deprecated tap aliases (>= [email protected]) | ||
Tap deprecated assertion aliases as of version [`15.0.0`](https://node-tap.org/changelog/#150---2021-03-30). | ||
This rule supersedes `logdna/tap-consistent-assertions` and will enforce the use of unaliased | ||
assertion methods. | ||
|
||
```js | ||
// Bad | ||
test('foo', async (t) => { | ||
t.is_equal(1, 1) | ||
t.strictEqual(1, 1) | ||
t.identical(1, 1) | ||
}) | ||
|
||
// Good | ||
test('foo', async (t) => { | ||
t.equal(1, 1) | ||
t.equal(1, 1) | ||
t.equal(1, 1) | ||
}) | ||
``` | ||
|
||
#### Options | ||
* `calleePattern` [`<String>`][] - pattern to match for tap's `Test` object (default: `/^t+$/`) | ||
|
||
### [**Deprecated**] `logdna/tap-consistent-assertions` | ||
|
||
> Enforce consistent aliases for tap assertions | ||
```js | ||
// { | ||
|
@@ -112,7 +132,7 @@ test('foo', async (t) => { | |
|
||
### `logdna/require-file-extension` | ||
|
||
Enforce file extension for local modules/files | ||
> Enforce file extension for local modules/files | ||
```js | ||
// Bad | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
'use strict' | ||
|
||
module.exports = function tapAssertionSelector(targetMap, calleePattern) { | ||
const matches = [] | ||
for (const method of targetMap.keys()) { | ||
matches.push(`[callee.property.name="${method}"]`) | ||
} | ||
|
||
const scopeSelector = `:function[params.0.name=${calleePattern}]` | ||
const calleeSelector = [ | ||
'[type="CallExpression"]' | ||
, '[callee.type="MemberExpression"]' | ||
, '[callee.computed=false]' | ||
, '[callee.property.type="Identifier"]' | ||
, `:matches(${matches.join(', ')})` | ||
, '[callee.object.type="Identifier"]' | ||
, `[callee.object.name=${calleePattern}]` | ||
].join('') | ||
|
||
return `${scopeSelector} ${calleeSelector}` | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
'use strict' | ||
|
||
const synonyms = require('../common/tap-synonyms.js') | ||
const tapAssertionSelector = require('../common/tap-assertion-selector.js') | ||
|
||
const DEFAULT_CALLEE_PATTERN = '/^t+$/' | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'suggestion' | ||
, fixable: 'code' | ||
, docs: { | ||
description: 'Prevent usage of deprecated tap aliases (>= [email protected])' | ||
, category: 'LogDNA' | ||
} | ||
, schema: [ | ||
{ | ||
type: 'object' | ||
, properties: { | ||
calleePattern: { | ||
type: 'string' | ||
} | ||
} | ||
, additionalProperties: false | ||
} | ||
] | ||
} | ||
, create(context) { | ||
const { | ||
calleePattern = DEFAULT_CALLEE_PATTERN | ||
} = context.options[0] || {} | ||
|
||
const targetMap = Object.keys(synonyms).reduce((map, primary) => { | ||
for (const alias of synonyms[primary]) { | ||
map.set(alias, primary) | ||
} | ||
return map | ||
}, new Map()) | ||
|
||
const selector = tapAssertionSelector(targetMap, calleePattern) | ||
return { | ||
[selector]: (node) => { | ||
const alias = node.callee.property.name | ||
const main = targetMap.get(alias) | ||
|
||
if (main && main !== alias) { | ||
context.report({ | ||
node | ||
, message: `The "${alias}" alias is deprecated in favor of "${main}"` | ||
, fix(fixer) { | ||
return fixer.replaceText(node.callee.property, main) | ||
} | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
'use strict' | ||
|
||
const {test} = require('tap') | ||
const {testRule} = require('../../common/bootstrap.js') | ||
const rules = require('../../../lib/rules/index.js') | ||
|
||
const RULE_NAME = 'tap-no-deprecated-aliases' | ||
|
||
test(RULE_NAME, async (t) => { | ||
testRule(t, RULE_NAME, rules[RULE_NAME], { | ||
valid: [ | ||
{ | ||
code: ` | ||
test('foo', async (t) => { | ||
t.same(true) | ||
}) | ||
` | ||
} | ||
, { | ||
code: ` | ||
test('foo', async (t) => { | ||
t.match(true) | ||
}) | ||
` | ||
} | ||
] | ||
, invalid: [ | ||
{ | ||
code: ` | ||
test('foo', async (t) => { | ||
t.deepEqual(true) | ||
}) | ||
` | ||
, output: ` | ||
test('foo', async (t) => { | ||
t.same(true) | ||
}) | ||
` | ||
, errors: [ | ||
{message: 'The "deepEqual" alias is deprecated in favor of "same"'} | ||
] | ||
} | ||
, { | ||
code: ` | ||
test('foo', async (tt) => { | ||
tt.loose_equal(true) | ||
}) | ||
` | ||
, output: ` | ||
test('foo', async (tt) => { | ||
tt.same(true) | ||
}) | ||
` | ||
, errors: [ | ||
{message: 'The "loose_equal" alias is deprecated in favor of "same"'} | ||
] | ||
} | ||
, { | ||
code: ` | ||
test('foo', async (tea) => { | ||
tea.tearDown(() => { | ||
return | ||
}) | ||
// unchanged - calleePattern does not match | ||
t.deepEqual(true) | ||
}) | ||
` | ||
, output: ` | ||
test('foo', async (tea) => { | ||
tea.teardown(() => { | ||
return | ||
}) | ||
// unchanged - calleePattern does not match | ||
t.deepEqual(true) | ||
}) | ||
` | ||
, options: [{ | ||
calleePattern: '/^tea+$/' | ||
}] | ||
, errors: [ | ||
{message: 'The "tearDown" alias is deprecated in favor of "teardown"'} | ||
] | ||
} | ||
] | ||
}) | ||
}) |