Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Named import and ABI v2 encoder pragma #18

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 27 additions & 9 deletions lib/merger.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,25 @@ class Merger {
getPragmaRegex() {
return /(pragma solidity (.+?);)/g;
}
getABIEncoderPragmaRegex() {
return /(pragma experimental ABIEncoderV2;)/g;
}

getImportRegex() {
return /import ['"](.+?)['"];/g;
return /import .*['"](.+?)['"];/g;
}

getPragma(contents) {
let group = this.getPragmaRegex().exec(contents);
return group && group[1];
}
getABIEncoderPragma(contents) {
let group = this.getABIEncoderPragmaRegex().exec(contents);
return group && group[1];
}

stripPragma(contents) {
return contents.replace(this.getPragmaRegex(), '').trim();
return contents.replace(this.getPragmaRegex(), '').replace(this.getABIEncoderPragmaRegex(), '').trim();
}

async processFile(file, isRoot) {
Expand All @@ -51,21 +58,32 @@ class Merger {
let contents = await fs.readFileAsync(file, { encoding: 'utf-8' });

let result = '';
let importedContents = '';

let imports = await this.processImports(file, contents);

for (let i = 0; i < imports.length; i++) {
importedContents += imports[i] + this.delimeter;
}

if (isRoot) {
let pragma = this.getPragma(contents);
result = pragma + this.delimeter;
}
contents = this.stripPragma(contents);
let imports = await this.processImports(file, contents);

for (let i = 0; i < imports.length; i++) {
result += imports[i] + this.delimeter;
let hasExperimentalPragma = this.getABIEncoderPragma(contents) || this.getABIEncoderPragma(importedContents);
if (hasExperimentalPragma) {
result = result + 'pragma experimental ABIEncoderV2;' + this.delimeter;
}

contents = this.stripPragma(contents);
importedContents = this.stripPragma(importedContents);
}

contents = this.stripImports(contents);
result += contents;

importedContents = this.stripImports(importedContents);

result += importedContents + this.delimeter + contents;
result = result.replace(/^\s*[\r\n]/gm, '\n');
return result;
}

Expand Down
2 changes: 2 additions & 0 deletions test/compiled/LocalImports.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
pragma solidity ^0.4.11;

pragma experimental ABIEncoderV2;

contract Ownable {
address public owner;

Expand Down
1 change: 0 additions & 1 deletion test/compiled/MultiImports.sol
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ contract StandardToken is ERC20, BasicToken {

mapping (address => mapping (address => uint256)) internal allowed;


/**
* @dev Transfer tokens from one address to another
* @param _from address The address which you want to send tokens from
Expand Down
9 changes: 9 additions & 0 deletions test/contracts/NamedImports.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
pragma solidity ^0.4.11;

import {Ownable} from "./imports/ownable.sol";

contract MyOwned is Ownable {
string public constant name = "My Owned";

function MyOwned() {}
}
1 change: 1 addition & 0 deletions test/contracts/imports/ownable.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pragma solidity ^0.4.11;
pragma experimental ABIEncoderV2;

contract Ownable {
address public owner;
Expand Down
9 changes: 8 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,15 @@ describe('Solidity Merger', () => {
let file = path.join(__dirname, '/contracts/MultiImports.sol');

let result = await merger.processFile(file, true);

assertWithFile(result, 'MultiImports.sol');
});

it('should import named contract', async () => {
let merger = new Merger({ delimeter: '\n\n' });
let file = path.join(__dirname, '/contracts/NamedImports.sol');

let result = await merger.processFile(file, true);

assertWithFile(result, 'LocalImports.sol');
});
});