Skip to content

Commit 89e891d

Browse files
Amxxfrangio
andauthored
Simplify peer project mode by renaming imported peer symbols
Co-authored-by: Francisco Giordano <[email protected]>
1 parent 24f406e commit 89e891d

14 files changed

+154
-127
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 0.3.31 (2023-09-30)
4+
5+
- Simplify peer project mode by renaming imported peer symbols.
6+
37
## 0.3.30 (2023-09-27)
48

59
- Fix `@inheritdoc` in peer project mode.

src/generate-with-init.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ export function generateWithInit(
2323

2424
for (const sourceUnit of transform.asts()) {
2525
for (const contract of findAll('ContractDefinition', sourceUnit)) {
26-
if (contract.contractKind !== 'contract' || !contract.fullyImplemented) {
26+
if (
27+
contract.contractKind !== 'contract' ||
28+
!contract.fullyImplemented ||
29+
transform.getData(contract).importFromPeer !== undefined
30+
) {
2731
continue;
2832
}
2933

src/index.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export async function transpile(
101101
transform.apply(renameContractDefinition);
102102
transform.apply(renameInheritdoc);
103103
transform.apply(prependInitializableBase);
104-
transform.apply(fixImportDirectives(options.peerProject !== undefined));
104+
transform.apply(fixImportDirectives);
105105
transform.apply(appendInitializableImport(outputPaths.initializable));
106106
transform.apply(fixNewStatement);
107107
transform.apply(transformConstructor(namespaceInclude));
@@ -110,13 +110,12 @@ export async function transpile(
110110
transform.apply(removeInheritanceListArguments);
111111
transform.apply(removeStateVarInits);
112112
transform.apply(removeImmutable);
113-
transform.apply(peerImport);
114-
115113
if (options.namespaced) {
116114
transform.apply(addNamespaceStruct(namespaceInclude));
117115
} else {
118116
transform.apply(addStorageGaps);
119117
}
118+
transform.apply(peerImport);
120119

121120
// build a final array of files to return
122121
const outputFiles: OutputFile[] = [];
@@ -169,7 +168,7 @@ function transpileInitializable(
169168
transform.apply(function* (ast, tools) {
170169
if (ast.absolutePath === options.initializablePath) {
171170
yield* renameIdentifiers(ast, tools);
172-
yield* fixImportDirectives(options.peerProject !== undefined)(ast, tools);
171+
yield* fixImportDirectives(ast, tools);
173172
}
174173
});
175174

src/prepare-peer-project.test.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,13 @@ test.serial.before('compile', async t => {
2929
});
3030
});
3131

32-
for (const fileName of ['ISomeInterface.sol', 'SomeLibrary.sol', 'SomeStatelessContract.sol']) {
33-
test(`do not transpile ${fileName}`, t => {
34-
const file = t.context.files.find(f => f.fileName === fileName);
35-
// source file exists
36-
t.true(t.context.inputs.includes(path.join(projectDir, fileName)));
37-
// transpiled file does not exist
38-
t.is(file, undefined, 'file should not be transpiled');
39-
});
40-
}
41-
42-
for (const fileName of ['SomeContract.sol', 'SomeOtherContract.sol']) {
32+
for (const fileName of [
33+
'ISomeInterface.sol',
34+
'SomeLibrary.sol',
35+
'SomeStatelessContract.sol',
36+
'SomeContract.sol',
37+
'SomeOtherContract.sol',
38+
]) {
4339
test(`transpile ${fileName}`, t => {
4440
const file = t.context.files.find(f => f.fileName === fileName);
4541
// source file exists

src/prepare-peer-project.test.ts.md

Lines changed: 61 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,50 @@ The actual snapshot is saved in `prepare-peer-project.test.ts.snap`.
44

55
Generated by [AVA](https://avajs.dev).
66

7+
## transpile ISomeInterface.sol
8+
9+
> Snapshot 1
10+
11+
{
12+
fileName: 'ISomeInterface.sol',
13+
path: 'contracts/project/ISomeInterfaceUpgradeable.sol',
14+
source: `// SPDX-License-Identifier: UNLICENSED␊
15+
pragma solidity ^0.8.0;␊
16+
17+
import {ISomeInterface as ISomeInterfaceUpgradeable} from "@openzeppelin/contracts/project/ISomeInterface.sol";␊
18+
`,
19+
}
20+
21+
## transpile SomeLibrary.sol
22+
23+
> Snapshot 1
24+
25+
{
26+
fileName: 'SomeLibrary.sol',
27+
path: 'contracts/project/SomeLibraryUpgradeable.sol',
28+
source: `// SPDX-License-Identifier: UNLICENSED␊
29+
pragma solidity ^0.8.0;␊
30+
31+
import {ISomeInterfaceUpgradeable} from "./ISomeInterfaceUpgradeable.sol";␊
32+
33+
import {SomeLibrary as SomeLibraryUpgradeable} from "@openzeppelin/contracts/project/SomeLibrary.sol";␊
34+
`,
35+
}
36+
37+
## transpile SomeStatelessContract.sol
38+
39+
> Snapshot 1
40+
41+
{
42+
fileName: 'SomeStatelessContract.sol',
43+
path: 'contracts/project/SomeStatelessContractUpgradeable.sol',
44+
source: `// SPDX-License-Identifier: UNLICENSED␊
45+
pragma solidity ^0.8.0;␊
46+
47+
import {SomeStatelessContract as SomeStatelessContractUpgradeable} from "@openzeppelin/contracts/project/SomeStatelessContract.sol";␊
48+
`,
49+
}
50+
751
## transpile SomeContract.sol
852

953
> Snapshot 1
@@ -14,27 +58,27 @@ Generated by [AVA](https://avajs.dev).
1458
source: `// SPDX-License-Identifier: UNLICENSED␊
1559
pragma solidity ^0.8.0;␊
1660
17-
import {ISomeInterface} from "@openzeppelin/contracts/project/ISomeInterface.sol";␊
18-
import {SomeLibrary} from "@openzeppelin/contracts/project/SomeLibrary.sol";␊
19-
import {SomeStatelessContract} from "@openzeppelin/contracts/project/SomeStatelessContract.sol";␊
61+
import {ISomeInterfaceUpgradeable} from "./ISomeInterfaceUpgradeable.sol";␊
62+
import {SomeLibraryUpgradeable} from "./SomeLibraryUpgradeable.sol";␊
63+
import {SomeStatelessContractUpgradeable} from "./SomeStatelessContractUpgradeable.sol";␊
2064
import {Initializable} from "../Initializable.sol";␊
2165
22-
import { ISomeContract } from "@openzeppelin/contracts/project/SomeContract.sol";␊
66+
import {ISomeContract as ISomeContractUpgradeable} from "@openzeppelin/contracts/project/SomeContract.sol";␊
2367
24-
import { Error1 } from "@openzeppelin/contracts/project/SomeContract.sol";␊
68+
import {Error1 as Error1Upgradeable} from "@openzeppelin/contracts/project/SomeContract.sol";␊
2569
26-
import { freeFn_1 } from "@openzeppelin/contracts/project/SomeContract.sol";␊
70+
import {freeFn_1 as freeFn_1Upgradeable} from "@openzeppelin/contracts/project/SomeContract.sol";␊
2771
28-
import { SomeStruct } from "@openzeppelin/contracts/project/SomeContract.sol";␊
72+
import {SomeStruct as SomeStructUpgradeable} from "@openzeppelin/contracts/project/SomeContract.sol";␊
2973
3074
contract SomeBaseContractUpgradeable is Initializable {␊
3175
function __SomeBaseContract_init() internal onlyInitializing {␊
3276
}␊
3377
3478
function __SomeBaseContract_init_unchained() internal onlyInitializing {␊
3579
}␊
36-
function test(ISomeInterface other) public virtual returns (bool) {␊
37-
return SomeLibrary.bothFunctions(other);␊
80+
function test(ISomeInterfaceUpgradeable other) public virtual returns (bool) {␊
81+
return SomeLibraryUpgradeable.bothFunctions(other);␊
3882
}␊
3983
4084
/**␊
@@ -45,27 +89,27 @@ Generated by [AVA](https://avajs.dev).
4589
uint256[50] private __gap;␊
4690
}␊
4791
48-
contract SomeContractUpgradeable is Initializable, ISomeContract, SomeBaseContractUpgradeable {␊
92+
contract SomeContractUpgradeable is Initializable, ISomeContractUpgradeable, SomeBaseContractUpgradeable {␊
4993
SomeStruct s;␊
5094
5195
function __SomeContract_init() internal onlyInitializing {␊
5296
}␊
5397
5498
function __SomeContract_init_unchained() internal onlyInitializing {␊
5599
}␊
56-
/// @inheritdoc ISomeInterface
100+
/// @inheritdoc ISomeInterfaceUpgradeable
57101
function someFunction() public pure override returns (bool) {␊
58102
return false;␊
59103
}␊
60104
61-
/// @inheritdoc ISomeInterface
105+
/// @inheritdoc ISomeInterfaceUpgradeable
62106
function someOtherFunction() public pure override returns (bool) {␊
63107
return true;␊
64108
}␊
65109
66110
/// @inheritdoc SomeBaseContractUpgradeable␊
67-
function test(ISomeInterface other) public override returns (bool) {␊
68-
return SomeLibrary.bothFunctions(this) && super.test(other);␊
111+
function test(ISomeInterfaceUpgradeable other) public override returns (bool) {␊
112+
return SomeLibraryUpgradeable.bothFunctions(this) && super.test(other);␊
69113
}␊
70114
71115
/**␊
@@ -88,10 +132,9 @@ Generated by [AVA](https://avajs.dev).
88132
source: `// SPDX-License-Identifier: UNLICENSED␊
89133
pragma solidity ^0.8.0;␊
90134
91-
import {ISomeInterface} from "@openzeppelin/contracts/project/ISomeInterface.sol";␊
92-
import {SomeLibrary} from "@openzeppelin/contracts/project/SomeLibrary.sol";␊
93-
import {ISomeContract} from "@openzeppelin/contracts/project/SomeContract.sol";␊
94-
import {SomeContractUpgradeable} from "./SomeContractUpgradeable.sol";␊
135+
import {ISomeInterfaceUpgradeable} from "./ISomeInterfaceUpgradeable.sol";␊
136+
import {SomeLibraryUpgradeable} from "./SomeLibraryUpgradeable.sol";␊
137+
import {ISomeContractUpgradeable, SomeContractUpgradeable} from "./SomeContractUpgradeable.sol";␊
95138
import {Initializable} from "../Initializable.sol";␊
96139
97140
contract SomeOtherContractUpgradeable is Initializable, SomeContractUpgradeable {␊

src/prepare-peer-project.test.ts.snap

157 Bytes
Binary file not shown.

src/prepare-peer-project.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@ import { isStorageVariable } from './transformations/utils/is-storage-variable';
66

77
export function preparePeerProject(transform: Transform, peerProject: string) {
88
for (const ast of transform.asts()) {
9-
let shouldExclude = true;
109
for (const node of ast.nodes) {
1110
switch (node.nodeType) {
1211
case 'ContractDefinition': {
1312
if (node.contractKind === 'contract') {
1413
if (!extractContractStateless(node)) {
15-
shouldExclude = false;
16-
break;
14+
continue;
1715
}
1816
if (extractContractStorageSize(node) !== undefined) {
1917
throw transform.error(
@@ -58,8 +56,5 @@ export function preparePeerProject(transform: Transform, peerProject: string) {
5856
}
5957
}
6058
}
61-
if (shouldExclude) {
62-
transform.exclude(ast.absolutePath);
63-
}
6459
}
6560
}

src/transform.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,14 @@ test('skip contract rename when Upgradeable suffix', t => {
121121

122122
test('fix import directives', t => {
123123
const file = 'contracts/solc-0.6/Local.sol';
124-
t.context.transform.apply(fixImportDirectives(false));
124+
t.context.transform.apply(fixImportDirectives);
125125
t.snapshot(t.context.transform.results()[file]);
126126
});
127127

128128
test('fix import directives complex', t => {
129129
const file = 'contracts/TransformImport2.sol';
130130
t.context.transform.apply(renameIdentifiers);
131-
t.context.transform.apply(fixImportDirectives(false));
131+
t.context.transform.apply(fixImportDirectives);
132132
t.snapshot(t.context.transform.results()[file]);
133133
});
134134

src/transform.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,6 @@ export class Transform {
186186
asts(): SourceUnit[] {
187187
return Object.values(this.state).map(s => s.ast);
188188
}
189-
190-
exclude(source: string) {
191-
delete this.state[source];
192-
}
193189
}
194190

195191
function insertSortedAndValidate(

src/transformations/append-initializable-import.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,17 @@ import { relativePath } from '../utils/relative-path';
77

88
import { getNodeBounds } from '../solc/ast-utils';
99
import { Transformation } from './type';
10+
import { TransformerTools } from '../transform';
1011

1112
export function appendInitializableImport(initializablePath: string) {
12-
return function* (sourceUnit: SourceUnit): Generator<Transformation> {
13+
return function* (
14+
sourceUnit: SourceUnit,
15+
{ getData }: TransformerTools,
16+
): Generator<Transformation> {
1317
const contracts = [...findAll('ContractDefinition', sourceUnit)];
14-
if (!contracts.some(c => c.contractKind === 'contract')) {
18+
if (
19+
!contracts.some(c => c.contractKind === 'contract' && getData(c).importFromPeer === undefined)
20+
) {
1521
return;
1622
}
1723

0 commit comments

Comments
 (0)