From 1c9e3fdccbd7571cec4ea2758412ace1dfe19222 Mon Sep 17 00:00:00 2001 From: Lucas Vazquez Date: Sat, 12 Mar 2022 08:18:04 -0300 Subject: [PATCH] feat: allow to ignore remark config (#374) --- README.md | 4 ++++ packages/eslint-mdx/README.md | 4 ++++ packages/eslint-mdx/src/parser.ts | 1 + packages/eslint-mdx/src/processor.ts | 13 ++++++++++--- packages/eslint-mdx/src/types.ts | 1 + packages/eslint-plugin-mdx/src/rules/remark.ts | 11 ++++++++++- packages/eslint-plugin-mdx/src/worker.ts | 7 ++++++- test/parser.test.ts | 7 +++++-- test/remark.test.ts | 6 ++++++ 9 files changed, 47 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 212e40f9..2d586fa3 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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][]_. diff --git a/packages/eslint-mdx/README.md b/packages/eslint-mdx/README.md index 212e40f9..2d586fa3 100644 --- a/packages/eslint-mdx/README.md +++ b/packages/eslint-mdx/README.md @@ -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 @@ -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][]_. diff --git a/packages/eslint-mdx/src/parser.ts b/packages/eslint-mdx/src/parser.ts index 0f1a92b3..8bb6fe3f 100644 --- a/packages/eslint-mdx/src/parser.ts +++ b/packages/eslint-mdx/src/parser.ts @@ -172,6 +172,7 @@ export class Parser { const root = getRemarkProcessor( getPhysicalFilename(options.filePath), isMdx, + Boolean(options.ignoreRemarkConfig), ).parse(code) as Parent this._ast = { diff --git a/packages/eslint-mdx/src/processor.ts b/packages/eslint-mdx/src/processor.ts index c17d178c..058250e5 100644 --- a/packages/eslint-mdx/src/processor.ts +++ b/packages/eslint-mdx/src/processor.ts @@ -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' @@ -69,8 +70,12 @@ const explorer = cosmiconfigSync('remark', { // @internal - exported for testing export const processorCache = new Map() -// 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) @@ -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}` : '' diff --git a/packages/eslint-mdx/src/types.ts b/packages/eslint-mdx/src/types.ts index 39a11ff3..d5b91157 100644 --- a/packages/eslint-mdx/src/types.ts +++ b/packages/eslint-mdx/src/types.ts @@ -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 diff --git a/packages/eslint-plugin-mdx/src/rules/remark.ts b/packages/eslint-plugin-mdx/src/rules/remark.ts index 0911c0af..d36da139 100644 --- a/packages/eslint-plugin-mdx/src/rules/remark.ts +++ b/packages/eslint-plugin-mdx/src/rules/remark.ts @@ -25,6 +25,7 @@ const lazyRemark = { fileOptions: VFileOptions, physicalFilename: string, isMdx: boolean, + ignoreRemarkConfig: boolean, ) => { messages: VFile['messages'] content: string @@ -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, @@ -85,6 +92,7 @@ export const remark: Rule.RuleModule = { fileOptions, physicalFilename, isMdx, + ignoreRemarkConfig, ) file.messages = messages fixedText = content @@ -102,6 +110,7 @@ export const remark: Rule.RuleModule = { fileOptions, physicalFilename, isMdx, + ignoreRemarkConfig, ) file.messages = messages fixedText = content diff --git a/packages/eslint-plugin-mdx/src/worker.ts b/packages/eslint-plugin-mdx/src/worker.ts index b7a2bada..c70a10d9 100644 --- a/packages/eslint-plugin-mdx/src/worker.ts +++ b/packages/eslint-plugin-mdx/src/worker.ts @@ -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) diff --git a/test/parser.test.ts b/test/parser.test.ts index 53012086..d1c4e699 100644 --- a/test/parser.test.ts +++ b/test/parser.test.ts @@ -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', () => { diff --git a/test/remark.test.ts b/test/remark.test.ts index cade655b..c167f918 100644 --- a/test/remark.test.ts +++ b/test/remark.test.ts @@ -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: [ {