[Pkg-javascript-commits] [node-astw] 01/04: Import Upstream version 2.0.0
Shirish Togarla
shirish12-guest at moszumanska.debian.org
Tue Feb 7 20:23:59 UTC 2017
This is an automated email from the git hooks/post-receive script.
shirish12-guest pushed a commit to branch master
in repository node-astw.
commit 730352f6ff8390e7c7f0f9f688454e9bfc160dec
Author: Shirish Togarla <shirishtogarla533 at gmail.com>
Date: Tue Feb 7 20:13:19 2017 +0000
Import Upstream version 2.0.0
---
.travis.yml | 8 ++++++++
LICENSE | 18 ++++++++++++++++++
example/types.js | 8 ++++++++
index.js | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++
package.json | 45 +++++++++++++++++++++++++++++++++++++++++++++
readme.markdown | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++
test/json.js | 13 +++++++++++++
test/parent.js | 25 +++++++++++++++++++++++++
8 files changed, 220 insertions(+)
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..74c57bf
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,8 @@
+language: node_js
+node_js:
+ - "0.8"
+ - "0.10"
+ - "0.12"
+ - "iojs"
+before_install:
+ - npm install -g npm@~1.4.6
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..ee27ba4
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,18 @@
+This software is released under the MIT license:
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software is furnished to do so,
+subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/example/types.js b/example/types.js
new file mode 100644
index 0000000..08cbe28
--- /dev/null
+++ b/example/types.js
@@ -0,0 +1,8 @@
+var astw = require('../');
+var deparse = require('escodegen').generate;
+var walk = astw('4 + beep(5 * 2)');
+
+walk(function (node) {
+ var src = deparse(node);
+ console.log(node.type + ' :: ' + JSON.stringify(src));
+});
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..8e34110
--- /dev/null
+++ b/index.js
@@ -0,0 +1,51 @@
+var parse = require('acorn').parse;
+
+module.exports = function (src) {
+ var ast = src;
+ if (typeof src === 'string') {
+ try {
+ ast = parse(src, {
+ ecmaVersion: 6,
+ allowReturnOutsideFunction: true
+ })
+ }
+ catch (err) { ast = parse('(' + src + ')') }
+ }
+ return function (cb) {
+ walk(ast, undefined, cb);
+ };
+};
+
+function walk (node, parent, cb) {
+ var keys = objectKeys(node);
+ for (var i = 0; i < keys.length; i++) {
+ var key = keys[i];
+ if (key === 'parent') continue;
+
+ var child = node[key];
+ if (isArray(child)) {
+ for (var j = 0; j < child.length; j++) {
+ var c = child[j];
+ if (c && typeof c.type === 'string') {
+ c.parent = node;
+ walk(c, node, cb);
+ }
+ }
+ }
+ else if (child && typeof child.type === 'string') {
+ child.parent = node;
+ walk(child, node, cb);
+ }
+ }
+ cb(node);
+}
+
+var isArray = Array.isArray || function (xs) {
+ return Object.prototype.toString.call(xs) === '[object Array]';
+};
+
+var objectKeys = Object.keys || function (obj) {
+ var keys = [];
+ for (var key in obj) keys.push(key);
+ return keys;
+};
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..4047665
--- /dev/null
+++ b/package.json
@@ -0,0 +1,45 @@
+{
+ "name": "astw",
+ "version": "2.0.0",
+ "description": "walk the ast with references to parent nodes",
+ "main": "index.js",
+ "dependencies": {
+ "acorn": "^1.0.3"
+ },
+ "devDependencies": {
+ "tape": "~2.4.1",
+ "escodegen": "~0.0.17"
+ },
+ "scripts": {
+ "test": "tape test/*.js"
+ },
+ "testling": {
+ "files": "test/*.js",
+ "browsers": [
+ "ie/6..latest",
+ "chrome/20..latest",
+ "firefox/10..latest",
+ "safari/latest",
+ "opera/11.0..latest",
+ "iphone/6",
+ "ipad/6"
+ ]
+ },
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/substack/astw.git"
+ },
+ "homepage": "https://github.com/substack/astw",
+ "keywords": [
+ "ast",
+ "walk",
+ "source",
+ "acorn"
+ ],
+ "author": {
+ "name": "James Halliday",
+ "email": "mail at substack.net",
+ "url": "http://substack.net"
+ },
+ "license": "MIT"
+}
diff --git a/readme.markdown b/readme.markdown
new file mode 100644
index 0000000..807b5ad
--- /dev/null
+++ b/readme.markdown
@@ -0,0 +1,52 @@
+# astw
+
+walk the ast
+
+[![browser support](http://ci.testling.com/substack/astw.png)](http://ci.testling.com/substack/astw)
+
+[![build status](https://secure.travis-ci.org/substack/astw.png)](http://travis-ci.org/substack/astw)
+
+This module is a faster version of
+[falafel](https://github.com/substack/node-falafel)
+that only does ast walking and `.parent` tracking, not source transforms.
+
+# example
+
+``` js
+var astw = require('astw');
+var deparse = require('escodegen').generate;
+var walk = astw('4 + beep(5 * 2)');
+
+walk(function (node) {
+ var src = deparse(node);
+ console.log(node.type + ' :: ' + JSON.stringify(src));
+});
+```
+
+# methods
+
+``` js
+var astw = require('astw')
+```
+
+## var walk = astw(src)
+
+Return a `walk()` function from the source string or ast object `src`.
+
+## walk(cb)
+
+Walk the nodes in the ast with `cb(node)` where `node` is each element in the
+ast from [esprima](http://esprima.org/) but with an additional `.parent`
+reference to the parent node.
+
+# install
+
+With [npm](https://npmjs.org) do:
+
+```
+npm install astw
+```
+
+# license
+
+MIT
diff --git a/test/json.js b/test/json.js
new file mode 100644
index 0000000..cd9bc8c
--- /dev/null
+++ b/test/json.js
@@ -0,0 +1,13 @@
+var astw = require('../');
+var test = require('tape');
+
+test('json', function (t) {
+ t.plan(2);
+ var numbers = [ 1, 2 ];
+ var walk = astw('{"a":1,"b":2}');
+ walk(function (node) {
+ if (node.type === 'Literal' && typeof node.value === 'number') {
+ t.equal(node.value, numbers.shift());
+ }
+ });
+});
diff --git a/test/parent.js b/test/parent.js
new file mode 100644
index 0000000..a7403d3
--- /dev/null
+++ b/test/parent.js
@@ -0,0 +1,25 @@
+var astw = require('../');
+var test = require('tape');
+var generate = require('escodegen').generate;
+
+function unparse (s) {
+ return generate(s, { format: { compact: true } });
+}
+
+test('parent', function (t) {
+ t.plan(4);
+
+ var walk = astw('(' + function () {
+ var xs = [ 1, 2, 3 ];
+ fn(ys);
+ } + ')()');
+
+ walk(function (node) {
+ if (node.type === 'ArrayExpression') {
+ t.equal(node.parent.type, 'VariableDeclarator');
+ t.equal(unparse(node.parent), 'xs=[1,2,3]');
+ t.equal(node.parent.parent.type, 'VariableDeclaration');
+ t.equal(unparse(node.parent.parent), 'var xs=[1,2,3];');
+ }
+ });
+});
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-astw.git
More information about the Pkg-javascript-commits
mailing list