Skip to content

Commit

Permalink
feat(ts-helpers): added exclude option to resolver, #10
Browse files Browse the repository at this point in the history
  • Loading branch information
Marty Mcfly committed Feb 9, 2019
1 parent 04785b0 commit a54901e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
20 changes: 20 additions & 0 deletions packages/ts-helpers/__tests__/ImportPathsResolver.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,24 @@ describe('ImportPathsResolver', () => {
'./bla/index.ts',
)).toBeUndefined()
})

it('should exclude some paths tokens', () => {
const resolver = createResolver({
exclude: ['*'],
})
expect(resolver.getImportSuggestions(
'pkg_test',
'./bla/index.ts',
)).toBeUndefined()
})

it('should parse *', () => {
const resolver = createResolver()
expect(resolver.getImportSuggestions(
'pkg_test',
'./bla/index.ts',
)).toEqual([
'../../types/pkg_test'
])
})
})
11 changes: 7 additions & 4 deletions packages/ts-helpers/src/ImportPathsResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {Tokenizer, regExpEscape} from './Tokenizer'
export interface TSOptions {
paths?: Record<string, string[]>
baseUrl?: string
exclude?: string[] | void
}

export const winSepRegex = new RegExp(regExpEscape(path.sep), 'g')
Expand All @@ -25,10 +26,12 @@ export class ImportPathsResolver {
)
: null

this.tokenizers = Object.keys(paths).map(key => new Tokenizer(
key,
mapBaseUrl ? paths[key].map(mapBaseUrl) : paths[key]
))
this.tokenizers = Object.keys(paths)
.filter(key => !opts.exclude || !opts.exclude.includes(key))
.map(key => new Tokenizer(
key,
mapBaseUrl ? paths[key].map(mapBaseUrl) : paths[key]
))
}

getImportSuggestions(oldImport: string, fileName: string): string[] | void {
Expand Down

0 comments on commit a54901e

Please sign in to comment.