1 /*
   2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * JDK-8188098:NPE in SimpleTreeVisitorES6 visitor when parsing a tagged template literal
  26  *
  27  * @test
  28  * @run
  29  */
  30 
  31 var Parser = Java.type("jdk.nashorn.api.tree.Parser");
  32 var MemberSelectTree = Java.type("jdk.nashorn.api.tree.MemberSelectTree");
  33 var SimpleTreeVisitor = Java.type("jdk.nashorn.api.tree.SimpleTreeVisitorES6");
  34 var parser = Parser.create("--language=es6");
  35 
  36 var ast = parser.parse("hello.js", "foo`PI (${Math.PI}) is transcendental`", print);
  37 
  38 var reachedCall = false;
  39 ast.accept(new (Java.extend(SimpleTreeVisitor)) {
  40     visitFunctionCall: function(node, extra) {
  41         reachedCall = true;
  42         Assert.assertTrue(node.functionSelect.name == "foo");
  43         var args = node.arguments;
  44         Assert.assertTrue(args.size() == 2);
  45         var strs = args.get(0).elements;
  46         Assert.assertTrue(String(strs.get(0).value) == "PI (");
  47         Assert.assertTrue(String(strs.get(1).value) == ") is transcendental");
  48         var expr = args.get(1);
  49         Assert.assertTrue(expr instanceof MemberSelectTree);
  50         Assert.assertTrue(expr.expression.name == "Math");
  51         Assert.assertTrue(expr.identifier == "PI");
  52     }
  53 }, null);
  54 
  55 Assert.assertTrue(reachedCall);