Skip to content

Commit 1cf365b

Browse files
committed
feature: no-unused-modules: try-catch: remove all nesting
1 parent fdc2007 commit 1cf365b

File tree

2 files changed

+40
-35
lines changed

2 files changed

+40
-35
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@
114114
"pkg-up": "^2.0.0",
115115
"read-pkg-up": "^3.0.0",
116116
"resolve": "^1.20.0",
117+
"try-catch": "^3.0.0",
117118
"tsconfig-paths": "^3.9.0"
118119
}
119120
}

src/rules/no-unused-modules.js

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,52 +14,56 @@ import values from 'object.values';
1414
import includes from 'array-includes';
1515
import tryCatch from 'try-catch';
1616

17+
let listFilesToProcess;
18+
1719
const [, eslint8] = tryCatch(require, 'eslint/use-at-your-own-risk');
20+
// has been moved to eslint/lib/cli-engine/file-enumerator in version 6
1821
const [, eslint7] = tryCatch(require, 'eslint/lib/cli-engine/file-enumerator');
22+
// eslint/lib/util/glob-util has been moved to eslint/lib/util/glob-utils with version 5.3
1923
const [, eslint6] = tryCatch(require, 'eslint/lib/util/glob-utils');
2024
const [, eslint5] = tryCatch(require, 'eslint/lib/util/glob-util');
2125

2226
const modernESLint = eslint8 || eslint7;
2327

2428
if (modernESLint) {
25-
// eslint/lib/util/glob-util has been moved to eslint/lib/util/glob-utils with version 5.3
26-
// and has been moved to eslint/lib/cli-engine/file-enumerator in version 6
27-
const {FileEnumerator} = modernESLint;
29+
const { FileEnumerator } = modernESLint;
2830

29-
listFilesToProcess = function (src, extensions) {
30-
const e = new FileEnumerator({
31-
extensions: extensions,
32-
});
31+
listFilesToProcess = function (src, extensions) {
32+
const e = new FileEnumerator({
33+
extensions: extensions,
34+
});
3335

34-
return Array.from(e.iterateFiles(src), ({ filePath, ignored }) => ({
35-
ignored,
36-
filename: filePath,
37-
}));
36+
return Array.from(e.iterateFiles(src), ({ filePath, ignored }) => ({
37+
ignored,
38+
filename: filePath,
39+
}));
40+
};
41+
}
42+
43+
// Prevent passing invalid options (extensions array) to old versions of the function.
44+
// https://github.com/eslint/eslint/blob/v5.16.0/lib/util/glob-utils.js#L178-L280
45+
// https://github.com/eslint/eslint/blob/v5.2.0/lib/util/glob-util.js#L174-L269
46+
if (eslint6) {
47+
const originalListFilesToProcess = eslint6.listFilesToProcess;
48+
listFilesToProcess = function (src, extensions) {
49+
return originalListFilesToProcess(src, {
50+
extensions: extensions,
51+
});
3852
};
3953
}
40-
41-
if (eslin6) {
42-
originalListFilesToProcess = eslin6.listFilesToProcess;
43-
listFilesToProcess = function (src, extensions) {
44-
return originalListFilesToProcess(src, {
45-
extensions: extensions,
46-
});
47-
};
48-
}
49-
50-
if (eslin5) {
51-
originalListFilesToProcess = eslint5.listFilesToProcess;
5254

53-
listFilesToProcess = function (src, extensions) {
54-
const patterns = src.reduce((carry, pattern) => {
55-
return carry.concat(extensions.map((extension) => {
56-
return /\*\*|\*\./.test(pattern) ? pattern : `${pattern}/**/*${extension}`;
57-
}));
58-
}, src.slice());
55+
if (eslint5) {
56+
const originalListFilesToProcess = eslint5.listFilesToProcess;
5957

60-
return originalListFilesToProcess(patterns);
61-
};
62-
}
58+
listFilesToProcess = function (src, extensions) {
59+
const patterns = src.reduce((carry, pattern) => {
60+
return carry.concat(extensions.map((extension) => {
61+
return /\*\*|\*\./.test(pattern) ? pattern : `${pattern}/**/*${extension}`;
62+
}));
63+
}, src.slice());
64+
65+
return originalListFilesToProcess(patterns);
66+
};
6367
}
6468

6569
const EXPORT_DEFAULT_DECLARATION = 'ExportDefaultDeclaration';
@@ -236,7 +240,7 @@ const prepareImportsAndExports = (srcFiles, context) => {
236240
}
237241
const localImport = imports.get(key) || new Set();
238242
value.declarations.forEach(({ importedSpecifiers }) =>
239-
importedSpecifiers.forEach(specifier => localImport.add(specifier))
243+
importedSpecifiers.forEach(specifier => localImport.add(specifier)),
240244
);
241245
imports.set(key, localImport);
242246
});
@@ -552,13 +556,13 @@ module.exports = {
552556
if (exportStatement.whereUsed.size < 1) {
553557
context.report(
554558
node,
555-
`exported declaration '${value}' not used within other modules`
559+
`exported declaration '${value}' not used within other modules`,
556560
);
557561
}
558562
} else {
559563
context.report(
560564
node,
561-
`exported declaration '${value}' not used within other modules`
565+
`exported declaration '${value}' not used within other modules`,
562566
);
563567
}
564568
};

0 commit comments

Comments
 (0)