Skip to content

Commit

Permalink
feat: upgrade to use autocorrect-node (FFI version) (#234)
Browse files Browse the repository at this point in the history
Co-authored-by: JounQin <admin@1stg.me>
  • Loading branch information
huacnlee and JounQin committed Nov 8, 2022
1 parent 9a06461 commit 103e4c0
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 43 deletions.
5 changes: 5 additions & 0 deletions .changeset/tidy-gifts-suffer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"prettier-plugin-autocorrect": minor
---

feat: upgrade to use autocorrect-node (FFI version).
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"workspaces": [
"packages/*"
],
"packageManager": "pnpm@7.9.5",
"packageManager": "pnpm@7.14.2",
"scripts": {
"build": "run-p build:*",
"build:r": "r -f cjs",
Expand Down
2 changes: 1 addition & 1 deletion packages/autocorrect/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"prettier": "^2.0.0"
},
"dependencies": {
"@huacnlee/autocorrect-node": "^1.10.2",
"autocorrect-node": "^2.4.3",
"cosmiconfig": "^7.0.1",
"synckit": "^0.8.4",
"tslib": "^2.4.0"
Expand Down
6 changes: 1 addition & 5 deletions packages/autocorrect/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,7 @@ const AutocorrectPlugin: Plugin<string> = {
printers: {
autocorrect: {
print(path, { filepath }) {
const result = formatFor(path.getValue(), filepath)
if (result.error) {
throw new Error(result.error)
}
return result.out
return formatFor(path.getValue(), filepath)
},
},
},
Expand Down
12 changes: 1 addition & 11 deletions packages/autocorrect/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1 @@
export interface FormatResult {
out: string
error: string
raw?: string
enable?: boolean
}

export type FormatFor = (
source: string,
filename: string,
) => Promise<FormatResult>
export type FormatFor = (source: string, filename: string) => Promise<string>
6 changes: 2 additions & 4 deletions packages/autocorrect/src/worker.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { formatFor, loadConfig } from '@huacnlee/autocorrect-node'
import { formatFor, loadConfig } from 'autocorrect-node'
import { cosmiconfig } from 'cosmiconfig'
import { runAsWorker } from 'synckit'

import type { FormatResult } from './types.js'

const explorer = cosmiconfig('autocorrect')

runAsWorker(async (source: string, filename: string) => {
Expand All @@ -16,5 +14,5 @@ runAsWorker(async (source: string, filename: string) => {
)
}

return formatFor(source, filename) as Promise<FormatResult>
return formatFor(source, filename)
})
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ exports[`parser and printer > should format all fixtures > test.md 1`] = `
**加粗**
*倾斜*
~~删除线~~
这是**Bold 加粗**在 1 个段落中,这端会 correct 掉,如果是 inline code,例如\`Rust 语言\`,也可以应该处理。
这是**Bold 加粗**在 1 个段落中,这端会 correct 掉,如果是 inline code,例如\`Rust语言\`,也可以应该处理。
> 引用文本:Quote 也是可以的。
\`\`\`rust
// Codeblock 里面也会处理
Expand Down
78 changes: 58 additions & 20 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 103e4c0

Please sign in to comment.