Skip to content

Commit

Permalink
Add a test for CRLF line endings
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit0 committed Jul 10, 2024
1 parent 21cf5ae commit e2051dc
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 21 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ trim_trailing_whitespace = true

[package.json]
indent_size = 2

[src/test/converter2/issues/gh2631/crlf.md]
end_of_line = crlf
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Disable core.autocrlf's line ending conversion behavior to prevent test failures on Windows
* text=auto eol=lf
src/test/converter2/issues/gh2631/crlf.md text=auto eol=crlf

*.png binary
*.psd binary
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
- `@link` tags will now be validated in referenced markdown documents, #2629.
- `@link` tags are now resolved in project documents, #2629.
- HTML/JSON output generated by TypeDoc now contains a trailing newline, #2632.
- TypeDoc now correctly handles markdown documents with CRLF line endings, #2628.
- `@hidden` is now properly applied when placed in a function implementation comment, #2634.
- Comments on re-exports are now rendered.

### Thanks!

- @bukowa
- @garrett-hopper

## v0.26.3 (2024-06-28)
Expand Down
26 changes: 6 additions & 20 deletions src/lib/converter/comments/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,42 +203,28 @@ export function parseCommentString(
// Check for frontmatter
let frontmatterData: Record<string, unknown> = {};
const firstBlock = content[0];

let lineBreak: string;
switch (firstBlock.text.startsWith("---\r\n")) {
case true:
lineBreak = "\r\n";
break;
case false:
lineBreak = "\n";
break;
}

if (firstBlock.text.startsWith(`---${lineBreak}`)) {
const end = firstBlock.text.indexOf(`${lineBreak}---${lineBreak}`);
if (firstBlock.text.startsWith("---\n")) {
const end = firstBlock.text.indexOf("\n---\n");
if (end !== -1) {
const yamlText = firstBlock.text.slice(
`---${lineBreak}`.length,
end,
);
const yamlText = firstBlock.text.slice("---\n".length, end);
firstBlock.text = firstBlock.text
.slice(end + `${lineBreak}---${lineBreak}`.length)
.slice(end + "\n---\n".length)
.trimStart();

const frontmatter = parseYamlDoc(yamlText, { prettyErrors: false });
for (const warning of frontmatter.warnings) {
// Can't translate issues coming from external library...
logger.warn(
warning.message as TranslatedString,
warning.pos[0] + `---${lineBreak}`.length,
warning.pos[0] + "---\n".length,
file,
);
}
for (const error of frontmatter.errors) {
// Can't translate issues coming from external library...
logger.error(
error.message as TranslatedString,
error.pos[0] + `---${lineBreak}`.length,
error.pos[0] + "---\n".length,
file,
);
}
Expand Down
12 changes: 11 additions & 1 deletion src/lib/utils/minimalSourceFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,20 @@ import { binaryFindPartition } from "./array";
const lineStarts = new WeakMap<MinimalSourceFile, number[]>();

export class MinimalSourceFile implements SourceFileLike {
readonly text: string;
constructor(
readonly text: string,
text: string,
readonly fileName: string,
) {
// This is unfortunate, but the yaml library we use relies on the source
// text using LF line endings https://github.com/eemeli/yaml/issues/127.
// If we don't do this, in a simple document which includes a single key
// like:
// ---<CR><LF>
// title: Windows line endings<CR><LF>
// ---<CR><LF>
// we'll end up with a parsed title of "Windows line endings\r"
this.text = text.replaceAll("\r\n", "\n");
lineStarts.set(this, [0]);
}

Expand Down
5 changes: 5 additions & 0 deletions src/test/converter2/issues/gh2631/crlf.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
title: "Windows Line Endings"
---

This file contains CRLF line endings
5 changes: 5 additions & 0 deletions src/test/converter2/issues/gh2631/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/**
* @module
* @document crlf.md
*/
export const a = 123;
8 changes: 8 additions & 0 deletions src/test/issues.c2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1637,6 +1637,14 @@ describe("Issue Tests", () => {
logger.expectNoOtherMessages();
});

it("#2631 handles CRLF line endings in frontmatter", () => {
const project = convert();
equal(
project.documents?.map((d) => d.name),
["Windows Line Endings"],
);
});

it("#2634 handles @hidden on function implementations", () => {
const project = convert();
equal(project.children?.map((c) => c.name) || [], []);
Expand Down

0 comments on commit e2051dc

Please sign in to comment.