Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prettier v3 support #27

Closed
wants to merge 4 commits into from
Closed

Prettier v3 support #27

wants to merge 4 commits into from

Conversation

electrovir
Copy link
Owner

Trying to add support for Prettier v3 #26

@electrovir electrovir self-assigned this Jul 23, 2023
@electrovir
Copy link
Owner Author

Quick test command for debugging: npm test src/test/typescript.test.ts -- --parallel=false

} else if (debug) {
console.info('concat child doc was not a line break');
}
// } else if (isDocCommand(currentDoc) && currentDoc.type === 'concat') {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where currentDoc.type === 'concat' was true in Prettier v2, in v3 it's just an array (according to the changelog docs here and here)

Copy link
Contributor

@richardsimko richardsimko Sep 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

diff --git a/src/augments/doc.ts b/src/augments/doc.ts
index e7de548..4cf38e3 100644
--- a/src/augments/doc.ts
+++ b/src/augments/doc.ts
@@ -38,3 +38,7 @@ export function isDocCommand(
 ): inputDoc is doc.builders.DocCommand {
     return !!inputDoc && typeof inputDoc !== 'string' && !Array.isArray(inputDoc);
 }
+
+export function isDocArray(inputDoc: Doc | undefined | null): inputDoc is Doc[] {
+    return Array.isArray(inputDoc);
+}
diff --git a/src/printer/insert-new-lines.ts b/src/printer/insert-new-lines.ts
index ccad3fe..8a866d5 100644
--- a/src/printer/insert-new-lines.ts
+++ b/src/printer/insert-new-lines.ts
@@ -1,7 +1,7 @@
 import {getObjectTypedKeys, PropertyValueType} from '@augment-vir/common';
 import {CallExpression, Node} from 'estree';
 import {AstPath, Doc, doc, ParserOptions} from 'prettier';
-import {isDocCommand, stringifyDoc} from '../augments/doc';
+import {isDocArray, isDocCommand, stringifyDoc} from '../augments/doc';
 import {MultilineArrayOptions} from '../options';
 import {walkDoc} from './child-docs';
 import {
@@ -92,13 +92,12 @@ function insertLinesIntoArray(
                 console.info({firstIndentContentsChild: startingLine});
             }
             if (!isDocCommand(startingLine) || startingLine.type !== 'line') {
-                // // todo: I think this needs to check for arrays now instead of concat
-                // if (isDocCommand(startingLine) && startingLine.type === 'concat') {
-                //     undoAllMutations();
-                //     return false;
-                // } else {
+                if (isDocArray(startingLine)) {
+                    undoAllMutations();
+                    return false;
+                } else {
                     throw new Error(`${found} first indent child was not a line.`);
-                // }
+                }
             }
             indentContents[0] = '';
             undoMutations.push(() => {
@@ -331,35 +330,35 @@ function insertLinesIntoArray(
                                     parentToMutate[siblingIndex] = commaSibling;
                                 });
                             }
-                        // } else if (isDocCommand(currentDoc) && currentDoc.type === 'concat') {
-                        //     const firstPart = currentDoc.parts[0];
-                        //     const secondPart = currentDoc.parts[1];
-                        //     if (debug) {
-                        //         console.info('got concat child doc');
-                        //         console.info(currentDoc.parts, {firstPart, secondPart});
-                        //         console.info(
-                        //             isDocCommand(firstPart),
-                        //             isDocCommand(secondPart),
-                        //             (firstPart as any).type === 'line',
-                        //             (firstPart as any).hard,
-                        //             (secondPart as any).type === 'break-parent',
-                        //         );
-                        //     }
-                        //     if (
-                        //         isDocCommand(firstPart) &&
-                        //         isDocCommand(secondPart) &&
-                        //         firstPart.type === 'line' &&
-                        //         firstPart.hard &&
-                        //         secondPart.type === 'break-parent'
-                        //     ) {
-                        //         if (debug) {
-                        //             console.info('concat child was indeed a line break');
-                        //         }
-                        //         forceFinalLineBreakExists = true;
-                        //         return false;
-                        //     } else if (debug) {
-                        //         console.info('concat child doc was not a line break');
-                        //     }
+                        } else if (isDocArray(currentDoc)) {
+                            const firstPart = currentDoc[0];
+                            const secondPart = currentDoc[1];
+                            if (debug) {
+                                console.info('got concat child doc');
+                                console.info(currentDoc, {firstPart, secondPart});
+                                console.info(
+                                    isDocCommand(firstPart),
+                                    isDocCommand(secondPart),
+                                    (firstPart as any).type === 'line',
+                                    (firstPart as any).hard,
+                                    (secondPart as any).type === 'break-parent',
+                                );
+                            }
+                            if (
+                                isDocCommand(firstPart) &&
+                                isDocCommand(secondPart) &&
+                                firstPart.type === 'line' &&
+                                firstPart.hard &&
+                                secondPart.type === 'break-parent'
+                            ) {
+                                if (debug) {
+                                    console.info('concat child was indeed a line break');
+                                }
+                                forceFinalLineBreakExists = true;
+                                return false;
+                            } else if (debug) {
+                                console.info('concat child doc was not a line break');
+                            }
                         }
                         return true;
                     },
@@ -464,7 +463,7 @@ export function printWithMultilineArrays(
             `Could not find valid root node in ${path.stack.map((entry) => entry.type).join(',')}`,
         );
     }
-    const node = path.getValue() as Node | undefined;
+    const node = path.getNode();
 
     if (
         node &&

richard.patch

My understanding is that this should fix it, but the tests fail because of the other issue so I'm not really able to validate if it's actually correct. This is based on my understanding of the docs, I have no previous experience in building Prettier plugins 😅

const options = args[1] as ParserOptions;
debugger;
const argsToUse = args[3] ? args : args.slice(0,3);
const originalOutput = originalPrinter.print.call(
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This call is not working anymore, for an unknown reason

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code that's throwing the error seems to be this:

https://github.com/prettier/prettier/blob/c0d464f3cfb56a0280d06941fdfff266f0ac89f7/src/language-js/print/angular.js#L14-L19

For some reason node in args[0] is indeed undefined. I'm not quite sure what args[0] actually is, it seems to be an instance of AstPath, but perhaps this can give some clue as to what might be the issue? I'll dig a bit more.

@electrovir electrovir added the help wanted Extra attention is needed label Sep 7, 2023
@tthornton3-chwy
Copy link
Contributor

#28 should hopefully pick up where this left off :) Thanks @electrovir and @richardsimko !

The biggest "gotcha" was they moved avoidAstMutation: true from within handleComments to the printer itself!

@electrovir
Copy link
Owner Author

closed in favor of #28

@electrovir electrovir closed this Oct 22, 2023
electrovir pushed a commit that referenced this pull request Oct 22, 2023
… concat checks with array checks

Provided in PR 27 in this comment:
#27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants