1+ # Ensure that data.table options in code match documentation
2+ check_options_documentation = function (rd_file ) {
3+ if (! grepl(" data.table-options" , rd_file )) return (invisible ())
4+
5+ # Find options in R code
6+ get_options_from_code = function () {
7+ found = character (0 )
8+
9+ walk_ast = function (expr ) {
10+ result = character (0 )
11+ if (is.call(expr ) && length(expr ) > = 2 && identical(expr [[1 ]], quote(getOption )) && is.character(expr [[2 ]]) && startsWith(expr [[2 ]], " datatable." )) {
12+ result = expr [[2 ]]
13+ }
14+ if (is.recursive(expr )) {
15+ result = c(result , unlist(lapply(expr , walk_ast )))
16+ }
17+ result
18+ }
19+ r_files = list.files(" R" , pattern = " \\ .R$" , full.names = TRUE )
20+ for (file in r_files ) {
21+ tryCatch({
22+ found = c(found , unlist(lapply(parse(file = file ), walk_ast )))
23+ }, error = function (e ) {})
24+ }
25+ sort(unique(found ))
26+ }
27+
28+ # Find options in documentation
29+ get_options_from_doc = function (rd_file ) {
30+ if (! file.exists(rd_file )) return (character (0 ))
31+
32+ tryCatch({
33+ found = character (0 )
34+ walk_rd = function (rd_element ) {
35+ result = character (0 )
36+ if (is.list(rd_element )) {
37+ if (! is.null(attr(rd_element , " Rd_tag" )) && attr(rd_element , " Rd_tag" ) == " \\ code" && length(rd_element ) > = 1 ) {
38+ content = rd_element [[1 ]]
39+ if (is.character(content ) && startsWith(content , " datatable." )) {
40+ result = content
41+ }
42+ }
43+ result = c(result , unlist(lapply(rd_element , walk_rd )))
44+ }
45+ result
46+ }
47+ found = walk_rd(tools :: parse_Rd(rd_file ))
48+ sort(unique(found ))
49+ }, error = function (e ) character (0 ))
50+ }
51+
52+ code_opts = get_options_from_code()
53+ doc_opts = get_options_from_doc(rd_file )
54+ code_opts = setdiff(code_opts , " datatable.alloc" )
55+
56+ miss_in_doc = setdiff(code_opts , doc_opts )
57+ miss_in_code = setdiff(doc_opts , code_opts )
58+
59+ if (length(miss_in_doc ) > 0 || length(miss_in_code ) > 0 ) {
60+ if (length(miss_in_doc ) > 0 ) {
61+ cat(" Options in code but missing from docs:" , paste(miss_in_doc , collapse = " , " ), " \n " )
62+ }
63+ if (length(miss_in_code ) > 0 ) {
64+ cat(" Options in docs but not in code:" , paste(miss_in_code , collapse = " , " ), " \n " )
65+ }
66+ stop(" Please sync man/data.table-options.Rd with code options" )
67+ }
68+ }
0 commit comments