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

Support Prettier v3 #2947

Merged
merged 15 commits into from
Apr 21, 2023
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to the "prettier-vscode" extension will be documented in thi

<!-- Check [Keep a Changelog](https://keepachangelog.com/) for recommendations on how to structure this file. -->

## [9.12.0]
Copy link
Member

Choose a reason for hiding this comment

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

This should be a major release. 10.0.0

Copy link
Member

Choose a reason for hiding this comment

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

Maybe we should release v10 after Prettier releases v3? This PR is not breaking anything, just add support for v3 alpha.

Copy link
Member Author

Choose a reason for hiding this comment

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

@ntotten I agree with fisker. If you too can agree with this, please merge and release.

Copy link
Member

@fisker fisker Apr 20, 2023

Choose a reason for hiding this comment

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

Seems there is a release workflow. So I guess we can create a v9.12.0 tag to trigger release?

release:
runs-on: ubuntu-latest
needs: package
if: startsWith(github.ref, 'refs/tags/v')

But let's wait for ntotten to respond first.


- Support Prettier v3

## [9.11.0]

- Prettier 2.8.7
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"scripts": {
"clean": "node ./scripts/clean.js",
"lint": "eslint -c .eslintrc.js --ext .ts .",
"pretest": "yarn test-compile && cd test-fixtures/plugins && yarn install && cd ../plugins-pnpm && pnpm i && cd ../outdated && yarn install && cd ../module && yarn install && cd ../specific-version && yarn install && cd ../explicit-dep && yarn install && cd implicit-dep && yarn install",
"pretest": "yarn test-compile && cd test-fixtures/plugins && yarn install && cd ../plugins-pnpm && pnpm i && cd ../outdated && yarn install && cd ../module && yarn install && cd ../specific-version && yarn install && cd ../explicit-dep && yarn install && cd implicit-dep && yarn install && cd ../../v3 && yarn install",
"prettier": "prettier --write '**/*.{ts,json,md,hbs,yml,js}'",
"test-compile": "yarn clean && tsc -p ./ && yarn webpack",
"test": "node ./out/test/runTests.js",
Expand Down
2 changes: 1 addition & 1 deletion src/ModuleResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ export class ModuleResolver implements ModuleResolverInterface {
* Clears the module and config cache
*/
public async dispose() {
prettier.clearConfigCache();
await prettier.clearConfigCache();
this.path2Module.forEach((module) => {
try {
module.clearConfigCache();
Expand Down
10 changes: 7 additions & 3 deletions src/PrettierEditService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ export default class PrettierEditService implements Disposable {
prettierInstance: PrettierModule,
uri?: Uri
): Promise<ISelectors> => {
const { languages } = prettierInstance.getSupportInfo();
const { languages } = await prettierInstance.getSupportInfo();

languages.forEach((lang) => {
if (lang && lang.vscodeLanguageIds) {
Expand Down Expand Up @@ -442,7 +442,7 @@ export default class PrettierEditService implements Disposable {
this.loggingService.logWarning(
`Parser not inferred, trying VS Code language.`
);
const languages = prettierInstance.getSupportInfo().languages;
const { languages } = await prettierInstance.getSupportInfo();
parser = getParserFromLanguageId(languages, uri, languageId);
}

Expand All @@ -465,7 +465,11 @@ export default class PrettierEditService implements Disposable {
this.loggingService.logInfo("Prettier Options:", prettierOptions);

try {
const formattedText = prettierInstance.format(text, prettierOptions);
// Since Prettier v3, `format` returns Promise.
const formattedText = await prettierInstance.format(
text,
prettierOptions
);
this.statusBar.update(FormatterStatus.Success);

return formattedText;
Expand Down
2 changes: 1 addition & 1 deletion src/TemplateService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class TemplateService {
useTabs: settings.useTabs,
};

const templateSource = this.prettierModule.format(
const templateSource = await this.prettierModule.format(
JSON.stringify(settings, null, 2),
formatterOptions
);
Expand Down
2 changes: 1 addition & 1 deletion src/test/suite/format.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ async function formatSameAsPrettier(
},
};
const { actual, source } = await format("project", file);
const prettierFormatted = prettier.format(source, prettierOptions);
const prettierFormatted = await prettier.format(source, prettierOptions);
assert.equal(actual, prettierFormatted);
}

Expand Down
11 changes: 11 additions & 0 deletions src/test/suite/v3.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import * as assert from "assert";
import { format, getText } from "./format.test";

suite("Tests for Prettier v3", function () {
this.timeout(10000);
test("it formats by Prettier v3", async () => {
const { actual } = await format("v3", "index.ts");
const expected = await getText("v3", "index.result.ts");
assert.equal(actual, expected);
});
});
3 changes: 3 additions & 0 deletions test-fixtures/test.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
},
{
"path": "explicit-dep"
},
{
"path": "v3"
}
],
"settings": {
Expand Down
6 changes: 6 additions & 0 deletions test-fixtures/v3/index.result.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const Counter = decorator("my-counter")((props: {
initialCount?: number;
label?: string;
}) => {
// ...
});
5 changes: 5 additions & 0 deletions test-fixtures/v3/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const Counter = decorator("my-counter")(
(props: { initialCount?: number; label?: string }) => {
// ...
}
);
15 changes: 15 additions & 0 deletions test-fixtures/v3/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "v3",
"version": "1.0.0",
"description": "Test folder for Prettier v3",
"private": true,
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Prettier",
"license": "MIT",
"devDependencies": {
"prettier": "3.0.0-alpha.6"
},
"dependencies": {}
}
8 changes: 8 additions & 0 deletions test-fixtures/v3/yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


[email protected]:
version "3.0.0-alpha.6"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-3.0.0-alpha.6.tgz#c562f9f54966aff792c6920e6a29fbffd1ae07b6"
integrity sha512-AdbQSZ6Oo+iy9Ekzmsgno05P1uX2vqPkjOMJqRfP8hTe+m6iDw4Nt7bPFpWZ/HYCU+3f0P5U0o2ghxQwwkLH7A==