Socket
Socket
Sign inDemoInstall

espower

Package Overview
Dependencies
Maintainers
1
Versions
41
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

espower - npm Package Compare versions

Comparing version 0.6.0 to 0.7.0

49

bower.json
{
"name": "espower",
"version": "0.6.0",
"main": "lib/espower.js",
"ignore": [
"**/.*",
"Gruntfile.js",
"expr.js",
"esp.js",
"node_modules",
"package.json",
"coverage",
"test"
],
"dependencies": {
"esprima": "~1.0.4",
"estraverse": "~1.5.0"
},
"devDependencies": {
"escodegen": "1.3.0",
"mocha": "~1.18.2",
"requirejs": "~2.1.11",
"assert": "Jxck/assert"
}
"name": "espower",
"version": "0.7.0",
"main": "lib/espower.js",
"ignore": [
"**/.*",
"Gruntfile.js",
"expr.js",
"esp.js",
"node_modules",
"package.json",
"coverage",
"test"
],
"dependencies": {
"esprima": "~1.2.0",
"escodegen": "1.3.0",
"estraverse": "~1.5.0"
},
"devDependencies": {
"mocha": "~1.18.2",
"requirejs": "~2.1.11",
"assert": "Jxck/assert"
},
"resolutions": {
"esprima": "~1.2.0"
}
}
module.exports = function(grunt) {
var pkg = grunt.file.readJSON('package.json');
require('jit-grunt')(grunt);
require('jit-grunt')(grunt, {
bower: 'grunt-bower-requirejs'
});

@@ -6,0 +8,0 @@ grunt.initConfig({

@@ -25,11 +25,15 @@ /**

if (typeof define === 'function' && define.amd) {
define(['estraverse', 'esprima'], factory);
define(['estraverse', 'escodegen'], factory);
} else if (typeof exports === 'object') {
module.exports = factory(require('estraverse'), require('esprima'));
module.exports = factory(require('estraverse'), require('escodegen'));
} else {
root.espower = factory(root.estraverse, root.esprima);
root.espower = factory(root.estraverse, root.escodegen);
}
}(this, function (estraverse, esprima) {
}(this, function (estraverse, escodegen) {
'use strict';
// see: https://github.com/Constellation/escodegen/issues/115
if (typeof define === 'function' && define.amd) {
escodegen = window.escodegen;
}

@@ -59,3 +63,11 @@ var syntax = estraverse.Syntax,

powerAssertVariableName: 'assert',
lineSeparator: '\n',
escodegenOptions: {
format: {
indent: {
style: ''
},
newline: ''
},
verbatim: 'x-verbatim-espower'
},
targetMethods: {

@@ -85,3 +97,3 @@ oneArg: [

function espower (ast, options) {
var instrumentor = new SourceInstrumentor(extend(defaultOptions(), (options || {})));
var instrumentor = new Instrumentor(extend(defaultOptions(), (options || {})));
return instrumentor.instrument(ast);

@@ -91,18 +103,17 @@ }

function SourceInstrumentor (options) {
function Instrumentor (options) {
ensureOptionPrerequisites(options);
this.options = options;
this.lines = options.source.split(options.lineSeparator);
}
SourceInstrumentor.prototype.instrument = function (ast) {
Instrumentor.prototype.instrument = function (ast) {
ensureAstPrerequisites(ast, this.options);
var that = this,
capturingStartPath,
capturedMetaTree,
capturedTokens,
assertionPath,
argumentPath,
canonicalCode,
lineNum,
argumentModified = false,
skipping = false,
instrumentor,
result = (this.options.destructive) ? ast : deepCopy(ast);
this.tokens = result.tokens;

@@ -113,7 +124,6 @@ estraverse.replace(result, {

path = controller.path(),
currentPath = path ? path[path.length - 1] : null,
numTargetArgs, indexOfCurrentArg;
currentPath = path ? path[path.length - 1] : null;
//console.log('enter currentNode:' + currentNode.type + ' parentNode: ' + parentNode.type + ' path: ' + path);
if (capturingStartPath) {
if (argumentPath) {
if ((!isSupportedNodeType(currentNode)) ||

@@ -129,23 +139,17 @@ (isLeftHandSideOfAssignment(parentNode, currentPath)) ||

} else {
if (!parentNode || parentNode.type !== syntax.CallExpression) {
if (!parentNode || parentNode.type !== syntax.CallExpression || !isSupportedNodeType(currentNode)) {
return undefined;
}
if (parentNode.callee === currentNode) {
if (that.isCalleeOfTargetAssertion(currentNode)) {
// entering target assertion
lineNum = parentNode.loc.start.line;
canonicalCode = generateCanonicalCode(parentNode, that.options.escodegenOptions);
assertionPath = path.slice(0, -1);
return undefined;
}
if (!isSupportedNodeType(currentNode)) {
if (that.isTargetAssertionArgument(parentNode, currentNode)) {
// entering target argument
argumentPath = path;
return undefined;
}
numTargetArgs = numberOfTargetArguments(parentNode.callee, that.options);
if (numTargetArgs === 0) {
return undefined;
}
indexOfCurrentArg = parentNode.arguments.indexOf(currentNode);
if (indexOfCurrentArg < numTargetArgs) {
capturingStartPath = path;
capturedMetaTree = metaAst(currentNode);
capturedTokens = metaAst(subTokens(result.tokens, currentNode.loc.start, currentNode.loc.end));
instrumentor = that.instrumentArgument(parentNode, currentNode);
return undefined;
}
}

@@ -158,10 +162,6 @@ return undefined;

path = controller.path(),
modifiedTree,
subpath,
instrumentedTree;
resultTree = currentNode,
relativeEsPath;
//console.log('leave ' + currentNode.type + ' path: ' + path);
if (!capturingStartPath) {
return undefined;
}
if (skipping) {

@@ -171,8 +171,21 @@ skipping = false;

}
if (parentNode.callee === currentNode) {
if (isPathIdentical(assertionPath, path)) {
// leaving target assertion
canonicalCode = null;
lineNum = null;
assertionPath = null;
return undefined;
}
subpath = path.slice(capturingStartPath.length);
if (!argumentPath) {
return undefined;
}
if (isLastPieceOfFunctionCall(parentNode, currentNode)) {
return undefined;
}
relativeEsPath = path.slice(assertionPath.length);
//console.log('leave ' + currentNode.type + ' path: ' + path + ' ' + currentNode.name);

@@ -189,9 +202,5 @@ switch(currentNode.type) {

case syntax.NewExpression:
modifiedTree = instrumentor.captureNode(currentNode, subpath);
resultTree = that.captureNode(currentNode, relativeEsPath);
argumentModified = true;
break;
case syntax.ObjectExpression:
case syntax.ArrayExpression:
case syntax.ConditionalExpression:
modifiedTree = currentNode;
break;
default:

@@ -201,11 +210,12 @@ break;

if (modifiedTree && path && isLeavingAssertion(capturingStartPath, path)) {
instrumentedTree = instrumentor.captureAssertion(modifiedTree, capturedMetaTree, capturedTokens);
capturingStartPath = null;
capturedMetaTree = null;
capturedTokens = null;
return instrumentedTree;
if (isPathIdentical(argumentPath, path)) {
// leaving target argument
argumentPath = null;
if (argumentModified) {
argumentModified = false;
return that.captureAssertion(resultTree, canonicalCode, lineNum);
}
}
return modifiedTree;
return resultTree;
}

@@ -216,77 +226,22 @@ });

SourceInstrumentor.prototype.instrumentArgument = function (callExpression, argumentNode) {
return new LineInstrumentor(this.retrieveLineFor(callExpression), this.options, this.tokens);
Instrumentor.prototype.isCalleeOfTargetAssertion = function (node) {
return numberOfTargetArguments(node, this.options) !== 0;
};
SourceInstrumentor.prototype.retrieveLineFor = function (callExpression) {
var lineNumber = callExpression.loc.start.line;
return this.lines[lineNumber - 1];
// if (typeof callExpression.raw !== 'undefined') {
// return callExpression.raw;
// }
Instrumentor.prototype.isTargetAssertionArgument = function (parentNode, currentNode) {
var numTargetArgs, indexOfCurrentArg;
numTargetArgs = numberOfTargetArguments(parentNode.callee, this.options);
if (numTargetArgs === 0) {
return false;
}
indexOfCurrentArg = parentNode.arguments.indexOf(currentNode);
return (indexOfCurrentArg < numTargetArgs);
};
function LineInstrumentor (line, options, tokens) {
this.line = line;
this.options = options;
this.tokens = tokens;
}
LineInstrumentor.prototype.captureAssertion = function (node, metaAstTree, metaAstTokens) {
Instrumentor.prototype.captureAssertion = function (node, canonicalCode, lineNum) {
var n = newNodeWithLocationCopyOf(node),
namedArgs = n({
type: syntax.ObjectExpression,
properties: []
});
if (metaAstTree) {
namedArgs.properties.push(n({
type: syntax.Property,
key: n({
type: syntax.Identifier,
name: 'tree'
}),
value: metaAstTree,
kind: 'init'
}));
}
if (metaAstTokens) {
namedArgs.properties.push(n({
type: syntax.Property,
key: n({
type: syntax.Identifier,
name: 'tokens'
}),
value: metaAstTokens,
kind: 'init'
}));
}
if (this.line) {
namedArgs.properties.push(n({
type: syntax.Property,
key: n({
type: syntax.Identifier,
name: 'content'
}),
value: n({
type: syntax.Literal,
value: this.line
}),
kind: 'init'
}));
}
if (this.options.path) {
namedArgs.properties.push(n({
type: syntax.Property,
key: n({
type: syntax.Identifier,
name: 'filepath'
}),
value: n({
type: syntax.Literal,
value: this.options.path
}),
kind: 'init'
}));
}
props = [];
addLiteralTo(props, n, 'content', canonicalCode);
addLiteralTo(props, n, 'filepath', this.options.path);
addLiteralTo(props, n, 'line', lineNum);
return n({

@@ -306,7 +261,10 @@ type: syntax.CallExpression,

}),
arguments: [node].concat(namedArgs)
arguments: [node].concat(n({
type: syntax.ObjectExpression,
properties: props
}))
});
};
LineInstrumentor.prototype.captureNode = function (target, subpath) {
Instrumentor.prototype.captureNode = function (target, relativeEsPath) {
var n = newNodeWithLocationCopyOf(target);

@@ -331,3 +289,3 @@ return n({

type: syntax.Literal,
value: subpath.join('/')
value: relativeEsPath.join('/')
})

@@ -339,28 +297,46 @@ ]

function metaAst (tree) {
var tempAst = esprima.parse('!' + JSON.stringify(tree));
return tempAst.body[0].expression.argument;
function generateCanonicalCode(node, escodegenOptions) {
var ast = deepCopy(node);
estraverse.replace(ast, {
leave: function (currentNode, parentNode) {
if (currentNode.type === syntax.Literal && typeof currentNode.raw !== 'undefined') {
currentNode['x-verbatim-espower'] = {
content : currentNode.raw,
precedence : escodegen.Precedence.Primary
};
return currentNode;
} else {
return undefined;
}
}
});
return escodegen.generate(ast, escodegenOptions);
}
function subTokens (tokens, from, to) {
var i, token, result = [];
for(i = 0; i < tokens.length; i += 1) {
token = tokens[i];
if (token.loc.start.line < from.line) {
continue;
}
if (to.line < token.loc.end.line) {
break;
}
if (token.loc.start.column < from.column) {
continue;
}
if (to.column < token.loc.end.column) {
break;
}
result.push(token);
function addLiteralTo(props, createNode, name, data) {
if (data) {
addToProps(props, createNode, name, createNode({
type: syntax.Literal,
value: data
}));
}
return result;
}
function addToProps(props, createNode, name, value) {
props.push(createNode({
type: syntax.Property,
key: createNode({
type: syntax.Identifier,
name: name
}),
value: value,
kind: 'init'
}));
}
function isLastPieceOfFunctionCall(parentNode, currentNode) {
return (parentNode.type === syntax.CallExpression || parentNode.type === syntax.NewExpression) &&
parentNode.callee === currentNode;
}
function isLeftHandSideOfAssignment(parentNode, currentPath) {

@@ -391,4 +367,7 @@ // Do not instrument left due to 'Invalid left-hand side in assignment'

function isLeavingAssertion(capturingStartPath, path) {
return capturingStartPath.join('/') === path.join('/');
function isPathIdentical(path1, path2) {
if (!path1 || !path2) {
return false;
}
return path1.join('/') === path2.join('/');
}

@@ -466,3 +445,2 @@

'powerAssertVariableName',
'lineSeparator',
'targetMethods'

@@ -473,5 +451,2 @@ ].forEach(function (propName) {

if (! options.source) {
throw new Error('Target source code content should be specified by options.source.');
}
if (typeof options.targetMethods.oneArg === 'undefined') {

@@ -478,0 +453,0 @@ throw new Error('options.targetMethods.oneArg should be specified.');

{
"name": "espower",
"description": "Power Assert feature instrumentor based on the Mozilla JavaScript AST",
"version": "0.6.0",
"version": "0.7.0",
"keywords": [

@@ -33,3 +33,4 @@ "power-assert",

"dependencies": {
"esprima": "~1.1.1",
"escodegen": "~1.3.2",
"esprima": "~1.2.0",
"estraverse": "~1.5.0"

@@ -39,5 +40,4 @@ },

"blanket": "~1.1.6",
"bower": "~1.3.1",
"bower": "~1.3.3",
"coveralls": "~2.10.0",
"escodegen": "~1.3.1",
"grunt": "~0.4.2",

@@ -50,4 +50,4 @@ "grunt-bower-requirejs": "~0.9.2",

"grunt-mocha": "~0.4.10",
"grunt-mocha-test": "~0.10.0",
"jit-grunt": "~0.4.1",
"grunt-mocha-test": "~0.10.2",
"jit-grunt": "~0.5.0",
"mocha-lcov-reporter": "0.0.1"

@@ -54,0 +54,0 @@ },

espower
================================
[![Build Status](https://travis-ci.org/twada/espower.png?branch=master)](https://travis-ci.org/twada/espower)
[![NPM version](https://badge.fury.io/js/espower.png)](http://badge.fury.io/js/espower)
[![Dependency Status](https://gemnasium.com/twada/espower.png)](https://gemnasium.com/twada/espower)
[![Build Status](https://travis-ci.org/twada/espower.svg?branch=master)](https://travis-ci.org/twada/espower)
[![NPM version](https://badge.fury.io/js/espower.svg)](http://badge.fury.io/js/espower)
[![Dependency Status](https://gemnasium.com/twada/espower.svg)](https://gemnasium.com/twada/espower)
[![Coverage Status](https://coveralls.io/repos/twada/espower/badge.png?branch=master)](https://coveralls.io/r/twada/espower?branch=master)

@@ -8,0 +8,0 @@ [![Built with Grunt](https://cdn.gruntjs.com/builtwith.png)](http://gruntjs.com/)

@@ -51,3 +51,3 @@ (function (root, factory) {

function instrument (jsCode, options) {
var jsAST = esprima.parse(jsCode, {tolerant: true, loc: true, tokens: true});;
var jsAST = esprima.parse(jsCode, {tolerant: true, loc: true, tokens: true, raw: true});
var espoweredAST = espower(jsAST, options);

@@ -62,3 +62,3 @@ var instrumentedCode = escodegen.generate(espoweredAST, {format: {compact: true}});

it(testName, function () {
var tree = esprima.parse('assert(falsyStr);', {tolerant: true, loc: true, range: true, tokens: true}),
var tree = esprima.parse('assert(falsyStr);', {tolerant: true, loc: true, range: true, tokens: true, raw: true}),
saved = espower.deepCopy(tree),

@@ -90,7 +90,7 @@ result = espower(tree, option);

var instrumentedCode = instrument('assert(falsyStr);', {source: 'assert(falsyStr);'});
assert.equal(instrumentedCode, "assert(assert._expr(assert._capt(falsyStr,''),{tree:{'type':'Identifier','name':'falsyStr','loc':{'start':{'line':1,'column':7},'end':{'line':1,'column':15}}},tokens:[{'type':'Identifier','value':'falsyStr','loc':{'start':{'line':1,'column':7},'end':{'line':1,'column':15}}}],content:'assert(falsyStr);'}));");
assert.equal(instrumentedCode, "assert(assert._expr(assert._capt(falsyStr,'arguments/0'),{content:'assert(falsyStr)',line:1}));");
});
it('with source and path', function () {
var instrumentedCode = instrument('assert(falsyStr);', {source: 'assert(falsyStr);', path: '/path/to/baz_test.js'});
assert.equal(instrumentedCode, "assert(assert._expr(assert._capt(falsyStr,''),{tree:{'type':'Identifier','name':'falsyStr','loc':{'start':{'line':1,'column':7},'end':{'line':1,'column':15}}},tokens:[{'type':'Identifier','value':'falsyStr','loc':{'start':{'line':1,'column':7},'end':{'line':1,'column':15}}}],content:'assert(falsyStr);',filepath:'/path/to/baz_test.js'}));");
assert.equal(instrumentedCode, "assert(assert._expr(assert._capt(falsyStr,'arguments/0'),{content:'assert(falsyStr)',filepath:'/path/to/baz_test.js',line:1}));");
});

@@ -103,7 +103,7 @@ });

var instrumentedCode = instrument('assert(falsyStr);', {source: 'assert(falsyStr);'});
assert.equal(instrumentedCode, "assert(assert._expr(assert._capt(falsyStr,''),{tree:{'type':'Identifier','name':'falsyStr','loc':{'start':{'line':1,'column':7},'end':{'line':1,'column':15}}},tokens:[{'type':'Identifier','value':'falsyStr','loc':{'start':{'line':1,'column':7},'end':{'line':1,'column':15}}}],content:'assert(falsyStr);'}));");
assert.equal(instrumentedCode, "assert(assert._expr(assert._capt(falsyStr,'arguments/0'),{content:'assert(falsyStr)',line:1}));");
});
it('powerAssertVariableName: "test"', function () {
var instrumentedCode = instrument('test.ok(falsyStr);', {source: 'test.ok(falsyStr);', powerAssertVariableName: 'test'});
assert.equal(instrumentedCode, "test.ok(test._expr(test._capt(falsyStr,''),{tree:{'type':'Identifier','name':'falsyStr','loc':{'start':{'line':1,'column':8},'end':{'line':1,'column':16}}},tokens:[{'type':'Identifier','value':'falsyStr','loc':{'start':{'line':1,'column':8},'end':{'line':1,'column':16}}}],content:'test.ok(falsyStr);'}));");
assert.equal(instrumentedCode, "test.ok(test._expr(test._capt(falsyStr,'arguments/0'),{content:'test.ok(falsyStr)',line:1}));");
});

@@ -117,7 +117,5 @@ it('not instrumented if powerAssertVariableName and actual variable name is different.', function () {

describe('lineSeparator option', function () {
var lineDetected = "var falsyStr='';assert.ok(assert._expr(assert._capt(falsyStr,''),{tree:{'type':'Identifier','name':'falsyStr','loc':{'start':{'line':3,'column':10},'end':{'line':3,'column':18}}},tokens:[{'type':'Identifier','value':'falsyStr','loc':{'start':{'line':3,'column':10},'end':{'line':3,'column':18}}}],content:'assert.ok(falsyStr);'}));",
describe('lineSeparator', function () {
var lineDetected = "var falsyStr='';assert.ok(assert._expr(assert._capt(falsyStr,'arguments/0'),{content:'assert.ok(falsyStr)',line:3}));";
lineNotDetected = "var falsyStr='';assert.ok(assert._expr(assert._capt(falsyStr,''),{tree:{'type':'Identifier','name':'falsyStr','loc':{'start':{'line':3,'column':10},'end':{'line':3,'column':18}}},tokens:[{'type':'Identifier','value':'falsyStr','loc':{'start':{'line':3,'column':10},'end':{'line':3,'column':18}}}]}));";
function lineSeparatorTest (name, lineSeparatorInCode, options, expected) {

@@ -139,5 +137,2 @@ it(name, function () {

when('option: default', {}, lineDetected);
when('option: LF', {lineSeparator: '\n'}, lineDetected);
when('option: CR', {lineSeparator: '\r'}, lineNotDetected);
when('option: CRLF', {lineSeparator: '\r\n'}, lineNotDetected);
});

@@ -148,6 +143,3 @@ context('code: CR', function () {

}
when('option: default', {}, lineNotDetected);
when('option: LF', {lineSeparator: '\n'}, lineNotDetected);
when('option: CR', {lineSeparator: '\r'}, lineDetected);
when('option: CRLF', {lineSeparator: '\r\n'}, lineNotDetected);
when('option: default', {}, lineDetected);
});

@@ -159,5 +151,2 @@ context('code: CRLF', function () {

when('option: default', {}, lineDetected);
when('option: LF', {lineSeparator: '\n'}, lineDetected);
when('option: CR', {lineSeparator: '\r'}, "var falsyStr='';assert.ok(assert._expr(assert._capt(falsyStr,''),{tree:{'type':'Identifier','name':'falsyStr','loc':{'start':{'line':3,'column':10},'end':{'line':3,'column':18}}},tokens:[{'type':'Identifier','value':'falsyStr','loc':{'start':{'line':3,'column':10},'end':{'line':3,'column':18}}}],content:'\\nassert.ok(falsyStr);'}));");
when('option: CRLF', {lineSeparator: '\r\n'}, lineDetected);
});

@@ -170,3 +159,3 @@ });

beforeEach(function () {
this.tree = esprima.parse('assert(falsyStr);', {tolerant: true, loc: true, range: true, tokens: true});
this.tree = esprima.parse('assert(falsyStr);', {tolerant: true, loc: true, range: true, tokens: true, raw: true});
});

@@ -185,10 +174,2 @@ function optionPrerequisitesTest (name, options, expected) {

optionPrerequisitesTest('source option is undefined',
{destructive: false, powerAssertVariableName: 'assert'},
'Target source code content should be specified by options.source.');
optionPrerequisitesTest('source option is empty',
{source: '', destructive: false, powerAssertVariableName: 'assert'},
'Target source code content should be specified by options.source.');
optionPrerequisitesTest('destructive option is undefined',

@@ -202,6 +183,2 @@ {source: 'assert(falsyStr);', destructive: undefined},

optionPrerequisitesTest('lineSeparator option is undefined',
{source: 'assert(falsyStr);', lineSeparator: undefined},
'options.lineSeparator should be specified.');
optionPrerequisitesTest('targetMethods option is undefined',

@@ -224,3 +201,3 @@ {source: 'assert(falsyStr);', targetMethods: undefined},

this.jsCode = 'assert(falsyStr);';
this.tree = esprima.parse(this.jsCode, {tolerant: true, loc: false, tokens: true});
this.tree = esprima.parse(this.jsCode, {tolerant: true, loc: false, tokens: true, raw: true});
});

@@ -251,3 +228,3 @@ it('error message when path option is not specified', function () {

var jsCode = 'assert((three * (seven * ten)) === three);',
tree = esprima.parse(jsCode, {tolerant: true, loc: true, range: true, tokens: true}),
tree = esprima.parse(jsCode, {tolerant: true, loc: true, range: true, tokens: true, raw: true}),
saved = espower.deepCopy(tree),

@@ -254,0 +231,0 @@ result = espower(tree, {destructive: false, source: jsCode, path: '/path/to/baz_test.js'});

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc