[Pkg-javascript-commits] [uglifyjs] 74/77: Add tool to extract property names

Jonas Smedegaard dr at jones.dk
Tue May 19 00:02:35 UTC 2015


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

js pushed a commit to tag v2.4.18
in repository uglifyjs.

commit 43991f8d2faa5101bf98473e11eda5516c878b98
Author: Mihai Bazon <mihai.bazon at gmail.com>
Date:   Sun Mar 29 12:38:06 2015 +0300

    Add tool to extract property names
---
 bin/extract-props.js | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 77 insertions(+)

diff --git a/bin/extract-props.js b/bin/extract-props.js
new file mode 100755
index 0000000..a5b6145
--- /dev/null
+++ b/bin/extract-props.js
@@ -0,0 +1,77 @@
+#! /usr/bin/env node
+
+"use strict";
+
+var U2 = require("../tools/node");
+var fs = require("fs");
+var yargs = require("yargs");
+var ARGS = yargs
+    .describe("o", "Output file")
+    .argv;
+var files = ARGS._.slice();
+var output = {
+    vars: {},
+    props: {}
+};
+
+if (ARGS.o) try {
+    output = JSON.parse(fs.readFileSync(ARGS.o, "utf8"));
+} catch(ex) {}
+
+files.forEach(getProps);
+
+if (ARGS.o) {
+    fs.writeFileSync(ARGS.o, JSON.stringify(output, null, 2), "utf8");
+} else {
+    console.log("%s", JSON.stringify(output, null, 2));
+}
+
+function getProps(filename) {
+    var code = fs.readFileSync(filename, "utf8");
+    var ast = U2.parse(code);
+
+    ast.walk(new U2.TreeWalker(function(node){
+        if (node instanceof U2.AST_ObjectKeyVal) {
+            add(node.key);
+        }
+        else if (node instanceof U2.AST_ObjectProperty) {
+            add(node.key.name);
+        }
+        else if (node instanceof U2.AST_Dot) {
+            add(node.property);
+        }
+        else if (node instanceof U2.AST_Sub) {
+            addStrings(node.property);
+        }
+    }));
+
+    function addStrings(node) {
+        var out = {};
+        try {
+            (function walk(node){
+                node.walk(new U2.TreeWalker(function(node){
+                    if (node instanceof U2.AST_Seq) {
+                        walk(node.cdr);
+                        return true;
+                    }
+                    if (node instanceof U2.AST_String) {
+                        add(node.value);
+                        return true;
+                    }
+                    if (node instanceof U2.AST_Conditional) {
+                        walk(node.consequent);
+                        walk(node.alternative);
+                        return true;
+                    }
+                    throw out;
+                }));
+            })(node);
+        } catch(ex) {
+            if (ex !== out) throw ex;
+        }
+    }
+
+    function add(name) {
+        output.props[name] = true;
+    }
+}

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/uglifyjs.git



More information about the Pkg-javascript-commits mailing list