Skip to content

Commit 4d5f5cb

Browse files
fiskersindresorhus
andcommitted
Change some options for prevent-abbreviations rule (#422)
Co-authored-by: Sindre Sorhus <[email protected]>
1 parent f8ad8f1 commit 4d5f5cb

File tree

4 files changed

+191
-20
lines changed

4 files changed

+191
-20
lines changed

docs/rules/prevent-abbreviations.md

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,24 @@ Pass `"extendDefaultWhitelist": false` to override the default `whitelist` compl
156156

157157
### checkDefaultAndNamespaceImports
158158

159-
Type: `boolean`<br>
160-
Default: `false`
159+
Type: `'internal' | boolean`<br>
160+
Default: `'internal'`
161161

162-
Pass `"checkDefaultAndNamespaceImports": true` to check variables declared in default or namespace import.
162+
- `'internal'` - Check variables declared in default or namespace import, **but only for internal modules**.
163+
- `true` - Check variables declared in default or namespace import.
164+
- `false` - Don't check variables declared in default or namespace import.
163165

164-
With this set to `true` the following code will be reported.
166+
By default, the following code will be reported:
167+
168+
```js
169+
import * as err from './err';
170+
```
171+
172+
```js
173+
import err from '/err';
174+
```
175+
176+
With this set to `true`, the following code will be reported:
165177

166178
```js
167179
import tempWrite from 'temp-write';
@@ -177,12 +189,20 @@ const err = require('err');
177189

178190
### checkShorthandImports
179191

180-
Type: `boolean`<br>
181-
Default: `false`
192+
Type: `'internal'` | `boolean`<br>
193+
Default: `'internal'`
182194

183-
Pass `"checkShorthandImports": true` to check variables declared in shorthand import.
195+
- `'internal'` - Check variables declared in shorthand import, **but only for internal modules**.
196+
- `true` - Check variables declared in shorthand import.
197+
- `false` - Don't check variables declared in default shorthand import.
184198

185-
With this set to `true` the following code will be reported.
199+
By default, the following code will be reported:
200+
201+
```js
202+
import {prop} from './foo';
203+
```
204+
205+
With this set to `true`, the following code will be reported:
186206

187207
```js
188208
import {prop} from 'ramda';

rules/prevent-abbreviations.js

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,8 @@ const prepareOptions = ({
198198
checkProperties = false,
199199
checkVariables = true,
200200

201-
checkDefaultAndNamespaceImports = false,
202-
checkShorthandImports = false,
201+
checkDefaultAndNamespaceImports = 'internal',
202+
checkShorthandImports = 'internal',
203203
checkShorthandProperties = false,
204204

205205
checkFilenames = true,
@@ -491,6 +491,18 @@ const shouldReportIdentifierAsProperty = identifier => {
491491
return false;
492492
};
493493

494+
const isInternalImport = node => {
495+
let source = '';
496+
497+
if (node.type === 'Variable') {
498+
source = node.node.init.arguments[0].value;
499+
} else if (node.type === 'ImportBinding') {
500+
source = node.parent.source.value;
501+
}
502+
503+
return !source.includes('node_modules') && (source.startsWith('.') || source.startsWith('/'));
504+
};
505+
494506
const create = context => {
495507
const {
496508
ecmaVersion
@@ -553,12 +565,24 @@ const create = context => {
553565

554566
const [definition] = variable.defs;
555567

556-
if (!options.checkDefaultAndNamespaceImports && isDefaultOrNamespaceImportName(definition.name)) {
557-
return;
568+
if (isDefaultOrNamespaceImportName(definition.name)) {
569+
if (!options.checkDefaultAndNamespaceImports) {
570+
return;
571+
}
572+
573+
if (options.checkDefaultAndNamespaceImports === 'internal' && !isInternalImport(definition)) {
574+
return;
575+
}
558576
}
559577

560-
if (!options.checkShorthandImports && isShorthandImportIdentifier(definition.name)) {
561-
return;
578+
if (isShorthandImportIdentifier(definition.name)) {
579+
if (!options.checkShorthandImports) {
580+
return;
581+
}
582+
583+
if (options.checkShorthandImports === 'internal' && !isInternalImport(definition)) {
584+
return;
585+
}
562586
}
563587

564588
if (!options.checkShorthandProperties && isShorthandPropertyIdentifier(definition.name)) {
@@ -684,8 +708,14 @@ const schema = [{
684708
checkProperties: {type: 'boolean'},
685709
checkVariables: {type: 'boolean'},
686710

687-
checkDefaultAndNamespaceImports: {type: 'boolean'},
688-
checkShorthandImports: {type: 'boolean'},
711+
checkDefaultAndNamespaceImports: {
712+
type: ['boolean', 'string'],
713+
pattern: 'internal'
714+
},
715+
checkShorthandImports: {
716+
type: ['boolean', 'string'],
717+
pattern: 'internal'
718+
},
689719
checkShorthandProperties: {type: 'boolean'},
690720

691721
checkFilenames: {type: 'boolean'},

test/get-documentation-url.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import test from 'ava';
2-
import pkg from '../package.json';
2+
import packageJson from '../package.json';
33
import getDocumentationUrl from '../rules/utils/get-documentation-url';
44

55
test('returns the URL of the a named rule\'s documentation', t => {
6-
const url = `https://github.com/sindresorhus/eslint-plugin-unicorn/blob/v${pkg.version}/docs/rules/foo.md`;
6+
const url = `https://github.com/sindresorhus/eslint-plugin-unicorn/blob/v${packageJson.version}/docs/rules/foo.md`;
77
t.is(getDocumentationUrl('foo.js'), url);
88
});
99

1010
test('determines the rule name from the file', t => {
11-
const url = `https://github.com/sindresorhus/eslint-plugin-unicorn/blob/v${pkg.version}/docs/rules/get-documentation-url.md`;
11+
const url = `https://github.com/sindresorhus/eslint-plugin-unicorn/blob/v${packageJson.version}/docs/rules/get-documentation-url.md`;
1212
t.is(getDocumentationUrl(__filename), url);
1313
});

test/prevent-abbreviations.js

Lines changed: 122 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ const extendedOptions = [{
5656
}
5757
}];
5858

59+
const noCheckShorthandImportsOptions = [{checkShorthandImports: false}];
60+
const noCheckDefaultAndNamespaceImports = [{checkDefaultAndNamespaceImports: false}];
61+
5962
const customOptions = [{
6063
checkProperties: true,
6164

@@ -1075,6 +1078,9 @@ moduleRuleTester.run('prevent-abbreviations', rule, {
10751078
export {foo as err};
10761079
`,
10771080

1081+
// Path includes `node_modules`
1082+
'import err from "./node_modules/err"',
1083+
10781084
// Default import names are allowed
10791085
'import err from "err"',
10801086
'import err, {foo as bar} from "err"',
@@ -1093,7 +1099,75 @@ moduleRuleTester.run('prevent-abbreviations', rule, {
10931099
// Names from object destructuring are allowed
10941100
'const {err} = require("err")',
10951101
'const {err} = foo',
1096-
'function f({err}) {}'
1102+
'function f({err}) {}',
1103+
1104+
// Option checkDefaultAndNamespaceImports: false
1105+
{
1106+
code: 'const err = require("err")',
1107+
options: noCheckDefaultAndNamespaceImports
1108+
},
1109+
{
1110+
code: 'const err = require("./err")',
1111+
options: noCheckDefaultAndNamespaceImports
1112+
},
1113+
{
1114+
code: 'import foo, * as err from "err"',
1115+
options: noCheckDefaultAndNamespaceImports
1116+
},
1117+
{
1118+
code: 'import foo, * as err from "./err"',
1119+
options: noCheckDefaultAndNamespaceImports
1120+
},
1121+
{
1122+
code: 'import { default as err, foo as bar } from "err"',
1123+
options: noCheckDefaultAndNamespaceImports
1124+
},
1125+
{
1126+
code: 'import { default as err, foo as bar } from "./err"',
1127+
options: noCheckDefaultAndNamespaceImports
1128+
},
1129+
{
1130+
code: 'import err, { foo as bar } from "err"',
1131+
options: noCheckDefaultAndNamespaceImports
1132+
},
1133+
{
1134+
code: 'import err, { foo as bar } from "./err"',
1135+
options: noCheckDefaultAndNamespaceImports
1136+
},
1137+
{
1138+
code: 'import * as err from "err"',
1139+
options: noCheckDefaultAndNamespaceImports
1140+
},
1141+
{
1142+
code: 'import * as err from "./err"',
1143+
options: noCheckDefaultAndNamespaceImports
1144+
},
1145+
{
1146+
code: 'import err from "err"',
1147+
options: noCheckDefaultAndNamespaceImports
1148+
},
1149+
{
1150+
code: 'import err from "./err"',
1151+
options: noCheckDefaultAndNamespaceImports
1152+
},
1153+
1154+
// Option checkShorthandImports: false
1155+
{
1156+
code: 'import { err } from "err"',
1157+
options: noCheckShorthandImportsOptions
1158+
},
1159+
{
1160+
code: 'import { err } from "./err"',
1161+
options: noCheckShorthandImportsOptions
1162+
},
1163+
{
1164+
code: 'import { default as foo, err } from "err"',
1165+
options: noCheckShorthandImportsOptions
1166+
},
1167+
{
1168+
code: 'import { default as foo, err } from "./err"',
1169+
options: noCheckShorthandImportsOptions
1170+
}
10971171
],
10981172

10991173
invalid: [
@@ -1155,6 +1229,53 @@ moduleRuleTester.run('prevent-abbreviations', rule, {
11551229
errors: createErrors()
11561230
},
11571231

1232+
// Internal import
1233+
{
1234+
code: 'const err = require("../err")',
1235+
output: 'const error = require("../err")',
1236+
errors: createErrors()
1237+
},
1238+
{
1239+
code: 'const err = require("/err")',
1240+
output: 'const error = require("/err")',
1241+
errors: createErrors()
1242+
},
1243+
{
1244+
code: 'import err from "./err"',
1245+
output: 'import error from "./err"',
1246+
errors: createErrors()
1247+
},
1248+
{
1249+
code: 'import err, {foo as bar} from "./err"',
1250+
output: 'import error, {foo as bar} from "./err"',
1251+
errors: createErrors()
1252+
},
1253+
{
1254+
code: 'import {default as err, foo as bar} from "./err"',
1255+
output: 'import {default as error, foo as bar} from "./err"',
1256+
errors: createErrors()
1257+
},
1258+
{
1259+
code: 'import * as err from "./err"',
1260+
output: 'import * as error from "./err"',
1261+
errors: createErrors()
1262+
},
1263+
{
1264+
code: 'import foo, * as err from "./err"',
1265+
output: 'import foo, * as error from "./err"',
1266+
errors: createErrors()
1267+
},
1268+
{
1269+
code: 'import {err} from "./err"',
1270+
output: 'import {err as error} from "./err"',
1271+
errors: createErrors()
1272+
},
1273+
{
1274+
code: 'import {default as foo, err} from "./err"',
1275+
output: 'import {default as foo, err as error} from "./err"',
1276+
errors: createErrors()
1277+
},
1278+
11581279
{
11591280
code: outdent`
11601281
let err;

0 commit comments

Comments
 (0)