Skip to content

Commit

Permalink
Fix defined in edge case
Browse files Browse the repository at this point in the history
Resolves #2307
  • Loading branch information
Gerrit0 committed Dec 28, 2023
1 parent 800b97e commit 72726d3
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
### Bug Fixes

- `notDocumented` validation will no longer require documentation for data within parameters that cannot be documented via `@param`, #2291.
- "defined in" locations for signatures will now always be contained within the function declaration's location. This prevents defined in sometimes pointing to node_modules, #2307.
- Type parameters will now be resolved for arrow-methods on classes like regular class methods, #2320.
- TypeDoc now inherits `typedocOptions` fields from extended tsconfig files, #2334.
- Methods which return function types no longer have duplicated comments, #2336.
Expand Down
40 changes: 40 additions & 0 deletions src/lib/converter/plugins/SourcePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ export class SourcePlugin extends ConverterComponent {
continue;
}

if (replaceSourcesWithParentSources(refl)) {
refl.sources = (refl.parent as DeclarationReflection).sources;
}

for (const source of refl.sources || []) {
if (this.disableGit || gitIsInstalled()) {
const repo = this.getRepository(
Expand Down Expand Up @@ -255,3 +259,39 @@ function getLocationNode(node: ts.Node) {
if (isNamedNode(node)) return node.name;
return node;
}

function replaceSourcesWithParentSources(
refl: SignatureReflection | DeclarationReflection,
) {
if (refl instanceof DeclarationReflection || !refl.sources) {
return false;
}

if (refl.name === "privateArrow") {
console.log("privateArrow");

Check warning on line 271 in src/lib/converter/plugins/SourcePlugin.ts

View workflow job for this annotation

GitHub Actions / Node 18 Windows

Unexpected console statement

Check warning on line 271 in src/lib/converter/plugins/SourcePlugin.ts

View workflow job for this annotation

GitHub Actions / Node 16

Unexpected console statement

Check warning on line 271 in src/lib/converter/plugins/SourcePlugin.ts

View workflow job for this annotation

GitHub Actions / Node 18

Unexpected console statement

Check warning on line 271 in src/lib/converter/plugins/SourcePlugin.ts

View workflow job for this annotation

GitHub Actions / Node 20

Unexpected console statement
}

const symbol = refl.project.getSymbolFromReflection(refl.parent);
if (!symbol?.declarations) {
return false;
}

for (const decl of symbol.declarations) {
const file = decl.getSourceFile();
const pos = file.getLineAndCharacterOfPosition(decl.pos);
const end = file.getLineAndCharacterOfPosition(decl.end);

if (
refl.sources.some(
(src) =>
src.fullFileName === file.fileName &&
pos.line <= src.line - 1 &&
src.line - 1 <= end.line,
)
) {
return false;
}
}

return true;
}
2 changes: 1 addition & 1 deletion src/lib/models/sources/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class SourceReference {
fullFileName: string;

/**
* The number of the line that emitted the declaration.
* The one based number of the line that emitted the declaration.
*/
line: number;

Expand Down
6 changes: 3 additions & 3 deletions src/test/converter/js/specs.json
Original file line number Diff line number Diff line change
Expand Up @@ -894,9 +894,9 @@
"sources": [
{
"fileName": "index.js",
"line": 49,
"character": 3,
"url": "typedoc://index.js#L49"
"line": 55,
"character": 13,
"url": "typedoc://index.js#L55"
}
],
"parameters": [
Expand Down
24 changes: 12 additions & 12 deletions src/test/converter/mixin/specs.json
Original file line number Diff line number Diff line change
Expand Up @@ -585,9 +585,9 @@
"sources": [
{
"fileName": "mixin.ts",
"line": 9,
"character": 41,
"url": "typedoc://mixin.ts#L9"
"line": 34,
"character": 10,
"url": "typedoc://mixin.ts#L34"
}
],
"parameters": [
Expand Down Expand Up @@ -1656,9 +1656,9 @@
"sources": [
{
"fileName": "mixin.ts",
"line": 9,
"character": 41,
"url": "typedoc://mixin.ts#L9"
"line": 34,
"character": 10,
"url": "typedoc://mixin.ts#L34"
}
],
"parameters": [
Expand Down Expand Up @@ -1852,9 +1852,9 @@
"sources": [
{
"fileName": "mixin.ts",
"line": 9,
"character": 41,
"url": "typedoc://mixin.ts#L9"
"line": 54,
"character": 10,
"url": "typedoc://mixin.ts#L54"
}
],
"parameters": [
Expand Down Expand Up @@ -2041,9 +2041,9 @@
"sources": [
{
"fileName": "mixin.ts",
"line": 9,
"character": 41,
"url": "typedoc://mixin.ts#L9"
"line": 74,
"character": 10,
"url": "typedoc://mixin.ts#L74"
}
],
"parameters": [
Expand Down
10 changes: 10 additions & 0 deletions src/test/converter2/issues/gh2307.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const times = (b: number) => (a: number) => a * b;

export const double = times(2);

export const foo = () => 123;

export const all: {
<T>(fn: (item: T) => boolean, iterator: Iterable<T>): boolean;
<T>(fn: (item: T) => boolean): (iterator: Iterable<T>) => boolean;
} = () => false as any;
15 changes: 15 additions & 0 deletions src/test/issues.c2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1143,6 +1143,21 @@ describe("Issue Tests", () => {
equal(tp.flags.isConst, true);
});

it("Detects source locations coming from types and prefers value declarations, #2307", () => {
const project = convert();

const getLines = (name: string) => {
const refl = query(project, name);
return refl.signatures?.flatMap((sig) =>
sig.sources!.map((src) => src.line),
);
};

equal(getLines("double"), [3]);
equal(getLines("foo"), [5]);
equal(getLines("all"), [8, 9]);
});

it("Uses type parameters from parent class in arrow-methods, #2320", () => {
const project = convert();
const arrow = querySig(project, "ResolvedSubclass.arrowFunction");
Expand Down

0 comments on commit 72726d3

Please sign in to comment.