Skip to content

🎥 Make videos programmatically with React

no-remote URL rule

Jonny Burgerc730a1a74fbf
diff --git a/packages/eslint-plugin/src/index.ts b/packages/eslint-plugin/src/index.ts
index 7626156302..5d60c3c364 100644
--- a/packages/eslint-plugin/src/index.ts
+++ b/packages/eslint-plugin/src/index.ts
@@ -4,6 +4,7 @@ import durationInFrames from "./rules/no-duration-frames-infinity";
import nomp4Import from "./rules/no-mp4-import";
import noStringAssets from "./rules/no-string-assets";
import staticFileNoRelative from "./rules/staticfile-no-relative";
+import staticFileNoRemote from "./rules/staticfile-no-remote";
import useGifComponent from "./rules/use-gif-component";
import volumeCallback from "./rules/volume-callback";
import warnNativeMediaTag from "./rules/warn-native-media-tag";
@@ -18,6 +19,7 @@ const rules = {
"volume-callback": volumeCallback,
"use-gif-component": useGifComponent,
"staticfile-no-relative": staticFileNoRelative,
+ "staticfile-no-remote": staticFileNoRemote,
};
export = {
@@ -34,6 +36,7 @@ export = {
"@remotion/volume-callback": "error",
"@remotion/use-gif-component": "error",
"@remotion/staticfile-no-relative": "error",
+ "@remotion/staticfile-no-remote": "error",
},
},
},
diff --git a/packages/eslint-plugin/src/rules/staticfile-no-remote.ts b/packages/eslint-plugin/src/rules/staticfile-no-remote.ts
new file mode 100644
index 0000000000..743202832b
--- /dev/null
+++ b/packages/eslint-plugin/src/rules/staticfile-no-remote.ts
@@ -0,0 +1,74 @@
+import { ESLintUtils } from "@typescript-eslint/utils";
+
+const createRule = ESLintUtils.RuleCreator(() => {
+ return `https://remotion.dev/docs/staticfile-remote-urls`;
+});
+
+type Options = [];
+
+type MessageIds = "RelativePathStaticFile";
+
+const RelativePathStaticFile = [
+ "Don't pass a remote URL to staticFile().",
+ "See: https://remotion.dev/docs/staticfile-remote-urls",
+].join("\n");
+
+export default createRule<Options, MessageIds>({
+ name: "staticfile-no-remote",
+ meta: {
+ type: "problem",
+ docs: {
+ description: RelativePathStaticFile,
+ recommended: "warn",
+ },
+ fixable: undefined,
+ schema: [],
+ messages: {
+ RelativePathStaticFile: RelativePathStaticFile,
+ },
+ },
+ defaultOptions: [],
+ create: (context) => {
+ return {
+ CallExpression: (node) => {
+ const value = node;
+ // src={"some string"}
+ if (!value) {
+ return;
+ }
+
+ if (
+ node.type === "CallExpression" &&
+ node.callee.type === "Identifier" &&
+ node.callee.name === "staticFile"
+ ) {
+ const args = node.arguments;
+ if (args.length === 0) {
+ return;
+ }
+
+ const firstArg = args[0];
+
+ if (firstArg.type === "Literal") {
+ const value = firstArg.value;
+ if (typeof value !== "string") {
+ return;
+ }
+ if (value.startsWith("http://")) {
+ context.report({
+ messageId: "RelativePathStaticFile",
+ node,
+ });
+ }
+ if (value.startsWith("https://")) {
+ context.report({
+ messageId: "RelativePathStaticFile",
+ node,
+ });
+ }
+ }
+ }
+ },
+ };
+ },
+});
diff --git a/packages/eslint-plugin/src/tests/staticfile-no-remote.test.ts b/packages/eslint-plugin/src/tests/staticfile-no-remote.test.ts
new file mode 100644
index 0000000000..d25a3a78f0
--- /dev/null
+++ b/packages/eslint-plugin/src/tests/staticfile-no-remote.test.ts
@@ -0,0 +1,51 @@
+import { ESLintUtils } from "@typescript-eslint/utils";
+import rule from "../rules/staticfile-no-remote";
+
+const ruleTester = new ESLintUtils.RuleTester({
+ parser: "@typescript-eslint/parser",
+ parserOptions: {
+ ecmaFeatures: {
+ jsx: true,
+ },
+ },
+});
+
+ruleTester.run("staticfile-no-remote", rule, {
+ valid: [
+ `
+import {Img, staticFile} from 'remotion';
+
+export const Re = () => {
+ return (
+ <Img src={staticFile("image.png")} />
+ );
+}
+ `,
+ ],
+ invalid: [
+ {
+ code: `
+import {staticFile} from 'remotion';
+
+staticFile("http://relative.png")
+ `,
+ errors: [
+ {
+ messageId: "RelativePathStaticFile",
+ },
+ ],
+ },
+ {
+ code: `
+import {staticFile} from 'remotion';
+
+staticFile("https://relative.png")
+ `,
+ errors: [
+ {
+ messageId: "RelativePathStaticFile",
+ },
+ ],
+ },
+ ],
+});