Skip to content

Commit bbf9ebf

Browse files
authored
make import/extensions great again (#482)
* always ignore invalid extensions if `import/extensions` is set (fixes #478) * reboot of npm `watch` script
1 parent a97443e commit bbf9ebf

File tree

6 files changed

+39
-3
lines changed

6 files changed

+39
-3
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
1111
### Changed
1212
- Modified [`no-nodejs-modules`] error message to include the module's name ([#453], [#461])
1313

14+
### Fixed
15+
- [`import/extensions` setting] is respected in spite of the appearance of imports
16+
in an imported file. (fixes [#478], thaks [@rhys-vdw])
17+
1418
## [1.12.0] - 2016-07-26
1519
### Added
1620
- [`import/external-module-folders` setting]: a possibility to configure folders for "external" modules ([#444], thanks [@zloirock])
@@ -311,6 +315,7 @@ for info on changes for earlier releases.
311315
[#157]: https://github.com/benmosher/eslint-plugin-import/pull/157
312316
[#314]: https://github.com/benmosher/eslint-plugin-import/pull/314
313317

318+
[#478]: https://github.com/benmosher/eslint-plugin-import/issues/478
314319
[#456]: https://github.com/benmosher/eslint-plugin-import/issues/456
315320
[#453]: https://github.com/benmosher/eslint-plugin-import/issues/453
316321
[#441]: https://github.com/benmosher/eslint-plugin-import/issues/441
@@ -399,3 +404,4 @@ for info on changes for earlier releases.
399404
[@ljharb]: https://github.com/ljharb
400405
[@rhettlivingston]: https://github.com/rhettlivingston
401406
[@zloirock]: https://github.com/zloirock
407+
[@rhys-vdw]: https://github.com/rhys-vdw

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"memo-parser"
1313
],
1414
"scripts": {
15-
"watch": "cross-env NODE_PATH=./lib gulp watch-test",
15+
"watch": "cross-env NODE_PATH=./src mocha --watch --compilers js:babel-register --recursive tests/src",
1616
"cover": "gulp pretest && cross-env NODE_PATH=./lib istanbul cover --dir reports/coverage _mocha tests/lib/ -- --recursive -R progress",
1717
"posttest": "eslint ./src",
1818
"test": "cross-env BABEL_ENV=test NODE_PATH=./src nyc mocha --recursive tests/src -t 5s",

src/core/getExports.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import * as doctrine from 'doctrine'
77

88
import parse from './parse'
99
import resolve, { relative as resolveRelative } from './resolve'
10-
import isIgnored from './ignore'
10+
import isIgnored, { hasValidExtension } from './ignore'
1111

1212
import { hashObject } from './hash'
1313

@@ -71,6 +71,12 @@ export default class ExportMap {
7171
// future: check content equality?
7272
}
7373

74+
// check valid extensions first
75+
if (!hasValidExtension(path, context)) {
76+
exportCache.set(cacheKey, null)
77+
return null
78+
}
79+
7480
const content = fs.readFileSync(path, { encoding: 'utf8' })
7581

7682
// check for and cache ignore

src/core/ignore.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export default function ignore(path, context) {
2626
: ['node_modules']
2727

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

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

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

3838
return false
3939
}
40+
41+
export function hasValidExtension(path, context) {
42+
return validExtensions(context).has(extname(path))
43+
}

tests/files/typescript.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
type X = { y: string | null }
2+
3+
export function getX() : X {
4+
return null
5+
}

tests/src/core/getExports.js

+15
Original file line numberDiff line numberDiff line change
@@ -292,4 +292,19 @@ describe('getExports', function () {
292292
})
293293
})
294294

295+
context('issue #478: never parse non-whitelist extensions', function () {
296+
const context = Object.assign({}, fakeContext,
297+
{ settings: { 'import/extensions': ['.js'] } })
298+
299+
let imports
300+
before('load imports', function () {
301+
imports = ExportMap.get('./typescript.ts', context)
302+
})
303+
304+
it('returns nothing for a TypeScript file', function () {
305+
expect(imports).not.to.exist
306+
})
307+
308+
})
309+
295310
})

0 commit comments

Comments
 (0)