|
| 1 | +'use strict'; |
| 2 | +const {getStaticValue} = require('eslint-utils'); |
| 3 | +const {methodCallSelector} = require('./selectors/index.js'); |
| 4 | +const getPropertyName = require('./utils/get-property-name.js'); |
| 5 | +const getKeyName = require('./utils/get-key-name.js'); |
| 6 | + |
| 7 | +const MESSAGE_ID_OBJECT = 'no-thenable-object'; |
| 8 | +const MESSAGE_ID_EXPORT = 'no-thenable-export'; |
| 9 | +const MESSAGE_ID_CLASS = 'no-thenable-class'; |
| 10 | +const messages = { |
| 11 | + [MESSAGE_ID_OBJECT]: 'Do not add `then` to an object.', |
| 12 | + [MESSAGE_ID_EXPORT]: 'Do not export `then`.', |
| 13 | + [MESSAGE_ID_CLASS]: 'Do not add `then` to a class.', |
| 14 | +}; |
| 15 | + |
| 16 | +const isStringThen = (node, context) => { |
| 17 | + const result = getStaticValue(node, context.getScope()); |
| 18 | + |
| 19 | + return result && result.value === 'then'; |
| 20 | +}; |
| 21 | + |
| 22 | +const cases = [ |
| 23 | + // `{then() {}}`, |
| 24 | + // `{get then() {}}`, |
| 25 | + // `{[computedKey]() {}}`, |
| 26 | + // `{get [computedKey]() {}}`, |
| 27 | + { |
| 28 | + selector: 'ObjectExpression > Property.properties > .key', |
| 29 | + test: (node, context) => getKeyName(node.parent, context.getScope()) === 'then', |
| 30 | + messageId: MESSAGE_ID_OBJECT, |
| 31 | + }, |
| 32 | + // `class Foo {then}`, |
| 33 | + // `class Foo {static then}`, |
| 34 | + // `class Foo {get then() {}}`, |
| 35 | + // `class Foo {static get then() {}}`, |
| 36 | + { |
| 37 | + selector: ':matches(PropertyDefinition, MethodDefinition) > .key', |
| 38 | + test: (node, context) => getKeyName(node.parent, context.getScope()) === 'then', |
| 39 | + messageId: MESSAGE_ID_CLASS, |
| 40 | + }, |
| 41 | + // `foo.then = …` |
| 42 | + // `foo[computedKey] = …` |
| 43 | + { |
| 44 | + selector: 'AssignmentExpression > MemberExpression.left > .property', |
| 45 | + test: (node, context) => getPropertyName(node.parent, context.getScope()) === 'then', |
| 46 | + messageId: MESSAGE_ID_OBJECT, |
| 47 | + }, |
| 48 | + // `Object.defineProperty(foo, 'then', …)` |
| 49 | + // `Reflect.defineProperty(foo, 'then', …)` |
| 50 | + { |
| 51 | + selector: [ |
| 52 | + methodCallSelector({ |
| 53 | + objects: ['Object', 'Reflect'], |
| 54 | + method: 'defineProperty', |
| 55 | + minimumArguments: 3, |
| 56 | + }), |
| 57 | + '[arguments.0.type!="SpreadElement"]', |
| 58 | + ' > .arguments:nth-child(2)', |
| 59 | + ].join(''), |
| 60 | + test: isStringThen, |
| 61 | + messageId: MESSAGE_ID_OBJECT, |
| 62 | + }, |
| 63 | + // `Object.fromEntries(['then', …])` |
| 64 | + { |
| 65 | + selector: [ |
| 66 | + methodCallSelector({ |
| 67 | + object: 'Object', |
| 68 | + method: 'fromEntries', |
| 69 | + argumentsLength: 1, |
| 70 | + }), |
| 71 | + ' > ArrayExpression.arguments:nth-child(1)', |
| 72 | + ' > .elements:nth-child(1)', |
| 73 | + ].join(''), |
| 74 | + test: isStringThen, |
| 75 | + messageId: MESSAGE_ID_OBJECT, |
| 76 | + }, |
| 77 | + // `export {then}` |
| 78 | + { |
| 79 | + selector: 'ExportSpecifier.specifiers > Identifier.exported[name="then"]', |
| 80 | + messageId: MESSAGE_ID_EXPORT, |
| 81 | + }, |
| 82 | + // `export function then() {}`, |
| 83 | + // `export class then {}`, |
| 84 | + { |
| 85 | + selector: 'ExportNamedDeclaration > :matches(FunctionDeclaration, ClassDeclaration).declaration > Identifier[name="then"].id', |
| 86 | + messageId: MESSAGE_ID_EXPORT, |
| 87 | + }, |
| 88 | + // `export const … = …`; |
| 89 | + { |
| 90 | + selector: 'ExportNamedDeclaration > VariableDeclaration.declaration', |
| 91 | + messageId: MESSAGE_ID_EXPORT, |
| 92 | + getNodes: (node, context) => context.getDeclaredVariables(node).flatMap(({name, identifiers}) => name === 'then' ? identifiers : []), |
| 93 | + }, |
| 94 | +]; |
| 95 | + |
| 96 | +/** @param {import('eslint').Rule.RuleContext} context */ |
| 97 | +const create = context => Object.fromEntries( |
| 98 | + cases.map(({selector, test, messageId, getNodes}) => [ |
| 99 | + selector, |
| 100 | + function * (node) { |
| 101 | + if (getNodes) { |
| 102 | + for (const problematicNode of getNodes(node, context)) { |
| 103 | + yield {node: problematicNode, messageId}; |
| 104 | + } |
| 105 | + |
| 106 | + return; |
| 107 | + } |
| 108 | + |
| 109 | + if (test && !test(node, context)) { |
| 110 | + return; |
| 111 | + } |
| 112 | + |
| 113 | + yield {node, messageId}; |
| 114 | + }, |
| 115 | + ]), |
| 116 | +); |
| 117 | + |
| 118 | +/** @type {import('eslint').Rule.RuleModule} */ |
| 119 | +module.exports = { |
| 120 | + create, |
| 121 | + meta: { |
| 122 | + type: 'problem', |
| 123 | + docs: { |
| 124 | + description: 'Disallow `then` property.', |
| 125 | + }, |
| 126 | + schema: [], |
| 127 | + messages, |
| 128 | + }, |
| 129 | +}; |
0 commit comments