[Pkg-mozext-commits] [adblock-plus] 25/464: Comment processing. Pretty neat, huh?

David Prévot taffit at moszumanska.debian.org
Tue Jul 22 20:43:58 UTC 2014


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch master
in repository adblock-plus.

commit c71c8ed86f24bdaad3ddb9055c6aff362fd38c5a
Author: Joshua Cranmer <Pidgeot18 at gmail.com>
Date:   Sat Apr 25 13:16:53 2009 -0400

    Comment processing. Pretty neat, huh?
---
 autotest/test_comments.js          | 32 +++++++++++++++++
 autotest/test_comments.js.expected |  7 ++++
 utils/comments.js                  | 72 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 111 insertions(+)

diff --git a/autotest/test_comments.js b/autotest/test_comments.js
new file mode 100644
index 0000000..80c21e9
--- /dev/null
+++ b/autotest/test_comments.js
@@ -0,0 +1,32 @@
+// Arguments: test_comments.js
+// Name: Comment association test
+
+// This comment is associated with only code, so I don't see this.
+include("../utils/cleanast.js");
+include("../utils/comments.js");
+
+// Processes an AST tree
+function process_js(ast, file) {
+  let objects = clean_ast(ast);
+  associate_comments(file, objects);
+
+  for each (let f in objects.functions) {
+    if (f.comment)
+      _print(f.name + ": " + f.comment);
+  }
+  for each (let o in objects.objects) {
+    if (o.comment)
+      _print(o.name + ": " + o.comment);
+    for (let m in o.functions) {
+      _print(m + ": " + o.functions[m].comment);
+    }
+  }
+}
+
+// Documenting an object!
+var test_object = {
+  /**
+   * This method does absolutely nothing.
+   */
+  someMethod: function () {}
+};
diff --git a/autotest/test_comments.js.expected b/autotest/test_comments.js.expected
new file mode 100644
index 0000000..f30991c
--- /dev/null
+++ b/autotest/test_comments.js.expected
@@ -0,0 +1,7 @@
+process_js: // Processes an AST tree
+
+test_object: // Documenting an object!
+
+someMethod: /**
+* This method does absolutely nothing.
+*/
diff --git a/utils/comments.js b/utils/comments.js
new file mode 100644
index 0000000..cdc70be
--- /dev/null
+++ b/utils/comments.js
@@ -0,0 +1,72 @@
+/**
+ * This processes comments.
+ */
+function associate_comments(filename, scopeObject) {
+  // Read the script and give us line stuff
+  let file = read_file(filename).split(/\r\n|[\r\n]/);
+
+  // Now, get us a sorted list of all important AST locations
+  let locations = [{loc: {line: 0, column: -1}}];
+  for each (let v in scopeObject.variables)
+    locations.push({loc: v.loc, obj: v, commentWanted: true});
+  for each (let v in scopeObject.constants)
+    locations.push({loc: v.loc, obj: v, commentWanted: true});
+  for each (let v in scopeObject.functions)
+    locations.push({loc: v.loc, obj: v, commentWanted: true});
+  for each (let v in scopeObject.code)
+    locations.push({loc: {line: v.line, column: v.column}, obj: v});
+  for each (let o in scopeObject.objects) {
+    locations.push({loc: o.loc, obj: o, commentWanted: true});
+    for each (let x in o.variables)
+      locations.push({loc: x.loc, obj: x, commentWanted: true});
+    for each (let x in o.functions)
+      locations.push({loc: x.loc, obj: x, commentWanted: true});
+    for each (let x in o.getters)
+      locations.push({loc: x.loc, obj: x, commentWanted: true});
+    for each (let x in o.setters)
+      locations.push({loc: x.loc, obj: x, commentWanted: true});
+  }
+  for each (let o in scopeObject.classes) {
+    locations.push({loc: o.loc, obj: o, commentWanted: true});
+    for each (let x in o.variables)
+      locations.push({loc: x.loc, obj: x, commentWanted: true});
+    for each (let x in o.functions)
+      locations.push({loc: x.loc, obj: x, commentWanted: true});
+    for each (let x in o.getters)
+      locations.push({loc: x.loc, obj: x, commentWanted: true});
+    for each (let x in o.setters)
+      locations.push({loc: x.loc, obj: x, commentWanted: true});
+  }
+  locations.sort(function (a, b) {
+    if (a.loc.line == b.loc.line)
+      return a.loc.column - b.loc.column;
+    return a.loc.line - b.loc.line;
+  });
+
+  // With that list done, let's find comments in the range.
+  for (let i = 1; i < locations.length; i++) {
+    if (!locations[i].commentWanted)
+      continue;
+    let comment = find_comments(locations[i - 1].loc, locations[i].loc, file);
+    if (comment)
+      locations[i].obj.comment = comment;
+  }
+}
+
+let comment_regex = /\/\*[\s\S]*?\*\/|\/\/.*?\n/mg;
+function find_comments(start, end, file) {
+  let lines = [];
+  lines[0] = file[start.line].substr(start.column + 1).trim();
+  for (let l = start.line + 1; l < end.line; l++) {
+    lines.push(file[l].trim());
+  }
+  lines.push(file[end.line].substr(0, end.column).trim());
+
+  let goop = lines.join("\n");
+  let match;
+  lines = [];
+  while ((match = comment_regex.exec(goop)) != null) {
+    lines.push(match);
+  }
+  return lines.join("\n");
+}

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-mozext/adblock-plus.git



More information about the Pkg-mozext-commits mailing list