Skip to content

Commit 359cbbb

Browse files
committed
import/extensions setting: parser whitelist. fixes #267
1 parent 73164f8 commit 359cbbb

File tree

10 files changed

+61
-4
lines changed

10 files changed

+61
-4
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
88
- [`newline-after-import`], new rule. ([#245], thanks [@singles])
99
- Added an `optionalDependencies` option to [`no-extraneous-dependencies`] to allow/forbid optional dependencies ([#266], thanks [@jfmengels]).
1010

11+
### Breaking
12+
- [`import/extensions` setting]: a whitelist of file extensions to parse as modules
13+
and search for `export`s. Defaults to `['.js']`.
14+
1115
## resolvers/webpack/0.2.4 - 2016-04-29
1216
### Changed
1317
- automatically find webpack config with `interpret`-able extensions ([#287], thanks [@taion])
@@ -174,6 +178,7 @@ for info on changes for earlier releases.
174178

175179
[`import/cache` setting]: ./README.md#importcache
176180
[`import/ignore` setting]: ./README.md#importignore
181+
[`import/extensions` setting]: ./README.md#importextensions
177182

178183
[`no-unresolved`]: ./docs/rules/no-unresolved.md
179184
[`no-deprecated`]: ./docs/rules/no-deprecated.md

README.md

+13
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,19 @@ If you are interesting in writing a resolver, see the [spec](./resolvers/README.
146146

147147
You may set the following settings in your `.eslintrc`:
148148

149+
#### `import/extensions`
150+
151+
A whitelist of file extensions that will be parsed as modules and inspected for
152+
`export`s. This defaults to `['.js']`, unless you are using the `react` shared config,
153+
in which case it is specified as `['.js', '.jsx']`.
154+
155+
Note that this is different from (and likely a subset of) any `import/resolver`
156+
extensions settings, which may include `.json`, `.coffee`, etc. which will still
157+
factor into the `no-unresolved` rule.
158+
159+
Also, `import/ignore` patterns will overrule this whitelist, so `node_modules` that
160+
end in `.js` will still be ignored by default.
161+
149162
#### `import/ignore`
150163

151164
A list of regex strings that, if matched by a path, will

config/react.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* - adds `.jsx` as an extension
3+
*/
4+
module.exports = {
5+
settings: {
6+
'import/extensions': ['.js', '.jsx'],
7+
},
8+
}

src/core/ignore.js

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,28 @@
1+
import { extname } from 'path'
2+
import Set from 'es6-set'
3+
4+
// one-shot memoized
5+
let cachedSet, lastSettings
6+
function validExtensions({ settings }) {
7+
if (cachedSet && settings === lastSettings) {
8+
return cachedSet
9+
}
10+
11+
// todo: add 'mjs'?
12+
lastSettings = settings
13+
cachedSet = new Set(settings['import/extensions'] || [ '.js' ])
14+
return cachedSet
15+
}
16+
117
export default function ignore(path, context) {
218
// ignore node_modules by default
3-
var ignoreStrings = context.settings['import/ignore']
19+
const ignoreStrings = context.settings['import/ignore']
420
? [].concat(context.settings['import/ignore'])
521
: ['node_modules']
622

23+
// check extension whitelist first (cheap)
24+
if (!validExtensions(context).has(extname(path))) return true
25+
726
if (ignoreStrings.length === 0) return false
827

928
for (var i = 0; i < ignoreStrings.length; i++) {

src/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ export const configs = {
2727
'errors': require('../config/errors'),
2828
'warnings': require('../config/warnings'),
2929

30+
// useful stuff for folks using React
31+
'react': require('../config/react'),
32+
3033
// shhhh... work in progress "secret" rules
3134
'stage-0': require('../config/stage-0'),
3235
}

tests/files/data.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "foo": "bar" }

tests/src/rules/named.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { test } from '../utils'
1+
import { test, SYNTAX_CASES } from '../utils'
22
import { RuleTester } from 'eslint'
33

44
var ruleTester = new RuleTester()
@@ -96,6 +96,7 @@ ruleTester.run('named', rule, {
9696
settings: { 'import/ignore': ['common'] },
9797
}),
9898

99+
...SYNTAX_CASES,
99100
],
100101

101102
invalid: [
@@ -166,6 +167,7 @@ ruleTester.run('named', rule, {
166167
// parse errors
167168
test({
168169
code: "import { a } from './test.coffee';",
170+
settings: { 'import/extensions': ['.js', '.coffee'] },
169171
errors: [{
170172
message: "Parse errors in imported module './test.coffee': Unexpected token > (1:20)",
171173
type: 'Literal',

tests/src/rules/namespace.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var test = require('../utils').test
1+
import { test, SYNTAX_CASES } from '../utils'
22
import { RuleTester } from 'eslint'
33

44
var ruleTester = new RuleTester({ env: { es6: true }})
@@ -86,6 +86,7 @@ const valid = [
8686
parser: 'babel-eslint',
8787
}),
8888

89+
...SYNTAX_CASES,
8990
]
9091

9192
const invalid = [

tests/src/rules/no-named-as-default.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { test } from '../utils'
1+
import { test, SYNTAX_CASES } from '../utils'
22
import { RuleTester } from 'eslint'
33

44
const ruleTester = new RuleTester()
@@ -16,6 +16,8 @@ ruleTester.run('no-named-as-default', rule, {
1616
, parser: 'babel-eslint' }),
1717
test({ code: 'export bar from "./bar";'
1818
, parser: 'babel-eslint' }),
19+
20+
...SYNTAX_CASES,
1921
],
2022

2123
invalid: [

tests/src/utils.js

+3
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,7 @@ export const SYNTAX_CASES = [
5757
test({ code: 'export default x' }),
5858
test({ code: 'export default class x {}' }),
5959

60+
// issue #267: parser whitelist
61+
test({ code: 'import json from "./data.json"' }),
62+
6063
]

0 commit comments

Comments
 (0)