Skip to content

🎥 Make videos programmatically with React

basic GIF rule

Jonny Burgere16529794649
diff --git a/packages/eslint-plugin/src/index.ts b/packages/eslint-plugin/src/index.ts
index cb04c02e9f..31b17f50bd 100644
--- a/packages/eslint-plugin/src/index.ts
+++ b/packages/eslint-plugin/src/index.ts
@@ -1,10 +1,11 @@
import deterministicRandomness from "./rules/deterministic-randomness";
import evenDimensions from "./rules/even-dimensions";
+import durationInFrames from "./rules/no-duration-frames-infinity";
import nomp4Import from "./rules/no-mp4-import";
import noStringAssets from "./rules/no-string-assets";
-import warnNativeMediaTag from "./rules/warn-native-media-tag";
-import durationInFrames from "./rules/no-duration-frames-infinity";
+import useGifComponent from "./rules/use-gif-component";
import volumeCallback from "./rules/volume-callback";
+import warnNativeMediaTag from "./rules/warn-native-media-tag";
const rules = {
"no-mp4-import": nomp4Import,
@@ -14,6 +15,7 @@ const rules = {
"even-dimensions": evenDimensions,
"duration-in-frames": durationInFrames,
"volume-callback": volumeCallback,
+ "use-gif-component": useGifComponent,
};
export = {
@@ -28,6 +30,7 @@ export = {
"@remotion/even-dimensions": "error",
"@remotion/duration-in-frames": "error",
"@remotion/volume-callback": "error",
+ "@remotion/use-gif-component": "error",
},
},
},
diff --git a/packages/eslint-plugin/src/rules/use-gif-component.ts b/packages/eslint-plugin/src/rules/use-gif-component.ts
new file mode 100644
index 0000000000..1b24a9c7c2
--- /dev/null
+++ b/packages/eslint-plugin/src/rules/use-gif-component.ts
@@ -0,0 +1,84 @@
+import { ESLintUtils } from "@typescript-eslint/utils";
+
+const createRule = ESLintUtils.RuleCreator(() => {
+ return `https://github.com/remotion-dev/remotion`;
+});
+
+type Options = [];
+
+type MessageIds = "UseGifComponent";
+
+const UseGifComponent = [
+ "Use the <Gif> component for animated GIFs instead.",
+ "See: https://www.remotion.dev/docs/gif",
+].join("\n");
+
+export default createRule<Options, MessageIds>({
+ name: "use-gif-component",
+ meta: {
+ type: "problem",
+ docs: {
+ description: UseGifComponent,
+ recommended: "warn",
+ },
+ fixable: undefined,
+ schema: [],
+ messages: {
+ UseGifComponent: UseGifComponent,
+ },
+ },
+ defaultOptions: [],
+ create: (context) => {
+ return {
+ JSXAttribute: (node) => {
+ if (node.type !== "JSXAttribute") {
+ return;
+ }
+ if (node.name.name !== "src") {
+ return;
+ }
+ const value = node.value;
+ // src={"some string"}
+ const insideCurlyBraces =
+ value &&
+ value.type === "JSXExpressionContainer" &&
+ value.expression.type === "Literal";
+ if (!value || (value.type !== "Literal" && !insideCurlyBraces)) {
+ return;
+ }
+ const stringValue =
+ value &&
+ value.type === "JSXExpressionContainer" &&
+ value.expression.type === "Literal"
+ ? value.expression.value
+ : value.type === "Literal"
+ ? value.value
+ : null;
+ if (typeof stringValue !== "string") {
+ return;
+ }
+
+ const parent = node.parent;
+ if (!parent) {
+ return;
+ }
+ if (parent.type !== "JSXOpeningElement") {
+ return;
+ }
+ const name = parent.name;
+ if (name.type !== "JSXIdentifier") {
+ return;
+ }
+ if (name.name === "Img" || name.name === "img") {
+ // Network and inline URLs are okay
+ if (stringValue.includes(".gif")) {
+ context.report({
+ messageId: "UseGifComponent",
+ node,
+ });
+ }
+ }
+ },
+ };
+ },
+});
diff --git a/packages/eslint-plugin/src/tests/use-gif-component.test.ts b/packages/eslint-plugin/src/tests/use-gif-component.test.ts
new file mode 100644
index 0000000000..3e947d18e3
--- /dev/null
+++ b/packages/eslint-plugin/src/tests/use-gif-component.test.ts
@@ -0,0 +1,69 @@
+import { ESLintUtils } from "@typescript-eslint/utils";
+import rule from "../rules/use-gif-component";
+
+const ruleTester = new ESLintUtils.RuleTester({
+ parser: "@typescript-eslint/parser",
+ parserOptions: {
+ ecmaFeatures: {
+ jsx: true,
+ },
+ },
+});
+
+ruleTester.run("use-gif-component", rule, {
+ valid: [
+ // Network image should be allowed
+ `
+import {Img} from 'remotion';
+
+export const Re = () => {
+ return (
+ <Img src="https://google.com/favicon.png" />
+ );
+}
+ `,
+ `
+import {Img} from 'remotion';
+
+export const Re = () => {
+ return (
+ <Img src="data:image/png;base64,
+ iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAABGdBTUEAALGP
+ C/xhBQAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9YGARc5KB0XV+IA
+ AAAddEVYdENvbW1lbnQAQ3JlYXRlZCB3aXRoIFRoZSBHSU1Q72QlbgAAAF1J
+ REFUGNO9zL0NglAAxPEfdLTs4BZM4DIO4C7OwQg2JoQ9LE1exdlYvBBeZ7jq
+ ch9//q1uH4TLzw4d6+ErXMMcXuHWxId3KOETnnXXV6MJpcq2MLaI97CER3N0
+ vr4MkhoXe0rZigAAAABJRU5ErkJggg==" />
+ );
+}
+ `,
+ `
+import {Img} from 'remotion';
+import img from './img.png';
+
+export const Re = () => {
+ return (
+ <Img src={img} />
+ );
+}
+ `,
+ ],
+ invalid: [
+ {
+ code: `
+import {Img} from 'remotion';
+
+export const Re = () => {
+ return (
+ <Img src="https://reddit.com/image.gif" />
+ );
+}
+ `,
+ errors: [
+ {
+ messageId: "UseGifComponent",
+ },
+ ],
+ },
+ ],
+});