From 0dc0af9ad782a44b46d465511b06a4f42b1226e5 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Mon, 12 Sep 2016 00:40:20 -0700 Subject: [PATCH] [New] `extensions`: accept both a string, and an object to override it. Fixes #390. --- docs/rules/extensions.md | 4 ++- src/rules/extensions.js | 59 ++++++++++++++++++++++------------- tests/src/rules/extensions.js | 53 +++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+), 23 deletions(-) diff --git a/docs/rules/extensions.md b/docs/rules/extensions.md index 4fe70eea92..6ca33bb447 100644 --- a/docs/rules/extensions.md +++ b/docs/rules/extensions.md @@ -6,10 +6,12 @@ In order to provide a consistent use of file extensions across your code base, t ## Rule Details -This rule has one option which could be either a string or an object. If it is `"never"` (the default value) the rule forbids the use for any extension. If `"always"` then the rule enforces the use of extensions for all import statements. +This rule either takes one string option, one object option, or a string and an object option. If it is the string `"never"` (the default value), then the rule forbids the use for any extension. If it is the string `"always"`, then the rule enforces the use of extensions for all import statements. By providing an object you can configure each extension separately, so for example `{ "js": "always", "json": "never" }` would always enforce the use of the `.js` extension but never allow the use of the `.json` extension. +By providing both a string and an object, the string will set the default setting for all extensions, and the object can be used to set granular overrides for specific extensions. For example, `[, "never", { "svg": "always" }]` would require that all extensions are omitted, except for "svg". + ### Exception When disallowing the use of certain extensions this rule makes an exception and allows the use of extension when the file would not be resolvable without extension. diff --git a/src/rules/extensions.js b/src/rules/extensions.js index f8bb86e187..23f878e824 100644 --- a/src/rules/extensions.js +++ b/src/rules/extensions.js @@ -6,13 +6,15 @@ 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' - } + function isUseOfExtensionRequired(extension) { + return (modifiers[extension] || defaultConfig) === 'always' + } - return configuration === 'always' + function isUseOfExtensionForbidden(extension) { + return (modifiers[extension] || defaultConfig) === 'never' } function isResolvableWithoutExtension(file) { @@ -37,7 +39,7 @@ module.exports = function (context) { const extension = path.extname(resolvedPath || importPath).substring(1) if (!extension || !endsWith(importPath, extension)) { - if (isUseOfExtensionEnforced(extension)) { + if (isUseOfExtensionRequired(extension) && !isUseOfExtensionForbidden(extension)) { context.report({ node: source, message: @@ -45,7 +47,7 @@ module.exports = function (context) { }) } } else if (extension) { - if (!isUseOfExtensionEnforced(extension) && isResolvableWithoutExtension(importPath)) { + if (isUseOfExtensionForbidden(extension) && isResolvableWithoutExtension(importPath)) { context.report({ node: source, message: `Unexpected use of file extension "${extension}" for "${importPath}"`, @@ -59,18 +61,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, + }, + ], +} diff --git a/tests/src/rules/extensions.js b/tests/src/rules/extensions.js index 74f8c1473a..dfc58bfec9 100644 --- a/tests/src/rules/extensions.js +++ b/tests/src/rules/extensions.js @@ -33,6 +33,25 @@ 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' ] } }, + }), + + test({ + code: [ + 'import lib from "./bar.js"', + 'import lib from "./package"', + ].join('\n'), + options: [ 'never', { js: 'always', json: 'never' } ], + settings: { 'import/resolve': { 'extensions': [ '.js', '.json' ] } }, + }), + // unresolved (#271/#295) test({ code: 'import path from "path"' }), test({ code: 'import path from "path"', options: [ 'never' ] }), @@ -124,6 +143,40 @@ ruleTester.run('extensions', rule, { ], }), + test({ + code: [ + 'import lib from "./bar.js"', + 'import lib from "./bar.json"', + 'import lib from "./bar"', + ].join('\n'), + options: [ 'always', { json: 'always', js: 'never', jsx: 'never' } ], + settings: { 'import/resolve': { 'extensions': [ '.js', '.jsx', '.json' ] } }, + errors: [ + { + message: 'Unexpected use of file extension "js" for "./bar.js"', + line: 1, + column: 17, + }, + ], + }), + + test({ + code: [ + 'import lib from "./bar.js"', + 'import lib from "./bar.json"', + 'import lib from "./bar"', + ].join('\n'), + options: [ 'never', { json: 'always', js: 'never', jsx: 'never' } ], + settings: { 'import/resolve': { 'extensions': [ '.js', '.jsx', '.json' ] } }, + errors: [ + { + message: 'Unexpected use of file extension "js" for "./bar.js"', + line: 1, + column: 17, + }, + ], + }), + // unresolved (#271/#295) test({ code: 'import thing from "./fake-file.js"',