🎥 Make videos programmatically with React
linting rule
Jonny Burger87fa8c22e52c
diff --git a/packages/eslint-config/src/index.ts b/packages/eslint-config/src/index.tsindex 9c98c77d59..9e8929c0be 100644--- a/packages/eslint-config/src/index.ts+++ b/packages/eslint-config/src/index.ts@@ -40,6 +40,7 @@ const getRules = (typescript: boolean) => {"@remotion/no-mp4-import": "warn","@remotion/warn-native-media-tag": "warn","@remotion/deterministic-randomness": "warn",+ "@remotion/no-string-assets": "warn",};};diff --git a/packages/eslint-plugin/src/index.ts b/packages/eslint-plugin/src/index.tsindex 7354e7ac5d..09d1056f59 100644--- a/packages/eslint-plugin/src/index.ts+++ b/packages/eslint-plugin/src/index.ts@@ -1,11 +1,13 @@import deterministicRandomness from "./rules/deterministic-randomness";import nomp4Import from "./rules/no-mp4-import";+import noStringAssets from "./rules/no-string-assets";import warnNativeMediaTag from "./rules/warn-native-media-tag";const rules = {"no-mp4-import": nomp4Import,"warn-native-media-tag": warnNativeMediaTag,"deterministic-randomness": deterministicRandomness,+ "no-string-assets": noStringAssets,};export = {diff --git a/packages/eslint-plugin/src/rules/no-string-assets.ts b/packages/eslint-plugin/src/rules/no-string-assets.tsnew file mode 100644index 0000000000..1ab5b6fbed--- /dev/null+++ b/packages/eslint-plugin/src/rules/no-string-assets.ts@@ -0,0 +1,79 @@+import { ESLintUtils } from "@typescript-eslint/experimental-utils";++const createRule = ESLintUtils.RuleCreator((name) => {+ return `https://github.com/JonnyBurger/remotion`;+});++type Options = [];++type MessageIds = "NoStringAssets";++const NoStringAssets = [+ "Don't reference local assets by string, use an import statement instead.",+ "See: https://www.remotion.dev/docs/assets",+].join("\n");++export default createRule<Options, MessageIds>({+ name: "no-string-assets",+ meta: {+ type: "problem",+ docs: {+ description: NoStringAssets,+ category: "Best Practices",+ recommended: "warn",+ },+ fixable: undefined,+ schema: [],+ messages: {+ NoStringAssets: NoStringAssets,+ },+ },+ defaultOptions: [],+ create: (context) => {+ return {+ JSXAttribute: (node) => {+ if (node.type !== "JSXAttribute") {+ return;+ }+ if (node.name.name !== "src") {+ return;+ }+ const value = node.value;+ if (!value || value.type !== "Literal") {+ return;+ }+ const stringValue = value.value;+ 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.startsWith("http://")) {+ return;+ }+ if (stringValue.startsWith("https://")) {+ return;+ }+ if (stringValue.startsWith("data:")) {+ return;+ }+ context.report({+ messageId: "NoStringAssets",+ node,+ });+ }+ },+ };+ },+});diff --git a/packages/eslint-plugin/src/tests/no-string-assets.test.ts b/packages/eslint-plugin/src/tests/no-string-assets.test.tsnew file mode 100644index 0000000000..00220aab65--- /dev/null+++ b/packages/eslint-plugin/src/tests/no-string-assets.test.ts@@ -0,0 +1,69 @@+import { ESLintUtils } from "@typescript-eslint/experimental-utils";+import rule from "../rules/no-string-assets";++const ruleTester = new ESLintUtils.RuleTester({+ parser: "@typescript-eslint/parser",+ parserOptions: {+ ecmaFeatures: {+ jsx: true,+ },+ },+});++ruleTester.run("no-string-assets", rule, {+ valid: [+ // Network image should be allowed+ `+import {Img} from 'remotion';++export const Re = () => {+ return (+ <Img src="https://google.com/favicon.ico" />+ );+}+ `,+ `+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="hithere" />+ );+}+ `,+ errors: [+ {+ messageId: "NoStringAssets",+ },+ ],+ },+ ],+});