1/* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16/// Global 17eval('console.log("foo")'); 18let inf = Infinity; 19let nan = NaN; 20isFinite(1); 21isNaN(2); 22parseFloat('3'); 23parseInt('4', 10); 24encodeURI(''); 25encodeURIComponent(''); 26decodeURI(''); 27decodeURIComponent(''); 28escape(''); 29unescape(''); 30 31global.eval('console.log("foo")'); 32globalThis.eval('console.log("foo")'); 33const evl = "eval('console.log(1)')"; 34const res: void = Function(evl)(); 35const arr1 = ['foo', 'foo', 'foo']; 36 37class C {} 38let c = new C(); 39interface Obj { 40 a: string; 41 b: number; 42 c: boolean; 43} 44const object1: Obj = { 45 a: 'somestring', 46 b: 42, 47 c: false, 48}; 49 50/// Object 51/// Object . __proto__ () 52/// Object . __defineGetter__ () 53/// Object . __defineSetter__ () 54/// Object . __lookupGetter__ () 55/// Object . __lookupSetter__ () 56Object.assign<C, C>(c, c); 57Object.create(c); 58Object.defineProperties<C>(c, {}); 59Object.defineProperty<C>(c, 'p', c); 60Object.entries<C>([]); 61Object.freeze(() => {}); 62Object.fromEntries<number>([]); // OK 63Object.getOwnPropertyDescriptor(c, 'p'); 64Object.getOwnPropertyDescriptors<C>(c); 65Object.getOwnPropertySymbols(c); 66Object.getPrototypeOf(c); 67Object.hasOwnProperty('p'); 68Object.is(c, c); 69Object.isExtensible(c); 70Object.isFrozen(c); 71Object.isPrototypeOf(c); 72Object.isSealed(c); 73Object.preventExtensions<C>(c); 74Object.propertyIsEnumerable('p'); 75Object.seal<C>(c); 76Object.setPrototypeOf(c, c); 77console.log(Object.getOwnPropertyNames(object1)); 78console.log(Object.hasOwn(object1, 'a')); 79console.log(Object.hasOwn(object1, 'toString')); 80console.log(Object.keys(object1)); 81console.log(Object.values(object1)); 82console.log(Object.entries(object1)); 83 84/// Reflect 85Reflect.apply<C, number[], void>(() => {}, c, []); 86Reflect.construct<number[], C>(C, []); 87Reflect.defineProperty(c, 'p', {}); 88Reflect.deleteProperty(c, 'p', ); 89Reflect.get<C, string>(c, 'p'); 90Reflect.getOwnPropertyDescriptor<C, string>(c, 'p'); 91Reflect.getPrototypeOf(c); 92Reflect.isExtensible(c); 93Reflect.preventExtensions(c); 94Reflect.setPrototypeOf(c, c); 95console.log(Reflect.has(object1, 'a')); 96console.log(Reflect.has(object1, 'toString')); 97console.log(Reflect.get(object1, 'a')); 98console.log(Reflect.get(object1, 'd')); 99console.log(Reflect.ownKeys(object1)); 100Reflect.set(object1, 'b', 43); 101Reflect.set(arr1, 2, 'bar'); 102 103/// Proxy 104let handler: ProxyHandler<C> = {}; 105if (handler.apply) handler.apply(c, c, []); 106if (handler.construct) handler.construct(c, [], () => {}); 107if (handler.defineProperty) handler.defineProperty(c, "prop", {}); 108if (handler.deleteProperty) handler.deleteProperty(c, "prop"); 109if (handler.get) handler.get(c, "prop", {}); 110if (handler.getOwnPropertyDescriptor) handler.getOwnPropertyDescriptor(c, "prop"); 111if (handler.getPrototypeOf) handler.getPrototypeOf(c); 112if (handler.has) handler.has(c, "prop"); 113if (handler.isExtensible) handler.isExtensible(c); 114if (handler.ownKeys) handler.ownKeys(c); 115if (handler.preventExtensions) handler.preventExtensions(c); 116if (handler.set) handler.set(c, "prop", 1, c); 117if (handler.setPrototypeOf) handler.setPrototypeOf(c, null); 118 119/// Array 120ArrayBuffer.isView({}); 121let a: number[] = []; 122let b = new ArrayBuffer(1); 123Array.isArray(a); 124 125// 'Object.assign' is allowed only with signature like: 'assign(target: Record<string, V>, ...source: Object[]): Record<String, V>' 126class C2 { a: number } 127class C3 { [k: string]: Object }; 128const rec: Record<string, number> = {'1': 1, '2': 2}; 129const rec2: Record<string, number> = Object.assign(rec, new C2()); // OK 130const rec3: C2 = Object.assign(rec, new C2()); // NOT OK, return type is 'C2' 131const rec4: Object = Object.assign(rec, new C2()); // NOT OK, return type is 'Object' 132const rec5 = Object.assign(rec, new C2()); // NOT OK, return type is intersection type 'Record<K,V> & C2' 133const rec6: Record<string, Object> = Object.assign(new C2(), new C3()); // NOT OK, target type is 'C2' 134Object.assign(rec, new C2()); // NOT OK, no return (context) type 135