Skip to content

Commit

Permalink
fix: only apply fix once for the whole file (#309)
Browse files Browse the repository at this point in the history
close #308
  • Loading branch information
JounQin authored Apr 29, 2021
1 parent 94a08af commit 47f100b
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 133 deletions.
71 changes: 42 additions & 29 deletions packages/eslint-plugin-mdx/src/rules/remark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,20 @@ import type { ParserOptions } from 'eslint-mdx'
import { DEFAULT_EXTENSIONS, MARKDOWN_EXTENSIONS } 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 { ProcessSync, RemarkLintMessage } from './types'
import type { RemarkLintMessage } from './types'

const processSync = createSyncFn(require.resolve('../worker')) as ProcessSync
const processSync = createSyncFn(require.resolve('../worker')) as (
fileOptions: VFileOptions,
physicalFilename: string,
isFile: boolean,
) => {
messages: VFile['messages']
content: string
}

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

Expand Down Expand Up @@ -54,12 +62,18 @@ export const remark: Rule.RuleModule = {
}

const file = vfile(fileOptions)
let fixedText: string

let broken = brokenCache.get(remarkProcessor)

if (broken) {
const { messages } = processSync(fileOptions, physicalFilename, isMdx)
const { messages, content } = processSync(
fileOptions,
physicalFilename,
isMdx,
)
file.messages = messages
fixedText = content
} else {
try {
remarkProcessor.processSync(file)
Expand All @@ -70,20 +84,30 @@ export const remark: Rule.RuleModule = {
'`processSync` finished async. Use `process` instead'
) {
brokenCache.set(remarkProcessor, (broken = true))
const { messages } = processSync(
const { messages, content } = processSync(
fileOptions,
physicalFilename,
isMdx,
)
file.messages = messages
} else {
if (!file.messages.includes(err)) {
file.message(err).fatal = true
}
fixedText = content
} else if (!file.messages.includes(err)) {
file.message(err).fatal = true
}
}
}

if (!broken) {
fixedText = file.toString()
}

fixedText =
filename === physicalFilename || sourceText.endsWith('\n')
? fixedText
: fixedText.slice(0, -1)

let fixed = 0

for (const {
source,
reason,
Expand Down Expand Up @@ -120,27 +144,16 @@ export const remark: Rule.RuleModule = {
},
},
node,
fix(fixer) {
/* istanbul ignore if */
if (start.offset == null) {
return null
}
const range: [number, number] = [
start.offset,
/* istanbul ignore next */
end.offset == null ? start.offset + 1 : end.offset,
]
const partialText = sourceText.slice(...range)
const fixed = broken
? processSync(partialText, physicalFilename, isMdx)
: remarkProcessor.processSync(partialText).toString()
return fixer.replaceTextRange(
range,
partialText.endsWith('\n')
? /* istanbul ignore next */ fixed
: fixed.slice(0, -1), // remove redundant new line
)
},
fix:
fixedText === sourceText
? null
: () =>
fixed++
? null
: {
range: [0, sourceText.length],
text: fixedText,
},
})
}
},
Expand Down
14 changes: 0 additions & 14 deletions packages/eslint-plugin-mdx/src/rules/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { Linter } from 'eslint'
import type { ExpressionStatement, Node } from 'estree'
import type { Attacher } from 'unified'
import type { VFile, VFileOptions } from 'vfile'

export interface WithParent {
parent: NodeWithParent
Expand All @@ -26,16 +25,3 @@ export interface RemarkLintMessage {
ruleId: string
severity: Linter.Severity
}

export interface ProcessSync {
(text: string, physicalFilename: string, isFile: boolean): string
(fileOptions: VFileOptions, physicalFilename: string, isFile: boolean): Pick<
VFile,
'messages'
>
(
textOrFileOptions: string | VFileOptions,
physicalFilename: string,
isFile: boolean,
): string | VFileOptions
}
11 changes: 6 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,22 @@ import { getRemarkProcessor } from './rules'
// eslint-disable-next-line @typescript-eslint/no-floating-promises
runAsWorker(
async (
textOrFileOptions: string | VFileOptions,
fileOptions: VFileOptions,
physicalFilename: string,
isMdx: boolean,
) => {
const remarkProcessor = getRemarkProcessor(physicalFilename, isMdx)
const file = vfile(textOrFileOptions)
const file = vfile(fileOptions)
try {
await remarkProcessor.process(file)
} catch (err) {
if (!file.messages.includes(err)) {
file.message(err).fatal = true
}
}
return typeof textOrFileOptions === 'string'
? file.toString()
: { messages: file.messages }
return {
messages: file.messages,
content: file.toString(),
}
},
)
84 changes: 0 additions & 84 deletions test/__snapshots__/fixtures.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,6 @@ Array [
"column": 1,
"endColumn": 6,
"endLine": 3,
"fix": Object {
"range": Array [
7,
12,
],
"text": "# abc",
},
"line": 3,
"message": "Do not use headings with similar content (1:1)",
"nodeType": "Program",
Expand All @@ -23,13 +16,6 @@ Array [
"column": 1,
"endColumn": 6,
"endLine": 3,
"fix": Object {
"range": Array [
7,
12,
],
"text": "# abc",
},
"line": 3,
"message": "Don’t use multiple top level headings (1:1)",
"nodeType": "Program",
Expand All @@ -40,13 +26,6 @@ Array [
"column": 1,
"endColumn": 6,
"endLine": 36,
"fix": Object {
"range": Array [
406,
411,
],
"text": "# abc",
},
"line": 36,
"message": "Do not use headings with similar content (1:1)",
"nodeType": "Program",
Expand All @@ -57,13 +36,6 @@ Array [
"column": 1,
"endColumn": 6,
"endLine": 36,
"fix": Object {
"range": Array [
406,
411,
],
"text": "# abc",
},
"line": 36,
"message": "Don’t use multiple top level headings (1:1)",
"nodeType": "Program",
Expand All @@ -74,13 +46,6 @@ Array [
"column": 1,
"endColumn": 6,
"endLine": 46,
"fix": Object {
"range": Array [
472,
477,
],
"text": "# abc",
},
"line": 46,
"message": "Do not use headings with similar content (3:1)",
"nodeType": "Program",
Expand All @@ -91,13 +56,6 @@ Array [
"column": 1,
"endColumn": 6,
"endLine": 46,
"fix": Object {
"range": Array [
472,
477,
],
"text": "# abc",
},
"line": 46,
"message": "Don’t use multiple top level headings (3:1)",
"nodeType": "Program",
Expand All @@ -121,13 +79,6 @@ Array [
"column": 1,
"endColumn": 16,
"endLine": 27,
"fix": Object {
"range": Array [
550,
565,
],
"text": "# Hello, world!",
},
"line": 27,
"message": "Don’t use multiple top level headings (3:1)",
"nodeType": "Program",
Expand Down Expand Up @@ -160,13 +111,6 @@ Array [
"column": 1,
"endColumn": 36,
"endLine": 35,
"fix": Object {
"range": Array [
653,
688,
],
"text": "# Here's a text gradient shortcode!",
},
"line": 35,
"message": "Don’t use multiple top level headings (3:1)",
"nodeType": "Program",
Expand Down Expand Up @@ -202,13 +146,6 @@ Array [
"column": 1,
"endColumn": 6,
"endLine": 3,
"fix": Object {
"range": Array [
7,
12,
],
"text": "# abc",
},
"line": 3,
"message": "Do not use headings with similar content (1:1)",
"nodeType": "Program",
Expand All @@ -219,13 +156,6 @@ Array [
"column": 1,
"endColumn": 6,
"endLine": 3,
"fix": Object {
"range": Array [
7,
12,
],
"text": "# abc",
},
"line": 3,
"message": "Don’t use multiple top level headings (1:1)",
"nodeType": "Program",
Expand Down Expand Up @@ -295,13 +225,6 @@ Array [
"column": 1,
"endColumn": 6,
"endLine": 3,
"fix": Object {
"range": Array [
7,
12,
],
"text": "# abc",
},
"line": 3,
"message": "Do not use headings with similar content (1:1)",
"nodeType": "Program",
Expand All @@ -312,13 +235,6 @@ Array [
"column": 1,
"endColumn": 6,
"endLine": 3,
"fix": Object {
"range": Array [
7,
12,
],
"text": "# abc",
},
"line": 3,
"message": "Don’t use multiple top level headings (1:1)",
"nodeType": "Program",
Expand Down
16 changes: 16 additions & 0 deletions test/fixtures/style/.remarkrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"settings": {
"emphasis": "*",
"strong": "*"
},
"plugins": [
[
"lint-emphasis-marker",
"*"
],
[
"lint-strong-marker",
"*"
]
]
}
Loading

0 comments on commit 47f100b

Please sign in to comment.