Skip to content

Commit

Permalink
feat: cache broken sync processor
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin committed Apr 27, 2021
1 parent 5204f5a commit 2d532f1
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 30 deletions.
67 changes: 43 additions & 24 deletions packages/eslint-plugin-mdx/src/rules/remark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,25 @@ import {
hasProperties,
} from 'eslint-mdx'
import { createSyncFn } from 'synckit'
import type { FrozenProcessor } from 'unified'
import type { VFile, VFileOptions } from 'vfile'
import vfile from 'vfile'

import { getPhysicalFilename, getRemarkProcessor } from './helpers'
import type { RemarkLintMessage } from './types'

const processSync = createSyncFn(require.resolve('../worker')) as (
options: VFileOptions,
// eslint-disable-next-line @typescript-eslint/no-type-alias
type PartialVFile = Pick<VFile, 'messages'>

const processSync = createSyncFn(require.resolve('../worker')) as <
T extends string | PartialVFile = PartialVFile
>(
textOrFile: string | VFileOptions,
physicalFilename: string,
isFile: boolean,
) => Pick<VFile, 'messages'>
) => T

const brokenCache = new WeakMap<FrozenProcessor, true>()

export const remark: Rule.RuleModule = {
meta: {
Expand Down Expand Up @@ -61,24 +69,32 @@ export const remark: Rule.RuleModule = {

const file = vfile(fileOptions)

try {
remarkProcessor.processSync(file)
} catch (err) {
/* istanbul ignore else */
if (
hasProperties<Error>(err, ['message']) &&
err.message ===
'`processSync` finished async. Use `process` instead'
) {
const { messages } = processSync(
fileOptions,
physicalFilename,
isMdx,
)
file.messages = messages
} else {
if (!file.messages.includes(err)) {
file.message(err).fatal = true
const broken = brokenCache.get(remarkProcessor)

if (broken) {
const { messages } = processSync(fileOptions, physicalFilename, isMdx)
file.messages = messages
} else {
try {
remarkProcessor.processSync(file)
} catch (err) {
/* istanbul ignore else */
if (
hasProperties<Error>(err, ['message']) &&
err.message ===
'`processSync` finished async. Use `process` instead'
) {
brokenCache.set(remarkProcessor, true)
const { messages } = processSync(
fileOptions,
physicalFilename,
isMdx,
)
file.messages = messages
} else {
if (!file.messages.includes(err)) {
file.message(err).fatal = true
}
}
}
}
Expand Down Expand Up @@ -130,11 +146,14 @@ export const remark: Rule.RuleModule = {
end.offset == null ? start.offset + 1 : end.offset,
]
const partialText = sourceText.slice(...range)
const fixed = remarkProcessor.processSync(partialText).toString()
const fixed = broken
? processSync<string>(partialText, physicalFilename, isMdx)
: remarkProcessor.processSync(partialText).toString()
return fixer.replaceTextRange(
range,
/* istanbul ignore next */
partialText.endsWith('\n') ? fixed : fixed.slice(0, -1), // remove redundant new line
partialText.endsWith('\n')
? /* istanbul ignore next */ fixed
: fixed.slice(0, -1), // remove redundant new line
)
},
})
Expand Down
10 changes: 5 additions & 5 deletions packages/eslint-plugin-mdx/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ import { getRemarkProcessor } from './rules'
// eslint-disable-next-line @typescript-eslint/no-floating-promises
runAsWorker(
async (
fileOptions: VFileOptions,
textOrFile: string | VFileOptions,
physicalFilename: string,
isMdx: boolean,
) => {
const remarkProcessor = getRemarkProcessor(physicalFilename, isMdx)
const file = vfile(fileOptions)
const file = vfile(textOrFile)
try {
await remarkProcessor.process(file)
} catch (err) {
if (!file.messages.includes(err)) {
file.message(err).fatal = true
}
}
return {
messages: file.messages,
}
return typeof textOrFile === 'string'
? file.toString()
: { messages: file.messages }
},
)
22 changes: 21 additions & 1 deletion test/remark.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ ruleTester.run('remark 1', remark, {
},
},
{
code: '<header>Header5</header>',
code: '<header>Header6</header>',
parser,
parserOptions,
filename: path.resolve(__dirname, 'fixtures/async/test.mdx'),
Expand All @@ -73,5 +73,25 @@ ruleTester.run('remark 1', remark, {
},
],
},
{
code: '[CHANGELOG](./CHANGELOG.md)',
parser,
parserOptions,
filename: path.resolve(__dirname, 'fixtures/async/test.mdx'),
errors: [
{
message: JSON.stringify({
reason: 'Link to unknown file: `CHANGELOG.md`',
source: 'remark-validate-links',
ruleId: 'missing-file',
severity: 1,
}),
line: 1,
column: 1,
endLine: 1,
endColumn: 28,
},
],
},
],
})

0 comments on commit 2d532f1

Please sign in to comment.