Skip to content

Commit

Permalink
Add an ESLint rule to validate imports. (#1444)
Browse files Browse the repository at this point in the history
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
toolness authored May 16, 2020
1 parent 1f7cf42 commit 8762d44
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@
],
"parserOptions": {
"sourceType": "module"
},
"rules": {
"justfix-validate-import": "error"
}
}
35 changes: 35 additions & 0 deletions frontend/eslint/rules/justfix-validate-import.js
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}"!`,
});
}
},
};
},
};
28 changes: 28 additions & 0 deletions frontend/eslint/tests/justfix-validate-import.test.ts
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"!',
},
],
},
],
});
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"lint:fix": "yarn typecheck && yarn eslint && yarn prettier:fix",
"typecheck": "tsc --noEmit",
"typecheck:watch": "tsc --noEmit --watch --preserveWatchOutput",
"eslint": "eslint frontend/lib --ext .ts,.tsx",
"eslint": "eslint frontend/lib --ext .ts,.tsx --rulesdir frontend/eslint/rules",
"test": "jest",
"test:watch": "jest --watch",
"sass_norent": "node-sass-chokidar frontend/sass/norent/styles.scss frontend/static/frontend/styles-norent.css --source-map frontend/static/frontend/styles-norent.css.map --source-map-contents --output-style compressed",
Expand Down Expand Up @@ -46,6 +46,7 @@
"devDependencies": {
"@testing-library/dom": "7.5.2",
"@testing-library/react": "10.0.4",
"@types/eslint": "6.8.0",
"@types/jest": "25.1.4",
"@types/webpack": "4.41.0",
"@types/webpack-bundle-analyzer": "2.13.3",
Expand Down
15 changes: 14 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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==
Expand Down

0 comments on commit 8762d44

Please sign in to comment.