-
-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new rule
spec-only
to check for non-spec Promise methods (#502)
**What is the purpose of this pull request?** - [x] New rule **What changes did you make? (Give an overview)** Add a new rule `spec-only` to check for non-spec Promise methods Fixes #46.
- Loading branch information
Showing
8 changed files
with
143 additions
and
1 deletion.
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
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,57 @@ | ||
'use strict' | ||
|
||
const rule = require('../rules/spec-only') | ||
const { RuleTester } = require('./rule-tester') | ||
const ruleTester = new RuleTester() | ||
|
||
ruleTester.run('spec-only', rule, { | ||
valid: [ | ||
'Promise.resolve()', | ||
'Promise.reject()', | ||
'Promise.all()', | ||
'Promise.race()', | ||
'Promise.withResolvers()', | ||
'new Promise(function (resolve, reject) {})', | ||
'SomeClass.resolve()', | ||
'doSomething(Promise.all)', | ||
{ | ||
code: 'Promise.permittedMethod()', | ||
options: [ | ||
{ | ||
allowedMethods: ['permittedMethod'], | ||
}, | ||
], | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
code: 'Promise.done()', | ||
errors: [{ message: "Avoid using non-standard 'Promise.done'" }], | ||
}, | ||
{ | ||
code: 'Promise.something()', | ||
errors: [{ message: "Avoid using non-standard 'Promise.something'" }], | ||
}, | ||
{ | ||
code: 'new Promise.done()', | ||
errors: [{ message: "Avoid using non-standard 'Promise.done'" }], | ||
}, | ||
{ | ||
code: ` | ||
function foo() { | ||
var a = getA() | ||
return Promise.done(a) | ||
} | ||
`, | ||
errors: [{ message: "Avoid using non-standard 'Promise.done'" }], | ||
}, | ||
{ | ||
code: ` | ||
function foo() { | ||
getA(Promise.done) | ||
} | ||
`, | ||
errors: [{ message: "Avoid using non-standard 'Promise.done'" }], | ||
}, | ||
], | ||
}) |
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,24 @@ | ||
# Disallow use of non-standard Promise static methods (`promise/spec-only`) | ||
|
||
<!-- end auto-generated rule header --> | ||
|
||
It may become difficult to migrate code depending on non-standard Promise | ||
extensions. This rule reports any such method usage. | ||
|
||
## Valid | ||
|
||
```js | ||
const x = Promise.resolve('good') | ||
``` | ||
|
||
## Invalid | ||
|
||
```js | ||
const x = Promise.done('bad') | ||
``` | ||
|
||
## Options | ||
|
||
### `allowedMethods` | ||
|
||
An array of allowed non-standard methods. Defaults to an empty array. |
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 |
---|---|---|
|
@@ -7,4 +7,5 @@ module.exports = { | |
race: true, | ||
reject: true, | ||
resolve: true, | ||
withResolvers: true, | ||
} |
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,51 @@ | ||
'use strict' | ||
|
||
const PROMISE_STATICS = require('./lib/promise-statics') | ||
const getDocsUrl = require('./lib/get-docs-url') | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'Disallow use of non-standard Promise static methods.', | ||
url: getDocsUrl('spec-only'), | ||
}, | ||
schema: [ | ||
{ | ||
type: 'object', | ||
properties: { | ||
allowedMethods: { | ||
type: 'array', | ||
items: { | ||
type: 'string', | ||
}, | ||
}, | ||
}, | ||
additionalProperties: false, | ||
}, | ||
], | ||
messages: { | ||
avoidNonStandard: "Avoid using non-standard 'Promise.{{ name }}'", | ||
}, | ||
}, | ||
create(context) { | ||
const { allowedMethods = [] } = context.options[0] || {} | ||
|
||
return { | ||
MemberExpression(node) { | ||
if ( | ||
node.object.type === 'Identifier' && | ||
node.object.name === 'Promise' && | ||
!(node.property.name in PROMISE_STATICS) && | ||
!allowedMethods.includes(node.property.name) | ||
) { | ||
context.report({ | ||
node, | ||
messageId: 'avoidNonStandard', | ||
data: { name: node.property.name }, | ||
}) | ||
} | ||
}, | ||
} | ||
}, | ||
} |