|
6 | 6 | */ |
7 | 7 | 'use strict'; |
8 | 8 |
|
9 | | -const { RuleTester } = require('eslint'); |
| 9 | +const { testRule, testTypeScript } = require('../shared'); |
10 | 10 |
|
11 | | -const { ESLINT_TEST_CONFIG } = require('../shared'); |
12 | | -const rule = require('../../../lib/rules/no-async-operation'); |
| 11 | +// TODO: Type assertions break this rule |
13 | 12 |
|
14 | | -const ruleTester = new RuleTester(ESLINT_TEST_CONFIG); |
15 | | - |
16 | | -ruleTester.run('no-async-operations', rule, { |
| 13 | +testRule('no-async-operation', { |
17 | 14 | valid: [ |
18 | 15 | { |
19 | 16 | code: 'obj.setTimeout();', |
@@ -87,3 +84,78 @@ ruleTester.run('no-async-operations', rule, { |
87 | 84 | }, |
88 | 85 | ], |
89 | 86 | }); |
| 87 | + |
| 88 | +testTypeScript('no-async-operation', { |
| 89 | + valid: [ |
| 90 | + { |
| 91 | + code: 'obj.setTimeout();', |
| 92 | + }, |
| 93 | + { |
| 94 | + code: ` |
| 95 | + const obj: object = {}; |
| 96 | + obj.setTimeout(); |
| 97 | + `, |
| 98 | + }, |
| 99 | + { |
| 100 | + code: ` |
| 101 | + const setTimeout: Function = () => {}; |
| 102 | + setTimeout(); |
| 103 | + `, |
| 104 | + }, |
| 105 | + { |
| 106 | + code: ` |
| 107 | + const setTimeout: Function = () => {}; |
| 108 | +
|
| 109 | + function foo() { |
| 110 | + setTimeout(); |
| 111 | + } |
| 112 | + `, |
| 113 | + }, |
| 114 | + ], |
| 115 | + invalid: [ |
| 116 | + { |
| 117 | + code: ` |
| 118 | + setTimeout() satisfies number; |
| 119 | + setInterval() satisfies number; |
| 120 | + requestAnimationFrame() satisfies number; |
| 121 | + `, |
| 122 | + errors: [ |
| 123 | + { message: 'Restricted async operation "setTimeout"' }, |
| 124 | + { message: 'Restricted async operation "setInterval"' }, |
| 125 | + { message: 'Restricted async operation "requestAnimationFrame"' }, |
| 126 | + ], |
| 127 | + }, |
| 128 | + // { |
| 129 | + // code: ` |
| 130 | + // (window as any).setTimeout(); |
| 131 | + // (window as any).setInterval(); |
| 132 | + // (window as any).requestAnimationFrame(); |
| 133 | + // `, |
| 134 | + // errors: [ |
| 135 | + // { message: 'Restricted async operation "setTimeout"' }, |
| 136 | + // { message: 'Restricted async operation "setInterval"' }, |
| 137 | + // { message: 'Restricted async operation "requestAnimationFrame"' }, |
| 138 | + // ], |
| 139 | + // }, |
| 140 | + // { |
| 141 | + // code: ` |
| 142 | + // (window as any)["setTimeout"](); |
| 143 | + // (window as any)["setInterval"](); |
| 144 | + // (window as any)["requestAnimationFrame"](); |
| 145 | + // `, |
| 146 | + // errors: [ |
| 147 | + // { message: 'Restricted async operation "setTimeout"' }, |
| 148 | + // { message: 'Restricted async operation "setInterval"' }, |
| 149 | + // { message: 'Restricted async operation "requestAnimationFrame"' }, |
| 150 | + // ], |
| 151 | + // }, |
| 152 | + { |
| 153 | + code: ` |
| 154 | + function foo(): void { |
| 155 | + setTimeout(); |
| 156 | + } |
| 157 | + `, |
| 158 | + errors: [{ message: 'Restricted async operation "setTimeout"' }], |
| 159 | + }, |
| 160 | + ], |
| 161 | +}); |
0 commit comments