Skip to content

Commit 9bf06ae

Browse files
committed
refacotr how we handle files already parsed
1 parent abc89b2 commit 9bf06ae

File tree

2 files changed

+34
-16
lines changed

2 files changed

+34
-16
lines changed

lib/core/file.js

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,16 @@ class File {
1515
}
1616

1717
parseFileForImport(content, isHttpContract, callback) {
18+
const self = this;
1819
if (typeof isHttpContract === 'function') {
1920
callback = isHttpContract;
2021
isHttpContract = false;
2122
}
22-
const self = this;
23+
if (self.parsedImports) {
24+
// We already parsed this file
25+
return callback();
26+
}
27+
self.parsedImports = true;
2328
if (self.filename.indexOf('.sol') < 0) {
2429
// Only supported in Solidity
2530
return callback();
@@ -98,34 +103,25 @@ class File {
98103
content = fs.readFileSync(this.path).toString();
99104
} else if (this.type === File.types.custom) {
100105
return this.resolver((theContent) => {
101-
if (!this.parsedImports) {
102-
this.parsedImports = true;
103-
return this.parseFileForImport(content, () => {
104-
callback(theContent);
105-
});
106-
}
107-
callback(theContent);
106+
this.parseFileForImport(content, () => {
107+
callback(theContent);
108+
});
108109
});
109110
} else if (this.type === File.types.http) {
110111
return this.downloadFile(this.filename, this.path, (content) => {
111112
if (!content) {
112113
return callback(content);
113114
}
114-
this.parsedImports = true;
115115
this.path = this.filename;
116116
this.type = File.types.dapp_file;
117117
callback(content);
118118
});
119119
} else {
120120
throw new Error("unknown file: " + this.filename);
121121
}
122-
if (!this.parsedImports) {
123-
this.parsedImports = true;
124-
return this.parseFileForImport(content, () => {
125-
callback(content);
126-
});
127-
}
128-
callback(content);
122+
return this.parseFileForImport(content, () => {
123+
callback(content);
124+
});
129125
}
130126

131127
}

test/file.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,27 @@ describe('embark.File', function () {
5959
done();
6060
});
6161
});
62+
63+
it('should find all the imports but only once if called twice', function (done) {
64+
const contract = fs.readFileSync('./test/contracts/contract_with_http_import.sol').toString();
65+
const file = new File({filename: '.embark/contracts/embark-framework/embark/master/test_app/app/contracts/simple_storage.sol',
66+
path: 'https://raw.githubusercontent.com/embark-framework/embark/develop/test_apps/test_app/app/contracts/simple_storage.sol'});
67+
const downloadFileStub = sinon.stub(file, 'downloadFile')
68+
.callsFake((path, url, cb) => {
69+
cb();
70+
});
71+
72+
file.parseFileForImport(contract, () => {
73+
// Parse again
74+
file.parseFileForImport(contract, () => {
75+
assert.strictEqual(downloadFileStub.callCount, 1);
76+
assert.strictEqual(downloadFileStub.firstCall.args[0],
77+
'.embark/contracts/embark-framework/embark/develop/test_apps/contracts_app/contracts/contract_args.sol');
78+
assert.strictEqual(downloadFileStub.firstCall.args[1],
79+
'https://raw.githubusercontent.com/embark-framework/embark/develop/test_apps/contracts_app/contracts/contract_args.sol');
80+
done();
81+
});
82+
});
83+
});
6284
});
6385
});

0 commit comments

Comments
 (0)