Skip to content

Commit

Permalink
Merge pull request #44 from power-assert-js/remove-espower-error
Browse files Browse the repository at this point in the history
Remove EspowerError
  • Loading branch information
twada committed Feb 14, 2019
2 parents 5d68c1e + ef116ff commit 43830e2
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 53 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ API

`espower` function manipulates `ast` then returns `modifiedAst` that is also an AST node object defined in [The ESTree Spec](https://github.com/estree/estree). `ast` will be manipulated directly and returned `modifiedAst` will be the same instance of `ast`.

`espower` function throws `EspowerError` when
`espower` function throws `Error` when

* `ast` is already instrumented
* `ast` does not contain location information
Expand Down
13 changes: 6 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ const Instrumentor = require('./lib/instrumentor');
* @param {object} ast ECMAScript AST to be instrumented (directly modified)
* @param {object} options Instrumentation options.
* @returns {object} instrumented AST
* @throws {EspowerError} if `ast` is already instrumented
* @throws {EspowerError} if `ast` does not contain location information
* @throws {EspowerError} if `options` is not valid
* @throws {Error} if `ast` is already instrumented
* @throws {Error} if `ast` does not contain location information
* @throws {Error} if `options` is not valid
*/
const espower = (ast, options) => {
const instrumentor = new Instrumentor(Object.assign(defaultOptions(), options));
Expand All @@ -33,9 +33,9 @@ const espower = (ast, options) => {
* @param {object} ast ECMAScript AST to be instrumented (directly modified)
* @param {object} options Instrumentation options.
* @returns {object} visitor object for estraverse
* @throws {EspowerError} if `ast` is already instrumented
* @throws {EspowerError} if `ast` does not contain location information
* @throws {EspowerError} if `options` is not valid
* @throws {Error} if `ast` is already instrumented
* @throws {Error} if `ast` does not contain location information
* @throws {Error} if `options` is not valid
*/
const createVisitor = (ast, options) => {
const instrumentor = new Instrumentor(Object.assign(defaultOptions(), options));
Expand All @@ -44,5 +44,4 @@ const createVisitor = (ast, options) => {

espower.createVisitor = createVisitor;
espower.defaultOptions = defaultOptions;
espower.EspowerError = require('./lib/espower-error');
module.exports = espower;
5 changes: 2 additions & 3 deletions lib/assertion-visitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const espurify = require('espurify');
const espurifyWithRaw = espurify.customize({ extra: 'raw' });
const syntax = estraverse.Syntax;
const EspowerLocationDetector = require('espower-location-detector');
const EspowerError = require('./espower-error');
const toBeSkipped = require('./rules/to-be-skipped');
const toBeCaptured = require('./rules/to-be-captured');
const canonicalCodeOptions = {
Expand Down Expand Up @@ -190,11 +189,11 @@ class AssertionVisitor {
}
const prop = currentNode.callee.property;
if (prop.type === syntax.Identifier && prop.name === '_expr') {
let errorMessage = 'Attempted to transform AST twice.';
let errorMessage = '[espower] Attempted to transform AST twice.';
if (this.options.path) {
errorMessage += ' path: ' + this.options.path;
}
throw new EspowerError(errorMessage, this.verifyNotInstrumented);
throw new Error(errorMessage);
}
}

Expand Down
24 changes: 0 additions & 24 deletions lib/espower-error.js

This file was deleted.

9 changes: 4 additions & 5 deletions lib/instrumentor.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const escope = require('escope');
const escallmatch = require('escallmatch');
const AssertionVisitor = require('./assertion-visitor');
const Transformation = require('./transformation');
const EspowerError = require('./espower-error');
const isSpreadElement = (node) => node.type === 'SpreadElement';

class Instrumentor {
Expand Down Expand Up @@ -127,20 +126,20 @@ const isCalleeOfParentCallExpression = (parentNode, currentKey) => {

const verifyAstPrerequisites = (ast, options) => {
if (typeof ast.loc === 'undefined') {
let errorMessage = 'ECMAScript AST should contain location information.';
let errorMessage = '[espower] ECMAScript AST should contain location information.';
if (options.path) {
errorMessage += ' path: ' + options.path;
}
throw new EspowerError(errorMessage, verifyAstPrerequisites);
throw new Error(errorMessage);
}
};

const verifyOptionPrerequisites = (options) => {
if (options.destructive === false) {
throw new EspowerError('options.destructive is deprecated and always treated as destructive:true', verifyOptionPrerequisites);
throw new Error('[espower] options.destructive is deprecated and always treated as destructive:true');
}
if (!Array.isArray(options.patterns)) {
throw new EspowerError('options.patterns should be an array.', verifyOptionPrerequisites);
throw new Error('[espower] options.patterns should be an array.');
}
};

Expand Down
19 changes: 6 additions & 13 deletions test/espower_option_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ var sourceMap = require('source-map');
var assert = require('assert');
var join = require('path').join;

var EspowerError = espower.EspowerError;

function instrument (jsCode, options) {
var jsAST = acorn.parse(jsCode, {ecmaVersion: 7, locations: true, plugins: {asyncawait: true}});
var espoweredAST = espower(jsAST, options);
Expand Down Expand Up @@ -111,7 +109,7 @@ describe('instrumentation tests for options', function () {
var tree = acorn.parse('assert(falsyStr);', {ecmaVersion: 6, locations: true, ranges: true});
assert.throws(function () {
espower(tree, {destructive: false});
}, EspowerError);
}, Error);
});
destructiveOptionTest('destructive: true', {destructive: true}, function (assert, before, tree, after) {
assert.notDeepEqual(tree, before);
Expand Down Expand Up @@ -180,9 +178,8 @@ describe('option prerequisites', function () {
assert.ok(false, 'Error should be thrown');
} catch (e) {
assert.equal(e.message, expected);
assert.equal(e.name, 'EspowerError');
assert.equal(e.name, 'Error');
assert(e instanceof Error);
assert(e instanceof EspowerError);
assert(e.stack);
}
});
Expand All @@ -204,9 +201,8 @@ describe('AST prerequisites. Error should be thrown if location is missing.', fu
espower(this.tree);
assert.ok(false, 'Error should be thrown');
} catch (e) {
assert.equal(e.name, 'EspowerError');
assert.equal(e.name, 'Error');
assert(e instanceof Error);
assert(e instanceof EspowerError);
assert.equal(e.message, '[espower] ECMAScript AST should contain location information.');
assert(e.stack);
}
Expand All @@ -216,9 +212,8 @@ describe('AST prerequisites. Error should be thrown if location is missing.', fu
espower(this.tree, {path: '/path/to/baz_test.js'});
assert.ok(false, 'Error should be thrown');
} catch (e) {
assert.equal(e.name, 'EspowerError');
assert.equal(e.name, 'Error');
assert(e instanceof Error);
assert(e instanceof EspowerError);
assert.equal(e.message, '[espower] ECMAScript AST should contain location information. path: /path/to/baz_test.js');
assert(e.stack);
}
Expand All @@ -235,9 +230,8 @@ describe('AST prerequisites. Error should be thrown if AST is already instrument
espower(ast, {path: '/path/to/baz_test.js'});
assert.ok(false, 'Error should be thrown');
} catch (e) {
assert.equal(e.name, 'EspowerError');
assert.equal(e.name, 'Error');
assert(e instanceof Error);
assert(e instanceof EspowerError);
assert.equal(e.message, '[espower] Attempted to transform AST twice. path: /path/to/baz_test.js');
assert(e.stack);
}
Expand All @@ -255,9 +249,8 @@ describe('AST prerequisites. Error should be thrown if AST is already instrument
});
assert.ok(false, 'Error should be thrown');
} catch (e) {
assert.equal(e.name, 'EspowerError');
assert.equal(e.name, 'Error');
assert(e instanceof Error);
assert(e instanceof EspowerError);
assert.equal(e.message, '[espower] Attempted to transform AST twice. path: /path/to/foo_test.js');
assert(e.stack);
}
Expand Down

0 comments on commit 43830e2

Please sign in to comment.