[Pkg-javascript-commits] [node-is-reference] 01/03: New upstream version 1.0.1

Julien Puydt julien.puydt at laposte.net
Sun Jul 23 15:43:58 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-is-reference.

commit c847cbf9e2279c54a1873e70cea9c85b1b64cc2a
Author: Julien Puydt <julien.puydt at laposte.net>
Date:   Sun Jul 23 11:40:44 2017 +0200

    New upstream version 1.0.1
---
 CHANGELOG.md |  4 ++++
 index.js     |  4 +++-
 module.js    | 20 +++++++++++---------
 package.json |  2 +-
 test/test.js | 58 +++++++++++++++++++++++++++++++++-------------------------
 5 files changed, 52 insertions(+), 36 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 7461c07..0c8419b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,9 @@
 # is-reference changelog
 
+## 1.0.1
+
+* Ensure `isReference` returns a boolean
+
 ## 1.0.0
 
 * First release
diff --git a/index.js b/index.js
index f143a39..beb22e1 100644
--- a/index.js
+++ b/index.js
@@ -27,10 +27,12 @@ function isReference ( node, parent ) {
 		if ( parent.type === 'MethodDefinition' ) return false;
 
 		// disregard the `bar` in `export { foo as bar }`
-		if ( parent.type === 'ExportSpecifier' && node !== parent.local ) return;
+		if ( parent.type === 'ExportSpecifier' && node !== parent.local ) return false;
 
 		return true;
 	}
+
+	return false;
 }
 
 return isReference;
diff --git a/module.js b/module.js
index c2e6c89..931d44b 100644
--- a/module.js
+++ b/module.js
@@ -1,28 +1,30 @@
-export default function isReference ( node, parent ) {
-	if ( node.type === 'MemberExpression' ) {
-		return !node.computed && isReference( node.object, node );
+export default function isReference (node, parent) {
+	if (node.type === 'MemberExpression') {
+		return !node.computed && isReference(node.object, node);
 	}
 
-	if ( node.type === 'Identifier' ) {
+	if (node.type === 'Identifier') {
 		// the only time we could have an identifier node without a parent is
 		// if it's the entire body of a function without a block statement –
 		// i.e. an arrow function expression like `a => a`
-		if ( !parent ) return true;
+		if (!parent) return true;
 
 		// TODO is this right?
-		if ( parent.type === 'MemberExpression' || parent.type === 'MethodDefinition' ) {
+		if (parent.type === 'MemberExpression' || parent.type === 'MethodDefinition') {
 			return parent.computed || node === parent.object;
 		}
 
 		// disregard the `bar` in `{ bar: foo }`, but keep it in `{ [bar]: foo }`
-		if ( parent.type === 'Property' ) return parent.computed || node === parent.value;
+		if (parent.type === 'Property') return parent.computed || node === parent.value;
 
 		// disregard the `bar` in `class Foo { bar () {...} }`
-		if ( parent.type === 'MethodDefinition' ) return false;
+		if (parent.type === 'MethodDefinition') return false;
 
 		// disregard the `bar` in `export { foo as bar }`
-		if ( parent.type === 'ExportSpecifier' && node !== parent.local ) return;
+		if (parent.type === 'ExportSpecifier' && node !== parent.local) return false;
 
 		return true;
 	}
+
+	return false;
 }
diff --git a/package.json b/package.json
index 06b97ee..fd72f03 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "is-reference",
-  "version": "1.0.0",
+  "version": "1.0.1",
   "description": "Determine whether an AST node is a reference",
   "main": "index.js",
   "module": "module.js",
diff --git a/test/test.js b/test/test.js
index 56f749d..5895a25 100644
--- a/test/test.js
+++ b/test/test.js
@@ -1,10 +1,10 @@
-const assert = require( 'assert' );
-const { parse } = require( 'acorn' );
-const { walk } = require( 'estree-walker' );
+const assert = require('assert');
+const { parse } = require('acorn');
+const { walk } = require('estree-walker');
 
-const isReference = require( '../' );
+const isReference = require('../');
 
-describe( 'is-reference', () => {
+describe('is-reference', () => {
 	const positive = {
 		'simple identifier': `
 			foo;`,
@@ -36,40 +36,48 @@ describe( 'is-reference', () => {
 			var obj = { foo: 1 };`,
 
 		'member expression property': `
-			obj.foo;`
+			obj.foo;`,
+
+		'export-as': `
+			export { bar as foo }`
 	};
 
-	describe( 'positive', () => {
-		Object.keys( positive ).forEach( name => {
-			it( name, () => {
-				const code = positive[ name ];
-				const matches = findFooReferences( code );
+	describe('positive', () => {
+		Object.keys(positive).forEach(name => {
+			it(name, () => {
+				const code = positive[name];
+				const matches = findFooReferences(code);
 
-				assert.equal( matches.size, 1 );
+				assert.equal(matches.size, 1);
 			});
 		});
 	});
 
-	describe( 'negative', () => {
-		Object.keys( negative ).forEach( name => {
-			it( name, () => {
-				const code = negative[ name ];
-				const matches = findFooReferences( code );
+	describe('negative', () => {
+		Object.keys(negative).forEach(name => {
+			it(name, () => {
+				const code = negative[name];
+				const matches = findFooReferences(code);
 
-				assert.equal( matches.size, 0 );
+				assert.equal(matches.size, 0);
 			});
 		});
 	});
 
-	function findFooReferences ( code ) {
-		const ast = parse( code );
+	function findFooReferences(code) {
+		const ast = parse(code, {
+			sourceType: 'module',
+			ecmaVersion: 8
+		});
 
-		const matches = new Set;
+		const matches = new Set();
 
-		walk( ast, {
-			enter ( node, parent ) {
-				if ( isReference( node, parent ) && node.name === 'foo' ) {
-					matches.add( node );
+		walk(ast, {
+			enter(node, parent) {
+				const match = isReference(node, parent);
+				assert.equal(typeof match, 'boolean');
+				if (match && node.name === 'foo') {
+					matches.add(node);
 				}
 			}
 		});

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



More information about the Pkg-javascript-commits mailing list