1 // Copyright 2015 the V8 project authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 (function testVariableDeclarationsFunction() { 6 'use strict'; 7 var a = function(){}; 8 assertEquals('a', a.name); 9 let b = () => {}; 10 assertEquals('b', b.name); 11 const c = ((function(){})); 12 assertEquals('c', c.name); 13 14 var x = function(){}, y = () => {}, z = function withName() {}; 15 assertEquals('x', x.name); 16 assertEquals('y', y.name); 17 assertEquals('withName', z.name); 18 })(); 19 20 (function testVariableDeclarationsClass() { 21 'use strict'; 22 var a = class {}; 23 assertEquals('a', a.name); 24 let b = ((class {})); 25 assertEquals('b', b.name); 26 // Should not overwrite name property. 27 const c = class { static name() { } } 28 assertEquals('function', typeof c.name); 29 30 var x = class {}, y = class NamedClass {}; 31 assertEquals('x', x.name); 32 assertEquals('NamedClass', y.name); 33 })(); 34 35 (function testObjectProperties() { 36 'use strict'; 37 var obj = { 38 a: function() {}, 39 b: () => {}, 40 c() { }, 41 get d() { }, 42 set d(val) { }, 43 x: function withName() { }, 44 y: class { }, 45 z: class ClassName { }, 46 42: function() {}, 47 4.2: function() {}, 48 __proto__: function() {}, 49 }; 50 51 assertEquals('a', obj.a.name); 52 assertEquals('b', obj.b.name); 53 assertEquals('c', obj.c.name); 54 var dDescriptor = Object.getOwnPropertyDescriptor(obj, 'd'); 55 assertEquals('get d', dDescriptor.get.name); 56 assertEquals('set d', dDescriptor.set.name); 57 assertEquals('withName', obj.x.name); 58 assertEquals('y', obj.y.name); 59 assertEquals('ClassName', obj.z.name); 60 assertEquals('42', obj[42].name); 61 assertEquals('4.2', obj[4.2].name); 62 assertEquals('', obj.__proto__.name); 63 })(); 64 65 (function testClassProperties() { 66 'use strict'; 67 class C { 68 a() { } 69 static b() { } 70 get c() { } 71 set c(val) { } 72 42() { } 73 static 43() { } 74 get 44() { } 75 set 44(val) { } 76 }; 77 78 assertEquals('a', C.prototype.a.name); 79 assertEquals('b', C.b.name); 80 var descriptor = Object.getOwnPropertyDescriptor(C.prototype, 'c'); 81 assertEquals('get c', descriptor.get.name); 82 assertEquals('set c', descriptor.set.name); 83 assertEquals('42', C.prototype[42].name); 84 assertEquals('43', C[43].name); 85 var descriptor = Object.getOwnPropertyDescriptor(C.prototype, '44'); 86 assertEquals('get 44', descriptor.get.name); 87 assertEquals('set 44', descriptor.set.name); 88 })(); 89 90 (function testComputedProperties() { 91 'use strict'; 92 var a = 'a'; 93 var b = 'b'; 94 var sym1 = Symbol('1'); 95 var sym2 = Symbol('2'); 96 var sym3 = Symbol('3'); 97 var symNoDescription = Symbol(); 98 var obj = { 99 [a]: function() {}, 100 [sym1]: function() {}, 101 [sym2]: function withName() {}, 102 [symNoDescription]: function() {}, 103 104 get [sym3]() {}, 105 set [b](val) {}, 106 }; 107 108 assertEquals('a', obj[a].name); 109 assertEquals('[1]', obj[sym1].name); 110 assertEquals('withName', obj[sym2].name); 111 assertEquals('', obj[symNoDescription].name); 112 113 assertEquals('get [3]', Object.getOwnPropertyDescriptor(obj, sym3).get.name); 114 assertEquals('set b', Object.getOwnPropertyDescriptor(obj, 'b').set.name); 115 116 var objMethods = { 117 [a]() {}, 118 [sym1]() {}, 119 [symNoDescription]: function() {}, 120 }; 121 122 assertEquals('a', objMethods[a].name); 123 assertEquals('[1]', objMethods[sym1].name); 124 assertEquals('', objMethods[symNoDescription].name); 125 126 class C { 127 [a]() { } 128 [sym1]() { } 129 static [sym2]() { } 130 [symNoDescription]() { } 131 132 get [sym3]() { } 133 static set [b](val) { } 134 } 135 136 assertEquals('a', C.prototype[a].name); 137 assertEquals('[1]', C.prototype[sym1].name); 138 assertEquals('[2]', C[sym2].name); 139 assertEquals('', C.prototype[symNoDescription].name); 140 141 assertEquals('get [3]', Object.getOwnPropertyDescriptor(C.prototype, sym3).get.name); 142 assertEquals('set b', Object.getOwnPropertyDescriptor(C, 'b').set.name); 143 })(); 144 145 146 (function testAssignment() { 147 var basicFn, arrowFn, generatorFn, classLit; 148 149 basicFn = function() { return true; }; 150 assertEquals('basicFn', basicFn.name); 151 var basicFn2 = basicFn; 152 assertEquals('basicFn', basicFn2.name); 153 basicFn = function functionWithName() { }; 154 assertEquals("functionWithName", basicFn.name); 155 156 arrowFn = x => x; 157 assertEquals('arrowFn', arrowFn.name); 158 var arrowFn2 = arrowFn; 159 assertEquals('arrowFn', arrowFn2.name); 160 161 generatorFn = function*() { yield true; }; 162 assertEquals('generatorFn', generatorFn.name); 163 var generatorFn2 = generatorFn; 164 assertEquals('generatorFn', generatorFn2.name); 165 generatorFn = function* generatorWithName() { }; 166 assertEquals("generatorWithName", generatorFn.name); 167 168 classLit = class { constructor() {} }; 169 assertEquals('classLit', classLit.name); 170 var classLit2 = classLit; 171 assertEquals('classLit', classLit2.name); 172 classLit = class classWithName { constructor() {} }; 173 assertEquals('classWithName', classLit.name); 174 classLit = class { constructor() {} static name() {} }; 175 assertEquals('function', typeof classLit.name); 176 classLit = class { constructor() {} static get name() { return true; } }; 177 assertTrue(classLit.name); 178 classLit = class { constructor() {} static ['name']() {} }; 179 assertEquals('function', typeof classLit.name); 180 classLit = class { constructor() {} static get ['name']() { return true; } }; 181 assertTrue(classLit.name); 182 })(); 183 184 (function testObjectBindingPattern() { 185 var { 186 a = function() {}, 187 b = () => {}, 188 x = function withName() { }, 189 y = class { }, 190 z = class ClassName { }, 191 q = class { static name() { return 42 } }, 192 foo: bar = function() {}, 193 inParens = (() => {}), 194 inManyParens = ((((() => {})))), 195 } = {}; 196 assertEquals('a', a.name); 197 assertEquals('b', b.name); 198 assertEquals('withName', x.name); 199 assertEquals('y', y.name); 200 assertEquals('ClassName', z.name); 201 assertEquals('function', typeof q.name); 202 assertEquals('bar', bar.name); 203 assertEquals('inParens', inParens.name) 204 assertEquals('inManyParens', inManyParens.name) 205 })(); 206 207 (function testArrayBindingPattern() { 208 var [ 209 a = function() {}, 210 b = () => {}, 211 x = function withName() { }, 212 y = class { }, 213 z = class ClassName { }, 214 q = class { static name() { return 42 } }, 215 inParens = (() => {}), 216 inManyParens = ((((() => {})))), 217 ] = []; 218 assertEquals('a', a.name); 219 assertEquals('b', b.name); 220 assertEquals('withName', x.name); 221 assertEquals('y', y.name); 222 assertEquals('ClassName', z.name); 223 assertEquals('function', typeof q.name); 224 assertEquals('inParens', inParens.name) 225 assertEquals('inManyParens', inManyParens.name) 226 })(); 227 228 (function testObjectAssignmentPattern() { 229 var a, b, x, y, z, q; 230 ({ 231 a = function() {}, 232 b = () => {}, 233 x = function withName() { }, 234 y = class { }, 235 z = class ClassName { }, 236 q = class { static name() { return 42 } }, 237 foo: bar = function() {}, 238 inParens = (() => {}), 239 inManyParens = ((((() => {})))), 240 } = {}); 241 assertEquals('a', a.name); 242 assertEquals('b', b.name); 243 assertEquals('withName', x.name); 244 assertEquals('y', y.name); 245 assertEquals('ClassName', z.name); 246 assertEquals('function', typeof q.name); 247 assertEquals('bar', bar.name); 248 assertEquals('inParens', inParens.name) 249 assertEquals('inManyParens', inManyParens.name) 250 })(); 251 252 (function testArrayAssignmentPattern() { 253 var a, b, x, y, z, q; 254 [ 255 a = function() {}, 256 b = () => {}, 257 x = function withName() { }, 258 y = class { }, 259 z = class ClassName { }, 260 q = class { static name() { return 42 } }, 261 inParens = (() => {}), 262 inManyParens = ((((() => {})))), 263 ] = []; 264 assertEquals('a', a.name); 265 assertEquals('b', b.name); 266 assertEquals('withName', x.name); 267 assertEquals('y', y.name); 268 assertEquals('ClassName', z.name); 269 assertEquals('function', typeof q.name); 270 assertEquals('inParens', inParens.name) 271 assertEquals('inManyParens', inManyParens.name) 272 })(); 273 274 (function testParameterDestructuring() { 275 (function({ a = function() {}, 276 b = () => {}, 277 x = function withName() { }, 278 y = class { }, 279 z = class ClassName { }, 280 q = class { static name() { return 42 } }, 281 foo: bar = function() {}, 282 inParens = (() => {}), 283 inManyParens = ((((() => {})))) }) { 284 assertEquals('a', a.name); 285 assertEquals('b', b.name); 286 assertEquals('withName', x.name); 287 assertEquals('y', y.name); 288 assertEquals('ClassName', z.name); 289 assertEquals('function', typeof q.name); 290 assertEquals('bar', bar.name); 291 assertEquals('inParens', inParens.name) 292 assertEquals('inManyParens', inManyParens.name) 293 })({}); 294 295 (function([ a = function() {}, 296 b = () => {}, 297 x = function withName() { }, 298 y = class { }, 299 z = class ClassName { }, 300 q = class { static name() { return 42 } }, 301 inParens = (() => {}), 302 inManyParens = ((((() => {})))) ]) { 303 assertEquals('a', a.name); 304 assertEquals('b', b.name); 305 assertEquals('withName', x.name); 306 assertEquals('y', y.name); 307 assertEquals('ClassName', z.name); 308 assertEquals('function', typeof q.name); 309 assertEquals('inParens', inParens.name) 310 assertEquals('inManyParens', inManyParens.name) 311 })([]); 312 })(); 313 314 (function testDefaultParameters() { 315 (function(a = function() {}, 316 b = () => {}, 317 x = function withName() { }, 318 y = class { }, 319 z = class ClassName { }, 320 q = class { static name() { return 42 } }, 321 inParens = (() => {}), 322 inManyParens = ((((() => {}))))) { 323 assertEquals('a', a.name); 324 assertEquals('b', b.name); 325 assertEquals('withName', x.name); 326 assertEquals('y', y.name); 327 assertEquals('ClassName', z.name); 328 assertEquals('function', typeof q.name); 329 assertEquals('inParens', inParens.name) 330 assertEquals('inManyParens', inManyParens.name) 331 })(); 332 })(); 333 334 (function testComputedNameNotShared() { 335 function makeClass(propName) { 336 return class { 337 static [propName]() {} 338 } 339 } 340 341 var sym1 = Symbol('1'); 342 var sym2 = Symbol('2'); 343 var class1 = makeClass(sym1); 344 assertEquals('[1]', class1[sym1].name); 345 var class2 = makeClass(sym2); 346 assertEquals('[2]', class2[sym2].name); 347 assertEquals('[1]', class1[sym1].name); 348 })(); 349 350 351 (function testComputedNamesOnlyAppliedSyntactically() { 352 function factory() { return () => {}; } 353 354 var obj = { ['foo']: factory() }; 355 assertEquals('', obj.foo.name); 356 })(); 357 358 359 (function testNameNotReflectedInToString() { 360 var f = function() {}; 361 var g = function*() {}; 362 var obj = { 363 ['h']: function() {}, 364 i: () => {} 365 }; 366 assertEquals('function () {}', f.toString()); 367 assertEquals('function* () {}', g.toString()); 368 assertEquals('function () {}', obj.h.toString()); 369 assertEquals('() => {}', obj.i.toString()); 370 })(); 371