Skip to content

Commit

Permalink
always ignore invalid extensions if import/extensions is set (fixes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
benmosher committed Aug 11, 2016
1 parent 3f2a6fd commit 622aa9c
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
### Changed
- Modified [`no-nodejs-modules`] error message to include the module's name ([#453], [#461])

### Fixed
- [`import/extensions` setting] is respected in spite of the appearance of imports
in an imported file. (fixes [#478], thaks [@rhys-vdw])

## [1.12.0] - 2016-07-26
### Added
- [`import/external-module-folders` setting]: a possibility to configure folders for "external" modules ([#444], thanks [@zloirock])
Expand Down Expand Up @@ -311,6 +315,7 @@ for info on changes for earlier releases.
[#157]: https://github.com/benmosher/eslint-plugin-import/pull/157
[#314]: https://github.com/benmosher/eslint-plugin-import/pull/314

[#478]: https://github.com/benmosher/eslint-plugin-import/issues/478
[#456]: https://github.com/benmosher/eslint-plugin-import/issues/456
[#453]: https://github.com/benmosher/eslint-plugin-import/issues/453
[#441]: https://github.com/benmosher/eslint-plugin-import/issues/441
Expand Down Expand Up @@ -399,3 +404,4 @@ for info on changes for earlier releases.
[@ljharb]: https://github.com/ljharb
[@rhettlivingston]: https://github.com/rhettlivingston
[@zloirock]: https://github.com/zloirock
[@rhys-vdw]: https://github.com/rhys-vdw
8 changes: 7 additions & 1 deletion src/core/getExports.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as doctrine from 'doctrine'

import parse from './parse'
import resolve, { relative as resolveRelative } from './resolve'
import isIgnored from './ignore'
import isIgnored, { hasValidExtension } from './ignore'

import { hashObject } from './hash'

Expand Down Expand Up @@ -71,6 +71,12 @@ export default class ExportMap {
// future: check content equality?
}

// check valid extensions first
if (!hasValidExtension(path, context)) {
exportCache.set(cacheKey, null)
return null
}

const content = fs.readFileSync(path, { encoding: 'utf8' })

// check for and cache ignore
Expand Down
6 changes: 5 additions & 1 deletion src/core/ignore.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default function ignore(path, context) {
: ['node_modules']

// check extension list first (cheap)
if (!validExtensions(context).has(extname(path))) return true
if (!hasValidExtension(path, context)) return true

if (ignoreStrings.length === 0) return false

Expand All @@ -37,3 +37,7 @@ export default function ignore(path, context) {

return false
}

export function hasValidExtension(path, context) {
return validExtensions(context).has(extname(path))
}
5 changes: 5 additions & 0 deletions tests/files/typescript.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type X = { y: string | null }

export function getX() : X {
return null
}
15 changes: 15 additions & 0 deletions tests/src/core/getExports.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,4 +292,19 @@ describe('getExports', function () {
})
})

context('issue #478: never parse non-whitelist extensions', function () {
const context = Object.assign({}, fakeContext,
{ settings: { 'import/extensions': ['.js'] } })

let imports
before('load imports', function () {
imports = ExportMap.get('./typescript.ts', context)
})

it('returns nothing for a TypeScript file', function () {
expect(imports).not.to.exist
})

})

})

0 comments on commit 622aa9c

Please sign in to comment.