Skip to content

Commit

Permalink
feat: add jsdocSingleLineComment to options
Browse files Browse the repository at this point in the history
issue: #93
  • Loading branch information
hosseinmd committed Feb 25, 2021
1 parent 2a377f2 commit f755db1
Show file tree
Hide file tree
Showing 20 changed files with 136 additions and 57 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ module.exports = {

## Ignore

To ignore prettier use `/* */` or `//` instead of /** */
To ignore prettier use `/* */` or `//` instead of /\*\* \*/

## Examples

Expand Down Expand Up @@ -180,6 +180,7 @@ Description is formatting as Markdown, so you could use any features of Markdown
| jsdocDescriptionTag | Boolean | false |
| jsdocVerticalAlignment | Boolean | false |
| jsdocKeepUnParseAbleExampleIndent | Boolean | false |
| jsdocSingleLineComment | Boolean | true |

Full up to date list and description of options can be found in Prettier help. First install plugin then run Prettier with "--help" option.

Expand Down
4 changes: 2 additions & 2 deletions src/descriptionFormatter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { format } from "prettier";
import { DESCRIPTION, EXAMPLE, TODO } from "./tags";
import { JsdocOptions } from "./types";
import { AllOptions } from "./types";
import { capitalizer } from "./utils";

const EMPTY_LINE_SIGNATURE = "2@^5!~#sdE!_EMPTY_LINE_SIGNATURE";
Expand Down Expand Up @@ -48,7 +48,7 @@ interface FormatOptions {
function formatDescription(
tag: string,
text: string,
options: JsdocOptions,
options: AllOptions,
formatOptions: FormatOptions = {},
): string {
if (!text) return text;
Expand Down
21 changes: 18 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,70 @@ import { getParser } from "./parser";
import parserBabel from "prettier/parser-babel";
import parserFlow from "prettier/parser-flow";
import parserTypescript from "prettier/parser-typescript";
import prettier from "prettier";
import prettier, { SupportOption } from "prettier";
import { JsdocOptions } from "./types";

const options = {
const options: Record<keyof JsdocOptions, SupportOption> = {
jsdocParser: {
name: "jsdocParser",
type: "boolean",
category: "jsdoc",
default: true,
description: "Format with jsdoc if is true",
},
jsdocSpaces: {
name: "jsdocSpaces",
type: "int",
category: "jsdoc",
default: 1,
description: "How many spaces will be used to separate tag elements.",
},
jsdocDescriptionWithDot: {
name: "jsdocDescriptionWithDot",
type: "boolean",
category: "jsdoc",
default: false,
description: "Should dot be inserted at the end of description",
},
jsdocDescriptionTag: {
name: "jsdocDescriptionTag",
type: "boolean",
category: "jsdoc",
default: false,
description: "Should description tag be used",
},
jsdocVerticalAlignment: {
name: "jsdocVerticalAlignment",
type: "boolean",
category: "jsdoc",
default: false,
description: "Should tags, types, names and description be aligned",
},
jsdocKeepUnParseAbleExampleIndent: {
name: "jsdocKeepUnParseAbleExampleIndent",
type: "boolean",
category: "jsdoc",
default: false,
description:
"Should unParseAble example (pseudo code or no js code) keep its indentation",
},
jsdocSingleLineComment: {
name: "jsdocSingleLineComment",
type: "boolean",
category: "jsdoc",
default: true,
description: "Should compact single line comment",
},
};

const defaultOptions = {
const defaultOptions: JsdocOptions = {
jsdocParser: true,
jsdocSpaces: 1,
jsdocDescriptionWithDot: false,
jsdocDescriptionTag: false,
jsdocVerticalAlignment: false,
jsdocKeepUnParseAbleExampleIndent: false,
jsdocSingleLineComment: true,
};

const languages = prettier
Expand Down
11 changes: 7 additions & 4 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
TAGS_TYPELESS,
TAGS_VERTICALLY_ALIGN_ABLE,
} from "./roles";
import { AST, JsdocOptions, PrettierComment, Token } from "./types";
import { AST, AllOptions, PrettierComment, Token } from "./types";
import { stringify } from "./stringify";
import { Parser } from "prettier";
import { SPACE_TAG_DATA } from "./tags";
Expand All @@ -27,7 +27,7 @@ export const getParser = (parser: Parser["parse"]) =>
function jsdocParser(
text: string,
parsers: Parameters<Parser["parse"]>[1],
options: JsdocOptions,
options: AllOptions,
): AST {
const ast = parser(text, parsers, options) as AST;

Expand Down Expand Up @@ -201,7 +201,10 @@ export const getParser = (parser: Parser["parse"]) =>
comment.value = comment.value.trimEnd();

if (comment.value) {
comment.value = addStarsToTheBeginningOfTheLines(comment.value);
comment.value = addStarsToTheBeginningOfTheLines(
comment.value,
options,
);
}

if (eol === "cr") {
Expand All @@ -225,7 +228,7 @@ function isBlockComment(comment: PrettierComment): boolean {
function getIndentationWidth(
comment: PrettierComment,
text: string,
options: JsdocOptions,
options: AllOptions,
): number {
const line = text.split(/\r\n?|\n/g)[comment.loc.start.line - 1];

Expand Down
4 changes: 2 additions & 2 deletions src/stringify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import {
TAGS_PEV_FORMATE_DESCRIPTION,
TAGS_VERTICALLY_ALIGN_ABLE,
} from "./roles";
import { JsdocOptions } from "./types";
import { AllOptions } from "./types";

const stringify = (
{ name, description, type, tag }: Spec,
tagIndex: number,
finalTagsArray: Spec[],
options: JsdocOptions,
options: AllOptions,
maxTagTitleLength: number,
maxTagTypeNameLength: number,
maxTagNameLength: number,
Expand Down
8 changes: 6 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { ParserOptions } from "prettier";

export type JsdocOptions = {
export interface JsdocOptions {
jsdocSpaces: number;
jsdocDescriptionWithDot: boolean;
jsdocDescriptionTag: boolean;
jsdocVerticalAlignment: boolean;
jsdocKeepUnParseAbleExampleIndent: boolean;
jsdocParser: boolean;
} & ParserOptions;
/** default is true */
jsdocSingleLineComment: boolean;
}

export interface AllOptions extends ParserOptions, JsdocOptions {}

type LocationDetails = { line: number; column: number };
type Location = { start: LocationDetails; end: LocationDetails };
Expand Down
12 changes: 9 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { format, Options } from "prettier";
import { Token } from "./types";
import { AllOptions, Token } from "./types";
import BSearch from "binary-search-bounds";

function convertToModernType(oldType: string): string {
Expand Down Expand Up @@ -118,8 +118,14 @@ function formatType(type: string, options?: Options): string {
}
}

function addStarsToTheBeginningOfTheLines(comment: string): string {
if (numberOfAStringInString(comment.trim(), "\n") === 0) {
function addStarsToTheBeginningOfTheLines(
comment: string,
options: AllOptions,
): string {
if (
options.jsdocSingleLineComment &&
numberOfAStringInString(comment.trim(), "\n") === 0
) {
return `* ${comment.trim()} `;
}

Expand Down
23 changes: 23 additions & 0 deletions tests/__snapshots__/main.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,29 @@ exports[`Should convert to single line if necessary 3`] = `
"
`;

exports[`Should convert to single multiLine 1`] = `
"/**
* Single line description
*/
"
`;

exports[`Should convert to single multiLine 2`] = `
"/**
* Single line description
*/
"
`;

exports[`Should convert to single multiLine 3`] = `
"/**
* Single line description
*
* @returns {Boolean} Always true
*/
"
`;

exports[`Should format jsDoc default values 1`] = `
"/**
* @param {String} [arg1=\\"defaultTest\\"] Foo. Default is \`\\"defaultTest\\"\`
Expand Down
6 changes: 3 additions & 3 deletions tests/dottedNames.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import prettier from "prettier";
import { JsdocOptions } from "../src/types";
import { AllOptions } from "../src/types";

function subject(code: string, options: Partial<JsdocOptions> = {}) {
function subject(code: string, options: Partial<AllOptions> = {}) {
return prettier.format(code, {
plugins: ["."],
parser: "babel",
jsdocSpaces: 1,
...options,
} as JsdocOptions);
} as AllOptions);
}

test("dotted names function param", () => {
Expand Down
6 changes: 3 additions & 3 deletions tests/exampleTag.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import prettier from "prettier";
import { JsdocOptions } from "../src/types";
import { AllOptions } from "../src/types";

function subject(code: string, options: Partial<JsdocOptions> = {}) {
function subject(code: string, options: Partial<AllOptions> = {}) {
return prettier.format(code, {
parser: "babel",
plugins: ["."],
jsdocSpaces: 1,
...options,
} as JsdocOptions);
} as AllOptions);
}

test("Example javascript code", () => {
Expand Down
11 changes: 4 additions & 7 deletions tests/files.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import prettier from "prettier";
import { readFileSync } from "fs";
import { resolve } from "path";
import { JsdocOptions } from "../src/types";
import { AllOptions } from "../src/types";

require("jest-specific-snapshot");

function subjectFiles(
relativePath: string,
options: Partial<JsdocOptions> = {},
) {
function subjectFiles(relativePath: string, options: Partial<AllOptions> = {}) {
const filepath = resolve(__dirname, relativePath);

try {
Expand All @@ -20,7 +17,7 @@ function subjectFiles(
trailingComma: "all",
filepath,
...options,
} as JsdocOptions);
} as AllOptions);
} catch (error) {
console.error(error);
}
Expand All @@ -35,7 +32,7 @@ function subjectFiles(
*/
const files: {
name: string;
options?: Partial<JsdocOptions>;
options?: Partial<AllOptions>;
}[] = [
{ name: "typeScript.js" },
{ name: "typeScript.js" },
Expand Down
6 changes: 3 additions & 3 deletions tests/jsdocParserDisable.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import prettier from "prettier";
import { JsdocOptions } from "../src/types";
import { AllOptions } from "../src/types";

function subject(code: string, options: Partial<JsdocOptions> = {}) {
function subject(code: string, options: Partial<AllOptions> = {}) {
return prettier.format(code, {
plugins: ["."],
parser: "babel",
jsdocParser: false,
jsdocSpaces: 1,
...options,
} as JsdocOptions);
} as AllOptions);
}

test("template for callback", () => {
Expand Down
36 changes: 33 additions & 3 deletions tests/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import prettier from "prettier";
import { JsdocOptions } from "../src/types";
import { AllOptions } from "../src/types";

function subject(code: string, options: Partial<JsdocOptions> = {}) {
function subject(code: string, options: Partial<AllOptions> = {}) {
return prettier.format(code, {
parser: "babel",
plugins: ["."],
jsdocSpaces: 1,
...options,
} as JsdocOptions);
} as AllOptions);
}

test("JS code should be formatted as usuall", () => {
Expand Down Expand Up @@ -85,6 +85,36 @@ test("Should convert to single line if necessary", () => {
expect(Result3).toMatchSnapshot();
});

test("Should convert to single multiLine", () => {
const Result1 = subject(`/** single line description*/`, {
jsdocSingleLineComment: false,
});
const Result2 = subject(
subject(`/**
* single line description
* @example
*/`),
{
jsdocSingleLineComment: false,
},
);

const Result3 = subject(
`/**
* single line description
* @return {Boolean} Always true
* @example
*/`,
{
jsdocSingleLineComment: false,
},
);

expect(Result1).toMatchSnapshot();
expect(Result2).toMatchSnapshot();
expect(Result3).toMatchSnapshot();
});

test(" undefined|null|void type", () => {
const Result1 = subject(`/**
* @return {undefined}
Expand Down
6 changes: 3 additions & 3 deletions tests/modern.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import prettier from "prettier";
import { JsdocOptions } from "../src/types";
import { AllOptions } from "../src/types";

function subject(code: string, options: Partial<JsdocOptions> = {}) {
function subject(code: string, options: Partial<AllOptions> = {}) {
return prettier.format(code, {
plugins: ["."],
parser: "typescript",
jsdocSpaces: 1,
...options,
} as JsdocOptions);
} as AllOptions);
}

test("convert array to modern type", () => {
Expand Down

0 comments on commit f755db1

Please sign in to comment.