forked from mdx-js/eslint-mdx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.ts
56 lines (45 loc) · 1.77 KB
/
options.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
* based on @link https://github.com/sveltejs/eslint-plugin-svelte3/blob/master/src/processor_options.js
*/
import type { Linter, SourceCode } from 'eslint'
import type { ESLintMdxSettings, ProcessorOptions } from './types'
export const processorOptions = {} as ProcessorOptions
// find Linter instance
const linterPath = Object.keys(require.cache).find(path =>
/([/\\])eslint\1lib(?:\1linter){2}\.js$/.test(path),
)
/* istanbul ignore if */
if (!linterPath) {
throw new Error('Could not find ESLint Linter in require cache')
}
export interface LinterConfig extends Linter.Config {
extractConfig?(filename?: string): Linter.Config
}
// eslint-disable-next-line @typescript-eslint/consistent-type-imports, @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires
const ESLinter = (require(linterPath) as typeof import('eslint')).Linter
// patch Linter#verify
// eslint-disable-next-line @typescript-eslint/unbound-method
const { verify } = ESLinter.prototype
ESLinter.prototype.verify = function (
code: SourceCode | string,
config: LinterConfig,
options?: Linter.LintOptions | string,
) {
// fetch settings
const settings = ((config &&
(typeof config.extractConfig === 'function'
? config.extractConfig(
/* istanbul ignore next */
// eslint-disable-next-line unicorn/no-typeof-undefined
typeof options === 'undefined' || typeof options === 'string'
? options
: options.filename,
)
: config
).settings) ||
{}) as ESLintMdxSettings
processorOptions.lintCodeBlocks = settings['mdx/code-blocks'] === true
processorOptions.languageMapper = settings['mdx/language-mapper']
// call original Linter#verify
return verify.call(this, code, config, options as Linter.LintOptions)
}