Skip to content

Commit

Permalink
feature: no-unused-modules: try-catch: remove all nesting
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed Aug 17, 2021
1 parent fdc2007 commit 1cf365b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 35 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -114,6 +114,7 @@
"pkg-up": "^2.0.0",
"read-pkg-up": "^3.0.0",
"resolve": "^1.20.0",
"try-catch": "^3.0.0",
"tsconfig-paths": "^3.9.0"
}
}
74 changes: 39 additions & 35 deletions src/rules/no-unused-modules.js
Expand Up @@ -14,52 +14,56 @@ import values from 'object.values';
import includes from 'array-includes';
import tryCatch from 'try-catch';

let listFilesToProcess;

const [, eslint8] = tryCatch(require, 'eslint/use-at-your-own-risk');
// has been moved to eslint/lib/cli-engine/file-enumerator in version 6
const [, eslint7] = tryCatch(require, 'eslint/lib/cli-engine/file-enumerator');
// eslint/lib/util/glob-util has been moved to eslint/lib/util/glob-utils with version 5.3
const [, eslint6] = tryCatch(require, 'eslint/lib/util/glob-utils');
const [, eslint5] = tryCatch(require, 'eslint/lib/util/glob-util');

const modernESLint = eslint8 || eslint7;

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

listFilesToProcess = function (src, extensions) {
const e = new FileEnumerator({
extensions: extensions,
});
listFilesToProcess = function (src, extensions) {
const e = new FileEnumerator({
extensions: extensions,
});

return Array.from(e.iterateFiles(src), ({ filePath, ignored }) => ({
ignored,
filename: filePath,
}));
return Array.from(e.iterateFiles(src), ({ filePath, ignored }) => ({
ignored,
filename: filePath,
}));
};
}

// Prevent passing invalid options (extensions array) to old versions of the function.
// https://github.com/eslint/eslint/blob/v5.16.0/lib/util/glob-utils.js#L178-L280
// https://github.com/eslint/eslint/blob/v5.2.0/lib/util/glob-util.js#L174-L269
if (eslint6) {
const originalListFilesToProcess = eslint6.listFilesToProcess;
listFilesToProcess = function (src, extensions) {
return originalListFilesToProcess(src, {
extensions: extensions,
});
};
}

if (eslin6) {
originalListFilesToProcess = eslin6.listFilesToProcess;
listFilesToProcess = function (src, extensions) {
return originalListFilesToProcess(src, {
extensions: extensions,
});
};
}

if (eslin5) {
originalListFilesToProcess = eslint5.listFilesToProcess;

listFilesToProcess = function (src, extensions) {
const patterns = src.reduce((carry, pattern) => {
return carry.concat(extensions.map((extension) => {
return /\*\*|\*\./.test(pattern) ? pattern : `${pattern}/**/*${extension}`;
}));
}, src.slice());
if (eslint5) {
const originalListFilesToProcess = eslint5.listFilesToProcess;

return originalListFilesToProcess(patterns);
};
}
listFilesToProcess = function (src, extensions) {
const patterns = src.reduce((carry, pattern) => {
return carry.concat(extensions.map((extension) => {
return /\*\*|\*\./.test(pattern) ? pattern : `${pattern}/**/*${extension}`;
}));
}, src.slice());

return originalListFilesToProcess(patterns);
};
}

const EXPORT_DEFAULT_DECLARATION = 'ExportDefaultDeclaration';
Expand Down Expand Up @@ -236,7 +240,7 @@ const prepareImportsAndExports = (srcFiles, context) => {
}
const localImport = imports.get(key) || new Set();
value.declarations.forEach(({ importedSpecifiers }) =>
importedSpecifiers.forEach(specifier => localImport.add(specifier))
importedSpecifiers.forEach(specifier => localImport.add(specifier)),
);
imports.set(key, localImport);
});
Expand Down Expand Up @@ -552,13 +556,13 @@ module.exports = {
if (exportStatement.whereUsed.size < 1) {
context.report(
node,
`exported declaration '${value}' not used within other modules`
`exported declaration '${value}' not used within other modules`,
);
}
} else {
context.report(
node,
`exported declaration '${value}' not used within other modules`
`exported declaration '${value}' not used within other modules`,
);
}
};
Expand Down

0 comments on commit 1cf365b

Please sign in to comment.