Skip to content

Effort free video editing!

Add `nimble lint` for repo rules a compiler won't catch

WyattBluea616c76038be
diff --git a/.github/workflows/smoke.yml b/.github/workflows/smoke.yml
index 7b091d8a..a32eace3 100644
--- a/.github/workflows/smoke.yml
+++ b/.github/workflows/smoke.yml
@@ -38,8 +38,8 @@ jobs:
run: brew install nim
- name: Install Deps
run: nimble install --depsOnly
- - name: Check 32-bit
- run: nim check --cpu:i386 --hints:off src/main.nim
+ - name: Lint
+ run: nimble lint
fan:
runs-on: ubuntu-latest
steps:
diff --git a/ae.nimble b/ae.nimble
index 6971c152..84bfc4a5 100644
--- a/ae.nimble
+++ b/ae.nimble
@@ -64,6 +64,10 @@ proc stripProgram(kind: CrossKind = native) =
task test, "Run unit tests":
exec "nim c -r tests/unit"
+task lint, "Check repo rules and the 32-bit build":
+ exec "nim r --hints:off --skipParentCfg:on --skipProjCfg:on scripts/lint.nim"
+ exec "nim check --cpu:i386 --hints:off src/main.nim"
+
task make, "Export the project":
exec "nim c -d:danger -d:flto --out:auto-editor src/main.nim"
stripProgram()
diff --git a/scripts/lint.nim b/scripts/lint.nim
new file mode 100644
index 00000000..3b890c5e
--- /dev/null
+++ b/scripts/lint.nim
@@ -0,0 +1,124 @@
+# Repo rules that a compiler won't catch. Run with `nimble lint`.
+
+import std/[os, strutils]
+
+var failures = 0
+
+proc fail(path: string, line: int, msg: string) =
+ inc failures
+ stderr.writeLine path & ":" & $line & ": " & msg
+
+proc indentOf(s: string): int =
+ while result < s.len and s[result] == ' ':
+ inc result
+
+proc code(s: string): string =
+ let i = s.find('#')
+ result = if i < 0: s else: s[0 ..< i]
+
+proc dropBrackets(s: string): string =
+ var depth = 0
+ for c in s:
+ if c == '[': inc depth
+ elif c == ']': dec depth
+ elif depth == 0: result.add c
+
+proc isRoutine(line: string): bool =
+ let s = line.strip
+ for kw in ["iterator ", "proc ", "func ", "method ", "template "]:
+ if s.startsWith(kw):
+ return true
+
+# src/lib is shared by the app, the unit tests and the wasm build, so it may
+# only reach the Nim standard library and its own modules.
+proc checkLibImports(path: string, lines: seq[string], libModules: seq[string]) =
+ var i = 0
+ while i < lines.len:
+ let start = i
+ var stmt = lines[i].code.strip
+ if stmt.startsWith("import ") or stmt.startsWith("from ") or stmt.startsWith("include "):
+ while stmt.count('[') > stmt.count(']') and i + 1 < lines.len:
+ inc i
+ stmt.add " " & lines[i].code.strip
+ var body = stmt.split(maxsplit = 1)[1]
+ if stmt.startsWith("from "):
+ body = body.split(" import ")[0]
+ for m in body.dropBrackets.split(','):
+ var name = m.strip.split(" except ")[0].split(" as ")[0].strip
+ if name.len == 0 or name.startsWith("std/"):
+ continue
+ if name.startsWith("./"):
+ name = name[2 ..< name.len]
+ if name notin libModules:
+ fail(path, start + 1, "src/lib may only import std/ and src/lib modules, not " & name)
+ elif "libav" in stmt or "libsw" in stmt:
+ fail(path, start + 1, "src/lib must not bind FFmpeg")
+ inc i
+
+# The Nimony port miscompiles `..<`, but upstream keeps the idiomatic form.
+proc checkSlices(path: string, lines: seq[string]) =
+ for n, line in lines:
+ let c = line.code
+ var i = c.find("..")
+ while i >= 0:
+ var j = i + 2
+ while j < c.len and c[j] == ' ': inc j
+ if j < c.len and c[j] == '^':
+ inc j
+ while j < c.len and c[j] in {' ', '('}: inc j
+ if j < c.len and c[j] in {'2'..'9'}:
+ fail(path, n + 1, "write `..< ^N` instead of `.. ^(N+1)`")
+ i = c.find("..", i + 2)
+
+# History belongs in the commit message; a comment describes the code as it is.
+proc checkComments(path: string, lines: seq[string]) =
+ for n, line in lines:
+ let h = line.find('#')
+ if h < 0:
+ continue
+ let words = line[h ..< line.len].toLowerAscii.multiReplace(
+ {",": " ", ".": " ", ";": " ", ":": " ", "(": " ", ")": " "}).splitWhitespace
+ for k, w in words:
+ if w == "previously" or (w == "used" and k + 2 < words.len and words[k + 1] == "to" and
+ words[k + 2] in ["be", "do", "have", "drop", "read", "write", "take"]):
+ fail(path, n + 1, "comment narrates history; describe present behavior")
+ break
+
+# Nim inlines an iterator's loop body at every `yield`.
+proc checkYields(path: string, lines: seq[string]) =
+ for i, line in lines:
+ if not line.isRoutine:
+ continue
+ let base = line.indentOf
+ var yields: seq[int]
+ var j = i + 1
+ while j < lines.len and (lines[j].strip.len == 0 or lines[j].indentOf > base):
+ if lines[j].isRoutine:
+ # A nested routine is checked on its own.
+ let inner = lines[j].indentOf
+ inc j
+ while j < lines.len and (lines[j].strip.len == 0 or lines[j].indentOf > inner):
+ inc j
+ continue
+ if "yield" in lines[j].code.splitWhitespace:
+ yields.add j + 1
+ inc j
+ if yields.len > 1:
+ fail(path, yields[1], "more than one `yield` in a body (first at line " & $yields[0] & ")")
+
+var libModules: seq[string]
+for f in walkFiles("src/lib/*.nim"):
+ libModules.add f.splitFile.name
+
+for path in walkDirRec("src"):
+ if not path.endsWith(".nim"):
+ continue
+ let lines = readFile(path).splitLines
+ if path.parentDir == "src" / "lib":
+ checkLibImports(path, lines, libModules)
+ checkSlices(path, lines)
+ checkComments(path, lines)
+ checkYields(path, lines)
+
+if failures > 0:
+ quit 1