nashorn/samples/findvardelete.js
author sundar
Thu, 12 Nov 2015 12:30:15 +0530
changeset 33693 cc0617a78522
parent 29409 613987e37f79
permissions -rw-r--r--
8142501: nashorn tests failing after recent changes Reviewed-by: hannesw
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
29409
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
     1
/*
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
     3
 *
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
     4
 * Redistribution and use in source and binary forms, with or without
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
     5
 * modification, are permitted provided that the following conditions
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
     6
 * are met:
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
     7
 *
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
     8
 *   - Redistributions of source code must retain the above copyright
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
     9
 *     notice, this list of conditions and the following disclaimer.
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    10
 *
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    11
 *   - Redistributions in binary form must reproduce the above copyright
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    12
 *     notice, this list of conditions and the following disclaimer in the
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    13
 *     documentation and/or other materials provided with the distribution.
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    14
 *
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    15
 *   - Neither the name of Oracle nor the names of its
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    16
 *     contributors may be used to endorse or promote products derived
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    17
 *     from this software without specific prior written permission.
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    18
 *
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    19
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    20
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    21
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    22
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    23
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    24
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    25
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    26
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    27
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    28
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    29
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    30
 */
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    31
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    32
// delete of scope vars is reported as error in strict mode.
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    33
// This script finds scripts that such deletes. Scripts in
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    34
// the specified directory are scanned. If no directory is 
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    35
// specified, the current directory is scanned.
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    36
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    37
if (arguments.length == 0) {
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    38
    arguments[0] = ".";
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    39
}
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    40
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    41
var File = Java.type("java.io.File");
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    42
var file = new File(arguments[0]);
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    43
if (!file.exists()) {
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    44
    print(arguments[0] + " is neither a directory nor a file");
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    45
    exit(1);
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    46
}
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    47
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    48
var Files = Java.type("java.nio.file.Files");
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    49
var IdentifierTree = Java.type("jdk.nashorn.api.tree.IdentifierTree");
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    50
var Parser = Java.type("jdk.nashorn.api.tree.Parser");
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    51
var SimpleTreeVisitor = Java.type("jdk.nashorn.api.tree.SimpleTreeVisitorES5_1");
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    52
var Tree = Java.type("jdk.nashorn.api.tree.Tree");
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    53
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    54
var parser = Parser.create("-scripting", "--const-as-var");
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    55
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    56
function checkFile(file) {
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    57
    // print("checking " + file);
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    58
    var ast = parser.parse(file, print);
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    59
    if (!ast) {
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    60
        return;
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    61
    }
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    62
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    63
    // locate __proto__ usage and warn
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    64
    ast.accept(visitor = new (Java.extend(SimpleTreeVisitor)) {
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    65
        lineMap: null,
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    66
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    67
        printWarning: function(node, varName) {
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    68
            var pos = node.startPosition;
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    69
            var line = this.lineMap.getLineNumber(pos);
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    70
            var column = this.lineMap.getColumnNumber(pos);
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    71
            print("WARNING: delete " + varName + " in " + file + " @ " + line + ":" + column);
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    72
        },
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    73
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    74
        visitCompilationUnit: function(node, extra) {
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    75
            this.lineMap = node.lineMap;
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    76
            Java.super(visitor).visitCompilationUnit(node, extra);
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    77
        },
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    78
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    79
        visitUnary: function(node, extra) {
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    80
            if (node.kind == Tree.Kind.DELETE &&
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    81
                node.expression instanceof IdentifierTree) {
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    82
                this.printWarning(node, node.expression.name);
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    83
            } 
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    84
            Java.super(visitor).visitUnary(node, extra);
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    85
        },
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    86
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    87
    }, null);
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    88
}
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    89
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    90
if (file.isDirectory()) {
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    91
    Files.walk(file.toPath())
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    92
        .filter(function(p) Files.isRegularFile(p))
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    93
        .filter(function(p) p.toFile().name.endsWith('.js'))
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    94
        .forEach(checkFile);
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    95
} else {
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    96
    checkFile(file);
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    97
}