Skip to content

Commit 0089d19

Browse files
fix(typedoc0.16): Some fixes for 0.16.x (WIP)
- Use built-in getRawComment for version 0.16+. Continue to use monkey patch for version < 0.16 (for now, not sure if this is necessary) - Clean up project.reflections[] when deleting a reflection - Use registerReflection in 0.16+ to update symbol mappings - Add isTypedocVersion() helper
1 parent bd9b5cd commit 0089d19

7 files changed

Lines changed: 133 additions & 105 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
# Built using tsc
77
plugin.js
88
getRawComment.js
9+
typedocVersion.js
910

1011
# test cache
1112
.downstream_cache

downstream_projects.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"typedoc-0.11.1": "./test/typedoc-0.11.1",
1010
"typedoc-0.12.0": "./test/typedoc-0.12.0",
1111
"typedoc-0.13.0": "./test/typedoc-0.13.0",
12-
"typedoc-0.14.2": "./test/typedoc-0.14.2"
12+
"typedoc-0.14.2": "./test/typedoc-0.14.2",
13+
"typedoc-0.15.8": "./test/typedoc-0.15.8"
1314
},
1415
"nohoist": ["**/typedoc-plugin-external-module-name"]
1516
}

getRawComment.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@
99
import * as ts from 'typescript';
1010
import * as _ts from 'typedoc/dist/lib/ts-internal';
1111
import { getRawComment as realGetRawComment } from 'typedoc/dist/lib/converter/factories/comment';
12+
import { isTypedocVersion } from './typedocVersion';
13+
14+
const useMonkeyPatchedGetRawComment = isTypedocVersion('< 0.16.0');
1215

1316
function monkeyPatch() {
14-
const realGetJSDocCommentRanges = _ts.getJSDocCommentRanges;
17+
const realGetJSDocCommentRanges = (_ts as any).getJSDocCommentRanges;
1518

1619
function patchedGetJSDocCommentRanges() {
1720
const result = realGetJSDocCommentRanges.apply(this, arguments);
@@ -28,8 +31,9 @@ function monkeyPatch() {
2831
tsinternal.getJSDocCommentRanges = realGetJSDocCommentRanges;
2932
};
3033
}
34+
const getRawComment = useMonkeyPatchedGetRawComment ? monkeyPatchedGetRawComment : realGetRawComment;
3135

32-
function getRawComment(node: ts.Node): string {
36+
function monkeyPatchedGetRawComment(node: ts.Node): string {
3337
let unpatch = monkeyPatch();
3438
try {
3539
return realGetRawComment(node);

package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,25 @@
2020
"author": "Chris Thielen",
2121
"license": "MIT",
2222
"dependencies": {
23+
"lodash": "^4.1.2",
2324
"semver": "^7.1.1"
2425
},
2526
"peerDependencies": {
2627
"typedoc": ">=0.7.0 <0.15.0"
2728
},
2829
"devDependencies": {
2930
"@types/handlebars": "^4.0.37",
31+
"@types/lodash": "^4.1.6",
32+
"@types/node": "^13.1.6",
3033
"@types/semver": "^6.2.0",
3134
"@uirouter/publish-scripts": "^2.3.24",
3235
"husky": "^2.2.0",
3336
"prettier": "^1.13.7",
3437
"pretty-quick": "^1.4.1",
35-
"typedoc": "~0.14.0"
38+
"typedoc": "0.16.4"
39+
},
40+
"resolutions": {
41+
"typedoc": "0.16.4"
3642
},
3743
"husky": {
3844
"hooks": {

plugin.ts

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as ts from 'typescript';
12
import { Component, ConverterComponent } from 'typedoc/dist/lib/converter/components';
23
import { Context } from 'typedoc/dist/lib/converter/context';
34
import { Converter } from 'typedoc/dist/lib/converter/converter';
@@ -6,12 +7,9 @@ import { Comment, ProjectReflection } from 'typedoc/dist/lib/models';
67
import { Reflection, ReflectionKind } from 'typedoc/dist/lib/models/reflections/abstract';
78
import { ContainerReflection } from 'typedoc/dist/lib/models/reflections/container';
89
import { DeclarationReflection } from 'typedoc/dist/lib/models/reflections/declaration';
10+
import { isTypedocVersion } from './typedocVersion';
911
import { getRawComment } from './getRawComment';
1012

11-
import { satisfies } from 'semver';
12-
const version = require('typedoc/package.json').version;
13-
const useOldDeclarationReflectionConstructor = satisfies(version, '< 0.14.0');
14-
1513
/**
1614
* This plugin allows an ES6 module to specify its TypeDoc name.
1715
* It also allows multiple ES6 modules to be merged together into a single TypeDoc module.
@@ -79,7 +77,7 @@ export class ExternalModuleNamePlugin extends ConverterComponent {
7977
this.moduleRenames.push({
8078
renameTo: match[1],
8179
preferred: preferred != null,
82-
symbolId: context.getSymbolID(node.symbol),
80+
symbol: node.symbol,
8381
reflection: <ContainerReflection>reflection,
8482
});
8583
}
@@ -116,8 +114,7 @@ export class ExternalModuleNamePlugin extends ConverterComponent {
116114
for (let i = 0; i < nameParts.length - 1; ++i) {
117115
let child: DeclarationReflection = parent.children.filter(ref => ref.name === nameParts[i])[0];
118116
if (!child) {
119-
if (useOldDeclarationReflectionConstructor) {
120-
// for typedoc < 0.15.0
117+
if (isTypedocVersion('< 0.14.0')) {
121118
child = new (DeclarationReflection as any)(parent, nameParts[i], ReflectionKind.ExternalModule);
122119
} else {
123120
child = new DeclarationReflection(nameParts[i], ReflectionKind.ExternalModule, parent);
@@ -147,11 +144,11 @@ export class ExternalModuleNamePlugin extends ConverterComponent {
147144
}
148145
item.reflection.parent = parent;
149146
parent.children.push(<DeclarationReflection>renaming);
150-
updateSymbolMapping(context.project, item.symbolId, parent.id);
147+
updateSymbolMapping(context, item.symbol, parent);
151148
return;
152149
}
153150

154-
updateSymbolMapping(context.project, item.symbolId, mergeTarget.id);
151+
updateSymbolMapping(context, item.symbol, mergeTarget);
155152
if (!mergeTarget.children) {
156153
mergeTarget.children = [];
157154
}
@@ -171,7 +168,7 @@ export class ExternalModuleNamePlugin extends ConverterComponent {
171168
// Now that all the children have been relocated to the mergeTarget, delete the empty module
172169
// Make sure the module being renamed doesn't have children, or they will be deleted
173170
if (renaming.children) renaming.children.length = 0;
174-
CommentPlugin.removeReflection(context.project, renaming);
171+
removeReflection(context, renaming);
175172

176173
// Remove @module and @preferred from the comment, if found.
177174
CommentPlugin.removeTags(mergeTarget.comment, 'module');
@@ -183,13 +180,25 @@ export class ExternalModuleNamePlugin extends ConverterComponent {
183180
}
184181
}
185182

183+
function removeReflection(context: Context, reflection: Reflection) {
184+
CommentPlugin.removeReflection(context.project, reflection);
185+
if (isTypedocVersion('>=0.16.0 <0.17.0')) {
186+
delete context.project.reflections[reflection.id];
187+
}
188+
}
189+
186190
/**
187191
* When we delete reflections, update the symbol mapping in order to fix:
188192
* https://github.com/christopherthielen/typedoc-plugin-external-module-name/issues/313
189193
* https://github.com/christopherthielen/typedoc-plugin-external-module-name/issues/193
190194
*/
191-
function updateSymbolMapping(project: ProjectReflection, symbolId: number, mappedReflectionId: number) {
192-
project.symbolMapping[symbolId] = mappedReflectionId;
195+
function updateSymbolMapping(context: Context, symbol: ts.Symbol, reflection: Reflection) {
196+
if (isTypedocVersion('< 0.16.0')) {
197+
// (context as any).registerReflection(reflection, null, symbol);
198+
(context.project as any).symbolMapping[(symbol as any).id] = reflection.id;
199+
} else {
200+
context.registerReflection(reflection, symbol);
201+
}
193202
}
194203

195204
function isEmptyComment(comment: Comment) {
@@ -199,6 +208,6 @@ function isEmptyComment(comment: Comment) {
199208
interface ModuleRename {
200209
renameTo: string;
201210
preferred: boolean;
202-
symbolId: number;
211+
symbol: ts.Symbol;
203212
reflection: ContainerReflection;
204213
}

typedocVersion.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { memoize } from 'lodash';
2+
import { satisfies } from 'semver';
3+
4+
const typedocVersion = require('typedoc/package.json').version;
5+
6+
function checkTypedocVersion(semverString: string) {
7+
return satisfies(typedocVersion, semverString);
8+
}
9+
10+
export const isTypedocVersion = memoize(checkTypedocVersion);

0 commit comments

Comments
 (0)