Skip to content

Commit 0832f81

Browse files
lint(exercises): check files.editor field (#585)
Changes: - Make `hasArrayOfFiles` support checking an optional array of files. - Make `configlet lint` check the `files.editor` field in the `.meta/config.json` file of every Concept Exercise and Practice Exercise.
1 parent 8f8f76c commit 0832f81

3 files changed

Lines changed: 26 additions & 17 deletions

File tree

src/lint/concept_exercises.nim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ proc hasValidFiles(data: JsonNode; path, exerciseDir: Path): bool =
1010
hasArrayOfFiles(d, "solution", path, k, exerciseDir),
1111
hasArrayOfFiles(d, "test", path, k, exerciseDir),
1212
hasArrayOfFiles(d, "exemplar", path, k, exerciseDir),
13+
hasArrayOfFiles(d, "editor", path, k, exerciseDir, isRequired = false),
1314
]
1415
result = allTrue(checks)
1516

src/lint/practice_exercises.nim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ proc hasValidFiles(data: JsonNode; path, exerciseDir: Path): bool =
1010
hasArrayOfFiles(d, "solution", path, k, exerciseDir),
1111
hasArrayOfFiles(d, "test", path, k, exerciseDir),
1212
hasArrayOfFiles(d, "example", path, k, exerciseDir),
13+
hasArrayOfFiles(d, "editor", path, k, exerciseDir, isRequired = false),
1314
]
1415
result = allTrue(checks)
1516

src/lint/validators.nim

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -420,24 +420,31 @@ proc hasArrayOfFiles*(data: JsonNode;
420420
path: Path;
421421
context = "";
422422
relativeToPath: Path;
423-
errorAnnotation = ""): bool =
424-
if hasArrayOfStrings(data, key, path, context):
425-
result = true
426-
var processedItems = initHashSet[string](data[key].len)
427-
428-
for item in data[key]:
429-
let relativeFilePath = item.getStr()
430-
let absoluteFilePath = relativeToPath / relativeFilePath
431-
if fileExists(absoluteFilePath):
432-
let itemStr = item.getStr()
433-
if processedItems.containsOrIncl(itemStr):
434-
let msg = &"The {q context} array contains duplicate " &
435-
&"{q itemStr} values"
423+
errorAnnotation = "";
424+
isRequired = true): bool =
425+
## Returns true in any of these cases:
426+
## - `hasArrayOfStrings` returns true for `data[key]`.
427+
## - `data` lacks the key `key` and `isRequired` is false.
428+
if data.hasKey(key, path, context, isRequired):
429+
if hasArrayOfStrings(data, key, path, context):
430+
result = true
431+
var processedItems = initHashSet[string](data[key].len)
432+
433+
for item in data[key]:
434+
let relativeFilePath = item.getStr()
435+
let absoluteFilePath = relativeToPath / relativeFilePath
436+
if fileExists(absoluteFilePath):
437+
let itemStr = item.getStr()
438+
if processedItems.containsOrIncl(itemStr):
439+
let msg = &"The {q context} array contains duplicate " &
440+
&"{q itemStr} values"
441+
result.setFalseAndPrint(msg, path, annotation = errorAnnotation)
442+
else:
443+
let msg = &"The {q context} array contains value " &
444+
&"{q relativeFilePath} but {q $absoluteFilePath} could not be found"
436445
result.setFalseAndPrint(msg, path, annotation = errorAnnotation)
437-
else:
438-
let msg = &"The {q context} array contains value " &
439-
&"{q relativeFilePath} but {q $absoluteFilePath} could not be found"
440-
result.setFalseAndPrint(msg, path, annotation = errorAnnotation)
446+
elif not isRequired:
447+
result = true
441448

442449
type
443450
ItemCall = proc(data: JsonNode; context: string; path: Path): bool {.nimcall.}

0 commit comments

Comments
 (0)