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

fix: prettier run on newText #353

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/silver-books-yell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@xstate/cli': patch
'@xstate/tools-shared': patch
---

feat: import statement used to determine quote style
40 changes: 30 additions & 10 deletions apps/cli/src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,26 @@ function getPrettierInstance(cwd: string): typeof import('prettier') {
}
}

// Used to prettify text before writing
const prettify = async (
uri: string,
text: string,
{ cwd }: { cwd: string },
) => {
const prettierInstance = getPrettierInstance(cwd);
return prettierInstance.format(text, {
...(await prettierInstance.resolveConfig(uri)),
parser: 'typescript',
});
};

const writeToTypegenFile = async (
typegenUri: string,
types: TypegenData[],
{ cwd }: { cwd: string },
) => {
const prettierInstance = getPrettierInstance(cwd);
await fs.writeFile(
typegenUri,
// // Prettier v3 returns a promise
await prettierInstance.format(getTypegenOutput(types), {
...(await prettierInstance.resolveConfig(typegenUri)),
parser: 'typescript',
}),
);
const output = await prettify(typegenUri, getTypegenOutput(types), { cwd });
await fs.writeFile(typegenUri, output);
};

// TODO: just use the native one when support for node 12 gets dropped
Expand Down Expand Up @@ -88,6 +94,20 @@ const writeToFiles = async (uriArray: string[], { cwd }: { cwd: string }) => {
return;
}

// Find the first import statement and check what quote style it uses
// We will consider that the quote style to use for our import statement
const quoteMatch = fileContents.match(/import\s(.|\n)*?(['"])/);
// Default quote style to "
let quoteStyle = '"';
if (quoteMatch !== null && quoteMatch.length > 2) {
// Get the second group from the match
quoteStyle = quoteMatch[2];
// Ensure that the quote is either a ' or "
if (quoteStyle !== '"' && quoteStyle !== "'") {
quoteStyle = '"';
}
}

const typegenUri =
uri.slice(0, -path.extname(uri).length) + '.typegen.ts';

Expand All @@ -108,7 +128,7 @@ const writeToFiles = async (uriArray: string[], { cwd }: { cwd: string }) => {
await removeFile(typegenUri);
}

const edits = getTsTypesEdits(types);
const edits = getTsTypesEdits(types, quoteStyle);
if (edits.length > 0) {
const newFile = processFileEdits(fileContents, edits);
await fs.writeFile(uri, newFile);
Expand Down
8 changes: 4 additions & 4 deletions packages/shared/src/getTsTypesEdits.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { TypegenData } from './getTypegenData';

export const getTsTypesEdits = (types: TypegenData[]) =>
export const getTsTypesEdits = (types: TypegenData[], quoteStyle = '"') =>
types
.filter(
(type) =>
type =>
!type.typesNode.value ||
type.typesNode.value.argument !== type.data.tsTypesValue.argument ||
type.typesNode.value.qualifier !== type.data.tsTypesValue.qualifier,
)
.map((type) => ({
.map(type => ({
range: type.typesNode.range,
newText: `{} as import("${type.data.tsTypesValue.argument}").${type.data.tsTypesValue.qualifier}`,
newText: `{} as import(${quoteStyle}${type.data.tsTypesValue.argument}${quoteStyle}).${type.data.tsTypesValue.qualifier}`,
}));