Ensure all requires are within plugin functions

This commit is contained in:
HaveAGitGat 2022-01-01 15:30:29 +00:00
parent 30e9ae1b9b
commit 69e04a5c1f
7 changed files with 29 additions and 23 deletions

View file

@ -69,6 +69,23 @@ module.exports.plugin = plugin;`;
process.exit(1);
}
// check deps are within functions
const keyWord = 'require(';
const requires = read.split(keyWord);
if (requires.length >= 2) {
const allBefore = [];
for (let j = 0; j < requires.length - 1; j += 1) {
allBefore.push(requires[j]);
const countOpen = allBefore.join(keyWord).split('{').length - 1;
const countClose = allBefore.join(keyWord).split('}').length - 1;
if (countOpen === countClose) {
console.log(`Plugin has requires outside of function '${folder}/${files[i]}'`);
process.exit(1);
}
}
}
let pluginDetails;
try {
// eslint-disable-next-line import/no-dynamic-require,global-require