Skip to content

Commit

Permalink
feat: add support of partial + processing with filename, close #49
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin committed Aug 21, 2019
1 parent 15186a1 commit 4383a1e
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 33 deletions.
7 changes: 4 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
*.log
.changelog
.type-coverage
coverage
lib
node_modules
tsconfig.tsbuildinfo
.changelog
.type-coverage
*.log
test.*
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,6 @@
]
},
"typeCoverage": {
"atLeast": 98
"atLeast": 99
}
}
59 changes: 41 additions & 18 deletions packages/eslint-plugin-mdx/src/rules/remark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import remarkStringify from 'remark-stringify'
import unified, { Processor } from 'unified'
import remarkMdx from 'remark-mdx'
import remarkParse from 'remark-parse'
import vfile from 'vfile'

import { RemarkConfig } from './types'

Expand Down Expand Up @@ -34,17 +35,22 @@ const getRemarkProcessor = (searchFrom?: string) => {
(remarkConfig.searchSync(searchFrom) || ({} as CosmiconfigResult)).config ||
{}

return plugins.reduce((remarkProcessor, pluginWithSettings) => {
const [plugin, ...pluginSettings] = Array.isArray(pluginWithSettings)
? pluginWithSettings
: [pluginWithSettings]
return remarkProcessor.use(
/* istanbul ignore next */
// eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-require-imports
typeof plugin === 'string' ? require(plugin) : plugin,
...pluginSettings,
)
}, remarkProcessor().use({ settings }))
// TODO: add support of option, improve original linter options(support array)
plugins.push(['remark-lint-file-extension', 'mdx'])

return plugins
.reduce((remarkProcessor, pluginWithSettings) => {
const [plugin, ...pluginSettings] = Array.isArray(pluginWithSettings)
? pluginWithSettings
: [pluginWithSettings]
return remarkProcessor.use(
/* istanbul ignore next */
// eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-require-imports
typeof plugin === 'string' ? require(plugin) : plugin,
...pluginSettings,
)
}, remarkProcessor().use({ settings }))
.freeze()
}

export const remark: Rule.RuleModule = {
Expand All @@ -62,14 +68,18 @@ export const remark: Rule.RuleModule = {
schema: [],
},
create(context) {
const filename = context.getFilename()
const sourceCode = context.getSourceCode()
return {
Program(node) {
const file = getRemarkProcessor(context.getFilename()).processSync(
sourceCode.getText(node),
const sourceText = sourceCode.getText(node)
const remarkProcessor = getRemarkProcessor(filename)
const file = remarkProcessor.processSync(
vfile({
path: filename,
contents: sourceText,
}),
)
const content = file.toString()
let fixed = false
file.messages.forEach(
({ source, reason, ruleId, location: { start, end } }) =>
context.report({
Expand All @@ -92,11 +102,24 @@ export const remark: Rule.RuleModule = {
},
node,
fix(fixer) {
if (fixed) {
/* istanbul ignore if */
if (start.offset == null) {
return null
}
fixed = true
return fixer.replaceTextRange([0, content.length], content)
const range: [number, number] = [
start.offset,
/* istanbul ignore next */
end.offset == null ? start.offset + 1 : end.offset,
]
const partialText = sourceText.slice(...range)
const fixed = remarkProcessor
.processSync(partialText)
.toString()
return fixer.replaceTextRange(
range,
/* istanbul ignore next */
partialText.endsWith('\n') ? fixed : fixed.slice(0, -1), // remove redundant new line
)
},
}),
)
Expand Down
6 changes: 4 additions & 2 deletions packages/eslint-plugin-mdx/src/rules/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Settings, Attacher } from 'unified'
import { Attacher, Settings } from 'unified'

import { Node, ExpressionStatement } from 'estree'

Expand All @@ -14,7 +14,9 @@ export interface ExpressionStatementWithParent

export type RemarkPlugin = string | Attacher

export type RemarkPluginSettings = Settings | string | number | boolean

export interface RemarkConfig {
settings: Record<string, string>
plugins: Array<RemarkPlugin | [RemarkPlugin, ...Settings[]]>
plugins: Array<RemarkPlugin | [RemarkPlugin, ...RemarkPluginSettings[]]>
}
2 changes: 1 addition & 1 deletion test/__snapshots__/fixtures.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Lorem ipsum dolor sit _amet_, consectetur adipiscing elit. Ut ac lobortis velit.
`;

exports[`fixtures should match all snapshots: blank-lines.mdx 1`] = `
"import {Box} from '@rebass/emotion'
"import { Box } from '@rebass/emotion'
# Getting Started
Expand Down
8 changes: 0 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4275,14 +4275,6 @@ eslint-plugin-jest@^22.15.1:
dependencies:
"@typescript-eslint/experimental-utils" "^1.13.0"

eslint-plugin-mdx@^1.0.2-beta.2:
version "1.0.2-beta.2"
resolved "https://registry.yarnpkg.com/eslint-plugin-mdx/-/eslint-plugin-mdx-1.0.2-beta.2.tgz#3293b7503c1f4e529469ba1ca0c2006d295af73f"
integrity sha512-iGp+6s+gRdPZqqJpyAatnks9ST+W3bOGrNkDYE4DWAiI3FQ4ndoPAe6voRLi5OwXRf/kROoCZwucZDG7gbfh9g==
dependencies:
eslint-mdx "^0.11.2"
rebass "^4.0.2"

"eslint-plugin-mdx@link:packages/eslint-plugin-mdx/src":
version "0.0.0"
uid ""
Expand Down

0 comments on commit 4383a1e

Please sign in to comment.