Improve eslint derived fixtures

This commit is contained in:
Joe Savona
2023-05-08 14:10:07 -07:00
parent 8920fc0b2f
commit 597e70d69f
110 changed files with 87 additions and 17 deletions
@@ -16,6 +16,8 @@ const path = require("path");
const prettier = require("prettier");
const prettierConfigPath = require.resolve("../.prettierrc");
const process = require("process");
const { createHash } = require("crypto");
const { create } = require("domain");
const FIXTURES_DIR = path.join(
process.cwd(),
@@ -26,6 +28,10 @@ const FIXTURES_DIR = path.join(
"rules-of-hooks"
);
const PRETTIER_OPTIONS = prettier.resolveConfig.sync(FIXTURES_DIR, {
config: prettierConfigPath,
});
const fixtures = [];
for (const test of tests.valid) {
fixtures.push({ code: test.code, valid: true });
@@ -34,7 +40,6 @@ for (const test of tests.invalid) {
fixtures.push({ code: test.code, valid: false });
}
let index = 0;
for (const fixture of fixtures) {
let error = null;
let passes = true;
@@ -63,29 +68,29 @@ for (const fixture of fixtures) {
} catch (e) {
error = e;
}
let name = `rules-of-hooks-${index}.js`;
let code = fixture.code;
let prefix = "";
if (error !== null) {
name = `todo.${name}`;
code = `// @skip\n${code}`;
prefix = `todo.`;
code = `// @skip\n// Unsupported input\n${code}`;
} else if (fixture.valid === false) {
name = `error.${name}`;
prefix = `error.`;
if (passes) {
// oops, passed when we expected an error
name = `todo.${name}`;
code = `// @skip\n${code}`;
prefix = `todo.error.`;
code = `// @skip\n// Passed but should have errored\n${code}`;
}
} else if (!passes) {
// oops, error when it should have passed
name = `todo.${name}`;
code = `// @skip\n${code}`;
prefix = `todo.`;
code = `// @skip\n// Failed but should have passed\n${code}`;
}
const formatted = prettier.format(code, PRETTIER_OPTIONS);
const hmac = createHash("sha256");
hmac.update(formatted, "utf8");
let name = `${prefix}rules-of-hooks-${hmac
.digest("hex")
.substring(0, 12)}.js`;
const fixturePath = path.join(FIXTURES_DIR, name);
const options = prettier.resolveConfig.sync(fixturePath, {
config: prettierConfigPath,
});
const formatted = prettier.format(code, options);
fs.writeFileSync(fixturePath, formatted, "utf8");
index++;
}
@@ -0,0 +1,28 @@
## Input
```javascript
// Valid because exceptions abort rendering
function RegressionTest() {
if (page == null) {
throw new Error("oh no!");
}
useState();
}
```
## Code
```javascript
// Valid because exceptions abort rendering
function RegressionTest() {
if (page == null) {
throw new Error("oh no!");
}
useState();
}
```
@@ -1,5 +1,3 @@
// @skip
// Valid because exceptions abort rendering
function RegressionTest() {
if (page == null) {
@@ -1,4 +1,5 @@
// @skip
// Passed but should have errored
// This is invalid because "use"-prefixed functions used in named
// functions are assumed to be hooks.
@@ -1,4 +1,5 @@
// @skip
// Passed but should have errored
// Invalid because it's dangerous and might not warn otherwise.
// This *must* be invalid.
@@ -1,4 +1,5 @@
// @skip
// Passed but should have errored
// Invalid because it's a common misunderstanding.
// We *could* make it valid but the runtime error could be confusing.
@@ -1,4 +1,5 @@
// @skip
// Passed but should have errored
// Invalid because it's dangerous and might not warn otherwise.
// This *must* be invalid.
@@ -1,4 +1,5 @@
// @skip
// Passed but should have errored
// Invalid because it's a common misunderstanding.
// We *could* make it valid but the runtime error could be confusing.
@@ -1,4 +1,5 @@
// @skip
// Passed but should have errored
// Currently invalid because it violates the convention and removes the "taint"
// from a hook. We *could* make it valid to avoid some false positives but let's
@@ -1,4 +1,5 @@
// @skip
// Passed but should have errored
class ClassComponentWithFeatureFlag extends React.Component {
render() {
@@ -1,4 +1,5 @@
// @skip
// Passed but should have errored
// These are neither functions nor hooks.
function _normalFunctionWithHook() {
@@ -1,4 +1,5 @@
// @skip
// Passed but should have errored
// Invalid because it's dangerous and might not warn otherwise.
// This *must* be invalid.
@@ -1,4 +1,5 @@
// @skip
// Passed but should have errored
// Invalid because it's a common misunderstanding.
// We *could* make it valid but the runtime error could be confusing.
@@ -1,4 +1,5 @@
// @skip
// Passed but should have errored
// Invalid because it's dangerous and might not warn otherwise.
// This *must* be invalid.
@@ -1,4 +1,5 @@
// @skip
// Passed but should have errored
// Invalid because it's dangerous.
// Normally, this would crash, but not if you use inline requires.
@@ -1,4 +1,5 @@
// @skip
// Passed but should have errored
// Invalid because it's dangerous and might not warn otherwise.
// This *must* be invalid.
@@ -1,4 +1,5 @@
// @skip
// Passed but should have errored
// Invalid because it's a common misunderstanding.
// We *could* make it valid but the runtime error could be confusing.
@@ -1,4 +1,5 @@
// @skip
// Passed but should have errored
// Technically this is a false positive.
// We *could* make it valid (and it used to be).
@@ -1,4 +1,5 @@
// @skip
// Passed but should have errored
// Invalid because it's dangerous and might not warn otherwise.
// This *must* be invalid.
@@ -1,4 +1,5 @@
// @skip
// Passed but should have errored
class ClassComponentWithHook extends React.Component {
render() {
@@ -1,4 +1,5 @@
// @skip
// Passed but should have errored
// This is a false positive (it's valid) that unfortunately
// we cannot avoid. Prefer to rename it to not start with "use"
@@ -1,4 +1,5 @@
// @skip
// Unsupported input
// Valid -- this is a regression test.
jest.useFakeTimers();
@@ -1,4 +1,5 @@
// @skip
// Unsupported input
// Invalid because it's dangerous and might not warn otherwise.
// This *must* be invalid.
@@ -1,4 +1,5 @@
// @skip
// Unsupported input
// Valid because hooks can be used in anonymous function arguments to
// React.memo.
@@ -1,4 +1,5 @@
// @skip
// Unsupported input
// Invalid because it's a common misunderstanding.
// We *could* make it valid but the runtime error could be confusing.
@@ -1,4 +1,5 @@
// @skip
// Unsupported input
// Valid because the neither the conditions before or after the hook affect the hook call
// Failed prior to implementing BigInt because pathsFromStartToEnd and allPathsFromStartToEnd were too big and had rounding errors

Some files were not shown because too many files have changed in this diff Show More