newTarget.js (1645B)
1 // |reftest| skip-if(!xulRuntime.shell) 2 function testNewTarget() { 3 4 // new.target is currently valid inside any non-arrow, non-generator function 5 assertInFunctionExpr("new.target", newTarget()); 6 7 // even with gratuitous whitespace. 8 assertInFunctionExpr(`new. 9 target`, newTarget()); 10 11 // invalid in top-level scripts 12 assertError("new.target", SyntaxError); 13 14 // valid in arrow functions inside functions 15 assertInFunctionExpr("()=>new.target", arrowExpr([], newTarget())); 16 assertError("(() => new.target))", SyntaxError); 17 18 // valid in generators, too! 19 assertStmt("function *foo() { new.target; }", genFunDecl("es6", ident("foo"), [], 20 blockStmt([exprStmt(newTarget())]))); 21 22 // new.target is a member expression. You should be able to call, invoke, or 23 // access properties of it. 24 assertInFunctionExpr("new.target.foo", dotExpr(newTarget(), ident("foo"))); 25 assertInFunctionExpr("new.target[\"foo\"]", memExpr(newTarget(), literal("foo"))); 26 27 assertInFunctionExpr("new.target()", callExpr(newTarget(), [])); 28 assertInFunctionExpr("new new.target()", newExpr(newTarget(), [])); 29 30 // assignment to newTarget is an error 31 assertError("new.target = 4", SyntaxError); 32 33 // only new.target is a valid production, no shorn names or other names 34 assertError("new.", SyntaxError); 35 assertError("new.foo", SyntaxError); 36 assertError("new.targe", SyntaxError); 37 38 // obj.new.target is still a member expression 39 assertExpr("obj.new.target", dotExpr(dotExpr(ident("obj"), ident("new")), ident("target"))); 40 } 41 42 runtest(testNewTarget);