]> git.ipfire.org Git - thirdparty/blocklistproject/lists.git/commitdiff
Adding more lint tasks, including searching for commented out URLs that now exist...
authorCharlie Fish <fishcharlie@me.com>
Mon, 12 Jul 2021 01:21:24 +0000 (19:21 -0600)
committerCharlie Fish <fishcharlie@me.com>
Mon, 12 Jul 2021 01:21:24 +0000 (19:21 -0600)
scripts/lint.js

index fd61e22901ea3e68acf757b5bfe4de5f6ff0c599..b72d2191e5f65a50255e6ec889048f118e1c6319 100644 (file)
@@ -8,7 +8,20 @@ const path = require("path");
        await Promise.all(files.filter((file) => file !== "everything.txt").map(async (file) => { // For each file
                const fileContents = await fs.readFile(path.join(__dirname, "..", file), "utf8"); // Get file contents as a string
 
+               const commentedURLs = fileContents.split("\n").map((line) => {
+                       if (line.startsWith("# 0.0.0.0")) {
+                               return line.split(" ")[2].trim();
+                       }
+
+                       return null;
+               }).filter((a) => a !== null && !!a);
+
+               let isHeaderComplete = false;
                fileContents.split("\n").forEach((line, index) => {
+                       if (line.startsWith("0.0.0.0")) {
+                               isHeaderComplete = true;
+                       }
+
                        // Ensuring that all lines start with "#" or "0.0.0.0 "
                        if (line.length > 0 && !line.startsWith("#") && !line.startsWith("0.0.0.0 ")) {
                                console.error(`Line ${index + 1} in ${file} must start with "#" or "0.0.0.0 ".`);
@@ -30,6 +43,22 @@ const path = require("path");
                                console.error(`Line ${index + 1} in ${file} should have a space after #.`);
                                hasError = true;
                        }
+
+                       // Ensure that after header is complete that all lines that start with `#` start with `# 0.0.0.0`
+                       if (isHeaderComplete && line.startsWith("#") && !line.startsWith("# 0.0.0.0")) {
+                               console.error(`Line ${index + 1} in ${file} should start with "# 0.0.0.0".`);
+                               hasError = true;
+                       }
+
+                       // Ensure that the URL doesn't exist in the commentedURLs array
+                       if (line.startsWith("0.0.0.0 ")) {
+                               const lineNoIP = line.replace("0.0.0.0 ", "");
+                               const url = lineNoIP.split("#")[0].trim();
+                               if (commentedURLs.includes(url)) {
+                                       console.error(`Line ${index + 1} in ${file} url ${url} is commented out in this file. This suggests an error. Please either remove this line or remove the commented URL.`);
+                                       hasError = true;
+                               }
+                       }
                });
        }));