|
| 1 | +import { TSESLint } from '@typescript-eslint/utils'; |
| 2 | +import { getTypeServices } from '../etc'; |
| 3 | +import { ruleCreator } from '../utils'; |
| 4 | + |
| 5 | +// The implementation of this rule is similar to typescript-eslint's no-misused-promises. MIT License. |
| 6 | +// https://github.com/typescript-eslint/typescript-eslint/blob/fcd6cf063a774f73ea00af23705117a197f826d4/packages/eslint-plugin/src/rules/no-misused-promises.ts |
| 7 | + |
| 8 | +const defaultOptions: readonly { |
| 9 | + checksSpreads?: boolean; |
| 10 | +}[] = []; |
| 11 | + |
| 12 | +export const noMisusedObservablesRule = ruleCreator({ |
| 13 | + defaultOptions, |
| 14 | + meta: { |
| 15 | + docs: { |
| 16 | + description: 'Disallow Observables in places not designed to handle them.', |
| 17 | + requiresTypeChecking: true, |
| 18 | + }, |
| 19 | + messages: { |
| 20 | + forbiddenSpread: 'Expected a non-Observable value to be spread into an object.', |
| 21 | + }, |
| 22 | + schema: [ |
| 23 | + { |
| 24 | + properties: { |
| 25 | + checksSpreads: { type: 'boolean', default: true, description: 'Disallow `...` spreading an Observable.' }, |
| 26 | + }, |
| 27 | + type: 'object', |
| 28 | + }, |
| 29 | + ], |
| 30 | + type: 'problem', |
| 31 | + }, |
| 32 | + name: 'no-misused-observables', |
| 33 | + create: (context) => { |
| 34 | + const { couldBeObservable } = getTypeServices(context); |
| 35 | + const [config = {}] = context.options; |
| 36 | + const { checksSpreads = true } = config; |
| 37 | + |
| 38 | + const spreadChecks: TSESLint.RuleListener = { |
| 39 | + SpreadElement: (node) => { |
| 40 | + if (couldBeObservable(node.argument)) { |
| 41 | + context.report({ |
| 42 | + messageId: 'forbiddenSpread', |
| 43 | + node: node.argument, |
| 44 | + }); |
| 45 | + } |
| 46 | + }, |
| 47 | + }; |
| 48 | + |
| 49 | + return { |
| 50 | + ...(checksSpreads ? spreadChecks : {}), |
| 51 | + }; |
| 52 | + }, |
| 53 | +}); |
0 commit comments