Skip to content

Commit

Permalink
fix: tag group
Browse files Browse the repository at this point in the history
issue: #191
  • Loading branch information
hosseinmd committed Jun 17, 2023
1 parent 60776a4 commit afd080b
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 14 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,5 @@ dist/

# TernJS port file
.tern-port

.DS_STORE
37 changes: 23 additions & 14 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ import {
findPluginByParser,
isDefaultTag,
} from "./utils";
import { DESCRIPTION, PARAM, RETURNS, DEFAULT, DEFAULT_VALUE } from "./tags";
import {
DESCRIPTION,
PARAM,
RETURNS,
DEFAULT,
DEFAULT_VALUE,
EXAMPLE,
} from "./tags";
import {
TAGS_DESCRIPTION_NEEDED,
TAGS_GROUP_HEAD,
Expand All @@ -28,7 +35,7 @@ const {
name: nameTokenizer,
tag: tagTokenizer,
type: typeTokenizer,
description: descriptionTokenizer
description: descriptionTokenizer,
} = tokenizers;

/** @link https://prettier.io/docs/en/api.html#custom-parser-api} */
Expand Down Expand Up @@ -86,11 +93,11 @@ export const getParser = (originalParse: Parser["parse"], parserName: string) =>
return spec;
}

return typeTokenizer('preserve')(spec);
return typeTokenizer("preserve")(spec);
},
nameTokenizer(),
descriptionTokenizer('preserve')
]
descriptionTokenizer("preserve"),
],
})[0];

comment.value = "";
Expand Down Expand Up @@ -191,8 +198,8 @@ export const getParser = (originalParse: Parser["parse"], parserName: string) =>
const prevTag = tags[index - 1];
if (
prevTag &&
!prevTag.description?.includes("\n") &&
prevTag.tag !== DESCRIPTION &&
prevTag.tag !== EXAMPLE &&
prevTag.tag !== SPACE_TAG_DATA.tag &&
tag.tag !== SPACE_TAG_DATA.tag &&
prevTag.tag !== tag.tag
Expand Down Expand Up @@ -562,18 +569,20 @@ function assignOptionalAndDefaultToName({
description,
...rest
}: Spec): Spec {

if (isDefaultTag(tag)) {
const usefulSourceLine = source.find(x => x.source.includes(`@${tag}`))?.source || ''
const usefulSourceLine =
source.find((x) => x.source.includes(`@${tag}`))?.source || "";

const tagMatch = usefulSourceLine.match(/@default(Value)? (\[.*]|{.*}|\(.*\)|'.*'|".*"|`.*`|[A-z0-9_]+)( (.+))?/)
const tagValue = tagMatch?.[2] || ''
const tagDescription = tagMatch?.[4] || ''
const tagMatch = usefulSourceLine.match(
/@default(Value)? (\[.*]|{.*}|\(.*\)|'.*'|".*"|`.*`|[A-z0-9_]+)( (.+))?/,
);
const tagValue = tagMatch?.[2] || "";
const tagDescription = tagMatch?.[4] || "";

if (tagMatch) {
type = tagValue
name = ''
description = tagDescription
type = tagValue;
name = "";
description = tagDescription;
}
} else if (optional) {
if (name) {
Expand Down
1 change: 1 addition & 0 deletions tests/__snapshots__/files/prism-dependencies.js.shot
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ var getLoader = (function () {
*
* If a component is in this list, then all of its requirements will also be assumed to be in the list. Default is
* \`[]\`
*
* @returns {Loader}
*
*
Expand Down
25 changes: 25 additions & 0 deletions tests/__snapshots__/tagGroup.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Tag group 1`] = `
"/**
* Aliquip ex proident tempor eiusmod aliquip amet. Labore commodo nulla
* tempor consequat exercitation incididunt non. Duis laboris reprehenderit
* proident proident.
*
* @example
* const foo = 0;
* const a = \\"\\";
* const b = \\"\\";
*
* @param id A test id.
*
* @returns Minim sit a.
*
* @throws Minim sit ad commodo ut dolore magna magna minim consequat. Ex
* consequat esse incididunt qui voluptate id voluptate quis ex et. Ullamco
* cillum nisi amet fugiat.
*
* @see {@link http://acme.com}
*/
"
`;
38 changes: 38 additions & 0 deletions tests/tagGroup.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import prettier from "prettier";
import { AllOptions } from "../src/types";

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

test("Tag group", () => {
const result = subject(
`
/**
* Aliquip ex proident tempor eiusmod aliquip amet. Labore commodo nulla tempor
* consequat exercitation incididunt non. Duis laboris reprehenderit proident
* proident.
* @see {@link http://acme.com}
* @example
* const foo = 0;
* const a = "";
* const b = "";
*
* @param id A test id.
* @throws Minim sit ad commodo ut dolore magna magna minim consequat. Ex
* consequat esse incididunt qui voluptate id voluptate quis ex et. Ullamco
* cillum nisi amet fugiat.
* @return Minim sit a.
*/
`,
{
jsdocSeparateTagGroups: true,
},
);

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

0 comments on commit afd080b

Please sign in to comment.