Skip to content

Commit

Permalink
feat(sol-0.8): add support for export of user defined types at root l…
Browse files Browse the repository at this point in the history
…evel
  • Loading branch information
RyuuGan committed Feb 7, 2022
1 parent 7a9e2db commit 6ae568b
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 2 deletions.
28 changes: 28 additions & 0 deletions lib/antlr/visitors/exportVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
SolidityParser,
SourceUnitContext,
StructDefinitionContext,
UserDefinedValueTypeDefinitionContext,
} from '../generated/SolidityParser';
import { ExportVisitResult, VisitCallback } from './types';

Expand Down Expand Up @@ -398,4 +399,31 @@ class ExportVisitor implements SolidityParserListener {
name: name.text,
});
}

enterUserDefinedValueTypeDefinition(
ctx: UserDefinedValueTypeDefinitionContext,
): void {
if (!(ctx.parent instanceof SourceUnitContext)) {
return;
}

if (!ctx.stop) {
return;
}

const start = ctx.start.startIndex;
const end = ctx.stop.stopIndex;
const name = ctx.identifier();

if (!name?.stop) {
return;
}

this.#onVisit({
type: ExportType.userDefinedValueType,
start,
end,
name: name.text,
});
}
}
9 changes: 8 additions & 1 deletion lib/antlr/visitors/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ export interface ImportVisitNamedImport {
export type ExportVisitResult =
| ExportVisitResultContractLike
| ExportVisitResultConstant
| ExportVisitResultFunction;
| ExportVisitResultFunction
| ExportVisitResultUserDefinedValueType;

export interface ExportVisitResultContractLike extends RangeVisitResult {
abstract: boolean;
Expand All @@ -41,4 +42,10 @@ export interface ExportVisitResultFunction extends RangeVisitResult {
name: string;
}

export interface ExportVisitResultUserDefinedValueType
extends RangeVisitResult {
type: ExportType.userDefinedValueType;
name: string;
}

export type VisitCallback<T extends RangeVisitResult> = (v: T) => void;
27 changes: 26 additions & 1 deletion lib/exportsAnalyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { SolidityExportVisitor } from './antlr/visitors/exportVisitor';
import {
ExportVisitResultConstant,
ExportVisitResultFunction,
ExportVisitResultUserDefinedValueType,
} from './antlr/visitors/types';
import { ContractLikeExportType, ExportType } from './types';

Expand All @@ -11,7 +12,8 @@ const error = Debug('sol-merger:error');
export type ExportsAnalyzerResult =
| ExportsAnalyzerResultContractLike
| ExportsAnalyzerResultConstant
| ExportsAnalyzerResultFunction;
| ExportsAnalyzerResultFunction
| ExportsAnalyzerResultUserDefinedValueType;

export interface ExportsAnalyzerResultContractLike {
abstract: boolean;
Expand All @@ -34,6 +36,12 @@ export interface ExportsAnalyzerResultFunction {
body: string;
}

export interface ExportsAnalyzerResultUserDefinedValueType {
type: ExportType.userDefinedValueType;
name: string;
body: string;
}

export class ExportsAnalyzer {
constructor(private contents: string) {}

Expand Down Expand Up @@ -63,6 +71,13 @@ export class ExportsAnalyzer {
results.push(functionExport);
return;
}

if (e.type === ExportType.userDefinedValueType) {
const userDefinedValueTypeExport =
this.analyzeExportUserDefinedValueType(e);
results.push(userDefinedValueTypeExport);
return;
}
results.push({
abstract: e.abstract,
type: e.type,
Expand Down Expand Up @@ -100,4 +115,14 @@ export class ExportsAnalyzer {
type: ExportType.function,
};
}

private analyzeExportUserDefinedValueType(
e: ExportVisitResultUserDefinedValueType,
): ExportsAnalyzerResultUserDefinedValueType {
return {
body: this.contents.substring(e.start, e.end + 1),
name: e.name,
type: ExportType.userDefinedValueType,
};
}
}
4 changes: 4 additions & 0 deletions lib/fileAnalyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ export class FileAnalyzer {
return e.body;
}

if (e.type === ExportType.userDefinedValueType) {
return e.body;
}

let is = e.is;
if (is) {
globalRenames.forEach((i) => {
Expand Down
8 changes: 8 additions & 0 deletions test/compiled/ContractWithUserDefinitionType.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
pragma solidity ^0.8.8;


type Decimal18 is uint256;

interface MinimalERC20 {
function transfer(address to, Decimal18 value) external;
}
7 changes: 7 additions & 0 deletions test/contracts/ContractWithUserDefinitionType.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pragma solidity ^0.8.8;

type Decimal18 is uint256;

interface MinimalERC20 {
function transfer(address to, Decimal18 value) external;
}
16 changes: 16 additions & 0 deletions test/exportsAnalyzer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,5 +167,21 @@ describe('ExportsAnalyzer', () => {
},
]);
});

it('should analyze user defined value type export', () => {
const exportsAnalyzer = new ExportsAnalyzer(`
type Decimal18 is uint256;
`);

const exports = exportsAnalyzer.analyzeExports();

assert.deepEqual(exports, [
{
name: 'Decimal18',
type: ExportType.userDefinedValueType,
body: `type Decimal18 is uint256;`,
},
]);
});
});
});
4 changes: 4 additions & 0 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,8 @@ describe('Solidity Merger', () => {
it('should compile file with functions at root level (0.8 support)', async () => {
await testFile('ContractWithTopLevelFunction');
});

it('should compile file with user defined types at root level (0.8 support)', async () => {
await testFile('ContractWithUserDefinitionType');
});
});

0 comments on commit 6ae568b

Please sign in to comment.