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

Declare createSynchronizedPrettier as only named export #12

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

colinrotherham
Copy link
Contributor

@colinrotherham colinrotherham commented Jul 17, 2023

As discussed in alphagov/govuk-frontend#3971 (comment) @prettier/sync doesn't support named exports:

import { format } from '@prettier/sync'

This PR updates the type declarations to show that

I've also updated the tsconfig.json file so local test.js imports are checked too

Named exports not allowed

@@ -36,7 +36,7 @@ test("version", () => {
fakePrettierUrl.href,
fileURLToPath(fakePrettierUrl),
]) {
test(prettierEntry, async () => {
test(prettierEntry.toString(), async () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The compiler flagged this line because test() only accepts string not URL

Copy link
Contributor

@remcohaszing remcohaszing left a comment

Choose a reason for hiding this comment

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

I see what you’re trying to do, but I’m not sure if there is a proper way to do this correctly.

Interestingly, this works, but it relies on errors in the local type definitions:

// @ts-expect-error
declare const synchronizedPrettier: SynchronizedPrettier;

// @ts-expect-error
declare namespace synchronizedPrettier {
  function createSynchronizedPrettier(options: {
    prettierEntry: string | URL;
  }): SynchronizedPrettier
}

index.d.cts Outdated Show resolved Hide resolved
tsconfig.json Outdated Show resolved Hide resolved
@colinrotherham colinrotherham marked this pull request as draft July 17, 2023 16:18
@colinrotherham colinrotherham marked this pull request as ready for review July 17, 2023 19:53
test.js Outdated Show resolved Hide resolved
Updates types to show that `createSynchronizedPrettier` is the only ESM named export to ensure TypeScript throws errors for invalid named exports such as:

```
import { format } from '@prettier/sync'
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants