Skip to content

Commit

Permalink
fix: permit appropriate computed member expressions and prototype access
Browse files Browse the repository at this point in the history
  • Loading branch information
brettz9 committed Aug 7, 2024
1 parent 55c78db commit 65a8b7d
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 14 deletions.
19 changes: 19 additions & 0 deletions __tests__/spec-only.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ ruleTester.run('spec-only', rule, {
'Promise.resolve()',
'Promise.reject()',
'Promise.all()',
'Promise["all"]',
'Promise[method];',
'Promise.race()',
'var ctch = Promise.prototype.catch',
'Promise.withResolvers()',
'new Promise(function (resolve, reject) {})',
'SomeClass.resolve()',
Expand All @@ -22,6 +25,14 @@ ruleTester.run('spec-only', rule, {
},
],
},
{
code: 'Promise.prototype.permittedInstanceMethod',
options: [
{
allowedMethods: ['permittedInstanceMethod'],
},
],
},
],
invalid: [
{
Expand Down Expand Up @@ -53,5 +64,13 @@ ruleTester.run('spec-only', rule, {
`,
errors: [{ message: "Avoid using non-standard 'Promise.done'" }],
},
{
code: `var done = Promise.prototype.done`,
errors: [{ message: "Avoid using non-standard 'Promise.prototype'" }],
},
{
code: `Promise["done"];`,
errors: [{ message: "Avoid using non-standard 'Promise.done'" }],
},
],
})
2 changes: 1 addition & 1 deletion rules/lib/is-promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function isPromise(expression) {
expression.callee.type === 'MemberExpression' &&
expression.callee.object.type === 'Identifier' &&
expression.callee.object.name === 'Promise' &&
PROMISE_STATICS[expression.callee.property.name] &&
PROMISE_STATICS.has(expression.callee.property.name) &&
expression.callee.property.name !== 'withResolvers')
)
}
Expand Down
18 changes: 9 additions & 9 deletions rules/lib/promise-statics.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'use strict'

module.exports = {
all: true,
allSettled: true,
any: true,
race: true,
reject: true,
resolve: true,
withResolvers: true,
}
module.exports = new Set([
'all',
'allSettled',
'any',
'race',
'reject',
'resolve',
'withResolvers',
])
2 changes: 1 addition & 1 deletion rules/no-new-statics.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module.exports = {
if (
node.callee.type === 'MemberExpression' &&
node.callee.object.name === 'Promise' &&
PROMISE_STATICS[node.callee.property.name]
PROMISE_STATICS.has(node.callee.property.name)
) {
context.report({
node,
Expand Down
14 changes: 11 additions & 3 deletions rules/spec-only.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
const PROMISE_STATICS = require('./lib/promise-statics')
const getDocsUrl = require('./lib/get-docs-url')

const PROMISE_INSTANCE_METHODS = new Set(['then', 'catch', 'finally'])

module.exports = {
meta: {
type: 'problem',
Expand Down Expand Up @@ -35,14 +37,20 @@ module.exports = {
MemberExpression(node) {
if (
node.object.type === 'Identifier' &&
(!node.computed || node.property.type === 'Literal') &&
node.object.name === 'Promise' &&
!(node.property.name in PROMISE_STATICS) &&
!allowedMethods.includes(node.property.name)
((node.property.name && !PROMISE_STATICS.has(node.property.name)) ||
(node.property.value &&
!PROMISE_STATICS.has(node.property.value))) &&
(node.property.name !== 'prototype' ||
(!PROMISE_INSTANCE_METHODS.has(node?.parent?.property?.name) &&
!allowedMethods.includes(node?.parent?.property?.name))) &&
!allowedMethods.includes(node.property.name ?? node.property.value)
) {
context.report({
node,
messageId: 'avoidNonStandard',
data: { name: node.property.name },
data: { name: node.property.name ?? node.property.value },
})
}
},
Expand Down

0 comments on commit 65a8b7d

Please sign in to comment.