feat: avoid infinite loop

This commit is contained in:
Anthony Laibe 2019-01-24 15:45:49 +00:00 committed by Iuri Matias
parent 29db66be23
commit e8da329d60
2 changed files with 10 additions and 6 deletions

View File

@ -112,7 +112,6 @@ class Solidity {
}
}
};
self._compile(jsonObj, returnAllErrors, callback);
},
function createCompiledObject(output, callback) {
@ -164,7 +163,7 @@ class Solidity {
let self = this;
let input = {};
let originalFilepath = {};
async.waterfall([
function prepareInput(callback) {
async.each(contractFiles,
@ -180,7 +179,7 @@ class Solidity {
remapImports.prepareForCompilation(file, options.isCoverage)
.then(fileContent => {
input[filename] = {content: fileContent.replace(/\r\n/g, '\n')};
input[file.path] = {content: fileContent.replace(/\r\n/g, '\n')};
fileCb();
}).catch((_e) => {
self.logger.error(__('Error while loading the content of ') + filename);

View File

@ -84,11 +84,16 @@ const buildNewFile = (file: File, importPath: string) => {
return new File({ path: to, type: Types.dappFile });
};
const rescursivelyFindRemapImports = async (file: File) => {
const rescursivelyFindRemapImports = async (file: File, filesProcessed: string[] = []) => {
let remapImports: RemapImport[] = [];
const content = await file.content;
const imports = getImports(content);
if (filesProcessed.includes(file.path)) {
return [];
}
filesProcessed.push(file.path);
// if no imports, break recursion
if (!imports.length) {
return [];
@ -99,7 +104,7 @@ const rescursivelyFindRemapImports = async (file: File) => {
file.importRemappings.push({prefix: importPath, target: newFile.path});
remapImports.push({path: file.path, searchValue: importPath, replaceValue: newFile.path});
remapImports = remapImports.concat(
await rescursivelyFindRemapImports(newFile),
await rescursivelyFindRemapImports(newFile, filesProcessed),
);
}
@ -123,7 +128,7 @@ const replaceImports = (remapImports: RemapImport[]) => {
Object.keys(byPath).forEach((p) => {
let source = fs.readFileSync(p, "utf-8");
byPath[p].forEach(({searchValue, replaceValue}) => {
source = source.replace(`import "${searchValue}";`, `import "${replaceValue}";`);
source = source.replace(`${searchValue}`, `${replaceValue}`);
});
fs.writeFileSync(p, source);
});