|
| 1 | +import { RuleTester } from "@typescript-eslint/rule-tester"; |
| 2 | +import preferActiveDocRule from "../lib/rules/preferActiveDoc.js"; |
| 3 | + |
| 4 | +const ruleTester = new RuleTester(); |
| 5 | + |
| 6 | +ruleTester.run("prefer-active-doc", preferActiveDocRule, { |
| 7 | + valid: [ |
| 8 | + { |
| 9 | + name: "activeDocument is allowed", |
| 10 | + code: "activeDocument.createElement('div');", |
| 11 | + }, |
| 12 | + { |
| 13 | + name: "activeWindow is allowed", |
| 14 | + code: "activeWindow.requestAnimationFrame(() => {});", |
| 15 | + }, |
| 16 | + { |
| 17 | + name: "property named document on an object is allowed", |
| 18 | + code: "const obj = { document: 1 }; obj.document;", |
| 19 | + }, |
| 20 | + { |
| 21 | + name: "property named window on an object is allowed", |
| 22 | + code: "const obj = { window: 1 }; obj.window;", |
| 23 | + }, |
| 24 | + { |
| 25 | + name: "local variable named document is allowed", |
| 26 | + code: "const document = activeDocument; document.createElement('div');", |
| 27 | + }, |
| 28 | + { |
| 29 | + name: "local variable named window is allowed", |
| 30 | + code: "const window = activeWindow; window.setTimeout(() => {}, 0);", |
| 31 | + }, |
| 32 | + { |
| 33 | + name: "typeof window check is allowed", |
| 34 | + code: "if (typeof window !== 'undefined') {}", |
| 35 | + }, |
| 36 | + { |
| 37 | + name: "typeof document check is allowed", |
| 38 | + code: "if (typeof document !== 'undefined') {}", |
| 39 | + }, |
| 40 | + { |
| 41 | + name: "typeof globalThis check is allowed", |
| 42 | + code: "if (typeof globalThis !== 'undefined') {}", |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "property named global on an object is allowed", |
| 46 | + code: "const obj = { global: 1 }; obj.global;", |
| 47 | + }, |
| 48 | + ], |
| 49 | + invalid: [ |
| 50 | + { |
| 51 | + name: "bare document reference is forbidden", |
| 52 | + code: "document.createElement('div');", |
| 53 | + output: "activeDocument.createElement('div');", |
| 54 | + errors: [{ messageId: "preferActive", data: { original: "document", replacement: "activeDocument" } }], |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "bare window reference is forbidden", |
| 58 | + code: "window.requestAnimationFrame(() => {});", |
| 59 | + output: "activeWindow.requestAnimationFrame(() => {});", |
| 60 | + errors: [{ messageId: "preferActive", data: { original: "window", replacement: "activeWindow" } }], |
| 61 | + }, |
| 62 | + { |
| 63 | + name: "document.body is forbidden", |
| 64 | + code: "const body = document.body;", |
| 65 | + output: "const body = activeDocument.body;", |
| 66 | + errors: [{ messageId: "preferActive" }], |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "window.innerWidth is forbidden", |
| 70 | + code: "const width = window.innerWidth;", |
| 71 | + output: "const width = activeWindow.innerWidth;", |
| 72 | + errors: [{ messageId: "preferActive" }], |
| 73 | + }, |
| 74 | + { |
| 75 | + name: "document.querySelector is forbidden", |
| 76 | + code: "document.querySelector('.my-class');", |
| 77 | + output: "activeDocument.querySelector('.my-class');", |
| 78 | + errors: [{ messageId: "preferActive" }], |
| 79 | + }, |
| 80 | + { |
| 81 | + name: "document.addEventListener is forbidden", |
| 82 | + code: "document.addEventListener('click', handler);", |
| 83 | + output: "activeDocument.addEventListener('click', handler);", |
| 84 | + errors: [{ messageId: "preferActive" }], |
| 85 | + }, |
| 86 | + { |
| 87 | + name: "window.setTimeout is forbidden", |
| 88 | + code: "window.setTimeout(() => {}, 100);", |
| 89 | + output: "activeWindow.setTimeout(() => {}, 100);", |
| 90 | + errors: [{ messageId: "preferActive" }], |
| 91 | + }, |
| 92 | + { |
| 93 | + name: "globalThis reference is forbidden", |
| 94 | + code: "globalThis.setTimeout(() => {}, 100);", |
| 95 | + errors: [{ messageId: "avoidGlobal", data: { name: "globalThis" } }], |
| 96 | + }, |
| 97 | + { |
| 98 | + name: "global reference is forbidden", |
| 99 | + code: "global.process;", |
| 100 | + errors: [{ messageId: "avoidGlobal", data: { name: "global" } }], |
| 101 | + }, |
| 102 | + ], |
| 103 | +}); |
0 commit comments