author | mbaesken |
Thu, 28 Nov 2019 13:02:39 +0100 | |
changeset 59323 | ae2eb76c486d |
parent 47457 | 217860329f71 |
permissions | -rw-r--r-- |
47457
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
1 |
/* |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
2 |
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
3 |
* |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
4 |
* Redistribution and use in source and binary forms, with or without |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
5 |
* modification, are permitted provided that the following conditions |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
6 |
* are met: |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
7 |
* |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
8 |
* - Redistributions of source code must retain the above copyright |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
9 |
* notice, this list of conditions and the following disclaimer. |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
10 |
* |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
11 |
* - Redistributions in binary form must reproduce the above copyright |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
12 |
* notice, this list of conditions and the following disclaimer in the |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
13 |
* documentation and/or other materials provided with the distribution. |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
14 |
* |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
15 |
* - Neither the name of Oracle nor the names of its |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
16 |
* contributors may be used to endorse or promote products derived |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
17 |
* from this software without specific prior written permission. |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
18 |
* |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
19 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
20 |
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
21 |
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
22 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
23 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
24 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
25 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
26 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
27 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
28 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
29 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
30 |
*/ |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
31 |
|
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
32 |
// Usage: jjs --language=es6 staticchecker.js -- <file> |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
33 |
// or jjs --language=es6 staticchecker.js -- <directory> |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
34 |
// default argument is the current directory |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
35 |
|
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
36 |
if (arguments.length == 0) { |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
37 |
arguments[0] = "."; |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
38 |
} |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
39 |
|
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
40 |
const File = Java.type("java.io.File"); |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
41 |
const file = new File(arguments[0]); |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
42 |
if (!file.exists()) { |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
43 |
print(arguments[0] + " is neither a file nor a directory"); |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
44 |
exit(1); |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
45 |
} |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
46 |
|
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
47 |
// A simple static checker for javascript best practices. |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
48 |
// static checks performed are: |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
49 |
// |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
50 |
// * __proto__ magic property is bad (non-standard) |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
51 |
// * 'with' statements are bad |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
52 |
// * 'eval' calls are bad |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
53 |
// * 'delete foo' (scope variable delete) is bad |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
54 |
// * assignment to standard globals is bad (eg. Object = "hello") |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
55 |
// * assignment to property on standard prototype is bad (eg. String.prototype.foo = 45) |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
56 |
// * exception swallow (empty catch block in try-catch statements) |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
57 |
|
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
58 |
const Files = Java.type("java.nio.file.Files"); |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
59 |
const EmptyStatementTree = Java.type("jdk.nashorn.api.tree.EmptyStatementTree"); |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
60 |
const IdentifierTree = Java.type("jdk.nashorn.api.tree.IdentifierTree"); |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
61 |
const MemberSelectTree = Java.type("jdk.nashorn.api.tree.MemberSelectTree"); |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
62 |
const Parser = Java.type("jdk.nashorn.api.tree.Parser"); |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
63 |
const SimpleTreeVisitor = Java.type("jdk.nashorn.api.tree.SimpleTreeVisitorES6"); |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
64 |
const Tree = Java.type("jdk.nashorn.api.tree.Tree"); |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
65 |
|
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
66 |
const parser = Parser.create("-scripting", "--language=es6"); |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
67 |
|
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
68 |
// capture standard global upfront |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
69 |
const globals = new Set(); |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
70 |
for (let name of Object.getOwnPropertyNames(this)) { |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
71 |
globals.add(name); |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
72 |
} |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
73 |
|
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
74 |
const checkFile = function(file) { |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
75 |
print("Parsing " + file); |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
76 |
const ast = parser.parse(file, print); |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
77 |
if (!ast) { |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
78 |
print("FAILED to parse: " + file); |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
79 |
return; |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
80 |
} |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
81 |
|
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
82 |
const checker = new (Java.extend(SimpleTreeVisitor)) { |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
83 |
lineMap: null, |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
84 |
|
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
85 |
printWarning(node, msg) { |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
86 |
var pos = node.startPosition; |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
87 |
var line = this.lineMap.getLineNumber(pos); |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
88 |
var column = this.lineMap.getColumnNumber(pos); |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
89 |
print(`WARNING: ${msg} in ${file} @ ${line}:${column}`); |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
90 |
}, |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
91 |
|
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
92 |
printWithWarning(node) { |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
93 |
this.printWarning(node, "'with' usage"); |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
94 |
}, |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
95 |
|
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
96 |
printProtoWarning(node) { |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
97 |
this.printWarning(node, "__proto__ usage"); |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
98 |
}, |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
99 |
|
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
100 |
printScopeDeleteWarning(node, varName) { |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
101 |
this.printWarning(node, `delete ${varName}`); |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
102 |
}, |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
103 |
|
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
104 |
hasOnlyEmptyStats(stats) { |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
105 |
const itr = stats.iterator(); |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
106 |
while (itr.hasNext()) { |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
107 |
if (! (itr.next() instanceof EmptyStatementTree)) { |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
108 |
return false; |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
109 |
} |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
110 |
} |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
111 |
|
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
112 |
return true; |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
113 |
}, |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
114 |
|
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
115 |
checkProto(node, name) { |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
116 |
if (name == "__proto__") { |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
117 |
this.printProtoWarning(node); |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
118 |
} |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
119 |
}, |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
120 |
|
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
121 |
checkAssignment(lhs) { |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
122 |
if (lhs instanceof IdentifierTree && globals.has(lhs.name)) { |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
123 |
this.printWarning(lhs, `assignment to standard global "${lhs.name}"`); |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
124 |
} else if (lhs instanceof MemberSelectTree) { |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
125 |
const expr = lhs.expression; |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
126 |
if (expr instanceof MemberSelectTree && |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
127 |
expr.expression instanceof IdentifierTree && |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
128 |
globals.has(expr.expression.name) && |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
129 |
"prototype" == expr.identifier) { |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
130 |
this.printWarning(lhs, |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
131 |
`property set "${expr.expression.name}.prototype.${lhs.identifier}"`); |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
132 |
} |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
133 |
} |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
134 |
}, |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
135 |
|
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
136 |
visitAssignment(node, extra) { |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
137 |
this.checkAssignment(node.variable); |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
138 |
Java.super(checker).visitAssignment(node, extra); |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
139 |
}, |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
140 |
|
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
141 |
visitCatch(node, extra) { |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
142 |
var stats = node.block.statements; |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
143 |
if (stats.empty || this.hasOnlyEmptyStats(stats)) { |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
144 |
this.printWarning(node, "exception swallow"); |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
145 |
} |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
146 |
Java.super(checker).visitCatch(node, extra); |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
147 |
}, |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
148 |
|
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
149 |
visitCompilationUnit(node, extra) { |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
150 |
this.lineMap = node.lineMap; |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
151 |
Java.super(checker).visitCompilationUnit(node, extra); |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
152 |
}, |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
153 |
|
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
154 |
visitFunctionCall(node, extra) { |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
155 |
var func = node.functionSelect; |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
156 |
if (func instanceof IdentifierTree && func.name == "eval") { |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
157 |
this.printWarning(node, "eval call found"); |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
158 |
} |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
159 |
Java.super(checker).visitFunctionCall(node, extra); |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
160 |
}, |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
161 |
|
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
162 |
visitIdentifier(node, extra) { |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
163 |
this.checkProto(node, node.name); |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
164 |
Java.super(checker).visitIdentifier(node, extra); |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
165 |
}, |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
166 |
|
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
167 |
visitMemberSelect(node, extra) { |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
168 |
this.checkProto(node, node.identifier); |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
169 |
Java.super(checker).visitMemberSelect(node, extra); |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
170 |
}, |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
171 |
|
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
172 |
visitProperty(node, extra) { |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
173 |
this.checkProto(node, node.key); |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
174 |
Java.super(checker).visitProperty(node, extra); |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
175 |
}, |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
176 |
|
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
177 |
visitUnary(node, extra) { |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
178 |
if (node.kind == Tree.Kind.DELETE && |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
179 |
node.expression instanceof IdentifierTree) { |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
180 |
this.printScopeDeleteWarning(node, node.expression.name); |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
181 |
} |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
182 |
Java.super(checker).visitUnary(node, extra); |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
183 |
}, |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
184 |
|
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
185 |
visitWith(node, extra) { |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
186 |
this.printWithWarning(node); |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
187 |
Java.super(checker).visitWith(node, extra); |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
188 |
} |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
189 |
}; |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
190 |
|
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
191 |
try { |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
192 |
ast.accept(checker, null); |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
193 |
} catch (e) { |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
194 |
print(e); |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
195 |
if (e.printStackTrace) e.printStackTrace(); |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
196 |
if (e.stack) print(e.stack); |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
197 |
} |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
198 |
} |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
199 |
|
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
200 |
if (file.isDirectory()) { |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
201 |
Files.walk(file.toPath()) |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
202 |
.filter(function(p) Files.isRegularFile(p)) |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
203 |
.filter(function(p) p.toFile().name.endsWith('.js')) |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
204 |
.forEach(checkFile); |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
205 |
} else { |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
206 |
checkFile(file); |
217860329f71
8190217: Add a JS "static checker" sample for nashorn parser API
sundar
parents:
diff
changeset
|
207 |
} |