|
| 1 | +import {rollup, sort} from "d3"; |
| 2 | +import {Node, FunctionDeclaration, InterfaceDeclaration, Project, VariableDeclaration, VariableStatement} from "ts-morph"; |
| 3 | + |
| 4 | +// These interfaces tend to represent things that Plot constructs internally, |
| 5 | +// rather than objects that the user is expected to provide. |
| 6 | +function isInternalInterface(name) { |
| 7 | + return ( |
| 8 | + name === "AutoSpec" || |
| 9 | + name === "Channel" || |
| 10 | + name === "ChannelDomainOptions" || // TODO |
| 11 | + name === "ChannelTransform" || |
| 12 | + name === "Context" || |
| 13 | + name === "Dimensions" || |
| 14 | + name === "Plot" || |
| 15 | + name === "Scale" |
| 16 | + ); |
| 17 | +} |
| 18 | + |
| 19 | +// This tries to get a brief human-readable, one-sentence summary description of |
| 20 | +// the exported symbol. We might want to formalize this so that we can be more |
| 21 | +// intentional when authoring documentation. |
| 22 | +function getDescription(node: FunctionDeclaration | VariableStatement): string { |
| 23 | + return node |
| 24 | + .getJsDocs()[0] |
| 25 | + ?.getDescription() |
| 26 | + .replace(/\n/g, " ") // replace newlines with spaces |
| 27 | + .replace(/[*_]/g, "") // remove bold and italics formatting |
| 28 | + .replace(/[.:]($|\s+).*/g, "") // truncate at the first period or colon |
| 29 | + .replace(/\[([^[]+)\]\[\d+\]/g, "$1") // strip links (assuming [1] syntax) |
| 30 | + .trim(); |
| 31 | +} |
| 32 | + |
| 33 | +// While we try to keep the source code file structure as close as possible to |
| 34 | +// the documentation URL structure, there are some inevitable deviations that |
| 35 | +// are codified by this function. When new files are added, please try to keep |
| 36 | +// this function up-to-date, and try to generalize patterns so that we |
| 37 | +// automatically generate correct links. (TODO Verify that the links are valid.) |
| 38 | +function getHref(name: string, path: string): string { |
| 39 | + path = path.replace(/\.d\.ts$/, ""); // drop trailing .d.ts |
| 40 | + path = path.replace(/([a-z0-9])([A-Z])/, (_, a, b) => `${a}-${b.toLowerCase()}`); // camel case conversion |
| 41 | + if (path.split("/").length === 1) path = `features/${path}`; // top-level declarations are features |
| 42 | + switch (path) { |
| 43 | + case "features/curve": |
| 44 | + case "features/format": |
| 45 | + case "features/mark": |
| 46 | + case "features/plot": |
| 47 | + return `${path}s`; |
| 48 | + case "features/options": |
| 49 | + return "features/transforms"; |
| 50 | + case "marks/axis": { |
| 51 | + switch (name) { |
| 52 | + case "gridX": |
| 53 | + case "gridY": |
| 54 | + case "gridFx": |
| 55 | + case "gridFy": |
| 56 | + return "marks/grid"; |
| 57 | + } |
| 58 | + break; |
| 59 | + } |
| 60 | + case "marks/crosshair": |
| 61 | + return "interactions/crosshair"; |
| 62 | + case "transforms/basic": { |
| 63 | + switch (name) { |
| 64 | + case "filter": |
| 65 | + return "transforms/filter"; |
| 66 | + case "reverse": |
| 67 | + case "shuffle": |
| 68 | + case "sort": |
| 69 | + return "transforms/sort"; |
| 70 | + } |
| 71 | + return "features/transforms"; |
| 72 | + } |
| 73 | + } |
| 74 | + return path; |
| 75 | +} |
| 76 | + |
| 77 | +function getInterfaceName(name: string, path: string): string { |
| 78 | + name = name.replace(/(Transform|Corner|X|Y|Output)?(Defaults|Options|Styles)$/, ""); |
| 79 | + name = name.replace(/([a-z0-9])([A-Z])/, (_, a, b) => `${a} ${b}`); // camel case conversion |
| 80 | + name = name.toLowerCase(); |
| 81 | + if (name === "curve auto") name = "curve"; |
| 82 | + if (name === "plot facet") name = "plot"; |
| 83 | + if (path.startsWith("marks/")) name += " mark"; |
| 84 | + else if (path.startsWith("transforms/")) name += " transform"; |
| 85 | + return name; |
| 86 | +} |
| 87 | + |
| 88 | +export default { |
| 89 | + watch: [], |
| 90 | + async load() { |
| 91 | + const project = new Project({tsConfigFilePath: "tsconfig.json"}); |
| 92 | + const allMethods: {name: string; comment: string; href: string}[] = []; |
| 93 | + const allOptions: {name: string; context: {name: string; href: string}}[] = []; |
| 94 | + const index = project.getSourceFile("src/index.d.ts")!; |
| 95 | + for (const [name, declarations] of index.getExportedDeclarations()) { |
| 96 | + for (const declaration of declarations) { |
| 97 | + if (Node.isInterfaceDeclaration(declaration)) { |
| 98 | + if (isInternalInterface(name)) continue; |
| 99 | + for (const property of declaration.getProperties()) { |
| 100 | + const path = index.getRelativePathTo(declaration.getSourceFile()); |
| 101 | + const href = getHref(name, path); |
| 102 | + if (property.getJsDocs().some((d) => d.getTags().some((d) => Node.isJSDocDeprecatedTag(d)))) continue; |
| 103 | + allOptions.push({name: property.getName(), context: {name: getInterfaceName(name, path), href}}); |
| 104 | + } |
| 105 | + } else if (Node.isFunctionDeclaration(declaration)) { |
| 106 | + const comment = getDescription(declaration); |
| 107 | + if (comment) { |
| 108 | + const href = getHref(name, index.getRelativePathTo(declaration.getSourceFile())); |
| 109 | + allMethods.push({name, comment, href}); |
| 110 | + } |
| 111 | + } else if (Node.isVariableDeclaration(declaration)) { |
| 112 | + const comment = getDescription(declaration.getVariableStatement()!); |
| 113 | + if (comment) { |
| 114 | + const href = getHref(name, index.getRelativePathTo(declaration.getSourceFile())); |
| 115 | + allMethods.push({name, comment, href}); |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + return { |
| 121 | + methods: sort(allMethods, ({name}) => name), |
| 122 | + options: sort( |
| 123 | + rollup( |
| 124 | + allOptions, |
| 125 | + (D) => |
| 126 | + sort( |
| 127 | + rollup( |
| 128 | + D.map((d) => d.context), |
| 129 | + ([d]) => d, |
| 130 | + (d) => d.name |
| 131 | + ).values(), |
| 132 | + (d) => d.name |
| 133 | + ), |
| 134 | + (d) => d.name |
| 135 | + ), |
| 136 | + ([name]) => name |
| 137 | + ) |
| 138 | + }; |
| 139 | + } |
| 140 | +}; |
0 commit comments