Skip to content

Commit

Permalink
[New] extensions: accept both a string, and an object to override it.
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Sep 12, 2016
1 parent bd99bdf commit 8162233
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 20 deletions.
51 changes: 31 additions & 20 deletions src/rules/extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ import { isBuiltIn } from '../core/importType'

module.exports = function (context) {
const configuration = context.options[0] || 'never'
const defaultConfig = typeof configuration === 'string' ? configuration : null
const modifiers = typeof configuration === 'object' ? configuration : context.options[1] || {}

function isUseOfExtensionEnforced(extension) {
if (typeof configuration === 'object') {
return configuration[extension] === 'always'
}

return configuration === 'always'
return (modifiers[extension] || defaultConfig) === 'always'
}

function isResolvableWithoutExtension(file) {
Expand Down Expand Up @@ -59,18 +57,31 @@ module.exports = function (context) {
}
}

module.exports.schema = [
{
oneOf: [
{
enum: [ 'always', 'never' ],
},
{
type: 'object',
patternProperties: {
'.*': { enum: [ 'always', 'never' ] },
},
},
],
},
]
const enumValues = { enum: [ 'always', 'never' ] }
const patternProperties = {
type: 'object',
patternProperties: { '.*': enumValues },
}

module.exports.schema = {
anyOf: [
{
type: 'array',
items: [enumValues],
additionalItems: false,
},
{
type: 'array',
items: [patternProperties],
additionalItems: false,
},
{
type: 'array',
items: [
enumValues,
patternProperties,
],
additionalItems: false,
},
],
}
10 changes: 10 additions & 0 deletions tests/src/rules/extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ ruleTester.run('extensions', rule, {
settings: { 'import/resolve': { 'extensions': [ '.js', '.jsx', '.json' ] } },
}),

test({
code: [
'import lib from "./bar"',
'import lib from "./bar.json"',
'import lib from "./bar.hbs"',
].join('\n'),
options: [ 'always', { js: 'never', jsx: 'never' } ],
settings: { 'import/resolve': { 'extensions': [ '.js', '.jsx', '.json', '.hbs' ] } },
}),

// unresolved (#271/#295)
test({ code: 'import path from "path"' }),
test({ code: 'import path from "path"', options: [ 'never' ] }),
Expand Down

0 comments on commit 8162233

Please sign in to comment.