Skip to content

Commit

Permalink
feat: load textlint config automatically (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin authored Sep 7, 2022
1 parent 2a999ec commit a6dc873
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/fuzzy-lemons-camp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-text": patch
---

feat: load textlint config automatically
18 changes: 17 additions & 1 deletion packages/text/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import type { TextlintResult, TextlintFixResult } from '@textlint/kernel'
import { cosmiconfig } from 'cosmiconfig'
import type { CosmiconfigResult } from 'cosmiconfig/dist/types'
import { extractProperties, runAsWorker } from 'synckit'
import { textlint } from 'textlint'
import { TextLintCore } from 'textlint'
import { Config } from 'textlint/lib/src/config/config'
import type { FrozenProcessor, Plugin } from 'unified'
import type { VFileMessage } from 'vfile-message'

Expand Down Expand Up @@ -93,6 +94,8 @@ export const isTextlintFixResult = (
result: TextlintResult,
): result is TextlintFixResult => 'output' in result

const textlintCache = new Map<string, TextLintCore>()

runAsWorker(
async ({
text,
Expand Down Expand Up @@ -124,6 +127,19 @@ runAsWorker(
}
}
case 'textlint': {
let textlint: TextLintCore

if (textlintCache.has(filename)) {
textlint = textlintCache.get(filename)!
} else {
const textlintConfig = Config.initWithAutoLoading({
cwd: path.dirname(filename),
})

textlint = new TextLintCore(textlintConfig)
textlintCache.set(filename, textlint)
}

const result: TextlintFixResult | TextlintResult = await textlint[
fix ? 'fixText' : 'lintText'
](text, path.extname(filename))
Expand Down

0 comments on commit a6dc873

Please sign in to comment.