|
3 | 3 | const PROMISE_STATICS = require('./lib/promise-statics')
|
4 | 4 | const getDocsUrl = require('./lib/get-docs-url')
|
5 | 5 |
|
| 6 | +const PROMISE_INSTANCE_METHODS = new Set(['then', 'catch', 'finally']) |
| 7 | + |
| 8 | +function isPermittedProperty(expression, standardSet, allowedMethods) { |
| 9 | + // istanbul ignore if |
| 10 | + if (expression.type !== 'MemberExpression') return false |
| 11 | + |
| 12 | + if (expression.property.type === 'Literal') |
| 13 | + return ( |
| 14 | + standardSet.has(expression.property.value) || |
| 15 | + allowedMethods.includes(expression.property.value) |
| 16 | + ) |
| 17 | + |
| 18 | + // istanbul ignore else |
| 19 | + if (expression.property.type === 'Identifier') |
| 20 | + return ( |
| 21 | + expression.computed || |
| 22 | + standardSet.has(expression.property.name) || |
| 23 | + allowedMethods.includes(expression.property.name) |
| 24 | + ) |
| 25 | + |
| 26 | + // istanbul ignore next |
| 27 | + return false |
| 28 | +} |
| 29 | + |
6 | 30 | module.exports = {
|
7 | 31 | meta: {
|
8 | 32 | type: 'problem',
|
@@ -36,13 +60,20 @@ module.exports = {
|
36 | 60 | if (
|
37 | 61 | node.object.type === 'Identifier' &&
|
38 | 62 | node.object.name === 'Promise' &&
|
39 |
| - !(node.property.name in PROMISE_STATICS) && |
40 |
| - !allowedMethods.includes(node.property.name) |
| 63 | + ((node.property.name !== 'prototype' && |
| 64 | + !isPermittedProperty(node, PROMISE_STATICS, allowedMethods)) || |
| 65 | + (node.property.name === 'prototype' && |
| 66 | + node.parent.type === 'MemberExpression' && |
| 67 | + !isPermittedProperty( |
| 68 | + node.parent, |
| 69 | + PROMISE_INSTANCE_METHODS, |
| 70 | + allowedMethods, |
| 71 | + ))) |
41 | 72 | ) {
|
42 | 73 | context.report({
|
43 | 74 | node,
|
44 | 75 | messageId: 'avoidNonStandard',
|
45 |
| - data: { name: node.property.name }, |
| 76 | + data: { name: node.property.name ?? node.property.value }, |
46 | 77 | })
|
47 | 78 | }
|
48 | 79 | },
|
|
0 commit comments