Skip to content

Commit

Permalink
feat(espower): option powerAssertVariableName does not required any…
Browse files Browse the repository at this point in the history
… more
  • Loading branch information
twada committed Jul 31, 2014
1 parent e75e5d3 commit 2f023f9
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 49 deletions.
30 changes: 20 additions & 10 deletions build/espower.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ module.exports = deepCopy;
module.exports = function defaultOptions () {
return {
destructive: false,
powerAssertVariableName: 'assert',
escodegenOptions: {
format: {
indent: {
Expand Down Expand Up @@ -140,6 +139,7 @@ Instrumentor.prototype.instrument = function (ast) {
assertionPath,
argumentPath,
canonicalCode,
powerAssertVariableName,
lineNum,
argumentModified = false,
skipping = false,
Expand Down Expand Up @@ -171,6 +171,7 @@ Instrumentor.prototype.instrument = function (ast) {
lineNum = currentNode.loc.start.line;
canonicalCode = generateCanonicalCode(currentNode, that.options.escodegenOptions);
assertionPath = [].concat(path);
powerAssertVariableName = guessPowerAssertVariableName(currentNode.callee);
return undefined;
}
if (parentNode.type !== syntax.CallExpression || !isSupportedNodeType(currentNode)) {
Expand Down Expand Up @@ -216,6 +217,7 @@ Instrumentor.prototype.instrument = function (ast) {
canonicalCode = null;
lineNum = null;
assertionPath = null;
powerAssertVariableName = null;
return undefined;
}

Expand All @@ -240,7 +242,7 @@ Instrumentor.prototype.instrument = function (ast) {
case syntax.AssignmentExpression:
case syntax.UpdateExpression:
case syntax.NewExpression:
resultTree = that.captureNode(currentNode, relativeEsPath);
resultTree = that.captureNode(currentNode, relativeEsPath, powerAssertVariableName);
argumentModified = true;
break;
default:
Expand All @@ -252,7 +254,7 @@ Instrumentor.prototype.instrument = function (ast) {
argumentPath = null;
if (argumentModified) {
argumentModified = false;
return that.captureArgument(resultTree, canonicalCode, lineNum);
return that.captureArgument(resultTree, canonicalCode, powerAssertVariableName, lineNum);
}
}

Expand All @@ -262,7 +264,7 @@ Instrumentor.prototype.instrument = function (ast) {
return result;
};

Instrumentor.prototype.captureArgument = function (node, canonicalCode, lineNum) {
Instrumentor.prototype.captureArgument = function (node, canonicalCode, powerAssertVariableName, lineNum) {
var n = newNodeWithLocationCopyOf(node),
props = [];
addLiteralTo(props, n, 'content', canonicalCode);
Expand All @@ -275,7 +277,7 @@ Instrumentor.prototype.captureArgument = function (node, canonicalCode, lineNum)
computed: false,
object: n({
type: syntax.Identifier,
name: this.options.powerAssertVariableName
name: powerAssertVariableName
}),
property: n({
type: syntax.Identifier,
Expand All @@ -289,7 +291,7 @@ Instrumentor.prototype.captureArgument = function (node, canonicalCode, lineNum)
});
};

Instrumentor.prototype.captureNode = function (target, relativeEsPath) {
Instrumentor.prototype.captureNode = function (target, relativeEsPath, powerAssertVariableName) {
var n = newNodeWithLocationCopyOf(target);
return n({
type: syntax.CallExpression,
Expand All @@ -298,7 +300,7 @@ Instrumentor.prototype.captureNode = function (target, relativeEsPath) {
computed: false,
object: n({
type: syntax.Identifier,
name: this.options.powerAssertVariableName
name: powerAssertVariableName
}),
property: n({
type: syntax.Identifier,
Expand All @@ -316,6 +318,17 @@ Instrumentor.prototype.captureNode = function (target, relativeEsPath) {
};


function guessPowerAssertVariableName (node) {
switch(node.type) {
case syntax.Identifier:
return node.name;
case syntax.MemberExpression:
return guessPowerAssertVariableName(node.object);
}
return null;
}


function generateCanonicalCode(node, escodegenOptions) {
var ast = deepCopy(node);
estraverse.replace(ast, {
Expand Down Expand Up @@ -423,9 +436,6 @@ function ensureOptionPrerequisites (options) {
if (typeName(options.destructive) !== 'boolean') {
throw new Error('options.destructive should be a boolean value.');
}
if (typeName(options.powerAssertVariableName) !== 'string' || options.powerAssertVariableName === '') {
throw new Error('options.powerAssertVariableName should be a non-empty string.');
}
if (typeName(options.patterns) !== 'Array') {
throw new Error('options.patterns should be an array.');
}
Expand Down
1 change: 0 additions & 1 deletion lib/default-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
module.exports = function defaultOptions () {
return {
destructive: false,
powerAssertVariableName: 'assert',
escodegenOptions: {
format: {
indent: {
Expand Down
29 changes: 20 additions & 9 deletions lib/instrumentor.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Instrumentor.prototype.instrument = function (ast) {
assertionPath,
argumentPath,
canonicalCode,
powerAssertVariableName,
lineNum,
argumentModified = false,
skipping = false,
Expand Down Expand Up @@ -70,6 +71,7 @@ Instrumentor.prototype.instrument = function (ast) {
lineNum = currentNode.loc.start.line;
canonicalCode = generateCanonicalCode(currentNode, that.options.escodegenOptions);
assertionPath = [].concat(path);
powerAssertVariableName = guessPowerAssertVariableName(currentNode.callee);
return undefined;
}
if (parentNode.type !== syntax.CallExpression || !isSupportedNodeType(currentNode)) {
Expand Down Expand Up @@ -115,6 +117,7 @@ Instrumentor.prototype.instrument = function (ast) {
canonicalCode = null;
lineNum = null;
assertionPath = null;
powerAssertVariableName = null;
return undefined;
}

Expand All @@ -139,7 +142,7 @@ Instrumentor.prototype.instrument = function (ast) {
case syntax.AssignmentExpression:
case syntax.UpdateExpression:
case syntax.NewExpression:
resultTree = that.captureNode(currentNode, relativeEsPath);
resultTree = that.captureNode(currentNode, relativeEsPath, powerAssertVariableName);
argumentModified = true;
break;
default:
Expand All @@ -151,7 +154,7 @@ Instrumentor.prototype.instrument = function (ast) {
argumentPath = null;
if (argumentModified) {
argumentModified = false;
return that.captureArgument(resultTree, canonicalCode, lineNum);
return that.captureArgument(resultTree, canonicalCode, powerAssertVariableName, lineNum);
}
}

Expand All @@ -161,7 +164,7 @@ Instrumentor.prototype.instrument = function (ast) {
return result;
};

Instrumentor.prototype.captureArgument = function (node, canonicalCode, lineNum) {
Instrumentor.prototype.captureArgument = function (node, canonicalCode, powerAssertVariableName, lineNum) {
var n = newNodeWithLocationCopyOf(node),
props = [];
addLiteralTo(props, n, 'content', canonicalCode);
Expand All @@ -174,7 +177,7 @@ Instrumentor.prototype.captureArgument = function (node, canonicalCode, lineNum)
computed: false,
object: n({
type: syntax.Identifier,
name: this.options.powerAssertVariableName
name: powerAssertVariableName
}),
property: n({
type: syntax.Identifier,
Expand All @@ -188,7 +191,7 @@ Instrumentor.prototype.captureArgument = function (node, canonicalCode, lineNum)
});
};

Instrumentor.prototype.captureNode = function (target, relativeEsPath) {
Instrumentor.prototype.captureNode = function (target, relativeEsPath, powerAssertVariableName) {
var n = newNodeWithLocationCopyOf(target);
return n({
type: syntax.CallExpression,
Expand All @@ -197,7 +200,7 @@ Instrumentor.prototype.captureNode = function (target, relativeEsPath) {
computed: false,
object: n({
type: syntax.Identifier,
name: this.options.powerAssertVariableName
name: powerAssertVariableName
}),
property: n({
type: syntax.Identifier,
Expand All @@ -215,6 +218,17 @@ Instrumentor.prototype.captureNode = function (target, relativeEsPath) {
};


function guessPowerAssertVariableName (node) {
switch(node.type) {
case syntax.Identifier:
return node.name;
case syntax.MemberExpression:
return guessPowerAssertVariableName(node.object);
}
return null;
}


function generateCanonicalCode(node, escodegenOptions) {
var ast = deepCopy(node);
estraverse.replace(ast, {
Expand Down Expand Up @@ -322,9 +336,6 @@ function ensureOptionPrerequisites (options) {
if (typeName(options.destructive) !== 'boolean') {
throw new Error('options.destructive should be a boolean value.');
}
if (typeName(options.powerAssertVariableName) !== 'string' || options.powerAssertVariableName === '') {
throw new Error('options.powerAssertVariableName should be a non-empty string.');
}
if (typeName(options.patterns) !== 'Array') {
throw new Error('options.patterns should be an array.');
}
Expand Down
31 changes: 2 additions & 29 deletions test/espower_option_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ describe('espower.defaultOptions()', function () {
it('destructive: false', function () {
assert.equal(this.options.destructive, false);
});
it('powerAssertVariableName: "assert"', function () {
assert.equal(this.options.powerAssertVariableName, 'assert');
});
});


Expand Down Expand Up @@ -82,22 +79,6 @@ describe('instrumentation tests for options', function () {
});


describe('powerAssertVariableName option.', function () {
it('default is "assert"', function () {
var instrumentedCode = instrument('assert(falsyStr);', {source: 'assert(falsyStr);', patterns: ['assert(value)']});
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', patterns: ['test.ok(value)']});
assert.equal(instrumentedCode, "test.ok(test._expr(test._capt(falsyStr,'arguments/0'),{content:'test.ok(falsyStr)',line:1}));");
});
it('not instrumented if powerAssertVariableName and actual variable name is different.', function () {
var instrumentedCode = instrument('assert.ok(falsyStr);', {source: 'assert.ok(falsyStr);', powerAssertVariableName: 'test', patterns: ['test.ok(value)']});
assert.equal(instrumentedCode, "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}));";

Expand Down Expand Up @@ -154,14 +135,6 @@ describe('option prerequisites', function () {
{source: 'assert(falsyStr);', destructive: 1},
'options.destructive should be a boolean value.');

optionPrerequisitesTest('powerAssertVariableName option should be a string',
{source: 'assert(falsyStr);', powerAssertVariableName: true},
'options.powerAssertVariableName should be a non-empty string.');

optionPrerequisitesTest('powerAssertVariableName option should be a non-empty string',
{source: 'assert(falsyStr);', powerAssertVariableName: ''},
'options.powerAssertVariableName should be a non-empty string.');

optionPrerequisitesTest('patterns option should be an array',
{source: 'assert(falsyStr);', patterns: 'hoge'},
'options.patterns should be an array.');
Expand All @@ -175,7 +148,7 @@ describe('AST prerequisites. Error should be thrown if location is missing.', fu
});
it('error message when path option is not specified', function () {
try {
espower(this.tree, {destructive: false, source: this.jsCode, powerAssertVariableName: 'assert'});
espower(this.tree, {destructive: false, source: this.jsCode});
assert.ok(false, 'Error should be thrown');
} catch (e) {
assert.equal(e.name, 'Error');
Expand All @@ -184,7 +157,7 @@ describe('AST prerequisites. Error should be thrown if location is missing.', fu
});
it('error message when path option is specified', function () {
try {
espower(this.tree, {destructive: false, source: this.jsCode, powerAssertVariableName: 'assert', path: '/path/to/baz_test.js'});
espower(this.tree, {destructive: false, source: this.jsCode, path: '/path/to/baz_test.js'});
assert.ok(false, 'Error should be thrown');
} catch (e) {
assert.equal(e.name, 'Error');
Expand Down

0 comments on commit 2f023f9

Please sign in to comment.