|
| 1 | +/** |
| 2 | + * @author Pixel998 |
| 3 | + * See LICENSE file in root directory for full license. |
| 4 | + */ |
| 5 | +"use strict" |
| 6 | + |
| 7 | +const RuleTester = require("#test-helpers").RuleTester |
| 8 | +const rule = require("../../../../lib/rules/prefer-global/timers") |
| 9 | + |
| 10 | +const provideModuleMethods = ["require", "process.getBuiltinModule"] |
| 11 | + |
| 12 | +new RuleTester().run("prefer-global/timers", rule, { |
| 13 | + valid: [ |
| 14 | + "clearImmediate(id)", |
| 15 | + "clearInterval(id)", |
| 16 | + "clearTimeout(id)", |
| 17 | + "setImmediate(() => {})", |
| 18 | + "setInterval(() => {}, 1000)", |
| 19 | + "setTimeout(() => {}, 1000)", |
| 20 | + { |
| 21 | + code: "clearImmediate(id)", |
| 22 | + options: ["always"], |
| 23 | + }, |
| 24 | + { |
| 25 | + code: "clearInterval(id)", |
| 26 | + options: ["always"], |
| 27 | + }, |
| 28 | + { |
| 29 | + code: "clearTimeout(id)", |
| 30 | + options: ["always"], |
| 31 | + }, |
| 32 | + { |
| 33 | + code: "setImmediate(() => {})", |
| 34 | + options: ["always"], |
| 35 | + }, |
| 36 | + { |
| 37 | + code: "setInterval(() => {}, 1000)", |
| 38 | + options: ["always"], |
| 39 | + }, |
| 40 | + { |
| 41 | + code: "setTimeout(() => {}, 1000)", |
| 42 | + options: ["always"], |
| 43 | + }, |
| 44 | + ...provideModuleMethods.flatMap(method => [ |
| 45 | + { |
| 46 | + code: `const { clearImmediate } = ${method}('timers'); clearImmediate(id)`, |
| 47 | + options: ["never"], |
| 48 | + }, |
| 49 | + { |
| 50 | + code: `const { clearImmediate } = ${method}('node:timers'); clearImmediate(id)`, |
| 51 | + options: ["never"], |
| 52 | + }, |
| 53 | + { |
| 54 | + code: `const { clearInterval } = ${method}('timers'); clearInterval(id)`, |
| 55 | + options: ["never"], |
| 56 | + }, |
| 57 | + { |
| 58 | + code: `const { clearInterval } = ${method}('node:timers'); clearInterval(id)`, |
| 59 | + options: ["never"], |
| 60 | + }, |
| 61 | + { |
| 62 | + code: `const { clearTimeout } = ${method}('timers'); clearTimeout(id)`, |
| 63 | + options: ["never"], |
| 64 | + }, |
| 65 | + { |
| 66 | + code: `const { clearTimeout } = ${method}('node:timers'); clearTimeout(id)`, |
| 67 | + options: ["never"], |
| 68 | + }, |
| 69 | + { |
| 70 | + code: `const { setImmediate } = ${method}('timers'); setImmediate(() => {})`, |
| 71 | + options: ["never"], |
| 72 | + }, |
| 73 | + { |
| 74 | + code: `const { setImmediate } = ${method}('node:timers'); setImmediate(() => {})`, |
| 75 | + options: ["never"], |
| 76 | + }, |
| 77 | + { |
| 78 | + code: `const { setInterval } = ${method}('timers'); setInterval(() => {}, 1000)`, |
| 79 | + options: ["never"], |
| 80 | + }, |
| 81 | + { |
| 82 | + code: `const { setInterval } = ${method}('node:timers'); setInterval(() => {}, 1000)`, |
| 83 | + options: ["never"], |
| 84 | + }, |
| 85 | + { |
| 86 | + code: `const { setTimeout } = ${method}('timers'); setTimeout(() => {}, 1000)`, |
| 87 | + options: ["never"], |
| 88 | + }, |
| 89 | + { |
| 90 | + code: `const { setTimeout } = ${method}('node:timers'); setTimeout(() => {}, 1000)`, |
| 91 | + options: ["never"], |
| 92 | + }, |
| 93 | + ]), |
| 94 | + ], |
| 95 | + invalid: [ |
| 96 | + ...provideModuleMethods.flatMap(method => [ |
| 97 | + { |
| 98 | + code: `const { clearImmediate } = ${method}('timers'); clearImmediate(id)`, |
| 99 | + errors: [{ messageId: "preferGlobal" }], |
| 100 | + }, |
| 101 | + { |
| 102 | + code: `const { clearImmediate } = ${method}('node:timers'); clearImmediate(id)`, |
| 103 | + errors: [{ messageId: "preferGlobal" }], |
| 104 | + }, |
| 105 | + { |
| 106 | + code: `const { clearImmediate } = ${method}('timers'); clearImmediate(id)`, |
| 107 | + options: ["always"], |
| 108 | + errors: [{ messageId: "preferGlobal" }], |
| 109 | + }, |
| 110 | + { |
| 111 | + code: `const { clearImmediate } = ${method}('node:timers'); clearImmediate(id)`, |
| 112 | + options: ["always"], |
| 113 | + errors: [{ messageId: "preferGlobal" }], |
| 114 | + }, |
| 115 | + { |
| 116 | + code: `const { clearInterval } = ${method}('timers'); clearInterval(id)`, |
| 117 | + errors: [{ messageId: "preferGlobal" }], |
| 118 | + }, |
| 119 | + { |
| 120 | + code: `const { clearInterval } = ${method}('node:timers'); clearInterval(id)`, |
| 121 | + errors: [{ messageId: "preferGlobal" }], |
| 122 | + }, |
| 123 | + { |
| 124 | + code: `const { clearInterval } = ${method}('timers'); clearInterval(id)`, |
| 125 | + options: ["always"], |
| 126 | + errors: [{ messageId: "preferGlobal" }], |
| 127 | + }, |
| 128 | + { |
| 129 | + code: `const { clearInterval } = ${method}('node:timers'); clearInterval(id)`, |
| 130 | + options: ["always"], |
| 131 | + errors: [{ messageId: "preferGlobal" }], |
| 132 | + }, |
| 133 | + { |
| 134 | + code: `const { clearTimeout } = ${method}('timers'); clearTimeout(id)`, |
| 135 | + errors: [{ messageId: "preferGlobal" }], |
| 136 | + }, |
| 137 | + { |
| 138 | + code: `const { clearTimeout } = ${method}('node:timers'); clearTimeout(id)`, |
| 139 | + errors: [{ messageId: "preferGlobal" }], |
| 140 | + }, |
| 141 | + { |
| 142 | + code: `const { clearTimeout } = ${method}('timers'); clearTimeout(id)`, |
| 143 | + options: ["always"], |
| 144 | + errors: [{ messageId: "preferGlobal" }], |
| 145 | + }, |
| 146 | + { |
| 147 | + code: `const { clearTimeout } = ${method}('node:timers'); clearTimeout(id)`, |
| 148 | + options: ["always"], |
| 149 | + errors: [{ messageId: "preferGlobal" }], |
| 150 | + }, |
| 151 | + { |
| 152 | + code: `const { setImmediate } = ${method}('timers'); setImmediate(() => {})`, |
| 153 | + errors: [{ messageId: "preferGlobal" }], |
| 154 | + }, |
| 155 | + { |
| 156 | + code: `const { setImmediate } = ${method}('node:timers'); setImmediate(() => {})`, |
| 157 | + errors: [{ messageId: "preferGlobal" }], |
| 158 | + }, |
| 159 | + { |
| 160 | + code: `const { setImmediate } = ${method}('timers'); setImmediate(() => {})`, |
| 161 | + options: ["always"], |
| 162 | + errors: [{ messageId: "preferGlobal" }], |
| 163 | + }, |
| 164 | + { |
| 165 | + code: `const { setImmediate } = ${method}('node:timers'); setImmediate(() => {})`, |
| 166 | + options: ["always"], |
| 167 | + errors: [{ messageId: "preferGlobal" }], |
| 168 | + }, |
| 169 | + { |
| 170 | + code: `const { setInterval } = ${method}('timers'); setInterval(() => {}, 1000)`, |
| 171 | + errors: [{ messageId: "preferGlobal" }], |
| 172 | + }, |
| 173 | + { |
| 174 | + code: `const { setInterval } = ${method}('node:timers'); setInterval(() => {}, 1000)`, |
| 175 | + errors: [{ messageId: "preferGlobal" }], |
| 176 | + }, |
| 177 | + { |
| 178 | + code: `const { setInterval } = ${method}('timers'); setInterval(() => {}, 1000)`, |
| 179 | + options: ["always"], |
| 180 | + errors: [{ messageId: "preferGlobal" }], |
| 181 | + }, |
| 182 | + { |
| 183 | + code: `const { setInterval } = ${method}('node:timers'); setInterval(() => {}, 1000)`, |
| 184 | + options: ["always"], |
| 185 | + errors: [{ messageId: "preferGlobal" }], |
| 186 | + }, |
| 187 | + { |
| 188 | + code: `const { setTimeout } = ${method}('timers'); setTimeout(() => {}, 1000)`, |
| 189 | + errors: [{ messageId: "preferGlobal" }], |
| 190 | + }, |
| 191 | + { |
| 192 | + code: `const { setTimeout } = ${method}('node:timers'); setTimeout(() => {}, 1000)`, |
| 193 | + errors: [{ messageId: "preferGlobal" }], |
| 194 | + }, |
| 195 | + { |
| 196 | + code: `const { setTimeout } = ${method}('timers'); setTimeout(() => {}, 1000)`, |
| 197 | + options: ["always"], |
| 198 | + errors: [{ messageId: "preferGlobal" }], |
| 199 | + }, |
| 200 | + { |
| 201 | + code: `const { setTimeout } = ${method}('node:timers'); setTimeout(() => {}, 1000)`, |
| 202 | + options: ["always"], |
| 203 | + errors: [{ messageId: "preferGlobal" }], |
| 204 | + }, |
| 205 | + ]), |
| 206 | + { |
| 207 | + code: "clearImmediate(id)", |
| 208 | + options: ["never"], |
| 209 | + errors: [{ messageId: "preferModule" }], |
| 210 | + }, |
| 211 | + { |
| 212 | + code: "clearInterval(id)", |
| 213 | + options: ["never"], |
| 214 | + errors: [{ messageId: "preferModule" }], |
| 215 | + }, |
| 216 | + { |
| 217 | + code: "clearTimeout(id)", |
| 218 | + options: ["never"], |
| 219 | + errors: [{ messageId: "preferModule" }], |
| 220 | + }, |
| 221 | + { |
| 222 | + code: "setImmediate(() => {})", |
| 223 | + options: ["never"], |
| 224 | + errors: [{ messageId: "preferModule" }], |
| 225 | + }, |
| 226 | + { |
| 227 | + code: "setInterval(() => {}, 1000)", |
| 228 | + options: ["never"], |
| 229 | + errors: [{ messageId: "preferModule" }], |
| 230 | + }, |
| 231 | + { |
| 232 | + code: "setTimeout(() => {}, 1000)", |
| 233 | + options: ["never"], |
| 234 | + errors: [{ messageId: "preferModule" }], |
| 235 | + }, |
| 236 | + ], |
| 237 | +}) |
0 commit comments