Skip to content

Commit

Permalink
make import/extensions great again (#482)
Browse files Browse the repository at this point in the history
* always ignore invalid extensions if `import/extensions` is set (fixes #478)

* reboot of npm `watch` script
  • Loading branch information
benmosher authored Aug 11, 2016
1 parent a97443e commit bbf9ebf
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 3 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"memo-parser"
],
"scripts": {
"watch": "cross-env NODE_PATH=./lib gulp watch-test",
"watch": "cross-env NODE_PATH=./src mocha --watch --compilers js:babel-register --recursive tests/src",
"cover": "gulp pretest && cross-env NODE_PATH=./lib istanbul cover --dir reports/coverage _mocha tests/lib/ -- --recursive -R progress",
"posttest": "eslint ./src",
"test": "cross-env BABEL_ENV=test NODE_PATH=./src nyc mocha --recursive tests/src -t 5s",
Expand Down
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 bbf9ebf

Please sign in to comment.