Skip to content

Commit

Permalink
lint(exercises): check files.editor field (#585)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ErikSchierboom authored May 5, 2022
1 parent 8f8f76c commit 0832f81
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
1 change: 1 addition & 0 deletions src/lint/concept_exercises.nim
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ proc hasValidFiles(data: JsonNode; path, exerciseDir: Path): bool =
hasArrayOfFiles(d, "solution", path, k, exerciseDir),
hasArrayOfFiles(d, "test", path, k, exerciseDir),
hasArrayOfFiles(d, "exemplar", path, k, exerciseDir),
hasArrayOfFiles(d, "editor", path, k, exerciseDir, isRequired = false),
]
result = allTrue(checks)

Expand Down
1 change: 1 addition & 0 deletions src/lint/practice_exercises.nim
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ proc hasValidFiles(data: JsonNode; path, exerciseDir: Path): bool =
hasArrayOfFiles(d, "solution", path, k, exerciseDir),
hasArrayOfFiles(d, "test", path, k, exerciseDir),
hasArrayOfFiles(d, "example", path, k, exerciseDir),
hasArrayOfFiles(d, "editor", path, k, exerciseDir, isRequired = false),
]
result = allTrue(checks)

Expand Down
41 changes: 24 additions & 17 deletions src/lint/validators.nim
Original file line number Diff line number Diff line change
Expand Up @@ -420,24 +420,31 @@ proc hasArrayOfFiles*(data: JsonNode;
path: Path;
context = "";
relativeToPath: Path;
errorAnnotation = ""): bool =
if hasArrayOfStrings(data, key, path, context):
result = true
var processedItems = initHashSet[string](data[key].len)

for item in data[key]:
let relativeFilePath = item.getStr()
let absoluteFilePath = relativeToPath / relativeFilePath
if fileExists(absoluteFilePath):
let itemStr = item.getStr()
if processedItems.containsOrIncl(itemStr):
let msg = &"The {q context} array contains duplicate " &
&"{q itemStr} values"
errorAnnotation = "";
isRequired = true): bool =
## Returns true in any of these cases:
## - `hasArrayOfStrings` returns true for `data[key]`.
## - `data` lacks the key `key` and `isRequired` is false.
if data.hasKey(key, path, context, isRequired):
if hasArrayOfStrings(data, key, path, context):
result = true
var processedItems = initHashSet[string](data[key].len)

for item in data[key]:
let relativeFilePath = item.getStr()
let absoluteFilePath = relativeToPath / relativeFilePath
if fileExists(absoluteFilePath):
let itemStr = item.getStr()
if processedItems.containsOrIncl(itemStr):
let msg = &"The {q context} array contains duplicate " &
&"{q itemStr} values"
result.setFalseAndPrint(msg, path, annotation = errorAnnotation)
else:
let msg = &"The {q context} array contains value " &
&"{q relativeFilePath} but {q $absoluteFilePath} could not be found"
result.setFalseAndPrint(msg, path, annotation = errorAnnotation)
else:
let msg = &"The {q context} array contains value " &
&"{q relativeFilePath} but {q $absoluteFilePath} could not be found"
result.setFalseAndPrint(msg, path, annotation = errorAnnotation)
elif not isRequired:
result = true

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

0 comments on commit 0832f81

Please sign in to comment.