Skip to content

Commit 0e2486b

Browse files
authored
Merge pull request #2 from Fullscript/feat/no-jest-in-production
feat: create a rule to prevent jest from being used in production code
2 parents bba2a73 + e52a17d commit 0e2486b

7 files changed

Lines changed: 142 additions & 2 deletions

File tree

dist/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ const _gqlRules = require("./gqlRules");
1212
const _noRenamedTranslationImport = require("./noRenamedTranslationImport");
1313
const _noUnawaitedSkeletons = require("./noUnawaitedSkeletons");
1414
const _oneTranslationImport = require("./oneTranslationImport");
15+
const _noJestInProduction = require("./noJestInProduction");
1516
const rules = {
1617
"one-translation-import-per-file": _oneTranslationImport.oneTranslationImport,
1718
"no-renamed-translation-import": _noRenamedTranslationImport.noRenamedTranslationImport,
1819
"gql-objects": _gqlRules.gqlObjects,
1920
"gql-operation-name": _gqlRules.gqlOperationName,
2021
"cross-reference": _crossReference.crossReference,
2122
"circular-dependency": _circularDependency.circularDependency,
22-
"no-unawaited-skeletons": _noUnawaitedSkeletons.noUnawaitedSkeletons
23+
"no-unawaited-skeletons": _noUnawaitedSkeletons.noUnawaitedSkeletons,
24+
"no-jest-in-production": _noJestInProduction.noJestInProduction
2325
};

dist/noJestInProduction/index.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", {
3+
value: true
4+
});
5+
Object.defineProperty(exports, "noJestInProduction", {
6+
enumerable: true,
7+
get: ()=>_noJestInProduction
8+
});
9+
const _noJestInProduction = /*#__PURE__*/ _interopRequireWildcard(require("./noJestInProduction"));
10+
function _getRequireWildcardCache(nodeInterop) {
11+
if (typeof WeakMap !== "function") return null;
12+
var cacheBabelInterop = new WeakMap();
13+
var cacheNodeInterop = new WeakMap();
14+
return (_getRequireWildcardCache = function(nodeInterop) {
15+
return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
16+
})(nodeInterop);
17+
}
18+
function _interopRequireWildcard(obj, nodeInterop) {
19+
if (!nodeInterop && obj && obj.__esModule) {
20+
return obj;
21+
}
22+
if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
23+
return {
24+
default: obj
25+
};
26+
}
27+
var cache = _getRequireWildcardCache(nodeInterop);
28+
if (cache && cache.has(obj)) {
29+
return cache.get(obj);
30+
}
31+
var newObj = {};
32+
var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
33+
for(var key in obj){
34+
if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
35+
var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
36+
if (desc && (desc.get || desc.set)) {
37+
Object.defineProperty(newObj, key, desc);
38+
} else {
39+
newObj[key] = obj[key];
40+
}
41+
}
42+
}
43+
newObj.default = obj;
44+
if (cache) {
45+
cache.set(obj, newObj);
46+
}
47+
return newObj;
48+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", {
3+
value: true
4+
});
5+
function _export(target, all) {
6+
for(var name in all)Object.defineProperty(target, name, {
7+
enumerable: true,
8+
get: all[name]
9+
});
10+
}
11+
_export(exports, {
12+
meta: ()=>meta,
13+
create: ()=>create
14+
});
15+
const meta = {
16+
type: "problem",
17+
docs: {
18+
description: "Prevents jest from being used in production code",
19+
category: "no-jest-in-production",
20+
recommended: false
21+
},
22+
fixable: null,
23+
schema: []
24+
};
25+
const create = (context)=>{
26+
// split the filepath into an array
27+
const filePath = context.getFilename().split("/");
28+
// the last entry will always be the file name
29+
const fileName = filePath.at(-1);
30+
// Spec files are allowed to have jest, so we skip them
31+
const isNotSpec = ()=>{
32+
return fileName && !fileName.includes(".spec.");
33+
};
34+
const isNotValid = (node)=>{
35+
return isNotSpec() && node.name === "jest";
36+
};
37+
return {
38+
Identifier (node) {
39+
if (isNotValid(node)) {
40+
context.report({
41+
node,
42+
message: "Jest should not be used in production code"
43+
});
44+
}
45+
}
46+
};
47+
};

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"Hossam Mourad",
88
"Ryan O'Connor <charle692@gmail.com>",
99
"Aidan Feuerherm <aidan.feuerherm@fullscript.com>",
10-
"Valentyn Patsera <valentyn.patsera@fullscript.com>"
10+
"Valentyn Patsera <valentyn.patsera@fullscript.com>",
11+
"Omar Nasr <omar.nasr@fullscript.com>"
1112
],
1213
"license": "MIT",
1314
"private": false,

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { gqlObjects, gqlOperationName } from "./gqlRules";
44
import { noRenamedTranslationImport } from "./noRenamedTranslationImport";
55
import { noUnawaitedSkeletons } from "./noUnawaitedSkeletons";
66
import { oneTranslationImport } from "./oneTranslationImport";
7+
import { noJestInProduction } from "./noJestInProduction";
78

89
const rules = {
910
"one-translation-import-per-file": oneTranslationImport,
@@ -13,6 +14,7 @@ const rules = {
1314
"cross-reference": crossReference,
1415
"circular-dependency": circularDependency,
1516
"no-unawaited-skeletons": noUnawaitedSkeletons,
17+
"no-jest-in-production": noJestInProduction,
1618
};
1719

1820
export { rules };

src/noJestInProduction/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * as noJestInProduction from "./noJestInProduction";
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const meta = {
2+
type: "problem",
3+
docs: {
4+
description: "Prevents jest from being used in production code",
5+
category: "no-jest-in-production",
6+
recommended: false,
7+
},
8+
fixable: null,
9+
schema: [],
10+
};
11+
12+
const create = context => {
13+
// split the filepath into an array
14+
const filePath = context.getFilename().split("/");
15+
// the last entry will always be the file name
16+
const fileName = filePath.at(-1);
17+
18+
// Spec files are allowed to have jest, so we skip them
19+
const isNotSpec = () => {
20+
return fileName && !fileName.includes(".spec.");
21+
};
22+
23+
const isNotValid = node => {
24+
return isNotSpec() && node.name === "jest";
25+
};
26+
27+
return {
28+
Identifier(node) {
29+
if (isNotValid(node)) {
30+
context.report({
31+
node,
32+
message: "Jest should not be used in production code",
33+
});
34+
}
35+
},
36+
};
37+
};
38+
39+
export { meta, create };

0 commit comments

Comments
 (0)