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
Conversation
Quick test command for debugging: |
} else if (debug) { | ||
console.info('concat child doc was not a line break'); | ||
} | ||
// } else if (isDocCommand(currentDoc) && currentDoc.type === 'concat') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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 &&
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( |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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:
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.
#28 should hopefully pick up where this left off :) Thanks @electrovir and @richardsimko ! The biggest "gotcha" was they moved |
closed in favor of #28 |
… concat checks with array checks Provided in PR 27 in this comment: #27
Trying to add support for Prettier v3 #26