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

feat: upgrade coc-prettier prettier version to 3.x.x #172

Merged
merged 6 commits into from
Mar 26, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@
"es5",
"all"
],
"default": "es5",
"default": "all",
"description": "Controls the printing of trailing commas wherever possible.\n Valid options:\n 'none' - No trailing commas\n 'es5' - Trailing commas where valid in ES5 (objects, arrays, etc)\n 'all' - Trailing commas wherever possible (function arguments)",
"scope": "resource"
},
Expand Down Expand Up @@ -309,6 +309,6 @@
"typescript": "^4.6.4"
},
"dependencies": {
"prettier": "^2.6.2"
"prettier": "^3.1.1"
}
}
14 changes: 7 additions & 7 deletions src/ModuleResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { TextDocument, Uri, window } from "coc.nvim"
import * as findUp from "find-up"
import * as fs from "fs"
import * as path from "path"
import * as prettier from "prettier"
import type * as prettier from "prettier"
import * as resolve from "resolve"
import * as semver from "semver"
import { resolveGlobalNodePath, resolveGlobalYarnPath } from "./Files"
Expand Down Expand Up @@ -71,8 +71,8 @@ export class ModuleResolver implements ModuleResolverInterface {
this.findPkgCache = new Map()
}

public getGlobalPrettierInstance(): PrettierNodeModule {
return prettier
public async getGlobalPrettierInstance(): Promise<PrettierNodeModule> {
return await import('prettier')
}

/**
Expand Down Expand Up @@ -194,7 +194,7 @@ export class ModuleResolver implements ModuleResolverInterface {
return undefined
}
this.loggingService.logDebug(USING_BUNDLED_PRETTIER)
return prettier
return await import('prettier')
}
}

Expand All @@ -209,7 +209,7 @@ export class ModuleResolver implements ModuleResolverInterface {
let configPath: string | undefined
try {
if (!isVirtual) {
configPath = (await prettier.resolveConfigFile(fileName)) ?? undefined
configPath = (await (await import('prettier')).resolveConfigFile(fileName)) ?? undefined
}
} catch (error) {
this.loggingService.logError(
Expand All @@ -233,7 +233,7 @@ export class ModuleResolver implements ModuleResolverInterface {
try {
resolvedConfig = isVirtual
? null
: await prettier.resolveConfig(fileName, resolveConfigOptions)
: await (await import('prettier')).resolveConfig(fileName, resolveConfigOptions)
} catch (error) {
this.loggingService.logError(
"Invalid prettier configuration file detected.",
Expand Down Expand Up @@ -262,7 +262,7 @@ export class ModuleResolver implements ModuleResolverInterface {
* Clears the module and config cache
*/
public async dispose() {
prettier.clearConfigCache()
(await import('prettier')).clearConfigCache()
this.path2Module.forEach((module) => {
try {
module.clearConfigCache()
Expand Down
11 changes: 6 additions & 5 deletions src/PrettierEditService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ const PRETTIER_CONFIG_FILES = [
'.prettierrc.toml',
'.prettierrc.js',
'.prettierrc.cjs',
'package.json',
'.prettierrc.mjs',
'prettier.config.js',
'prettier.config.cjs',
'prettier.config.mjs',
'package.json',
'.editorconfig'
]

Expand Down Expand Up @@ -212,7 +214,7 @@ export default class PrettierEditService implements Disposable {

public async registerGlobal() {
const selectors = await this.getSelectors(
this.moduleResolver.getGlobalPrettierInstance()
await this.moduleResolver.getGlobalPrettierInstance()
)
this.registerDocumentFormatEditorProviders(selectors)
this.loggingService.logDebug('Enabling Prettier globally', selectors)
Expand Down Expand Up @@ -363,7 +365,6 @@ export default class PrettierEditService implements Disposable {
return TextEdit.replace(Range.create(pos0, pos1), newText)
}


/**
* Format the given text with user's configuration.
* @param text Text to format
Expand Down Expand Up @@ -445,7 +446,7 @@ export default class PrettierEditService implements Disposable {
// somebody has registered a custom file extension without properly
// configuring the parser in their prettier config.
this.loggingService.logWarning(`Parser not inferred, trying languageId.`)
const languages = prettierInstance.getSupportInfo().languages
const languages = (await prettierInstance.getSupportInfo()).languages
parser = getParserFromLanguageId(languages, Uri.parse(uri), languageId)
}

Expand All @@ -468,7 +469,7 @@ export default class PrettierEditService implements Disposable {
this.loggingService.logInfo('Prettier Options:', prettierOptions)

try {
const formattedText = prettierInstance.format(text, prettierOptions)
const formattedText = await prettierInstance.format(text, prettierOptions)
this.statusBar.update(FormatterStatus.Success)

return formattedText
Expand Down
6 changes: 3 additions & 3 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ type PrettierOptions = prettier.Options
type PrettierFileInfoOptions = prettier.FileInfoOptions

type PrettierModule = {
format(source: string, options?: prettier.Options): string
getSupportInfo(): { languages: PrettierSupportLanguage[] }
format(source: string, options?: prettier.Options): Promise<string>
getSupportInfo(): Promise<{ languages: PrettierSupportLanguage[] }>
getFileInfo(
filePath: string,
options?: PrettierFileInfoOptions
Expand All @@ -26,7 +26,7 @@ type PrettierModule = {

type ModuleResolverInterface = {
getPrettierInstance(fileName: string): Promise<PrettierModule | undefined>
getGlobalPrettierInstance(): PrettierModule
getGlobalPrettierInstance(): Promise<PrettierModule>
getResolvedConfig(
doc: TextDocument,
vscodeConfig: PrettierVSCodeConfig
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -285,10 +285,10 @@ path-parse@^1.0.7:
resolved "https://registry.yarnpkg.com/path-parse/-/path-parse-1.0.7.tgz#fbc114b60ca42b30d9daf5858e4bd68bbedb6735"
integrity sha512-LDJzPVEEEPR+y48z93A0Ed0yXb8pAByGWo/k5YYdYgpY2/2EsOsksJrq7lOHxryrVOn1ejG6oAp8ahvOIQD8sw==

prettier@^2.6.2:
version "2.6.2"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.6.2.tgz#e26d71a18a74c3d0f0597f55f01fb6c06c206032"
integrity sha512-PkUpF+qoXTqhOeWL9fu7As8LXsIUZ1WYaJiY/a7McAQzxjk82OF0tibkFXVCDImZtWxbvojFjerkiLb0/q8mew==
prettier@^3.1.1:
version "3.1.1"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-3.1.1.tgz#6ba9f23165d690b6cbdaa88cb0807278f7019848"
integrity sha512-22UbSzg8luF4UuZtzgiUOfcGM8s4tjBv6dJRT7j275NXsy2jb4aJa4NNveul5x4eqlF1wuhuR2RElK71RvmVaw==

resolve@^1.22.0:
version "1.22.0"
Expand Down