Debian Codegraph @

rhino/examples/checkParam.js

raw symbols SCIP
1 /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 /**
8 * checkParam.js
9 *
10 * The files given as arguments on the command line are assumed to be
11 * Java source code files. This program checks to see that the @param
12 * tags in the documentation comments match with the parameters for
13 * the associated Java methods.
14 * <p>
15 * Any errors found are reported.
16 *
17 */
18 defineClass("File")
19
20 // Return true if "str" ends with "suffix".
21 function stringEndsWith(str, suffix) {
22 return str.substring(str.length - suffix.length) == suffix;
23 }
24
25 /**
26 * Perform processing once the end of a documentation comment is seen.
27 *
28 * Look for a parameter list following the end of the comment and
29 * collect the parameters and compare to the @param entries.
30 * Report any discrepancies.
31 * @param f the current file
32 * @param a an array of parameters from @param comments
33 * @param line the string containing the comment end (in case the
34 * parameters are on the same line)
35 */
36 function processCommentEnd(f, a, line) {
37 while (line != null && !line.match(/\(/))
38 line = f.readLine();
39 while (line != null && !line.match(/\)/))
40 line += f.readLine();
41 if (line === null)
42 return;
43 var m = line.match(/\(([^\)]+)\)/);
44 var args = m ? m[1].split(",") : [];
45 if (a.length != args.length) {
46 print('"' + f.name +
47 '"; line ' + f.lineNumber +
48 ' mismatch: had a different number' +
49 ' of @param entries and parameters.');
50 } else {
51 for (var i=0; i < a.length; i++) {
52 if (!stringEndsWith(args[i], a[i])) {
53 print('"' + f.name +
54 '"; line ' + f.lineNumber +
55 ' mismatch: had "' + a[i] +
56 '" and "' + args[i] + '".');
57 break;
58 }
59 }
60 }
61 }
62
63 /**
64 * Process the given file, looking for mismatched @param lists and
65 * parameter lists.
66 * @param f the file to process
67 */
68 function processFile(f) {
69 var line;
70 var m;
71 var i = 0;
72 var a = [];
73 outer:
74 while ((line = f.readLine()) != null) {
75 if (line.match(/@param/)) {
76 while (m = line.match(/@param[ ]+([^ ]+)/)) {
77 a[i++] = m[1];
78 line = f.readLine();
79 if (line == null)
80 break outer;
81 }
82 }
83 if (i != 0 && line.match(/\*\//)) {
84 processCommentEnd(f, a, line);
85 i = 0;
86 a = [];
87 }
88 }
89 if (i != 0) {
90 print('"' + f.name +
91 '"; line ' + f.lineNumber +
92 ' missing parameters at end of file.');
93 }
94 }
95
96 // main script: process each file in arguments list
97
98 for (var i=0; i < arguments.length; i++) {
99 var filename = String(arguments[i]);
100 print("Checking " + filename + "...");
101 var f = new File(filename);
102 processFile(f);
103 }
104 print("done.");
105