-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an ESLint rule to validate imports. (#1444)
This addresses part of #1431 by adding a new ESLint rule to ensure that our production code never imports code from our test suite.
- Loading branch information
Showing
5 changed files
with
82 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,5 +10,8 @@ | |
], | ||
"parserOptions": { | ||
"sourceType": "module" | ||
}, | ||
"rules": { | ||
"justfix-validate-import": "error" | ||
} | ||
} |
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,35 @@ | ||
// @ts-check | ||
|
||
const TESTS_DIR_RE = /[\/\\]tests[\/\\]/; | ||
|
||
/** | ||
* Assert that the given condition is true; propagates to TypeScript. | ||
* | ||
* @param {boolean} condition | ||
* @returns {asserts condition} | ||
*/ | ||
function assert(condition) { | ||
if (!condition) { | ||
throw new Error("Assertion failed!"); | ||
} | ||
} | ||
|
||
/** @type import("eslint").Rule.RuleModule */ | ||
module.exports = { | ||
create: function (context) { | ||
return { | ||
ImportDeclaration(node) { | ||
assert(node.type === "ImportDeclaration"); | ||
assert(typeof node.source.value === "string"); | ||
const importStr = node.source.value; | ||
const filename = context.getFilename(); | ||
if (TESTS_DIR_RE.test(importStr) && !TESTS_DIR_RE.test(filename)) { | ||
context.report({ | ||
node, | ||
message: `Production code is importing test suite code at "${importStr}"!`, | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
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,28 @@ | ||
import { RuleTester } from "eslint"; | ||
|
||
import rule from "../rules/justfix-validate-import"; | ||
|
||
const ruleTester = new RuleTester({ | ||
parserOptions: { ecmaVersion: 2015, sourceType: "module" }, | ||
}); | ||
|
||
ruleTester.run("justfix-validate-import", rule, { | ||
valid: [ | ||
{ | ||
code: "import boop from '../boop'", | ||
filename: "lib/tests/boop.test.ts", | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
code: "import boop from '../tests/boop'", | ||
filename: "lib/main.ts", | ||
errors: [ | ||
{ | ||
message: | ||
'Production code is importing test suite code at "../tests/boop"!', | ||
}, | ||
], | ||
}, | ||
], | ||
}); |
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 |
---|---|---|
|
@@ -1596,6 +1596,19 @@ | |
resolved "https://registry.yarnpkg.com/@types/eslint-visitor-keys/-/eslint-visitor-keys-1.0.0.tgz#1ee30d79544ca84d68d4b3cdb0af4f205663dd2d" | ||
integrity sha512-OCutwjDZ4aFS6PB1UZ988C4YgwlBHJd6wCeQqaLdmadZ/7e+w79+hbMUFC1QXDNCmdyoRfAFdm0RypzwR+Qpag== | ||
|
||
"@types/[email protected]": | ||
version "6.8.0" | ||
resolved "https://registry.yarnpkg.com/@types/eslint/-/eslint-6.8.0.tgz#5f2289b9f01316da7cf31c9e63109a10602a23cb" | ||
integrity sha512-hqzmggoxkOubpgTdcOltkfc5N8IftRJqU70d1jbOISjjZVPvjcr+CLi2CI70hx1SUIRkLgpglTy9w28nGe2Hsw== | ||
dependencies: | ||
"@types/estree" "*" | ||
"@types/json-schema" "*" | ||
|
||
"@types/estree@*": | ||
version "0.0.44" | ||
resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.44.tgz#980cc5a29a3ef3bea6ff1f7d021047d7ea575e21" | ||
integrity sha512-iaIVzr+w2ZJ5HkidlZ3EJM8VTZb2MJLCjw3V+505yVts0gRC4UMvjw0d1HPtGqI/HQC/KdsYtayfzl+AXY2R8g== | ||
|
||
"@types/events@*": | ||
version "3.0.0" | ||
resolved "https://registry.yarnpkg.com/@types/events/-/events-3.0.0.tgz#2862f3f58a9a7f7c3e78d79f130dd4d71c25c2a7" | ||
|
@@ -1658,7 +1671,7 @@ | |
jest-diff "^25.1.0" | ||
pretty-format "^25.1.0" | ||
|
||
"@types/json-schema@^7.0.3": | ||
"@types/json-schema@*", "@types/json-schema@^7.0.3": | ||
version "7.0.4" | ||
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.4.tgz#38fd73ddfd9b55abb1e1b2ed578cb55bd7b7d339" | ||
integrity sha512-8+KAKzEvSUdeo+kmqnKrqgeE+LcA0tjYWFY7RPProVYwnqDjukzO+3b6dLD56rYX5TdWejnEOLJYOIeh4CXKuA== | ||
|