Skip to content

Commit

Permalink
chore(no-export): migrate to TS (#323)
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath authored and SimenB committed Jul 22, 2019
1 parent 12e601a commit 4270fca
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 49 deletions.
@@ -1,7 +1,7 @@
import { RuleTester } from 'eslint';
import { TSESLint } from '@typescript-eslint/experimental-utils';
import rule from '../no-export';

const ruleTester = new RuleTester({
const ruleTester = new TSESLint.RuleTester({
parserOptions: {
ecmaVersion: 2015,
sourceType: 'module',
Expand Down
47 changes: 0 additions & 47 deletions src/rules/no-export.js

This file was deleted.

72 changes: 72 additions & 0 deletions src/rules/no-export.ts
@@ -0,0 +1,72 @@
import {
AST_NODE_TYPES,
TSESTree,
} from '@typescript-eslint/experimental-utils';
import { createRule, isTestCase } from './tsUtils';

export default createRule({
name: __filename,
meta: {
docs: {
category: 'Best Practices',
description:
'Prevents exports from test files. If a file has at least 1 test in it, then this rule will prevent exports.',
recommended: false,
},
messages: {
unexpectedExport: `Do not export from a test file.`,
},
type: 'suggestion',
schema: [],
},
defaultOptions: [],
create(context) {
const exportNodes: Array<
| TSESTree.ExportNamedDeclaration
| TSESTree.ExportDefaultDeclaration
| TSESTree.MemberExpression
> = [];
let hasTestCase = false;

return {
'Program:exit'() {
if (hasTestCase && exportNodes.length > 0) {
for (const node of exportNodes) {
context.report({ node, messageId: 'unexpectedExport' });
}
}
},

CallExpression(node) {
if (isTestCase(node)) {
hasTestCase = true;
}
},
'ExportNamedDeclaration, ExportDefaultDeclaration'(
node:
| TSESTree.ExportNamedDeclaration
| TSESTree.ExportDefaultDeclaration,
) {
exportNodes.push(node);
},
'AssignmentExpression > MemberExpression'(
node: TSESTree.MemberExpression,
) {
let { object, property } = node;

if (object.type === AST_NODE_TYPES.MemberExpression) {
({ object, property } = object);
}

if (
'name' in object &&
object.name === 'module' &&
property.type === AST_NODE_TYPES.Identifier &&
/^exports?$/.test(property.name)
) {
exportNodes.push(node);
}
},
};
},
});

0 comments on commit 4270fca

Please sign in to comment.