Skip to content

Commit f0d12a6

Browse files
committed
Revert "Simplify peer project mode by renaming imported peer symbols"
This reverts commit 89e891d.
1 parent 1dcac9b commit f0d12a6

14 files changed

+132
-151
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
# Changelog
22

3+
## 0.3.32 (2023-09-30)
4+
5+
- Revert 0.3.31 changes.
6+
37
## 0.3.31 (2023-09-30)
48

5-
- Simplify peer project mode by renaming imported peer symbols.
9+
- (Reverted in 0.3.32) ~Simplify peer project mode by renaming imported peer symbols.~
610

711
## 0.3.30 (2023-09-27)
812

src/generate-with-init.ts

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

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

src/index.ts

Lines changed: 4 additions & 3 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);
104+
transform.apply(fixImportDirectives(options.peerProject !== undefined));
105105
transform.apply(appendInitializableImport(outputPaths.initializable));
106106
transform.apply(fixNewStatement);
107107
transform.apply(transformConstructor(namespaceInclude));
@@ -110,12 +110,13 @@ export async function transpile(
110110
transform.apply(removeInheritanceListArguments);
111111
transform.apply(removeStateVarInits);
112112
transform.apply(removeImmutable);
113+
transform.apply(peerImport);
114+
113115
if (options.namespaced) {
114116
transform.apply(addNamespaceStruct(namespaceInclude));
115117
} else {
116118
transform.apply(addStorageGaps);
117119
}
118-
transform.apply(peerImport);
119120

120121
// build a final array of files to return
121122
const outputFiles: OutputFile[] = [];
@@ -168,7 +169,7 @@ function transpileInitializable(
168169
transform.apply(function* (ast, tools) {
169170
if (ast.absolutePath === options.initializablePath) {
170171
yield* renameIdentifiers(ast, tools);
171-
yield* fixImportDirectives(ast, tools);
172+
yield* fixImportDirectives(options.peerProject !== undefined)(ast, tools);
172173
}
173174
});
174175

src/prepare-peer-project.test.ts

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

32-
for (const fileName of [
33-
'ISomeInterface.sol',
34-
'SomeLibrary.sol',
35-
'SomeStatelessContract.sol',
36-
'SomeContract.sol',
37-
'SomeOtherContract.sol',
38-
]) {
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']) {
3943
test(`transpile ${fileName}`, t => {
4044
const file = t.context.files.find(f => f.fileName === fileName);
4145
// source file exists

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

Lines changed: 18 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -4,50 +4,6 @@ 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-
517
## transpile SomeContract.sol
528

539
> Snapshot 1
@@ -58,27 +14,27 @@ Generated by [AVA](https://avajs.dev).
5814
source: `// SPDX-License-Identifier: UNLICENSED␊
5915
pragma solidity ^0.8.0;␊
6016
61-
import {ISomeInterfaceUpgradeable} from "./ISomeInterfaceUpgradeable.sol";␊
62-
import {SomeLibraryUpgradeable} from "./SomeLibraryUpgradeable.sol";␊
63-
import {SomeStatelessContractUpgradeable} from "./SomeStatelessContractUpgradeable.sol";␊
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";␊
6420
import {Initializable} from "../Initializable.sol";␊
6521
66-
import {ISomeContract as ISomeContractUpgradeable} from "@openzeppelin/contracts/project/SomeContract.sol";␊
22+
import { ISomeContract } from "@openzeppelin/contracts/project/SomeContract.sol";␊
6723
68-
import {Error1 as Error1Upgradeable} from "@openzeppelin/contracts/project/SomeContract.sol";␊
24+
import { Error1 } from "@openzeppelin/contracts/project/SomeContract.sol";␊
6925
70-
import {freeFn_1 as freeFn_1Upgradeable} from "@openzeppelin/contracts/project/SomeContract.sol";␊
26+
import { freeFn_1 } from "@openzeppelin/contracts/project/SomeContract.sol";␊
7127
72-
import {SomeStruct as SomeStructUpgradeable} from "@openzeppelin/contracts/project/SomeContract.sol";␊
28+
import { SomeStruct } from "@openzeppelin/contracts/project/SomeContract.sol";␊
7329
7430
contract SomeBaseContractUpgradeable is Initializable {␊
7531
function __SomeBaseContract_init() internal onlyInitializing {␊
7632
}␊
7733
7834
function __SomeBaseContract_init_unchained() internal onlyInitializing {␊
7935
}␊
80-
function test(ISomeInterfaceUpgradeable other) public virtual returns (bool) {␊
81-
return SomeLibraryUpgradeable.bothFunctions(other);␊
36+
function test(ISomeInterface other) public virtual returns (bool) {␊
37+
return SomeLibrary.bothFunctions(other);␊
8238
}␊
8339
8440
/**␊
@@ -89,27 +45,27 @@ Generated by [AVA](https://avajs.dev).
8945
uint256[50] private __gap;␊
9046
}␊
9147
92-
contract SomeContractUpgradeable is Initializable, ISomeContractUpgradeable, SomeBaseContractUpgradeable {␊
48+
contract SomeContractUpgradeable is Initializable, ISomeContract, SomeBaseContractUpgradeable {␊
9349
SomeStruct s;␊
9450
9551
function __SomeContract_init() internal onlyInitializing {␊
9652
}␊
9753
9854
function __SomeContract_init_unchained() internal onlyInitializing {␊
9955
}␊
100-
/// @inheritdoc ISomeInterfaceUpgradeable
56+
/// @inheritdoc ISomeInterface
10157
function someFunction() public pure override returns (bool) {␊
10258
return false;␊
10359
}␊
10460
105-
/// @inheritdoc ISomeInterfaceUpgradeable
61+
/// @inheritdoc ISomeInterface
10662
function someOtherFunction() public pure override returns (bool) {␊
10763
return true;␊
10864
}␊
10965
11066
/// @inheritdoc SomeBaseContractUpgradeable␊
111-
function test(ISomeInterfaceUpgradeable other) public override returns (bool) {␊
112-
return SomeLibraryUpgradeable.bothFunctions(this) && super.test(other);␊
67+
function test(ISomeInterface other) public override returns (bool) {␊
68+
return SomeLibrary.bothFunctions(this) && super.test(other);␊
11369
}␊
11470
11571
/**␊
@@ -132,9 +88,10 @@ Generated by [AVA](https://avajs.dev).
13288
source: `// SPDX-License-Identifier: UNLICENSED␊
13389
pragma solidity ^0.8.0;␊
13490
135-
import {ISomeInterfaceUpgradeable} from "./ISomeInterfaceUpgradeable.sol";␊
136-
import {SomeLibraryUpgradeable} from "./SomeLibraryUpgradeable.sol";␊
137-
import {ISomeContractUpgradeable, SomeContractUpgradeable} from "./SomeContractUpgradeable.sol";␊
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";␊
13895
import {Initializable} from "../Initializable.sol";␊
13996
14097
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: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ 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;
910
for (const node of ast.nodes) {
1011
switch (node.nodeType) {
1112
case 'ContractDefinition': {
1213
if (node.contractKind === 'contract') {
1314
if (!extractContractStateless(node)) {
14-
continue;
15+
shouldExclude = false;
16+
break;
1517
}
1618
if (extractContractStorageSize(node) !== undefined) {
1719
throw transform.error(
@@ -56,5 +58,8 @@ export function preparePeerProject(transform: Transform, peerProject: string) {
5658
}
5759
}
5860
}
61+
if (shouldExclude) {
62+
transform.exclude(ast.absolutePath);
63+
}
5964
}
6065
}

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);
124+
t.context.transform.apply(fixImportDirectives(false));
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);
131+
t.context.transform.apply(fixImportDirectives(false));
132132
t.snapshot(t.context.transform.results()[file]);
133133
});
134134

src/transform.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,10 @@ 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+
}
189193
}
190194

191195
function insertSortedAndValidate(

src/transformations/append-initializable-import.ts

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

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

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

0 commit comments

Comments
 (0)