forked from phewphewb/eslint-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from Fullscript/feat/no-jest-in-production
feat: create a rule to prevent jest from being used in production code
- Loading branch information
Showing
7 changed files
with
142 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
Object.defineProperty(exports, "noJestInProduction", { | ||
enumerable: true, | ||
get: ()=>_noJestInProduction | ||
}); | ||
const _noJestInProduction = /*#__PURE__*/ _interopRequireWildcard(require("./noJestInProduction")); | ||
function _getRequireWildcardCache(nodeInterop) { | ||
if (typeof WeakMap !== "function") return null; | ||
var cacheBabelInterop = new WeakMap(); | ||
var cacheNodeInterop = new WeakMap(); | ||
return (_getRequireWildcardCache = function(nodeInterop) { | ||
return nodeInterop ? cacheNodeInterop : cacheBabelInterop; | ||
})(nodeInterop); | ||
} | ||
function _interopRequireWildcard(obj, nodeInterop) { | ||
if (!nodeInterop && obj && obj.__esModule) { | ||
return obj; | ||
} | ||
if (obj === null || typeof obj !== "object" && typeof obj !== "function") { | ||
return { | ||
default: obj | ||
}; | ||
} | ||
var cache = _getRequireWildcardCache(nodeInterop); | ||
if (cache && cache.has(obj)) { | ||
return cache.get(obj); | ||
} | ||
var newObj = {}; | ||
var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; | ||
for(var key in obj){ | ||
if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { | ||
var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; | ||
if (desc && (desc.get || desc.set)) { | ||
Object.defineProperty(newObj, key, desc); | ||
} else { | ||
newObj[key] = obj[key]; | ||
} | ||
} | ||
} | ||
newObj.default = obj; | ||
if (cache) { | ||
cache.set(obj, newObj); | ||
} | ||
return newObj; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
function _export(target, all) { | ||
for(var name in all)Object.defineProperty(target, name, { | ||
enumerable: true, | ||
get: all[name] | ||
}); | ||
} | ||
_export(exports, { | ||
meta: ()=>meta, | ||
create: ()=>create | ||
}); | ||
const meta = { | ||
type: "problem", | ||
docs: { | ||
description: "Prevents jest from being used in production code", | ||
category: "no-jest-in-production", | ||
recommended: false | ||
}, | ||
fixable: null, | ||
schema: [] | ||
}; | ||
const create = (context)=>{ | ||
// split the filepath into an array | ||
const filePath = context.getFilename().split("/"); | ||
// the last entry will always be the file name | ||
const fileName = filePath.at(-1); | ||
// Spec files are allowed to have jest, so we skip them | ||
const isNotSpec = ()=>{ | ||
return fileName && !fileName.includes(".spec."); | ||
}; | ||
const isNotValid = (node)=>{ | ||
return isNotSpec() && node.name === "jest"; | ||
}; | ||
return { | ||
Identifier (node) { | ||
if (isNotValid(node)) { | ||
context.report({ | ||
node, | ||
message: "Jest should not be used in production code" | ||
}); | ||
} | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,8 @@ | |
"Hossam Mourad", | ||
"Ryan O'Connor <[email protected]>", | ||
"Aidan Feuerherm <[email protected]>", | ||
"Valentyn Patsera <[email protected]>" | ||
"Valentyn Patsera <[email protected]>", | ||
"Omar Nasr <[email protected]>" | ||
], | ||
"license": "MIT", | ||
"private": false, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * as noJestInProduction from "./noJestInProduction"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
const meta = { | ||
type: "problem", | ||
docs: { | ||
description: "Prevents jest from being used in production code", | ||
category: "no-jest-in-production", | ||
recommended: false, | ||
}, | ||
fixable: null, | ||
schema: [], | ||
}; | ||
|
||
const create = context => { | ||
// split the filepath into an array | ||
const filePath = context.getFilename().split("/"); | ||
// the last entry will always be the file name | ||
const fileName = filePath.at(-1); | ||
|
||
// Spec files are allowed to have jest, so we skip them | ||
const isNotSpec = () => { | ||
return fileName && !fileName.includes(".spec."); | ||
}; | ||
|
||
const isNotValid = node => { | ||
return isNotSpec() && node.name === "jest"; | ||
}; | ||
|
||
return { | ||
Identifier(node) { | ||
if (isNotValid(node)) { | ||
context.report({ | ||
node, | ||
message: "Jest should not be used in production code", | ||
}); | ||
} | ||
}, | ||
}; | ||
}; | ||
|
||
export { meta, create }; |