|
| 1 | +const dontEnumDesc = { value: 1, writable: true, enumerable: false, configurable: true }; |
| 2 | +const testCases = [ |
| 3 | + { |
| 4 | + name: "Object", |
| 5 | + createObject: () => Object.create(null, { foo: dontEnumDesc, bar: dontEnumDesc }), |
| 6 | + dontEnumKeys: ["foo", "bar"], |
| 7 | + }, |
| 8 | + { |
| 9 | + name: "Error", |
| 10 | + createObject: () => new Error(), |
| 11 | + dontEnumKeys: ["line", "column", "sourceURL", "stack"], |
| 12 | + }, |
| 13 | + { |
| 14 | + name: "Array (empty)", |
| 15 | + createObject: () => [], |
| 16 | + dontEnumKeys: ["length"], |
| 17 | + }, |
| 18 | + { |
| 19 | + name: "Array (sparse)", |
| 20 | + createObject: () => Object.defineProperties([0, 1, 2], { 0: dontEnumDesc, 2: dontEnumDesc }), |
| 21 | + dontEnumKeys: ["0", "2", "length"], |
| 22 | + }, |
| 23 | + { |
| 24 | + name: "Function (strict)", |
| 25 | + createObject: () => function() { "use strict"; }, |
| 26 | + dontEnumKeys: ["length", "name", "prototype"], |
| 27 | + }, |
| 28 | + { |
| 29 | + name: "Function (non-strict)", |
| 30 | + createObject: () => function() {}, |
| 31 | + dontEnumKeys: ["arguments", "caller", "length", "name", "prototype"], |
| 32 | + }, |
| 33 | + { |
| 34 | + name: "RegExp", |
| 35 | + createObject: () => /(?:)/g, |
| 36 | + dontEnumKeys: ["lastIndex"], |
| 37 | + }, |
| 38 | + { |
| 39 | + name: "String", |
| 40 | + createObject: () => new String("foo"), |
| 41 | + dontEnumKeys: ["length"], |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "Arguments (strict)", |
| 45 | + createObject: function(foo) { "use strict"; return arguments; }, |
| 46 | + dontEnumKeys: ["length", "callee"], |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "Arguments (non-strict)", |
| 50 | + createObject: function(foo) { return arguments; }, |
| 51 | + dontEnumKeys: ["length", "callee"], |
| 52 | + }, |
| 53 | + { |
| 54 | + name: "Reflect", |
| 55 | + createObject: () => $vm.createGlobalObject().Reflect, |
| 56 | + dontEnumKeys: ["apply", "get", "has", "set"], |
| 57 | + }, |
| 58 | + { |
| 59 | + name: "Date.prototype", |
| 60 | + createObject: () => $vm.createGlobalObject().Date.prototype, |
| 61 | + dontEnumKeys: ["toISOString", "getTime", "setYear"], |
| 62 | + }, |
| 63 | +]; |
| 64 | + |
| 65 | +// basic tests |
| 66 | +for (const t of testCases) { |
| 67 | + assert(!contains(forIn(t.createObject()), t.dontEnumKeys), t.name); |
| 68 | + assert(!contains(Object.keys(t.createObject()), t.dontEnumKeys), t.name); |
| 69 | + |
| 70 | + assert(contains(Object.getOwnPropertyNames(t.createObject()), t.dontEnumKeys), t.name); |
| 71 | + assert(contains(Reflect.ownKeys(t.createObject()), t.dontEnumKeys), t.name); |
| 72 | +} |
| 73 | + |
| 74 | +// shadowing: DontEnum => Enum |
| 75 | +for (const t of testCases) { |
| 76 | + assert( |
| 77 | + !contains( |
| 78 | + forIn(makePrototypeChain(t.createObject(), makeObject(t.dontEnumKeys))), |
| 79 | + t.dontEnumKeys, |
| 80 | + ), |
| 81 | + t.name, |
| 82 | + ); |
| 83 | +} |
| 84 | + |
| 85 | +// shadowing: {} => DontEnum => {} => Enum |
| 86 | +for (const t of testCases) { |
| 87 | + assert( |
| 88 | + !contains( |
| 89 | + forIn(makePrototypeChain({}, t.createObject(), {}, makeObject(t.dontEnumKeys))), |
| 90 | + t.dontEnumKeys, |
| 91 | + ), |
| 92 | + t.name, |
| 93 | + ); |
| 94 | +} |
| 95 | + |
| 96 | +// shadowing: DontEnum => {} => Enum => {} => Enum |
| 97 | +for (const t of testCases) { |
| 98 | + assert( |
| 99 | + !contains( |
| 100 | + forIn(makePrototypeChain(t.createObject(), {}, makeObject(t.dontEnumKeys), {}, makeObject(t.dontEnumKeys))), |
| 101 | + t.dontEnumKeys, |
| 102 | + ), |
| 103 | + t.name, |
| 104 | + ); |
| 105 | +} |
| 106 | + |
| 107 | +// shadowing: {} => DontEnum (enumerable: true => false) => {} => Enum |
| 108 | +for (const t of testCases) { |
| 109 | + const dontEnumObject = t.createObject(); |
| 110 | + const target = makePrototypeChain({}, dontEnumObject, {}, makeObject(t.dontEnumKeys)); |
| 111 | + assert(!contains(forIn(target), t.dontEnumKeys), t.name); |
| 112 | + |
| 113 | + const enumKeys = t.dontEnumKeys.filter(key => Reflect.defineProperty(dontEnumObject, key, { enumerable: true })); |
| 114 | + assert(contains(forIn(target), enumKeys), t.name); |
| 115 | +} |
| 116 | + |
| 117 | +// shadowing: {} => DontEnum (delete non-enumerable keys) => {} => Enum |
| 118 | +for (const t of testCases) { |
| 119 | + const dontEnumObject = t.createObject(); |
| 120 | + const target = makePrototypeChain({}, dontEnumObject, {}, makeObject(t.dontEnumKeys)); |
| 121 | + assert(!contains(forIn(target), t.dontEnumKeys), t.name); |
| 122 | + |
| 123 | + const enumKeys = t.dontEnumKeys.filter(key => Reflect.deleteProperty(dontEnumObject, key)); |
| 124 | + assert(contains(forIn(target), enumKeys), t.name); |
| 125 | +} |
| 126 | + |
| 127 | +// shadowing: {} => DontEnum (materialized) => {} => Enum |
| 128 | +for (const t of testCases) { |
| 129 | + const dontEnumObject = t.createObject(); |
| 130 | + const target = makePrototypeChain({}, dontEnumObject, {}, makeObject(t.dontEnumKeys)); |
| 131 | + assert(!contains(forIn(target), t.dontEnumKeys), t.name); |
| 132 | + |
| 133 | + const dontEnumKeys = t.dontEnumKeys.filter(key => { |
| 134 | + const desc = Object.getOwnPropertyDescriptor(dontEnumObject, key); |
| 135 | + if (!Reflect.deleteProperty(dontEnumObject, key)) return false; |
| 136 | + Object.defineProperty(dontEnumObject, key, desc); |
| 137 | + return true; |
| 138 | + }); |
| 139 | + |
| 140 | + if (dontEnumKeys.length) |
| 141 | + assert(!contains(forIn(target), dontEnumKeys), t.name); |
| 142 | +} |
| 143 | + |
| 144 | +// helpers |
| 145 | +function assert(value, name) { |
| 146 | + if (!value) |
| 147 | + throw new Error(`Bad value: ${value}. Test case: ${name}.`); |
| 148 | +} |
| 149 | + |
| 150 | +function contains(array, subarray) { |
| 151 | + return subarray.every(item => array.includes(item)); |
| 152 | +} |
| 153 | + |
| 154 | +function forIn(object) { |
| 155 | + const keys = []; |
| 156 | + for (const key in object) |
| 157 | + keys.push(key); |
| 158 | + return keys; |
| 159 | +} |
| 160 | + |
| 161 | +function makePrototypeChain(...objects) { |
| 162 | + objects.reduce((object, prototype) => { |
| 163 | + Object.setPrototypeOf(object, prototype); |
| 164 | + return prototype; |
| 165 | + }); |
| 166 | + return objects[0]; |
| 167 | +} |
| 168 | + |
| 169 | +function makeObject(keys) { |
| 170 | + const object = {}; |
| 171 | + for (const key of keys) |
| 172 | + object[key] = key; |
| 173 | + return object; |
| 174 | +} |
0 commit comments