Skip to content
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

Support named imports and destructured requires #232

Merged
merged 4 commits into from
Sep 16, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
135 changes: 129 additions & 6 deletions create-ava-rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const enhance = require('enhance-visitors');
const deepStrictEqual = require('deep-strict-equal');
const util = require('./util');

const avaImportDeclarationAst = {
const avaImportDeclarationAsts = [{
type: 'ImportDeclaration',
specifiers: [
{
Expand All @@ -19,9 +19,66 @@ const avaImportDeclarationAst = {
type: 'Literal',
value: 'ava'
}
};
}, {
type: 'ImportDeclaration',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not happy with the wall of ASTs... any way to make this easier to comprehend?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might have been able to reduce the amount of AST code if you did the checks programmatically instead of comparing the AST tree, but that will also be more fragile and IMHO less readable, so I think this is fine.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some comments here and there to define what each AST match against could help.

specifiers: [
{
type: 'ImportSpecifier',
imported: {
type: 'Identifier',
name: 'serial'
},
local: {
type: 'Identifier',
name: 'test'
}
}
],
source: {
type: 'Literal',
value: 'ava'
}
}, {
type: 'ImportDeclaration',
specifiers: [
{
type: 'ImportSpecifier',
imported: {
type: 'Identifier',
name: 'serial'
},
local: {
type: 'Identifier',
name: 'test'
}
}
],
source: {
type: 'Literal',
value: 'ava'
}
}, {
type: 'ImportDeclaration',
specifiers: [
{
type: 'ImportSpecifier',
imported: {
type: 'Identifier',
name: 'serial'
},
local: {
type: 'Identifier',
name: 'serial'
}
}
],
source: {
type: 'Literal',
value: 'ava'
}
}];

const avaVariableDeclaratorAst = {
const avaVariableDeclaratorAsts = [{
type: 'VariableDeclarator',
id: {
type: 'Identifier',
Expand All @@ -40,7 +97,73 @@ const avaVariableDeclaratorAst = {
}
]
}
};
}, {
type: 'VariableDeclarator',
id: {
type: 'ObjectPattern',
properties: [{
type: 'Property',
key: {
type: 'Identifier',
name: 'serial'
},
value: {
type: 'Identifier',
name: 'serial'
},
kind: 'init',
method: false,
shorthand: true,
computed: false
}]
},
init: {
type: 'CallExpression',
callee: {
type: 'Identifier',
name: 'require'
},
arguments: [
{
type: 'Literal',
value: 'ava'
}
]
}
}, {
type: 'VariableDeclarator',
id: {
type: 'ObjectPattern',
properties: [{
type: 'Property',
key: {
type: 'Identifier',
name: 'serial'
},
value: {
type: 'Identifier',
name: 'test'
},
kind: 'init',
method: false,
shorthand: false,
computed: false
}]
},
init: {
type: 'CallExpression',
callee: {
type: 'Identifier',
name: 'require'
},
arguments: [
{
type: 'Literal',
value: 'ava'
}
]
}
}];

function isTestFunctionCall(node) {
if (node.type === 'Identifier') {
Expand All @@ -65,12 +188,12 @@ module.exports = () => {
/* eslint quote-props: [2, "as-needed"] */
const predefinedRules = {
ImportDeclaration: node => {
if (!isTestFile && deepStrictEqual(espurify(node), avaImportDeclarationAst)) {
if (!isTestFile && avaImportDeclarationAsts.some(ast => deepStrictEqual(espurify(node), ast))) {
isTestFile = true;
}
},
VariableDeclarator: node => {
if (!isTestFile && deepStrictEqual(espurify(node), avaVariableDeclaratorAst)) {
if (!isTestFile && avaVariableDeclaratorAsts.some(ast => deepStrictEqual(espurify(node), ast))) {
isTestFile = true;
}
},
Expand Down
65 changes: 65 additions & 0 deletions test/create-ava-rule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import test from 'ava';
import avaRuleTester from 'eslint-ava-rule-tester';
import {visitIf} from 'enhance-visitors';
import createAvaRule from '../create-ava-rule';

const rule = {
create: context => {
const ava = createAvaRule();

return ava.merge({
'Program:exit': node => {
if (!ava.isInTestFile()) {
context.report({node, message: 'not a test file'});
}
}
});
}
};

const ruleTester = avaRuleTester(test, {
env: {
es6: true,
},
parserOptions: {
sourceType: 'module'
}
});

sindresorhus marked this conversation as resolved.
Show resolved Hide resolved
ruleTester.run('rule-fixture', rule, {
valid: [
'const test = require(\'ava\');',
'const {serial} = require(\'ava\');',
'const {serial: test} = require(\'ava\');',
'import test from \'ava\';',
'import {serial} from \'ava\';',
'import {serial as test} from \'ava\';'
],

invalid: [
{
code: 'const test2 = require(\'ava\');',
errors: [{message: 'not a test file'}]
},
{
code: 'const {serial2} = require(\'ava\');',
errors: [{message: 'not a test file'}]
},
{
code: 'const {serial2: test} = require(\'ava\');',
errors: [{message: 'not a test file'}]
},
{
code: 'import test2 from \'ava\';',
errors: [{message: 'not a test file'}]
},
{
code: 'import {serial2} from \'ava\';',
errors: [{message: 'not a test file'}]
},
{
code: 'import {serial2 as test} from \'ava\';',
errors: [{message: 'not a test file'}]
}
]
});