Skip to content

Commit

Permalink
import/extensions should have a ignorePackages option. Fixes #414
Browse files Browse the repository at this point in the history
  • Loading branch information
collinsauve committed May 10, 2017
1 parent bd0e5e3 commit c7f0e0d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
26 changes: 25 additions & 1 deletion docs/rules/extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ In order to provide a consistent use of file extensions across your code base, t

## Rule Details

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.
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. If it is the string `"ignorePackages"`, then the rule enforces the use of extensions for all import statements except package imports.

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.

Expand Down Expand Up @@ -86,6 +86,30 @@ import express from 'express/index.js';
import * as path from 'path';
```

The following patterns are considered problems when configuration set to "ignorePackages":

```js
import foo from './foo';

import bar from './bar';

import Component from './Component'

```

The following patterns are not considered problems when configuration set to "ignorePackages":

```js
import foo from './foo.js';

import bar from './bar.json';

import Component from './Component.jsx'

import express from 'express';

```

## When Not To Use It

If you are not concerned about a consistent usage of file extension.
2 changes: 1 addition & 1 deletion src/core/importType.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function isExternalPath(path, name, settings) {
}

const externalModuleRegExp = /^\w/
function isExternalModule(name, settings, path) {
export function isExternalModule(name, settings, path) {
return externalModuleRegExp.test(name) && isExternalPath(path, name, settings)
}

Expand Down
14 changes: 9 additions & 5 deletions src/rules/extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import path from 'path'
import has from 'has'

import resolve from 'eslint-module-utils/resolve'
import { isBuiltIn } from '../core/importType'
import { isBuiltIn, isExternalModule } from '../core/importType'

const enumValues = { enum: [ 'always', 'never' ] }
const enumValues = { enum: [ 'always', 'ignorePackages', 'never' ] }
const patternProperties = {
type: 'object',
patternProperties: { '.*': enumValues },
Expand Down Expand Up @@ -46,9 +46,10 @@ module.exports = {
typeof configuration === 'object' ? configuration : context.options[1]
)

function isUseOfExtensionRequired(extension) {
function isUseOfExtensionRequired(extension, isModule) {
if (!has(modifiers, extension)) { modifiers[extension] = defaultConfig }
return modifiers[extension] === 'always'
const modifier = modifiers[extension];
return (!isModule && modifier === 'ignorePackages') || modifier === 'always';
}

function isUseOfExtensionForbidden(extension) {
Expand Down Expand Up @@ -77,8 +78,11 @@ module.exports = {
// for unresolved, use source value.
const extension = path.extname(resolvedPath || importPath).substring(1)

// determine if this is a module
const isModule = isExternalModule(importPath, context.settings);

if (!extension || !importPath.endsWith(extension)) {
if (isUseOfExtensionRequired(extension) && !isUseOfExtensionForbidden(extension)) {
if (isUseOfExtensionRequired(extension, isModule) && !isUseOfExtensionForbidden(extension)) {
context.report({
node: source,
message:
Expand Down

0 comments on commit c7f0e0d

Please sign in to comment.