Skip to content

Commit

Permalink
fix: windows EOL now supported
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment authored and hosseinmd committed Feb 4, 2021
1 parent ccf2028 commit 4220471
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
addStarsToTheBeginningOfTheLines,
convertToModernType,
formatType,
detectEndOfLine,
} from "./utils";
import { DESCRIPTION } from "./tags";
import {
Expand Down Expand Up @@ -34,6 +35,10 @@ export const getParser = (parser: Parser["parse"]) =>
return ast;
}

const eol =
options.endOfLine === "auto" ? detectEndOfLine(text) : options.endOfLine;
options = { ...options, endOfLine: "lf" };

/**
* Control order of tags by weights. Smaller value brings tag higher.
*
Expand All @@ -55,7 +60,10 @@ export const getParser = (parser: Parser["parse"]) =>
/** Issue: https://github.com/hosseinmd/prettier-plugin-jsdoc/issues/18 */
comment.value = comment.value.replace(/^([*]+)/g, "*");

const commentString = `/*${comment.value}*/`;
// Create the full comment string with line ends normalized to \n
// This means that all following code can assume \n and should only use
// \n.
const commentString = `/*${comment.value.replace(/\r\n?/g, "\n")}*/`;

/**
* Check if this comment block is a JSDoc. Based on:
Expand Down Expand Up @@ -152,7 +160,7 @@ export const getParser = (parser: Parser["parse"]) =>
description = description
.trim()
.replace(/[ \t]*Default is `.*`\.?$/, "");
if (description && !/[.\r\n]$/.test(description)) {
if (description && !/[.\n]$/.test(description)) {
description += ".";
}
description += ` Default is \`${_default}\``;
Expand Down Expand Up @@ -229,6 +237,12 @@ export const getParser = (parser: Parser["parse"]) =>
if (comment.value) {
comment.value = addStarsToTheBeginningOfTheLines(comment.value);
}

if (eol === "cr") {
comment.value = comment.value.replace(/\n/g, "\r");
} else if (eol === "crlf") {
comment.value = comment.value.replace(/\n/g, "\r\n");
}
});

ast.comments = ast.comments.filter(
Expand Down
37 changes: 37 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,46 @@ function capitalizer(str: string): string {
return str[0].toUpperCase() + str.slice(1);
}

/**
* Detects the line ends of the given text.
*
* If multiple line ends are used, the most common one will be returned.
*
* If the given text is a single line, "lf" will be returned.
*
* @param text
*/
function detectEndOfLine(text: string): "cr" | "crlf" | "lf" {
const counter = {
"\r": 0,
"\r\n": 0,
"\n": 0,
};

const lineEndPattern = /\r\n?|\n/g;
let m;
while ((m = lineEndPattern.exec(text))) {
counter[m[0] as keyof typeof counter]++;
}

const cr = counter["\r"];
const crlf = counter["\r\n"];
const lf = counter["\n"];
const max = Math.max(cr, crlf, lf);

if (lf === max) {
return "lf";
} else if (crlf === max) {
return "crlf";
} else {
return "cr";
}
}

export {
convertToModernType,
formatType,
addStarsToTheBeginningOfTheLines,
capitalizer,
detectEndOfLine,
};
33 changes: 33 additions & 0 deletions tests/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -442,3 +442,36 @@ test("Format rest parameters properly", () => {

expect(result).toMatchSnapshot();
});

test("Line ends", () => {
const text = `
/**
* Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
* @param {Object} paramName param description that goes on and on and on until it will need to be wrapped
*
*/
function a(){}`;
const formatted = `/**
* Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
* tempor incididunt ut labore et dolore magna aliqua.
*
* @param {Object} paramName Param description that goes on and on and on
* until it will need to be wrapped
*/
function a() {}
`;

const text_crlf = text.replace(/\n/g, "\r\n");
const text_lf = text;
const formatted_crlf = formatted.replace(/\n/g, "\r\n");
const formatted_lf = formatted;

expect(subject(text_lf, { endOfLine: "crlf" })).toEqual(formatted_crlf);
expect(subject(text_lf, { endOfLine: "lf" })).toEqual(formatted_lf);

expect(subject(text_crlf, { endOfLine: "crlf" })).toEqual(formatted_crlf);
expect(subject(text_crlf, { endOfLine: "lf" })).toEqual(formatted_lf);

expect(subject(text_lf, { endOfLine: "auto" })).toEqual(formatted_lf);
expect(subject(text_crlf, { endOfLine: "auto" })).toEqual(formatted_crlf);
});

0 comments on commit 4220471

Please sign in to comment.