Skip to content

Commit

Permalink
Ignore @license and @import comments at the top of files
Browse files Browse the repository at this point in the history
Resolves #2552
  • Loading branch information
Gerrit0 committed Apr 27, 2024
1 parent 4e56239 commit 95bf5d0
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,7 @@
- Header anchor links in rendered markdown are now more consistent with headers generated by TypeDoc, #2546.
- Types rendered in the `Returns` header are now properly colored, #2546.
- Links added with the `navigationLinks` option are now moved into the pull out navigation on mobile displays, #2548.
- `@license` and `@import` comments will be ignored at the top of files, #2552.

### Thanks!

Expand Down
7 changes: 4 additions & 3 deletions scripts/testcase.js
Expand Up @@ -37,8 +37,8 @@ function guessExtension(code) {
}

async function main() {
if (process.argv.length !== 3) {
console.log("Usage: node scripts/testcase.js <issue number>");
if (process.argv.length !== 3 && process.argv.length !== 4) {
console.log("Usage: node scripts/testcase.js <issue number> [lang]");
process.exit(1);
}

Expand All @@ -62,7 +62,8 @@ async function main() {
return;
}

const file = `src/test/converter2/issues/gh${issue}${guessExtension(code)}`;
const ext = process.argv[3] ? `.${process.argv[3]}` : guessExtension(code);
const file = `src/test/converter2/issues/gh${issue}${ext}`;
await writeFile(file, code.text);
await exec(`code ${file} src/test/issues.c2.test.ts`);
}
Expand Down
12 changes: 6 additions & 6 deletions src/lib/converter/comments/discovery.ts
Expand Up @@ -110,7 +110,7 @@ export interface DiscoveredComment {
jsDoc: ts.JSDoc | undefined;
}

export function discoverFileComment(
export function discoverFileComments(
node: ts.SourceFile,
commentStyle: CommentStyle,
) {
Expand All @@ -120,17 +120,17 @@ export function discoverFileComment(
ts.getLeadingCommentRanges(text, node.pos),
);

const selectedDocComment = comments.find((ranges) =>
const selectedDocComments = comments.filter((ranges) =>
permittedRange(text, ranges, commentStyle),
);

if (selectedDocComment) {
return selectedDocComments.map((ranges) => {
return {
file: node,
ranges: selectedDocComment,
jsDoc: findJsDocForComment(node, selectedDocComment),
ranges,
jsDoc: findJsDocForComment(node, ranges),
};
}
});
}

export function discoverNodeComment(
Expand Down
35 changes: 26 additions & 9 deletions src/lib/converter/comments/index.ts
Expand Up @@ -9,7 +9,7 @@ import { lexBlockComment } from "./blockLexer";
import {
DiscoveredComment,
discoverComment,
discoverFileComment,
discoverFileComments,
discoverNodeComment,
discoverSignatureComment,
} from "./discovery";
Expand Down Expand Up @@ -143,8 +143,12 @@ export function getComment(
);
}

const sf = declarations.find(ts.isSourceFile);
if (sf) {
return getFileComment(sf, config, logger, commentStyle, checker);
}

const isModule = declarations.some((decl) => {
if (ts.isSourceFile(decl)) return true;
if (ts.isModuleDeclaration(decl) && ts.isStringLiteral(decl.name)) {
return true;
}
Expand Down Expand Up @@ -196,13 +200,26 @@ export function getFileComment(
commentStyle: CommentStyle,
checker: ts.TypeChecker | undefined,
): Comment | undefined {
return getCommentImpl(
discoverFileComment(file, commentStyle),
config,
logger,
/* moduleComment */ true,
checker,
);
for (const commentSource of discoverFileComments(file, commentStyle)) {
const comment = getCommentWithCache(
commentSource,
config,
logger,
checker,
);

if (comment?.getTag("@license") || comment?.getTag("@import")) {
continue;
}

if (
comment?.getTag("@module") ||
comment?.hasModifier("@packageDocumentation")
) {
return comment;
}
return;
}
}

function getConstructorParamPropertyComment(
Expand Down
2 changes: 2 additions & 0 deletions src/lib/utils/options/tsdoc-defaults.ts
Expand Up @@ -28,6 +28,8 @@ export const blockTags = [
"@prop",
"@property",
"@satisfies",
"@license",
"@import",
] as const;

export const tsdocInlineTags = ["@link", "@inheritDoc", "@label"] as const;
Expand Down
16 changes: 16 additions & 0 deletions src/test/converter2/issues/gh2552.js
@@ -0,0 +1,16 @@
/**
* Summary
* @license MIT
*
* Full permission notice.
*/

// TS 5.5 @import comments
/** @import * as ts from "typescript" */

/**
* This is an awesome module.
* @module good-module
*/

export function goodFunction() {}
8 changes: 8 additions & 0 deletions src/test/issues.c2.test.ts
Expand Up @@ -1430,4 +1430,12 @@ describe("Issue Tests", () => {
equal(cb2.type?.type, "reflection");
equal(cb2.type.declaration.signatures![0].comment, undefined);
});

it("Ignores @license and @import comments at the top of the file, #2552", () => {
const project = convert();
equal(
Comment.combineDisplayParts(project.comment?.summary),
"This is an awesome module.",
);
});
});
9 changes: 9 additions & 0 deletions tsdoc.json
@@ -1,5 +1,6 @@
{
"$schema": "https://developer.microsoft.com/json-schemas/tsdoc/v0/tsdoc.schema.json",
// If updating this, also update tsdoc-defaults.ts
"noStandardTags": false,
"tagDefinitions": [
{
Expand Down Expand Up @@ -93,6 +94,14 @@
"tagName": "@satisfies",
"syntaxKind": "block"
},
{
"tagName": "@license",
"syntaxKind": "block"
},
{
"tagName": "@import",
"syntaxKind": "block"
},
{
"tagName": "@overload",
"syntaxKind": "modifier"
Expand Down

0 comments on commit 95bf5d0

Please sign in to comment.