1 | import * as ts from 'typescript';
|
2 | import * as path from 'path';
|
3 | import * as fs from 'fs';
|
4 |
|
5 | import { Component, ConverterComponent } from 'typedoc/dist/lib/converter/components';
|
6 | import { Context } from 'typedoc/dist/lib/converter/context';
|
7 | import { Converter } from 'typedoc/dist/lib/converter/converter';
|
8 | import { Comment } from 'typedoc/dist/lib/models';
|
9 | import { Reflection, ReflectionKind } from 'typedoc/dist/lib/models/reflections/abstract';
|
10 | import { ContainerReflection } from 'typedoc/dist/lib/models/reflections/container';
|
11 | import { DeclarationReflection } from 'typedoc/dist/lib/models/reflections/declaration';
|
12 | import {
|
13 | createChildReflection,
|
14 | isModuleOrNamespace,
|
15 | removeReflection,
|
16 | removeTags,
|
17 | updateSymbolMapping,
|
18 | } from './typedocVersionCompatibility';
|
19 | import { getRawComment } from './getRawComment';
|
20 |
|
21 | const PLUGIN = 'typedoc-plugin-external-module-name';
|
22 | const CUSTOM_SCRIPT_FILENAMES = [`.${PLUGIN}.js`, `.${PLUGIN}.cjs`, `.${PLUGIN}.mjs`];
|
23 |
|
24 | type CustomModuleNameMappingFn = (
|
25 | explicitModuleAnnotation: string,
|
26 | implicitFromDirectory: string,
|
27 | path: string,
|
28 | reflection: Reflection,
|
29 | context: Context,
|
30 | ) => string;
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 | @Component({ name: 'external-module-name' })
|
59 | export class ExternalModuleNamePlugin extends ConverterComponent {
|
60 |
|
61 | private moduleRenames: ModuleRename[] = [];
|
62 | private baseDir = '';
|
63 | private customGetModuleNameFn: CustomModuleNameMappingFn;
|
64 | private defaultGetModuleNameFn: CustomModuleNameMappingFn = (match, guess) => match || guess;
|
65 | private disableAutoModuleName = false;
|
66 |
|
67 | initialize() {
|
68 | this.listenTo(this.owner, {
|
69 | [Converter.EVENT_BEGIN]: this.onBegin,
|
70 | [Converter.EVENT_CREATE_DECLARATION]: this.onDeclaration,
|
71 | [Converter.EVENT_RESOLVE_BEGIN]: this.onBeginResolve,
|
72 | });
|
73 |
|
74 | for (const filename of CUSTOM_SCRIPT_FILENAMES) {
|
75 | const pathToScript = path.join(process.cwd(), filename);
|
76 | try {
|
77 | if (fs.existsSync(pathToScript)) {
|
78 | const relativePath = path.relative(__dirname, pathToScript);
|
79 | this.customGetModuleNameFn = require(relativePath);
|
80 | console.log(`${PLUGIN}: Using custom module name mapping function from ${pathToScript}`);
|
81 | return;
|
82 | }
|
83 | } catch (error) {
|
84 | console.error(`${PLUGIN}: Failed to load custom module name mapping function from ${pathToScript}`);
|
85 | throw error;
|
86 | }
|
87 | }
|
88 | }
|
89 |
|
90 | private onBegin(context: Context) {
|
91 |
|
92 | const dir = context.program.getCurrentDirectory();
|
93 | const rootFileNames = context.program.getRootFileNames() ?? [];
|
94 | const options = context.getCompilerOptions();
|
95 |
|
96 | function commonPrefix(string1: string, string2: string) {
|
97 | let idx = 0;
|
98 | while (idx < string1.length && string1[idx] === string2[idx]) {
|
99 | idx++;
|
100 | }
|
101 | return string1.substr(0, idx);
|
102 | }
|
103 |
|
104 | const commonParent = rootFileNames.reduce(
|
105 | (acc, entry) => commonPrefix(acc, path.dirname(path.resolve(dir, entry))),
|
106 | path.resolve(rootFileNames[0] ?? dir),
|
107 | );
|
108 |
|
109 | this.baseDir = options.rootDir || options.baseUrl || commonParent;
|
110 |
|
111 |
|
112 | const option = this.application.options.getValue('disableAutoModuleName');
|
113 | let disableSources: boolean;
|
114 | try {
|
115 | disableSources = this.application.options.getValue('disableSources');
|
116 | } catch (ignored) {}
|
117 | this.disableAutoModuleName = option === 'true' || option === true || disableSources === true;
|
118 | }
|
119 |
|
120 | |
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 | private getModuleName(context: Context, reflection: Reflection, node): [string, boolean] {
|
129 | const comment = getRawComment(node);
|
130 | const preferred = /@preferred/.exec(comment) !== null;
|
131 |
|
132 | const [, match] = /@module\s+([\w\u4e00-\u9fa5\.\-_/@"]+)/.exec(comment) || [];
|
133 |
|
134 | let guess: string;
|
135 | let filename: string;
|
136 | if (!this.disableAutoModuleName) {
|
137 |
|
138 | filename = reflection.sources[0].file.fullFileName;
|
139 | guess = this.disableAutoModuleName ? undefined : path.dirname(path.relative(this.baseDir, filename));
|
140 |
|
141 | if (guess === '.') {
|
142 | guess = 'root';
|
143 | }
|
144 | }
|
145 |
|
146 |
|
147 | const mapper: CustomModuleNameMappingFn = this.customGetModuleNameFn || this.defaultGetModuleNameFn;
|
148 | const moduleName = mapper(match, guess, filename, reflection, context);
|
149 | return [moduleName, preferred];
|
150 | }
|
151 |
|
152 | |
153 |
|
154 |
|
155 |
|
156 | private onDeclaration(context: Context, reflection: Reflection, node?) {
|
157 | if (isModuleOrNamespace(reflection)) {
|
158 | const [moduleName, preferred] = this.getModuleName(context, reflection, node);
|
159 | if (moduleName) {
|
160 |
|
161 | this.moduleRenames.push({
|
162 | renameTo: moduleName,
|
163 | preferred: preferred,
|
164 | symbol: node.symbol,
|
165 | reflection: <ContainerReflection>reflection,
|
166 | });
|
167 | }
|
168 | }
|
169 |
|
170 |
|
171 | if (reflection.comment) {
|
172 | removeTags(reflection.comment, 'module');
|
173 | removeTags(reflection.comment, 'preferred');
|
174 | if (isEmptyComment(reflection.comment)) {
|
175 | delete reflection.comment;
|
176 | }
|
177 | }
|
178 | }
|
179 |
|
180 | |
181 |
|
182 |
|
183 |
|
184 | private onBeginResolve(context: Context) {
|
185 | let projRefs = context.project.reflections;
|
186 | let refsArray: Reflection[] = Object.values(projRefs);
|
187 |
|
188 |
|
189 | this.moduleRenames.forEach((item) => {
|
190 | let renaming = item.reflection as ContainerReflection;
|
191 |
|
192 |
|
193 | let nameParts = item.renameTo.split('.');
|
194 | let parent: ContainerReflection = context.project;
|
195 | for (let i = 0; i < nameParts.length - 1; ++i) {
|
196 | let child: DeclarationReflection = parent.children.filter((ref) => ref.name === nameParts[i])[0];
|
197 | if (!child) {
|
198 | child = createChildReflection(parent, nameParts[i]);
|
199 | child.parent = parent;
|
200 | child.children = [];
|
201 | context.project.reflections[child.id] = child;
|
202 | parent.children.push(child);
|
203 | }
|
204 | parent = child;
|
205 | }
|
206 |
|
207 |
|
208 | let mergeTarget = parent.children.filter(
|
209 | (ref) => ref.kind === renaming.kind && ref.name === nameParts[nameParts.length - 1],
|
210 | )[0] as ContainerReflection;
|
211 |
|
212 |
|
213 | if (!mergeTarget) {
|
214 | renaming.name = nameParts[nameParts.length - 1];
|
215 | let oldParent = <ContainerReflection>renaming.parent;
|
216 | for (let i = 0; i < oldParent.children.length; ++i) {
|
217 | if (oldParent.children[i] === renaming) {
|
218 | oldParent.children.splice(i, 1);
|
219 | break;
|
220 | }
|
221 | }
|
222 | item.reflection.parent = parent;
|
223 | parent.children.push(<DeclarationReflection>renaming);
|
224 | updateSymbolMapping(context, item.symbol, parent);
|
225 | return;
|
226 | }
|
227 |
|
228 | updateSymbolMapping(context, item.symbol, mergeTarget);
|
229 | if (!mergeTarget.children) {
|
230 | mergeTarget.children = [];
|
231 | }
|
232 |
|
233 |
|
234 | let childrenOfRenamed = refsArray.filter((ref) => ref.parent === renaming);
|
235 | childrenOfRenamed.forEach((ref: Reflection) => {
|
236 |
|
237 | ref.parent = mergeTarget;
|
238 | mergeTarget.children.push(<any>ref);
|
239 | });
|
240 |
|
241 |
|
242 |
|
243 | if (item.preferred) mergeTarget.comment = renaming.comment;
|
244 |
|
245 |
|
246 |
|
247 | if (renaming.children) renaming.children.length = 0;
|
248 | removeReflection(context.project, renaming);
|
249 |
|
250 |
|
251 | if (mergeTarget.comment) {
|
252 | removeTags(mergeTarget.comment, 'module');
|
253 | removeTags(mergeTarget.comment, 'preferred');
|
254 | }
|
255 | if (isEmptyComment(mergeTarget.comment)) {
|
256 | delete mergeTarget.comment;
|
257 | }
|
258 | });
|
259 | }
|
260 | }
|
261 |
|
262 | function isEmptyComment(comment: Comment) {
|
263 | return !comment || (!comment.text && !comment.shortText && (!comment.tags || comment.tags.length === 0));
|
264 | }
|
265 |
|
266 | interface ModuleRename {
|
267 | renameTo: string;
|
268 | preferred: boolean;
|
269 | symbol: ts.Symbol;
|
270 | reflection: ContainerReflection;
|
271 | }
|