From 555ffaf79f7d4871e5baa5247de67b7334ed2643 Mon Sep 17 00:00:00 2001 From: Valery Aligorsky Date: Sun, 17 May 2020 15:20:26 +0200 Subject: [PATCH] feat(abstract): add support for abstract class --- lib/exportsAnalyzer.ts | 2 ++ lib/fileAnalyzer.ts | 4 +++- test/compiled/AbstractContract.sol | 10 ++++++++++ test/contracts/AbstractContract.sol | 9 +++++++++ test/exportsAnalyzer.spec.ts | 9 ++++++++- test/index.spec.ts | 4 ++++ 6 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 test/compiled/AbstractContract.sol create mode 100644 test/contracts/AbstractContract.sol diff --git a/lib/exportsAnalyzer.ts b/lib/exportsAnalyzer.ts index e2f2b3b..9c8d5e5 100644 --- a/lib/exportsAnalyzer.ts +++ b/lib/exportsAnalyzer.ts @@ -5,6 +5,7 @@ import { ExportType } from './types'; const error = Debug('sol-merger:error'); export interface ExportsAnalyzerResult { + abstact: boolean; type: ExportType; name: string; is: string; @@ -30,6 +31,7 @@ export class ExportsAnalyzer { const visitor = new SolidityExportVisitor(this.contents); visitor.visit((e) => { results.push({ + abstact: e.abstract, type: e.type, name: e.name, body: this.contents.substring(e.body.start, e.body.end + 1).trim(), diff --git a/lib/fileAnalyzer.ts b/lib/fileAnalyzer.ts index 8389095..accfba2 100644 --- a/lib/fileAnalyzer.ts +++ b/lib/fileAnalyzer.ts @@ -39,7 +39,9 @@ export class FileAnalyzer { } }); } - return `${e.type} ${newName || e.name} ${is}${e.body}`; + + const abstract = e.abstact ? 'abstract ' : ''; + return `${abstract}${e.type} ${newName || e.name} ${is}${e.body}`; } /** * Filename to read to get contract data diff --git a/test/compiled/AbstractContract.sol b/test/compiled/AbstractContract.sol new file mode 100644 index 0000000..6aad5be --- /dev/null +++ b/test/compiled/AbstractContract.sol @@ -0,0 +1,10 @@ +pragma solidity ^0.6.0; + + +abstract contract Feline { + function utterance() public virtual returns (bytes32); +} + +contract Cat is Feline { + function utterance() public override returns (bytes32) { return "miaow"; } +} \ No newline at end of file diff --git a/test/contracts/AbstractContract.sol b/test/contracts/AbstractContract.sol new file mode 100644 index 0000000..514f32e --- /dev/null +++ b/test/contracts/AbstractContract.sol @@ -0,0 +1,9 @@ +pragma solidity ^0.6.0; + +abstract contract Feline { + function utterance() public virtual returns (bytes32); +} + +contract Cat is Feline { + function utterance() public override returns (bytes32) { return "miaow"; } +} \ No newline at end of file diff --git a/test/exportsAnalyzer.spec.ts b/test/exportsAnalyzer.spec.ts index 37dc324..0565097 100644 --- a/test/exportsAnalyzer.spec.ts +++ b/test/exportsAnalyzer.spec.ts @@ -33,32 +33,37 @@ describe('ExportsAnalyzer', () => { const exports = exportsAnalyzer.analyzeExports(); assert.deepEqual(exports, [ - { type: 'contract', name: 'A', is: '', body: '{ }' }, + { abstact: false, type: 'contract', name: 'A', is: '', body: '{ }' }, { + abstact: false, type: 'contract', name: 'B', is: 'is A ', body: '{\n // some body here...\n }', }, { + abstact: false, type: 'library', name: 'L', is: '', body: '{\n // l...\n }', }, { + abstact: false, type: 'interface', name: 'B', is: '', body: '{\n // i...\n }', }, { + abstact: false, body: '{\n uint weight;\n bool voted;\n }', is: '', name: 'S', type: 'struct', }, { + abstact: false, body: '{\n Created,\n Locked,\n Inactive\n }', is: '', @@ -78,12 +83,14 @@ describe('ExportsAnalyzer', () => { assert.deepEqual(exports, [ { + abstact: false, body: '// Some contracts without exports', is: '', name: 'Comment#9', type: 'comment', }, { + abstact: false, body: '// Some contract text that is not required here', is: '', name: 'Comment#52', diff --git a/test/index.spec.ts b/test/index.spec.ts index d695c24..4e552c0 100644 --- a/test/index.spec.ts +++ b/test/index.spec.ts @@ -87,6 +87,10 @@ describe('Solidity Merger', () => { await testFile('GlobalComments'); }); + it('should compile abstract contracts', async () => { + await testFile('AbstractContract'); + }); + it('should compile file without imports and exports (empty content)', async () => { const merger = new Merger(); const file = path.join(__dirname, `/contracts/EmptyFile.sol`);