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

Use local prettier instance for parsers #706

Merged
Merged
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
4 changes: 2 additions & 2 deletions src/PrettierEditProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ async function format(

const dynamicParsers = getParsersFromLanguageId(
languageId,
localPrettier.version,
localPrettier,
isUntitled ? undefined : fileName
);
let useBundled = false;
Expand All @@ -114,7 +114,7 @@ async function format(
if (!dynamicParsers.length) {
const bundledParsers = getParsersFromLanguageId(
languageId,
bundledPrettier.version,
bundledPrettier,
isUntitled ? undefined : fileName
);
parser = bundledParsers[0] || 'babylon';
Expand Down
10 changes: 6 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@ import {
ParserOption,
} from './types.d';

const bundledPrettier = require('prettier') as Prettier;

export function getConfig(uri?: Uri): PrettierVSCodeConfig {
return workspace.getConfiguration('prettier', uri) as any;
}

export function getParsersFromLanguageId(
languageId: string,
version: string,
prettierInstance: Prettier,
path?: string
): ParserOption[] {
const language = getSupportLanguages(version).find(
const language = getSupportLanguages(prettierInstance).find(
lang =>
Array.isArray(lang.vscodeLanguageIds) &&
lang.vscodeLanguageIds.includes(languageId) &&
Expand Down Expand Up @@ -54,6 +56,6 @@ export function getGroup(group: string): PrettierSupportInfo['languages'] {
return getSupportLanguages().filter(language => language.group === group);
}

function getSupportLanguages(version?: string) {
return (require('prettier') as Prettier).getSupportInfo(version).languages;
function getSupportLanguages(prettierInstance: Prettier = bundledPrettier) {
return prettierInstance.getSupportInfo(prettierInstance.version).languages;
Copy link
Member

Choose a reason for hiding this comment

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

I can't remember when prettier.getSupportInfo was added to prettier. But on quite old prettier versions, this may break.

Copy link
Contributor Author

@JHilker JHilker Jan 31, 2019

Choose a reason for hiding this comment

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

Looks like getSupportInfo was added in prettier/prettier#3033 which only landed in 1.8.0. Think it's worth adding a fallback to bundledPrettier.getSupportInfo if getSupportInfo isn't defined?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done in #707

Copy link
Member

Choose a reason for hiding this comment

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

It still would resolve to "babel" instead of "babylon". Without a fix in getSupportInfo. So quite pointless right now.

1.8 is 1 year old. Let's expect no one is using it with this extension 😆
Monitoring issues ...

Solution could be to simply use bundled if local version is below a given version. vscode now allows us to easily downgrade an extension. I think we would need to do that to support plugins anyway without having to support too many code path and keep this extension simple.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Without a fix in getSupportInfo. So quite pointless right now.

Yeah, it'll only be useful once there is an upstream fix in getSupportInfo, but required if that does happen. 🤷‍♂️

1.8 is 1 year old. Let's expect no one is using it with this extension 😆

You have more faith than I do in people updating deps! 😂 But if you don't think it'll be an issue, works for me 👍

Copy link
Member

Choose a reason for hiding this comment

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

You have more faith than I do in people updating deps! joy But if you don't think it'll be an issue, works for me +1

Prettier was new and it's not a major version change. That's why I'm expecting so. and certainly mistaken. Let's push people to upgrade their deps 😃

}