11import * as prettier from "prettier" ;
22import { AllOptions } from "../src/types" ;
3+ import { execSync } from "child_process" ;
4+ import { mkdirSync , writeFileSync , readFileSync , rmSync } from "fs" ;
5+ import { join } from "path" ;
36
47function subject ( code : string , options : Partial < AllOptions > = { } ) {
58 return prettier . format ( code , {
@@ -90,7 +93,7 @@ test("Should convert to single line if necessary", async () => {
9093test ( "Should compatible with tailwindcss" , async ( ) => {
9194 const code = `
9295/**
93- * @param {String} [arg1="defaultTest"] foo
96+ * @param {String} [arg1="defaultTest"] foo
9497* @param {number} [arg2=123] the width of the rectangle
9598* @param {number} [arg3= 123 ]
9699* @param {number} [arg4= Foo.bar.baz ]
@@ -101,3 +104,134 @@ test("Should compatible with tailwindcss", async () => {
101104
102105 expect ( result ) . toMatchSnapshot ( ) ;
103106} ) ;
107+
108+ describe ( "CLI Compatibility" , ( ) => {
109+ // CLI-based tests
110+ const testDir = join ( process . cwd ( ) , ".test-cli-temp" ) ;
111+
112+ beforeAll ( ( ) => {
113+ try {
114+ mkdirSync ( testDir , { recursive : true } ) ;
115+ } catch ( err ) {
116+ // Directory might already exist
117+ }
118+ } ) ;
119+
120+ afterAll ( ( ) => {
121+ try {
122+ rmSync ( testDir , { recursive : true , force : true } ) ;
123+ } catch ( err ) {
124+ // Ignore cleanup errors
125+ }
126+ } ) ;
127+
128+ function runCommand ( command : string ) : string {
129+ return execSync ( `cd ${ testDir } && ${ command } ` , {
130+ timeout : 10000 ,
131+ encoding : "utf8" ,
132+ cwd : process . cwd ( ) ,
133+ } ) ;
134+ }
135+
136+ test ( "Should format with tailwindcss plugin via CLI without infinite recursion" , ( ) => {
137+ const testFile = join ( testDir , "test-cli-tailwind.js" ) ;
138+ const configFile = join ( testDir , ".prettierrc.json" ) ;
139+
140+ const code = `/**
141+ * @param {String|Number} text - some text description
142+
143+
144+
145+ * @param {String} [defaultValue="defaultTest"] TODO
146+ * @returns {Boolean} Description for returns
147+ */
148+ const testFunction = (text, defaultValue) => true;
149+ ` ;
150+
151+ const prettierConfig = {
152+ semi : false ,
153+ tabWidth : 2 ,
154+ printWidth : 100 ,
155+ singleQuote : true ,
156+ plugins : [ "prettier-plugin-tailwindcss" , "prettier-plugin-jsdoc" ] ,
157+ } ;
158+
159+ writeFileSync ( testFile , code ) ;
160+ writeFileSync ( configFile , JSON . stringify ( prettierConfig , null , 2 ) ) ;
161+
162+ const output = runCommand (
163+ `npx prettier --config ".prettierrc.json" "test-cli-tailwind.js"` ,
164+ ) ;
165+
166+ expect ( output ) . toBeDefined ( ) ;
167+ expect ( output ) . toMatchSnapshot ( ) ;
168+ } ) ;
169+
170+ test ( "Should format via CLI with --write flag" , ( ) => {
171+ const testFile = join ( testDir , "test-cli-write.js" ) ;
172+ const configFile = join ( testDir , ".prettierrc.json" ) ;
173+
174+ const code = `/**
175+ * @param {number} [arg1=123] the width
176+
177+
178+
179+ * @returns {void}
180+ */
181+ function myFunc(arg1) {}
182+ ` ;
183+
184+ const prettierConfig = {
185+ semi : false ,
186+ plugins : [ "prettier-plugin-tailwindcss" , "prettier-plugin-jsdoc" ] ,
187+ } ;
188+
189+ writeFileSync ( testFile , code ) ;
190+ writeFileSync ( configFile , JSON . stringify ( prettierConfig , null , 2 ) ) ;
191+
192+ runCommand (
193+ `npx prettier --config ".prettierrc.json" --write "test-cli-write.js"` ,
194+ ) ;
195+
196+ const formatted = readFileSync ( testFile , "utf8" ) ;
197+ expect ( formatted ) . toMatchSnapshot ( ) ;
198+ } ) ;
199+
200+ test ( "Should work with plugins in different orders via CLI" , ( ) => {
201+ const testFile = join ( testDir , "test-cli-order.ts" ) ;
202+ const configFile = join ( testDir , ".prettierrc.json" ) ;
203+
204+ const code = `/**
205+ * @param {string} name
206+
207+
208+
209+
210+ * @returns {Promise<void>}
211+ */
212+ async function example(name: string): Promise<void> {}
213+ ` ;
214+
215+ const configs = [
216+ [ "prettier-plugin-jsdoc" , "prettier-plugin-tailwindcss" ] ,
217+ [ "prettier-plugin-tailwindcss" , "prettier-plugin-jsdoc" ] ,
218+ ] ;
219+
220+ configs . forEach ( ( plugins ) => {
221+ const prettierConfig = {
222+ parser : "typescript" ,
223+ plugins,
224+ } ;
225+
226+ writeFileSync ( testFile , code ) ;
227+ writeFileSync ( configFile , JSON . stringify ( prettierConfig , null , 2 ) ) ;
228+
229+ const output = runCommand (
230+ `npx prettier --config ".prettierrc.json" "test-cli-order.ts"` ,
231+ ) ;
232+
233+ expect ( output ) . toBeDefined ( ) ;
234+ expect ( output ) . toMatchSnapshot ( ) ;
235+ } ) ;
236+ } ) ;
237+ } ) ;
0 commit comments