|
| 1 | +# Ensure that data.table condition classes in code match documentation |
| 2 | +condition_classes_documentation_linter = function(rd_file) { |
| 3 | + if (!grepl("\\name{data.table-condition-classes}", readChar(rd_file, 100L), fixed = TRUE)) return(invisible()) |
| 4 | + |
| 5 | + # Find condition classes in R code |
| 6 | + walk_r_ast_for_classes = function(expr) { |
| 7 | + if (is.call(expr) && is.name(e <- expr[[1L]]) && as.character(e) %in% c("stopf", "warningf", "messagef", "packageStartupMessagef") && is.character(class_arg <- expr[["class"]]) && startsWith(class_arg, "dt_")) { |
| 8 | + class_arg |
| 9 | + } else if (is.recursive(expr)) { |
| 10 | + unlist(lapply(expr, walk_r_ast_for_classes)) |
| 11 | + } |
| 12 | + } |
| 13 | + |
| 14 | + # Find condition classes in documentation |
| 15 | + walk_rd_ast_for_classes = function(rd_element) { |
| 16 | + if (!is.list(rd_element)) return(character()) |
| 17 | + |
| 18 | + result = character() |
| 19 | + if (isTRUE(attr(rd_element, "Rd_tag") == "\\code") && length(rd_element) >= 1L) { |
| 20 | + content = rd_element[[1L]] |
| 21 | + if (is.character(content) && startsWith(content, "dt_")) { |
| 22 | + result = content |
| 23 | + } |
| 24 | + } |
| 25 | + c(result, unlist(lapply(rd_element, walk_rd_ast_for_classes))) |
| 26 | + } |
| 27 | + |
| 28 | + code_classes = list.files("R", pattern = "\\.R$", full.names = TRUE) |> |
| 29 | + lapply(\(f) lapply(parse(f), walk_r_ast_for_classes)) |> |
| 30 | + unlist() |> |
| 31 | + unique() |
| 32 | + |
| 33 | + doc_classes = rd_file |> |
| 34 | + tools::parse_Rd() |> |
| 35 | + walk_rd_ast_for_classes() |> |
| 36 | + unique() |
| 37 | + |
| 38 | + miss_in_doc = setdiff(code_classes, doc_classes) |
| 39 | + miss_in_code = setdiff(doc_classes, code_classes) |
| 40 | + |
| 41 | + if (length(miss_in_doc) > 0L || length(miss_in_code) > 0L) { |
| 42 | + if (length(miss_in_doc) > 0L) { |
| 43 | + cat(sprintf("Condition classes in code but missing from docs: %s\n", toString(miss_in_doc))) |
| 44 | + } |
| 45 | + if (length(miss_in_code) > 0L) { |
| 46 | + cat(sprintf("Condition classes in docs but not in code: %s\n", toString(miss_in_code))) |
| 47 | + } |
| 48 | + stop("Please sync man/datatable-condition-classes.Rd with code condition classes") |
| 49 | + } |
| 50 | +} |
| 51 | + |
0 commit comments