Skip to content

Commit

Permalink
fix: resolve relative plugin correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin committed Aug 22, 2019
1 parent 751e266 commit 99de99a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
22 changes: 17 additions & 5 deletions packages/eslint-plugin-mdx/src/rules/helper.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import path from 'path'

import remarkStringify from 'remark-stringify'
import unified, { Processor } from 'unified'
import remarkMdx from 'remark-mdx'
Expand All @@ -7,7 +9,14 @@ import { RemarkConfig } from './types'

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

export const requirePkg = (plugin: string, prefix: string) => {
export const requirePkg = (
plugin: string,
prefix: string,
filePath?: string,
) => {
if (filePath && /^\.\.?([\\/]|$)/.test(plugin)) {
plugin = path.resolve(path.dirname(filePath), plugin)
}
prefix = prefix.endsWith('-') ? prefix : prefix + '-'
const packages = [
plugin,
Expand Down Expand Up @@ -46,9 +55,10 @@ export const getRemarkProcessor = (searchFrom: string) => {
}

/* istanbul ignore next */
const { plugins = [], settings }: Partial<RemarkConfig> =
(remarkConfig.searchSync(searchFrom) || ({} as CosmiconfigResult)).config ||
{}
const { config, filepath }: Partial<CosmiconfigResult> =
remarkConfig.searchSync(searchFrom) || {}
/* istanbul ignore next */
const { plugins = [], settings }: Partial<RemarkConfig> = config || {}

// disable this rule automatically since we already have a parser option `extensions`
plugins.push(['lint-file-extension', false])
Expand All @@ -61,7 +71,9 @@ export const getRemarkProcessor = (searchFrom: string) => {
: [pluginWithSettings]
return remarkProcessor.use(
/* istanbul ignore next */
typeof plugin === 'string' ? requirePkg(plugin, 'remark') : plugin,
typeof plugin === 'string'
? requirePkg(plugin, 'remark', filepath)
: plugin,
...pluginSettings,
)
},
Expand Down
5 changes: 5 additions & 0 deletions test/helper.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import path from 'path'

import { requirePkg } from 'eslint-plugin-mdx'

describe('Helpers', () => {
it('should resolve package correctly', () => {
expect(requirePkg('@1stg/config', 'husky')).toBeDefined()
expect(requirePkg('lint', 'remark')).toBeDefined()
expect(requirePkg('remark-parse', 'non existed')).toBeDefined()
expect(
requirePkg('./.eslintrc', 'non existed', path.resolve('package.json')),
).toBeDefined()
})

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

0 comments on commit 99de99a

Please sign in to comment.