-
Notifications
You must be signed in to change notification settings - Fork 62
/
eslint-local-rules.js
45 lines (41 loc) · 1.34 KB
/
eslint-local-rules.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
'use strict';
const TEST_FILE_REGEX= /\.(spec|test)\.(js|ts|tsx)/
// we can add more modules here
const TEST_MODULES_REGEX= /jest/
module.exports = {
'no-test-imports': {
meta: {
type: 'problem',
docs: {
description: 'Disallow importing test files in source code',
},
fixable: 'code',
schema: [] // no options
},
create: function(context) {
return {
ImportDeclaration: function(node) {
const physicalFilename = context.getPhysicalFilename().toLowerCase();
// stop if the filename is not in the src/ directory or is a test file
if (!physicalFilename.includes('src/') ||
physicalFilename.match(TEST_FILE_REGEX)) {
return;
}
// if we get here, we're in a source file, not a test file
const importSource = node.source.value.trim();
// do not allow importing test files or modules that include 'jest', such as 'jest-fetch-mock'
if (importSource.match(TEST_FILE_REGEX) ||
importSource.match(TEST_MODULES_REGEX)) {
context.report({
node: node,
message: 'Do not import test modules in source code',
fix: function(fixer) {
return fixer.remove(node);
}
});
}
}
};
}
}
};