Skip to content

🎥 Make videos programmatically with React

add eslint rule

Jonny Burger1abd4196a0fb
diff --git a/packages/eslint-config/src/index.ts b/packages/eslint-config/src/index.ts
index c49e048a76..89dcff2e13 100644
--- a/packages/eslint-config/src/index.ts
+++ b/packages/eslint-config/src/index.ts
@@ -39,6 +39,7 @@ const getRules = (typescript: boolean) => {
// Enable Remotion specific rules
"@remotion/no-mp4-import": "warn",
"@remotion/warn-native-media-tag": "warn",
+ "@remotion/determinstic-randomness": "warn",
};
};
diff --git a/packages/eslint-plugin/src/index.ts b/packages/eslint-plugin/src/index.ts
index 990908c287..57c3a2f631 100644
--- a/packages/eslint-plugin/src/index.ts
+++ b/packages/eslint-plugin/src/index.ts
@@ -1,9 +1,11 @@
+import deterministicRandomness from "./rules/deterministic-randomness";
import nomp4Import from "./rules/no-mp4-import";
import warnNativeMediaTag from "./rules/warn-native-media-tag";
const rules = {
"no-mp4-import": nomp4Import,
"warn-native-media-tag": warnNativeMediaTag,
+ "determistic-randomess": deterministicRandomness,
};
export = {
diff --git a/packages/eslint-plugin/src/rules/deterministic-randomness.ts b/packages/eslint-plugin/src/rules/deterministic-randomness.ts
new file mode 100644
index 0000000000..38d31f278b
--- /dev/null
+++ b/packages/eslint-plugin/src/rules/deterministic-randomness.ts
@@ -0,0 +1,56 @@
+import { ESLintUtils } from "@typescript-eslint/experimental-utils";
+
+const createRule = ESLintUtils.RuleCreator((name) => {
+ return `https://github.com/JonnyBurger/remotion`;
+});
+
+type Options = [];
+
+type MessageIds = "DeterministicRandomness";
+
+const DeterministicRandomness = [
+ "The result of Math.random() will change between frames while rendering because multiple threads are used.",
+ "Use the `random()` API from Remotion to get a deterministic pseudorandom value.",
+ "If you are sure you want a true random value, use `random(null)` to hide this warning.",
+].join("\n");
+
+export default createRule<Options, MessageIds>({
+ name: "determinstic-randomness",
+ meta: {
+ type: "problem",
+ docs: {
+ description: DeterministicRandomness,
+ category: "Best Practices",
+ recommended: "warn",
+ },
+ fixable: undefined,
+ schema: [],
+ messages: {
+ DeterministicRandomness: DeterministicRandomness,
+ },
+ },
+ defaultOptions: [],
+ create: (context) => {
+ return {
+ CallExpression: (node) => {
+ const callee = node.callee;
+ if (callee.type === "MemberExpression") {
+ if (
+ callee.object.type === "Identifier" &&
+ callee.object.name === "Math"
+ ) {
+ if (
+ callee.property.type === "Identifier" &&
+ callee.property.name === "random"
+ ) {
+ context.report({
+ messageId: "DeterministicRandomness",
+ node,
+ });
+ }
+ }
+ }
+ },
+ };
+ },
+});
diff --git a/packages/eslint-plugin/src/tests/no-math-random.test.ts b/packages/eslint-plugin/src/tests/no-math-random.test.ts
new file mode 100644
index 0000000000..0c6fdd96c9
--- /dev/null
+++ b/packages/eslint-plugin/src/tests/no-math-random.test.ts
@@ -0,0 +1,19 @@
+import { ESLintUtils } from "@typescript-eslint/experimental-utils";
+import rule from "../rules/deterministic-randomness";
+const ruleTester = new ESLintUtils.RuleTester({
+ parser: "@typescript-eslint/parser",
+});
+
+ruleTester.run("determinstic-randomness", rule, {
+ valid: ['import {random} from "remotion";\nconst hi = random(null)'],
+ invalid: [
+ {
+ code: "Math.random()",
+ errors: [
+ {
+ messageId: "DeterministicRandomness",
+ },
+ ],
+ },
+ ],
+});