Skip to content

Commit

Permalink
feat: rename option directory to project - close #23
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin committed Jul 30, 2020
1 parent d0f19d3 commit a662fc1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,27 +69,27 @@ Add the following to your `.eslintrc` config:

// use <root>/path/to/folder/tsconfig.json
"typescript": {
"directory": "path/to/folder"
"project": "path/to/folder"
},

// Multiple tsconfigs (Useful for monorepos)

// use a glob pattern
"typescript": {
"directory": "packages/*/tsconfig.json"
"project": "packages/*/tsconfig.json"
},

// use an array
"typescript": {
"directory": [
"project": [
"packages/module-a/tsconfig.json",
"packages/module-b/tsconfig.json"
]
},

// use an array of glob patterns
"typescript": {
"directory": [
"project": [
"packages/*/tsconfig.json",
"other-packages/*/tsconfig.json"
]
Expand Down
30 changes: 22 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import isGlob from 'is-glob'
import { isCore, sync } from 'resolve'
import debug from 'debug'

const log = debug('eslint-import-resolver-typescript')
const IMPORTER_NAME = 'eslint-import-resolver-typescript'

const log = debug(IMPORTER_NAME)

const defaultExtensions = ['.ts', '.tsx', '.d.ts'].concat(
// eslint-disable-next-line node/no-deprecated-api
Expand All @@ -23,14 +25,19 @@ export const interfaceVersion = 2

export interface TsResolverOptions {
alwaysTryTypes?: boolean
/**
* @deprecated use `project` instead
*/
directory?: string | string[]
project?: string | string[]
extensions?: string[]
packageFilter?: (pkg: Record<string, string>) => Record<string, string>
}

/**
* @param {string} source the module to resolve; i.e './some-module'
* @param {string} file the importing file's full path; i.e. '/usr/local/bin/file.js'
* @param {TsResolverOptions} options
*/
export function resolve(
source: string,
Expand Down Expand Up @@ -140,17 +147,24 @@ function initMappers(options: TsResolverOptions) {
return
}

const isArrayOfStrings = (array?: string | string[]) =>
Array.isArray(array) && array.every(o => typeof o === 'string')
if (options.directory) {
console.warn(
`[${IMPORTER_NAME}]: option \`directory\` is deprecated, please use \`project\` instead`,
)

if (!options.project) {
options.project = options.directory
}
}

const configPaths =
typeof options.directory === 'string'
? [options.directory]
: isArrayOfStrings(options.directory)
? options.directory
typeof options.project === 'string'
? [options.project]
: Array.isArray(options.project)
? options.project
: [process.cwd()]

mappers = configPaths!
mappers = configPaths
// turn glob patterns into paths
.reduce<string[]>(
(paths, path) => paths.concat(isGlob(path) ? globSync(path) : path),
Expand Down
4 changes: 2 additions & 2 deletions tests/baseEslintConfig.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = directory => ({
module.exports = project => ({
parser: '@typescript-eslint/parser',
extends: [
'eslint:recommended',
Expand All @@ -10,7 +10,7 @@ module.exports = directory => ({
settings: {
'import/resolver': {
typescript: {
directory,
project,
alwaysTryTypes: true,
},
},
Expand Down

0 comments on commit a662fc1

Please sign in to comment.