Skip to content

Commit

Permalink
Support named imports and destructured requires (#232)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
Alex Zherdev and sindresorhus committed Sep 16, 2019
1 parent 6705aa6 commit e30eafa
Show file tree
Hide file tree
Showing 2 changed files with 200 additions and 6 deletions.
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',
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
71 changes: 71 additions & 0 deletions test/create-ava-rule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
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'
}
});

const errors = [
{
message: 'not a test file'
}
];

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
},
{
code: 'const {serial2} = require(\'ava\');',
errors
},
{
code: 'const {serial2: test} = require(\'ava\');',
errors
},
{
code: 'import test2 from \'ava\';',
errors
},
{
code: 'import {serial2} from \'ava\';',
errors
},
{
code: 'import {serial2 as test} from \'ava\';',
errors
}
]
});

0 comments on commit e30eafa

Please sign in to comment.