[Pkg-javascript-commits] [node-estree-walker] 01/03: New upstream version 0.4.0

Julien Puydt julien.puydt at laposte.net
Wed Jul 5 13:53:22 UTC 2017


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

jpuydt-guest pushed a commit to branch master
in repository node-estree-walker.

commit 625e3f343208006766206cc12b237d5dddc7874d
Author: Julien Puydt <julien.puydt at laposte.net>
Date:   Mon Jul 3 21:09:16 2017 +0200

    New upstream version 0.4.0
---
 .eslintrc            |  24 ++++++++++++
 .gitignore           |   2 +
 CHANGELOG.md         |  33 ++++++++++++++++
 README.md            |  45 ++++++++++++++++++++++
 index.d.ts           |  17 +++++++++
 package.json         |  34 +++++++++++++++++
 rollup.config.js     |  14 +++++++
 src/estree-walker.js |  49 ++++++++++++++++++++++++
 test/test.js         | 104 +++++++++++++++++++++++++++++++++++++++++++++++++++
 9 files changed, 322 insertions(+)

diff --git a/.eslintrc b/.eslintrc
new file mode 100644
index 0000000..b57b7b2
--- /dev/null
+++ b/.eslintrc
@@ -0,0 +1,24 @@
+{
+    "rules": {
+        "indent": [ 2, "tab", { "SwitchCase": 1 } ],
+        "quotes": [ 2, "single" ],
+        "linebreak-style": [ 2, "unix" ],
+        "semi": [ 2, "always" ],
+        "keyword-spacing": [ 2, { "before": true, "after": true } ],
+        "space-before-blocks": [ 2, "always" ],
+        "space-before-function-paren": [ 2, "always" ],
+        "no-mixed-spaces-and-tabs": [ 2, "smart-tabs" ],
+        "no-cond-assign": [ 0 ]
+    },
+    "env": {
+        "es6": true,
+        "browser": true,
+        "mocha": true,
+        "node": true
+    },
+    "extends": "eslint:recommended",
+    "parserOptions": {
+        "ecmaVersion": 6,
+        "sourceType": "module"
+    }
+}
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..f06235c
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+node_modules
+dist
diff --git a/CHANGELOG.md b/CHANGELOG.md
new file mode 100644
index 0000000..1593e20
--- /dev/null
+++ b/CHANGELOG.md
@@ -0,0 +1,33 @@
+# changelog
+
+## 0.4.0
+
+* Add TypeScript typings ([#3](https://github.com/Rich-Harris/estree-walker/pull/3))
+
+## 0.3.1
+
+* Include `pkg.repository` ([#2](https://github.com/Rich-Harris/estree-walker/pull/2))
+
+## 0.3.0
+
+* More predictable ordering
+
+## 0.2.1
+
+* Keep `context` shape
+
+## 0.2.0
+
+* Add ES6 build
+
+## 0.1.3
+
+* npm snafu
+
+## 0.1.2
+
+* Pass current prop and index to `enter`/`leave` callbacks
+
+## 0.1.1
+
+* First release
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..63e701e
--- /dev/null
+++ b/README.md
@@ -0,0 +1,45 @@
+# estree-walker
+
+Simple utility for walking an [ESTree](https://github.com/estree/estree)-compliant AST, such as one generated by [acorn](https://github.com/marijnh/acorn).
+
+
+## Installation
+
+```bash
+npm i estree-walker
+```
+
+
+## Usage
+
+```js
+var walk = require( 'estree-walker' ).walk;
+var acorn = require( 'acorn' );
+
+ast = acorn.parse( sourceCode, options ); // https://github.com/marijnh/acorn
+
+walk( ast, {
+  enter: function ( node, parent ) {
+    // some code happens
+  },
+  leave: function ( node, parent ) {
+  	// some code happens
+  }
+});
+```
+
+Inside the `enter` function, calling `this.skip()` will prevent the node's children being walked, or the `leave` function (which is optional) being called.
+
+
+## Why not use estraverse?
+
+The ESTree spec is evolving to accommodate ES6/7. I've had a couple of experiences where [estraverse](https://github.com/estools/estraverse) was unable to handle an AST generated by recent versions of acorn, because it hard-codes visitor keys.
+
+estree-walker, by contrast, simply enumerates a node's properties to find child nodes (and child lists of nodes), and is therefore resistant to spec changes. It's also much smaller. (The performance, if you're wondering, is basically identical.)
+
+None of which should be taken as criticism of estraverse, which has more features and has been battle-tested in many more situations, and for which I'm very grateful.
+
+
+## License
+
+MIT
\ No newline at end of file
diff --git a/index.d.ts b/index.d.ts
new file mode 100644
index 0000000..e4d0764
--- /dev/null
+++ b/index.d.ts
@@ -0,0 +1,17 @@
+declare module "estree-walker" {
+  export interface Node {
+    start: number;
+    end: number;
+    type: string;
+    [propName: string]: any;
+  }
+
+  export type WalkerListener = (node: Node, parent?: Node, prop?: string, index?: number) => void;
+
+  export interface WalkerOptions {
+    enter?: WalkerListener;
+    leave?: WalkerListener;
+  }
+
+  export function walk(ast: Node, options: WalkerOptions): void;
+}
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..1c6710f
--- /dev/null
+++ b/package.json
@@ -0,0 +1,34 @@
+{
+  "name": "estree-walker",
+  "description": "Traverse an ESTree-compliant AST",
+  "version": "0.4.0",
+  "author": "Rich Harris",
+  "license": "MIT",
+  "typings": "index.d.ts",
+  "repository": {
+    "type": "git",
+    "url": "https://github.com/Rich-Harris/estree-walker"
+  },
+  "main": "dist/estree-walker.umd.js",
+  "module": "dist/estree-walker.es.js",
+  "jsnext:main": "dist/estree-walker.es.js",
+  "scripts": {
+    "prepublish": "npm run lint && npm test",
+    "build": "rollup -c",
+    "test": "mocha test/test.js --compilers js:buble/register",
+    "pretest": "npm run build",
+    "lint": "eslint src"
+  },
+  "devDependencies": {
+    "buble": "^0.15.2",
+    "eslint": "^3.16.0",
+    "mocha": "^3.2.0",
+    "rollup": "^0.41.4",
+    "rollup-plugin-buble": "^0.15.0"
+  },
+  "files": [
+    "src",
+    "dist",
+    "README.md"
+  ]
+}
diff --git a/rollup.config.js b/rollup.config.js
new file mode 100644
index 0000000..c38bd04
--- /dev/null
+++ b/rollup.config.js
@@ -0,0 +1,14 @@
+import buble from 'rollup-plugin-buble';
+
+const pkg = require( './package.json' );
+
+export default {
+	entry: 'src/estree-walker.js',
+	targets: [
+		{ dest: pkg.main, format: 'umd' },
+		{ dest: pkg.module, format: 'es' }
+	],
+	moduleName: 'estreeWalker',
+	plugins: [ buble() ],
+	sourceMap: true
+};
diff --git a/src/estree-walker.js b/src/estree-walker.js
new file mode 100644
index 0000000..2070ab9
--- /dev/null
+++ b/src/estree-walker.js
@@ -0,0 +1,49 @@
+export function walk ( ast, { enter, leave }) {
+	visit( ast, null, enter, leave );
+}
+
+const context = {
+	skip: () => context.shouldSkip = true,
+	shouldSkip: false
+};
+
+let childKeys = {};
+
+const toString = Object.prototype.toString;
+
+function isArray ( thing ) {
+	return toString.call( thing ) === '[object Array]';
+}
+
+function visit ( node, parent, enter, leave, prop, index ) {
+	if ( !node ) return;
+
+	if ( enter ) {
+		context.shouldSkip = false;
+		enter.call( context, node, parent, prop, index );
+		if ( context.shouldSkip ) return;
+	}
+
+	const keys = childKeys[ node.type ] || (
+		childKeys[ node.type ] = Object.keys( node ).filter( key => typeof node[ key ] === 'object' )
+	);
+
+	for ( let i = 0; i < keys.length; i += 1 ) {
+		const key = keys[i];
+		const value = node[ key ];
+
+		if ( isArray( value ) ) {
+			for ( let j = 0; j < value.length; j += 1 ) {
+				visit( value[j], node, enter, leave, key, j );
+			}
+		}
+
+		else if ( value && value.type ) {
+			visit( value, node, enter, leave, key, null );
+		}
+	}
+
+	if ( leave ) {
+		leave( node, parent, prop, index );
+	}
+}
diff --git a/test/test.js b/test/test.js
new file mode 100644
index 0000000..99257b9
--- /dev/null
+++ b/test/test.js
@@ -0,0 +1,104 @@
+const { describe, it } = require( 'mocha' );
+const assert = require( 'assert' );
+const { walk } = require( '..' );
+
+describe( 'estree-walker', () => {
+	it( 'walks an AST', () => {
+		const ast = {
+			type: 'Program',
+			body: [{
+				type: 'VariableDeclaration',
+				declarations: [
+					{
+						type: 'VariableDeclarator',
+						id: { type: 'Identifier', name: 'a' },
+						init: { type: 'Literal', value: 1, raw: '1' }
+					},
+					{
+						type: 'VariableDeclarator',
+						id: { type: 'Identifier', name: 'b' },
+						init: { type: 'Literal', value: 2, raw: '2' }
+					}
+				],
+				kind: 'var'
+			}],
+			sourceType: 'module'
+		};
+
+		let entered = [];
+		let left = [];
+
+		walk( ast, {
+			enter ( node ) {
+				entered.push( node );
+			},
+			leave ( node ) {
+				left.push( node );
+			}
+		});
+
+		assert.deepEqual( entered, [
+			ast,
+			ast.body[0],
+			ast.body[0].declarations[0],
+			ast.body[0].declarations[0].id,
+			ast.body[0].declarations[0].init,
+			ast.body[0].declarations[1],
+			ast.body[0].declarations[1].id,
+			ast.body[0].declarations[1].init
+		]);
+
+		assert.deepEqual( left, [
+			ast.body[0].declarations[0].id,
+			ast.body[0].declarations[0].init,
+			ast.body[0].declarations[0],
+			ast.body[0].declarations[1].id,
+			ast.body[0].declarations[1].init,
+			ast.body[0].declarations[1],
+			ast.body[0],
+			ast
+		]);
+	});
+
+	it( 'handles null literals', () => {
+		const ast = {
+			type: 'Program',
+			start: 0,
+			end: 8,
+			body: [
+				{
+					type: 'ExpressionStatement',
+					start: 0,
+					end: 5,
+					expression: {
+						type: 'Literal',
+						start: 0,
+						end: 4,
+						value: null,
+						raw: 'null'
+					}
+				},
+				{
+					type: 'ExpressionStatement',
+					start: 6,
+					end: 8,
+					expression: {
+						type: 'Literal',
+						start: 6,
+						end: 7,
+						value: 1,
+						raw: '1'
+					}
+				}
+			],
+			sourceType: 'module'
+		};
+
+		walk( ast, {
+			enter () {},
+			leave () {}
+		});
+
+		assert.ok( true );
+	});
+});

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



More information about the Pkg-javascript-commits mailing list