Skip to content

Commit

Permalink
feat: allow to ignore remark config (#374)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasvazq authored Mar 12, 2022
1 parent 51a3ee0 commit 1c9e3fd
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 7 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ See [#251](/~https://github.com/mdx-js/eslint-mdx/issues/251#issuecomment-73613922

3. `markdownExtensions` (`string | string[]`): `eslint-mdx` will only treat `.md` files as plain markdown by default, and will lint them via remark plugins. If you want to resolve other extensions as like `.md`, you can use this option.

4. `ignoreRemarkConfig` (`boolean`): Ignore the `remark` configuration defined in the project.

## Rules

### mdx/no-jsx-html-comments
Expand Down Expand Up @@ -223,6 +225,8 @@ If you want to disable or change severity of some related rules, it won't work b
}
```
Some plugins are ESM and eslint don't supports them. So, a workaround is to set `ignoreRemarkConfig` to `true` and execute `remark-lint` through the terminal before running eslint. For example: `remark **/*.mdx --no-stdout && eslint . --fix --ext .mdx`.

## Prettier Integration

If you're using [remark-lint][] feature with [Prettier][] both together, you can try [remark-preset-prettier][] which helps you to _turn off all rules that are unnecessary or might conflict with [Prettier][]_.
Expand Down
4 changes: 4 additions & 0 deletions packages/eslint-mdx/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ See [#251](/~https://github.com/mdx-js/eslint-mdx/issues/251#issuecomment-73613922

3. `markdownExtensions` (`string | string[]`): `eslint-mdx` will only treat `.md` files as plain markdown by default, and will lint them via remark plugins. If you want to resolve other extensions as like `.md`, you can use this option.

4. `ignoreRemarkConfig` (`boolean`): Ignore the `remark` configuration defined in the project.

## Rules

### mdx/no-jsx-html-comments
Expand Down Expand Up @@ -223,6 +225,8 @@ If you want to disable or change severity of some related rules, it won't work b
}
```
Some plugins are ESM and eslint don't supports them. So, a workaround is to set `ignoreRemarkConfig` to `true` and execute `remark-lint` through the terminal before running eslint. For example: `remark **/*.mdx --no-stdout && eslint . --fix --ext .mdx`.

## Prettier Integration

If you're using [remark-lint][] feature with [Prettier][] both together, you can try [remark-preset-prettier][] which helps you to _turn off all rules that are unnecessary or might conflict with [Prettier][]_.
Expand Down
1 change: 1 addition & 0 deletions packages/eslint-mdx/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ export class Parser {
const root = getRemarkProcessor(
getPhysicalFilename(options.filePath),
isMdx,
Boolean(options.ignoreRemarkConfig),
).parse(code) as Parent

this._ast = {
Expand Down
13 changes: 10 additions & 3 deletions packages/eslint-mdx/src/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fs from 'fs'
import path from 'path'

import { cosmiconfigSync } from 'cosmiconfig'
import type { CosmiconfigResult } from 'cosmiconfig/dist/types'
import remarkMdx from 'remark-mdx'
import remarkParse from 'remark-parse'
import remarkStringify from 'remark-stringify'
Expand Down Expand Up @@ -69,8 +70,12 @@ const explorer = cosmiconfigSync('remark', {
// @internal - exported for testing
export const processorCache = new Map<string, FrozenProcessor>()

// eslint-disable-next-line sonarjs/cognitive-complexity
export const getRemarkProcessor = (searchFrom: string, isMdx: boolean) => {
export const getRemarkProcessor = (
searchFrom: string,
isMdx: boolean,
ignoreRemarkConfig: boolean,
// eslint-disable-next-line sonarjs/cognitive-complexity
) => {
const initCacheKey = `${String(isMdx)}-${searchFrom}`

let cachedProcessor = processorCache.get(initCacheKey)
Expand All @@ -79,7 +84,9 @@ export const getRemarkProcessor = (searchFrom: string, isMdx: boolean) => {
return cachedProcessor
}

const result = explorer.search(searchFrom)
const result: CosmiconfigResult = ignoreRemarkConfig
? null
: explorer.search(searchFrom)

const cacheKey = result ? `${String(isMdx)}-${result.filepath}` : ''

Expand Down
1 change: 1 addition & 0 deletions packages/eslint-mdx/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export interface ParserOptions extends Linter.ParserOptions {
markdownExtensions?: string[] | string
filePath?: string
parser?: ParserConfig | ParserFn | string
ignoreRemarkConfig?: boolean
}

export type Traverser = (node: Node, parent?: Parent) => void
Expand Down
11 changes: 10 additions & 1 deletion packages/eslint-plugin-mdx/src/rules/remark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const lazyRemark = {
fileOptions: VFileOptions,
physicalFilename: string,
isMdx: boolean,
ignoreRemarkConfig: boolean,
) => {
messages: VFile['messages']
content: string
Expand Down Expand Up @@ -65,10 +66,16 @@ export const remark: Rule.RuleModule = {
return
}

const ignoreRemarkConfig = Boolean(options.ignoreRemarkConfig)

const physicalFilename = getPhysicalFilename(filename)

const sourceText = sourceCode.getText(node)
const remarkProcessor = getRemarkProcessor(physicalFilename, isMdx)
const remarkProcessor = getRemarkProcessor(
physicalFilename,
isMdx,
ignoreRemarkConfig,
)

const fileOptions = {
path: physicalFilename,
Expand All @@ -85,6 +92,7 @@ export const remark: Rule.RuleModule = {
fileOptions,
physicalFilename,
isMdx,
ignoreRemarkConfig,
)
file.messages = messages
fixedText = content
Expand All @@ -102,6 +110,7 @@ export const remark: Rule.RuleModule = {
fileOptions,
physicalFilename,
isMdx,
ignoreRemarkConfig,
)
file.messages = messages
fixedText = content
Expand Down
7 changes: 6 additions & 1 deletion packages/eslint-plugin-mdx/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ runAsWorker(
fileOptions: VFileOptions,
physicalFilename: string,
isMdx: boolean,
ignoreRemarkConfig: boolean,
) => {
const remarkProcessor = getRemarkProcessor(physicalFilename, isMdx)
const remarkProcessor = getRemarkProcessor(
physicalFilename,
isMdx,
ignoreRemarkConfig,
)
const file = vfile(fileOptions)
try {
await remarkProcessor.process(file)
Expand Down
7 changes: 5 additions & 2 deletions test/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ import {

const stringToNode = (text: string) =>
first(
(getRemarkProcessor(PLACEHOLDER_FILE_PATH, true).parse(text) as Parent)
.children,
(
getRemarkProcessor(PLACEHOLDER_FILE_PATH, true, false).parse(
text,
) as Parent
).children,
)

describe('parser', () => {
Expand Down
6 changes: 6 additions & 0 deletions test/remark.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ ruleTester.run('remark', remark, {
parserOptions,
filename: path.resolve(__dirname, 'fixtures/async/test.mdx'),
},
{
code: '_emphasis_ and __strong__',
parser,
parserOptions: { ...parserOptions, ignoreRemarkConfig: true },
filename: path.resolve(__dirname, 'fixtures/style/test.mdx'),
},
],
invalid: [
{
Expand Down

0 comments on commit 1c9e3fd

Please sign in to comment.