-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: compatibility with other plugins
issue: #99
- Loading branch information
Showing
8 changed files
with
312 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
node_modules/ | ||
prettier-plugin-fake/ | ||
lib/ | ||
tests/files |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
const { parsers: typescriptParsers } = require("prettier/parser-typescript"); | ||
|
||
/** | ||
* | ||
* @param {*} text | ||
* @param {import("prettier/index").Options} options | ||
* @returns | ||
*/ | ||
const preprocess = (text, options) => { | ||
if ( | ||
options.plugins.find((plugin) => plugin.name === "prettier-plugin-fake") | ||
) { | ||
return `//prettier-plugin-fake\n${text}`; | ||
} | ||
return text; | ||
}; | ||
|
||
exports.parsers = { | ||
typescript: { | ||
...typescriptParsers.typescript, | ||
preprocess: typescriptParsers.typescript.preprocess | ||
? (text, options) => | ||
preprocess( | ||
typescriptParsers.typescript.preprocess(text, options), | ||
options, | ||
) | ||
: preprocess, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"name": "prettier-plugin-fake", | ||
"version": "1.1.1", | ||
"main": "index.js", | ||
"files": [ | ||
"index.js" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Should convert to single line if necessary 1`] = ` | ||
"//prettier-plugin-fake | ||
/** Single line description */ | ||
" | ||
`; | ||
|
||
exports[`Should convert to single line if necessary 2`] = ` | ||
"//prettier-plugin-fake | ||
/** Single line description */ | ||
" | ||
`; | ||
|
||
exports[`Should convert to single line if necessary 3`] = ` | ||
"//prettier-plugin-fake | ||
/** | ||
* Single line description | ||
* | ||
* @returns {Boolean} Always true | ||
*/ | ||
" | ||
`; | ||
|
||
exports[`Should format jsDoc default values 1`] = ` | ||
"//prettier-plugin-fake | ||
/** | ||
* @param {String} [arg1=\\"defaultTest\\"] Foo. Default is \`\\"defaultTest\\"\` | ||
* @param {number} [arg2=123] The width of the rectangle. Default is \`123\` | ||
* @param {number} [arg3=123] Default is \`123\` | ||
* @param {number} [arg4=Foo.bar.baz] Default is \`Foo.bar.baz\` | ||
* @param {number | string} [arg5=123] Something. Default is \`123\` | ||
*/ | ||
" | ||
`; | ||
|
||
exports[`Should format jsDoc default values 2`] = ` | ||
"//prettier-plugin-fake | ||
//prettier-plugin-fake | ||
/** | ||
* @param {String} [arg1=\\"defaultTest\\"] Foo. Default is \`\\"defaultTest\\"\` | ||
* @param {number} [arg2=123] The width of the rectangle. Default is \`123\` | ||
* @param {number} [arg3=123] Default is \`123\` | ||
* @param {number} [arg4=Foo.bar.baz] Default is \`Foo.bar.baz\` | ||
* @param {number | string} [arg5=123] Something. Default is \`123\` | ||
*/ | ||
" | ||
`; | ||
|
||
exports[`Should format regular jsDoc 1`] = ` | ||
"//prettier-plugin-fake | ||
import b from \\"b\\"; | ||
import { k } from \\"k\\"; | ||
import a from \\"a\\"; | ||
/** | ||
* Function example description that was wrapped by hand so it have more then | ||
* one line and don't end with a dot REPEATED TWO TIMES BECAUSE IT WAS EASIER to | ||
* copy function example description that was wrapped by hand so it have more | ||
* then one line. | ||
* | ||
* @async | ||
* @private | ||
* @memberof test | ||
* @example | ||
* //prettier-plugin-fake | ||
* var one = 5; | ||
* var two = 10; | ||
* | ||
* if (one > 2) { | ||
* two += one; | ||
* } | ||
* | ||
* @param {String | Number} text - Some text description that is very long and | ||
* needs to be wrapped | ||
* @param {String} [defaultValue=\\"defaultTest\\"] TODO. Default is \`\\"defaultTest\\"\` | ||
* @param {Number | Null} [optionalNumber] | ||
* @returns {Boolean} Description for @returns with s | ||
* @undefiendTag | ||
* @undefiendTag {number} name des | ||
*/ | ||
const testFunction = (text, defaultValue, optionalNumber) => true; | ||
" | ||
`; | ||
|
||
exports[`Should format regular jsDoc 2`] = ` | ||
"//prettier-plugin-fake | ||
//prettier-plugin-fake | ||
import b from \\"b\\"; | ||
import { k } from \\"k\\"; | ||
import a from \\"a\\"; | ||
/** | ||
* Function example description that was wrapped by hand so it have more then | ||
* one line and don't end with a dot REPEATED TWO TIMES BECAUSE IT WAS EASIER to | ||
* copy function example description that was wrapped by hand so it have more | ||
* then one line. | ||
* | ||
* @async | ||
* @private | ||
* @memberof test | ||
* @example | ||
* //prettier-plugin-fake | ||
* //prettier-plugin-fake | ||
* var one = 5; | ||
* var two = 10; | ||
* | ||
* if (one > 2) { | ||
* two += one; | ||
* } | ||
* | ||
* @param {String | Number} text - Some text description that is very long and | ||
* needs to be wrapped | ||
* @param {String} [defaultValue=\\"defaultTest\\"] TODO. Default is \`\\"defaultTest\\"\` | ||
* @param {Number | Null} [optionalNumber] | ||
* @returns {Boolean} Description for @returns with s | ||
* @undefiendTag | ||
* @undefiendTag {number} name des | ||
*/ | ||
const testFunction = (text, defaultValue, optionalNumber) => true; | ||
" | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import prettier from "prettier"; | ||
import { AllOptions } from "../src/types"; | ||
|
||
function subject(code: string, options: Partial<AllOptions> = {}) { | ||
return prettier.format(code, { | ||
parser: "typescript", | ||
plugins: ["prettier-plugin-fake", "prettier-plugin-jsdoc"], | ||
jsdocSpaces: 1, | ||
...options, | ||
} as AllOptions); | ||
} | ||
|
||
test("Should format regular jsDoc", () => { | ||
const result = subject(` | ||
import b from "b" | ||
import {k} from "k" | ||
import a from "a" | ||
/** | ||
* function example description that was wrapped by hand | ||
* so it have more then one line and don't end with a dot | ||
* REPEATED TWO TIMES BECAUSE IT WAS EASIER to copy | ||
* function example description that was wrapped by hand | ||
* so it have more then one line. | ||
* @return {Boolean} Description for @returns with s | ||
* @param {String|Number} text - some text description that is very long and needs to be wrapped | ||
* @param {String} [defaultValue="defaultTest"] TODO | ||
* @arg {Number|Null} [optionalNumber] | ||
* @private | ||
*@memberof test | ||
@async | ||
* @examples | ||
* var one = 5 | ||
* var two = 10 | ||
* | ||
* if(one > 2) { two += one } | ||
* @undefiendTag${" "} | ||
* @undefiendTag {number} name des | ||
*/ | ||
const testFunction = (text, defaultValue, optionalNumber) => true | ||
`); | ||
|
||
expect(result).toMatchSnapshot(); | ||
expect(subject(result)).toMatchSnapshot(); | ||
}); | ||
|
||
test("Should format jsDoc default values", () => { | ||
const result = subject(` | ||
/** | ||
* @param {String} [arg1="defaultTest"] foo | ||
* @param {number} [arg2=123] the width of the rectangle | ||
* @param {number} [arg3= 123 ] | ||
* @param {number} [arg4= Foo.bar.baz ] | ||
* @param {number|string} [arg5=123] Something. Default is \`"wrong"\` | ||
*/ | ||
`); | ||
|
||
expect(result).toMatchSnapshot(); | ||
expect(subject(result)).toMatchSnapshot(); | ||
}); | ||
|
||
test("Should convert to single line if necessary", () => { | ||
const Result1 = subject(`/** single line description*/`); | ||
const Result2 = subject(`/** | ||
* single line description | ||
* @example | ||
*/`); | ||
|
||
const Result3 = subject(`/** | ||
* single line description | ||
* @return {Boolean} Always true | ||
* @example | ||
*/`); | ||
|
||
expect(Result1).toMatchSnapshot(); | ||
expect(Result2).toMatchSnapshot(); | ||
expect(Result3).toMatchSnapshot(); | ||
}); |