aboutsummaryrefslogtreecommitdiff
path: root/node_modules/@ava/babel-plugin-throws-helper
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/@ava/babel-plugin-throws-helper')
-rw-r--r--node_modules/@ava/babel-plugin-throws-helper/index.js70
-rw-r--r--node_modules/@ava/babel-plugin-throws-helper/license21
-rw-r--r--node_modules/@ava/babel-plugin-throws-helper/package.json35
-rw-r--r--node_modules/@ava/babel-plugin-throws-helper/readme.md33
4 files changed, 159 insertions, 0 deletions
diff --git a/node_modules/@ava/babel-plugin-throws-helper/index.js b/node_modules/@ava/babel-plugin-throws-helper/index.js
new file mode 100644
index 000000000..e370480f1
--- /dev/null
+++ b/node_modules/@ava/babel-plugin-throws-helper/index.js
@@ -0,0 +1,70 @@
+'use strict';
+
+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();
+ }
+
+ return arg;
+}`);
+
+ 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;
+ }
+
+ // 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;
+ }
+ };
+
+ return {
+ visitor: {
+ 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, {
+ installHelper() {
+ if (!created) {
+ created = true;
+ path.unshiftContainer('body', helpers(helperIdentifiers));
+ }
+
+ return helperIdentifiers;
+ },
+ file: state.file
+ });
+ }
+ }
+ };
+};
diff --git a/node_modules/@ava/babel-plugin-throws-helper/license b/node_modules/@ava/babel-plugin-throws-helper/license
new file mode 100644
index 000000000..ad5d021ed
--- /dev/null
+++ b/node_modules/@ava/babel-plugin-throws-helper/license
@@ -0,0 +1,21 @@
+The MIT License (MIT)
+
+Copyright (c) James Talmage <james@talmage.io> (github.com/jamestalmage)
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
diff --git a/node_modules/@ava/babel-plugin-throws-helper/package.json b/node_modules/@ava/babel-plugin-throws-helper/package.json
new file mode 100644
index 000000000..83dbf4659
--- /dev/null
+++ b/node_modules/@ava/babel-plugin-throws-helper/package.json
@@ -0,0 +1,35 @@
+{
+ "name": "@ava/babel-plugin-throws-helper",
+ "version": "2.0.0",
+ "description": "Babel plugin for protecting against improper use of `t.throws()` in AVA",
+ "license": "MIT",
+ "repository": "avajs/babel-plugin-throws-helper",
+ "author": {
+ "name": "James Talmage",
+ "email": "james@talmage.io",
+ "url": "github.com/jamestalmage"
+ },
+ "engines": {
+ "node": ">=4"
+ },
+ "scripts": {
+ "test": "xo && ava",
+ "preversion": "WRITE_EXAMPLES=1 npm run test && git add example-output.md"
+ },
+ "files": [
+ "index.js"
+ ],
+ "keywords": [
+ "babel-plugin",
+ "babel",
+ "helper",
+ "ava",
+ "assertion",
+ "throws"
+ ],
+ "devDependencies": {
+ "ava": "^0.18.2",
+ "babel-core": "^6.7.5",
+ "xo": "^0.17.0"
+ }
+}
diff --git a/node_modules/@ava/babel-plugin-throws-helper/readme.md b/node_modules/@ava/babel-plugin-throws-helper/readme.md
new file mode 100644
index 000000000..faa91d49d
--- /dev/null
+++ b/node_modules/@ava/babel-plugin-throws-helper/readme.md
@@ -0,0 +1,33 @@
+# babel-plugin-throws-helper [![Build Status](https://travis-ci.org/avajs/babel-plugin-throws-helper.svg?branch=master)](https://travis-ci.org/avajs/babel-plugin-throws-helper)
+
+> Babel plugin for protecting against improper use of `t.throws()` in [AVA](https://ava.li)
+
+Probably not useful except as an internal plugin for the AVA test runner.
+
+[Genesis of the idea.](https://github.com/sindresorhus/eslint-plugin-ava/issues/75)
+
+## Issue
+
+> I've seen a lot of incorrect use of the throws assertion in other test runner and even done the mistake myself sometimes. Now I'm beginning to see it with AVA too, so would be nice to be preemptive about it.
+>
+> People don't realize they need to wrap their call in a function, so many end up doing `t.throws(foo())` instead of `t.throws(() => foo());`. It's an easy mistake to make.
+
+The difficulty is that `t.throws(foo())` is allowed if `foo()` returns a promise or a function. There is no good way to differentiate between the two at runtime. So providing a good error message is going to take some AST transform magic.
+
+
+## Solution
+
+See [`example-output.md`](example-output.md) for the transformation this plugin performs.
+
+The example output can be generated by calling:
+
+```
+$ WRITE_EXAMPLES=1 ava
+```
+
+Contributors should not commit new examples. They will be regenerated as part of the release process.
+
+
+## License
+
+MIT © [James Talmage](https://github.com/jamestalmage)