Skip to content
This repository was archived by the owner on Jan 6, 2020. It is now read-only.

Simplify implementation #8

Merged
merged 2 commits into from
Mar 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 37 additions & 77 deletions example-output.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,21 @@ t.throws(foo())
becomes:

```js
function _avaThrowsHelper(fn, data) {
try {
return fn();
} catch (e) {
var type = typeof e;

if (e !== null && (type === "object" || type === "function")) {
try {
Object.defineProperty(e, "_avaThrowsHelperData", {
value: data
});
} catch (e) {}
}

throw e;
function _avaThrowsHelperStart(t, assertion, file, line) {
if (t._throwsArgStart) {
t._throwsArgStart(assertion, file, line);
}
}

t.throws(_avaThrowsHelper(function () {
return foo();
}, {
line: 1,
column: 9,
source: "foo()",
filename: "some-file.js"
}));
function _avaThrowsHelperEnd(t, arg) {
if (t._throwsArgEnd) {
t._throwsArgEnd();
}

return arg;
}

t.throws((_avaThrowsHelperStart(t, "throws", "some-file.js", 1), _avaThrowsHelperEnd(t, foo())));
```

---
Expand All @@ -47,40 +36,22 @@ t.throws(bar());
becomes:

```js
function _avaThrowsHelper(fn, data) {
try {
return fn();
} catch (e) {
var type = typeof e;

if (e !== null && (type === "object" || type === "function")) {
try {
Object.defineProperty(e, "_avaThrowsHelperData", {
value: data
});
} catch (e) {}
}

throw e;
function _avaThrowsHelperStart(t, assertion, file, line) {
if (t._throwsArgStart) {
t._throwsArgStart(assertion, file, line);
}
}

function _avaThrowsHelperEnd(t, arg) {
if (t._throwsArgEnd) {
t._throwsArgEnd();
}

return arg;
}

t.throws(_avaThrowsHelper(function () {
return foo();
}, {
line: 1,
column: 9,
source: "foo()",
filename: "some-file.js"
}));
t.throws(_avaThrowsHelper(function () {
return bar();
}, {
line: 2,
column: 9,
source: "bar()",
filename: "some-file.js"
}));
t.throws((_avaThrowsHelperStart(t, "throws", "some-file.js", 1), _avaThrowsHelperEnd(t, foo())));
t.throws((_avaThrowsHelperStart(t, "throws", "some-file.js", 2), _avaThrowsHelperEnd(t, bar())));
```

---
Expand Down Expand Up @@ -108,32 +79,21 @@ t.notThrows(baz())
becomes:

```js
function _avaThrowsHelper(fn, data) {
try {
return fn();
} catch (e) {
var type = typeof e;

if (e !== null && (type === "object" || type === "function")) {
try {
Object.defineProperty(e, "_avaThrowsHelperData", {
value: data
});
} catch (e) {}
}

throw e;
function _avaThrowsHelperStart(t, assertion, file, line) {
if (t._throwsArgStart) {
t._throwsArgStart(assertion, file, line);
}
}

function _avaThrowsHelperEnd(t, arg) {
if (t._throwsArgEnd) {
t._throwsArgEnd();
}

return arg;
}

t.notThrows(_avaThrowsHelper(function () {
return baz();
}, {
line: 1,
column: 12,
source: "baz()",
filename: "some-file.js"
}));
t.notThrows((_avaThrowsHelperStart(t, "notThrows", "some-file.js", 1), _avaThrowsHelperEnd(t, baz())));
```

---
103 changes: 44 additions & 59 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,85 +1,70 @@
'use strict';
var t = require('babel-types');
var template = require('babel-template');

var wrapWithHelper = template([
'HELPER_ID(function () {',
' return EXP;',
'}, {',
' line: LINE,',
' column: COLUMN,',
' source: SOURCE,',
' filename: FILE',
'});'
].join('\n'));
module.exports = babelCore => {
const t = babelCore.types;
const wrapArg = babelCore.template('(START(t, ASSERTION, FILE, LINE), END(t, ARG))');
const helpers = babelCore.template(`function START(t, assertion, file, line) {
if (t._throwsArgStart) {
t._throwsArgStart(assertion, file, line);
}
}

function END(t, arg) {
if (t._throwsArgEnd) {
t._throwsArgEnd();
}

var buildHelper = template([
'function HELPER_ID(fn, data) {',
' try {',
' return fn();',
' } catch (e) {',
' var type = typeof e;',
' if (e !== null && (type === "object" || type === "function")) {',
' try {',
' Object.defineProperty(e, "_avaThrowsHelperData", {',
' value: data',
' });',
' } catch (e) {}',
' }',
' throw e;',
' }',
'}'
].join('\n'));
return arg;
}`);

var assertionVisitor = {
CallExpression: function (path, state) {
if (isThrowsMember(path.get('callee'))) {
var arg0 = path.node.arguments[0];
const assertionVisitor = {
CallExpression(path, state) {
const callee = path.get('callee');
if (!callee.isMemberExpression() || !callee.get('object').isIdentifier({name: 't'}) || !callee.get('property').isIdentifier()) {
return;
}

const assertion = callee.get('property').get('name').node;
if (assertion !== 'throws' && assertion !== 'notThrows') {
return;
}

const arg0 = path.node.arguments[0];
if (!(arg0 && arg0.loc && (typeof arg0.start === 'number') && (typeof arg0.end === 'number'))) {
return;
}

path.node.arguments[0] = wrapWithHelper({
HELPER_ID: t.identifier(this.avaThrowHelper()),
EXP: arg0,
LINE: t.numericLiteral(arg0.loc.start.line),
COLUMN: t.numericLiteral(arg0.loc.start.column),
SOURCE: t.stringLiteral(state.file.code.substring(arg0.start, arg0.end)),
FILE: t.stringLiteral(state.file.opts.filename)
}).expression;
// Wrap the argument expression, so that the stack trace of the assertion
// isn't affected.
path.node.arguments[0] = wrapArg(Object.assign({
ARG: arg0,
ASSERTION: t.stringLiteral(assertion),
FILE: t.stringLiteral(state.file.opts.filename),
LINE: t.numericLiteral(arg0.loc.start.line)
}, this.installHelper())).expression;
}
}
};
};

module.exports = function () {
return {
visitor: {
Program: function (path, state) {
var HELPER_ID = path.scope.generateUid('avaThrowsHelper');
var created = false;
Program(path, state) {
const START = t.identifier(path.scope.generateUid('avaThrowsHelperStart'));
const END = t.identifier(path.scope.generateUid('avaThrowsHelperEnd'));
const helperIdentifiers = {START, END};

let created = false;
path.traverse(assertionVisitor, {
avaThrowHelper: function () {
installHelper() {
if (!created) {
created = true;
path.unshiftContainer('body', buildHelper({
HELPER_ID: t.identifier(HELPER_ID)
}));
path.unshiftContainer('body', helpers(helperIdentifiers));
}

return HELPER_ID;
return helperIdentifiers;
},
file: state.file
});
}
}
};
};

function isThrowsMember(path) {
return path.isMemberExpression() && path.get('object').isIdentifier({name: 't'}) && (
path.get('property').isIdentifier({name: 'throws'}) ||
path.get('property').isIdentifier({name: 'notThrows'})
);
}
6 changes: 1 addition & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,8 @@
"assertion",
"throws"
],
"dependencies": {
"babel-template": "^6.7.0",
"babel-types": "^6.7.2"
},
"devDependencies": {
"ava": "^0.17.0",
"ava": "^0.18.2",
"babel-core": "^6.7.5",
"xo": "^0.17.0"
}
Expand Down
Loading