Skip to content

Make writing "within a test" and "t.XXX" matchers easier: #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jamestalmage opened this issue Apr 7, 2016 · 11 comments
Closed

Make writing "within a test" and "t.XXX" matchers easier: #72

jamestalmage opened this issue Apr 7, 2016 · 11 comments
Labels

Comments

@jamestalmage
Copy link
Contributor

We have this code construct everywhere:

CallExpression: function (node) {
  // only check if we are within a test file, and if this CallExpression is within a test.
  if (!ava.isTestFile || !ava.currentTestNode) {
    return;
  }
}

I propose we add some sugar to ava.merge() that allows something like this:

'CallExpression:inTest': function (node) {

},
'VariableDeclaration:inTestFile': function (node) {

},
'CallExpression:inTest:inTestFile': function (node) {

},

While we are at it, we should define one (specifically for CallExpression), that checks the callee is a MemberExpression of the form t.XXX:

'AssertionCallExpression:inTest': function (node) {

}
@jamestalmage
Copy link
Contributor Author

Counterpoint: Does our API change often enough that these sorts of helpers would be worth the effort it took to create?

Err wait - this is the linter, not codemods. Can't keep all the AVA related AST projects straight.

@sindresorhus
Copy link
Member

// @twada

@jfmengels
Copy link
Contributor

Agree with the idea, but I'm not sure it's a good idea. I noticed that we don't handle cases like this

function implementation(t) {
  t.doWhatever();
}

test(implementation);

though we might want to, especially with the talk about "macros" in avajs/ava#695. In this case, the "in test" part will pretty much always be wrong.

Otherwise, I think we don't need to add the inTest suffix, we could make ava.isTestFile && ava.currentTestNode a necessary condition for the visitor to be called (they all check that as far as I can tell).

We can definitely add the ava.isTestFile condition everywhere though (not ava.currentTestNode), and the AssertionCallExpression seems like a good idea too.

@twada
Copy link
Collaborator

twada commented Apr 8, 2016

Hmm, I think that's out of scope of static analysis tool, it's better and easier to detect and report at runtime.

@sindresorhus
Copy link
Member

@twada What specifically is out of scope?

@twada
Copy link
Collaborator

twada commented Apr 11, 2016

What specifically is out of scope?

Chasing some kind of macros, renamed variables, destructuring, etc.
They tend to make static analysis tools fragile.
(It's just my opinion)

@jfmengels
Copy link
Contributor

What do you guys think of specifying a simple filter function instead?

From

CallExpression: function (node) {
  // only check if we are within a test file, and if this CallExpression is within a test.
  if (!ava.isTestFile || !ava.currentTestNode) {
    return;
  }
}

to

// exported by create-ava-rule for instance
function ifInAVATest(fn) {
  return function(node) {
    if (ava.isTestFile && ava.currentTestNode) {
      return fn(node);
    }
  };
}

// test file
CallExpression: ifInAVATest(function (node) {
  // do stuff
})

No new sugar, just explicit functions, and they're chainable too if needed.

@jfmengels
Copy link
Contributor

jfmengels commented Apr 25, 2016

Even better: just have some predicate functions and a visitor filter function

// create-ava-rule.js
function isAssertion(node) {
  return node.type === 'CallExpression' && ...
}

function callIf(...fns) {
  return function(visitor) {
    return function (node) {
      // some kind of
      if (fns.every((fn) => fn(node)) {
        visitor(node);
      }
    };
  };
}

// test file
CallExpression: callIf(ava.isAssertion, ava.fooBar)(function (node) {
  // do stuff
  // I can still use ava.isAssertion programmatically
})

@sindresorhus
Copy link
Member

@jfmengels I like it. @jamestalmage ?

@twada
Copy link
Collaborator

twada commented May 18, 2016

@jfmengels Wow it looks nice :)

@jamestalmage
Copy link
Contributor Author

👍

jfmengels added a commit that referenced this issue May 18, 2016
jamestalmage pushed a commit that referenced this issue May 19, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants