Skip to content

Commit 99de99a

Browse files
committed
fix: resolve relative plugin correctly
1 parent 751e266 commit 99de99a

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

packages/eslint-plugin-mdx/src/rules/helper.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import path from 'path'
2+
13
import remarkStringify from 'remark-stringify'
24
import unified, { Processor } from 'unified'
35
import remarkMdx from 'remark-mdx'
@@ -7,7 +9,14 @@ import { RemarkConfig } from './types'
79

810
import cosmiconfig, { Explorer, CosmiconfigResult } from 'cosmiconfig'
911

10-
export const requirePkg = (plugin: string, prefix: string) => {
12+
export const requirePkg = (
13+
plugin: string,
14+
prefix: string,
15+
filePath?: string,
16+
) => {
17+
if (filePath && /^\.\.?([\\/]|$)/.test(plugin)) {
18+
plugin = path.resolve(path.dirname(filePath), plugin)
19+
}
1120
prefix = prefix.endsWith('-') ? prefix : prefix + '-'
1221
const packages = [
1322
plugin,
@@ -46,9 +55,10 @@ export const getRemarkProcessor = (searchFrom: string) => {
4655
}
4756

4857
/* istanbul ignore next */
49-
const { plugins = [], settings }: Partial<RemarkConfig> =
50-
(remarkConfig.searchSync(searchFrom) || ({} as CosmiconfigResult)).config ||
51-
{}
58+
const { config, filepath }: Partial<CosmiconfigResult> =
59+
remarkConfig.searchSync(searchFrom) || {}
60+
/* istanbul ignore next */
61+
const { plugins = [], settings }: Partial<RemarkConfig> = config || {}
5262

5363
// disable this rule automatically since we already have a parser option `extensions`
5464
plugins.push(['lint-file-extension', false])
@@ -61,7 +71,9 @@ export const getRemarkProcessor = (searchFrom: string) => {
6171
: [pluginWithSettings]
6272
return remarkProcessor.use(
6373
/* istanbul ignore next */
64-
typeof plugin === 'string' ? requirePkg(plugin, 'remark') : plugin,
74+
typeof plugin === 'string'
75+
? requirePkg(plugin, 'remark', filepath)
76+
: plugin,
6577
...pluginSettings,
6678
)
6779
},

test/helper.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1+
import path from 'path'
2+
13
import { requirePkg } from 'eslint-plugin-mdx'
24

35
describe('Helpers', () => {
46
it('should resolve package correctly', () => {
57
expect(requirePkg('@1stg/config', 'husky')).toBeDefined()
68
expect(requirePkg('lint', 'remark')).toBeDefined()
79
expect(requirePkg('remark-parse', 'non existed')).toBeDefined()
10+
expect(
11+
requirePkg('./.eslintrc', 'non existed', path.resolve('package.json')),
12+
).toBeDefined()
813
})
914

1015
it('should throw on non existed package', () =>

0 commit comments

Comments
 (0)