Skip to content

Commit 1caf2eb

Browse files
authored
Fix rename-inheritdoc with peer project (#133)
1 parent 8dc5010 commit 1caf2eb

File tree

4 files changed

+59
-20
lines changed

4 files changed

+59
-20
lines changed

contracts/project/SomeContract.sol

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,27 @@ struct SomeStruct {
1515
uint member;
1616
}
1717

18-
contract SomeContract is ISomeContract {
18+
contract SomeBaseContract {
19+
function test(ISomeInterface other) public virtual returns (bool) {
20+
return SomeLibrary.bothFunctions(other);
21+
}
22+
}
23+
24+
contract SomeContract is ISomeContract, SomeBaseContract {
1925
SomeStruct s;
2026

27+
/// @inheritdoc ISomeInterface
2128
function someFunction() public pure override returns (bool) {
2229
return false;
2330
}
2431

32+
/// @inheritdoc ISomeInterface
2533
function someOtherFunction() public pure override returns (bool) {
2634
return true;
2735
}
2836

29-
function test(ISomeInterface other) public returns (bool) {
30-
return SomeLibrary.bothFunctions(this) && SomeLibrary.bothFunctions(other);
37+
/// @inheritdoc SomeBaseContract
38+
function test(ISomeInterface other) public override returns (bool) {
39+
return SomeLibrary.bothFunctions(this) && super.test(other);
3140
}
3241
}

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

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# Snapshot report for `src/partial-transform.test.ts`
1+
# Snapshot report for `src/prepare-peer-project.test.ts`
22

3-
The actual snapshot is saved in `partial-transform.test.ts.snap`.
3+
The actual snapshot is saved in `prepare-peer-project.test.ts.snap`.
44

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

@@ -27,24 +27,45 @@ Generated by [AVA](https://avajs.dev).
2727
2828
import { SomeStruct } from "@openzeppelin/contracts/project/SomeContract.sol";␊
2929
30-
contract SomeContractUpgradeable is Initializable, ISomeContract {␊
30+
contract SomeBaseContractUpgradeable is Initializable {␊
31+
function __SomeBaseContract_init() internal onlyInitializing {␊
32+
}␊
33+
34+
function __SomeBaseContract_init_unchained() internal onlyInitializing {␊
35+
}␊
36+
function test(ISomeInterface other) public virtual returns (bool) {␊
37+
return SomeLibrary.bothFunctions(other);␊
38+
}␊
39+
40+
/**␊
41+
* @dev This empty reserved space is put in place to allow future versions to add new␊
42+
* variables without shifting down storage in the inheritance chain.␊
43+
* See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps␊
44+
*/␊
45+
uint256[50] private __gap;␊
46+
}␊
47+
48+
contract SomeContractUpgradeable is Initializable, ISomeContract, SomeBaseContractUpgradeable {␊
3149
SomeStruct s;␊
3250
3351
function __SomeContract_init() internal onlyInitializing {␊
3452
}␊
3553
3654
function __SomeContract_init_unchained() internal onlyInitializing {␊
3755
}␊
56+
/// @inheritdoc ISomeInterface␊
3857
function someFunction() public pure override returns (bool) {␊
3958
return false;␊
4059
}␊
4160
61+
/// @inheritdoc ISomeInterface␊
4262
function someOtherFunction() public pure override returns (bool) {␊
4363
return true;␊
4464
}␊
4565
46-
function test(ISomeInterface other) public returns (bool) {␊
47-
return SomeLibrary.bothFunctions(this) && SomeLibrary.bothFunctions(other);␊
66+
/// @inheritdoc SomeBaseContractUpgradeable␊
67+
function test(ISomeInterface other) public override returns (bool) {␊
68+
return SomeLibrary.bothFunctions(this) && super.test(other);␊
4869
}␊
4970
5071
/**␊

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

71 Bytes
Binary file not shown.

src/transformations/rename-inheritdoc.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,29 @@ import { matchBuffer } from '../utils/match';
99

1010
export function* renameInheritdoc(
1111
sourceUnit: SourceUnit,
12-
{ readOriginal }: TransformerTools,
12+
{ readOriginal, getData, resolver }: TransformerTools,
1313
): Generator<Transformation> {
14-
for (const doc of findAll('StructuredDocumentation', sourceUnit)) {
15-
const bounds = getNodeBounds(doc);
16-
const re = /(@inheritdoc\s+)([a-zA-Z0-9$_]+)/;
17-
const match = matchBuffer(readOriginal(doc, 'buffer'), re);
14+
for (const contract of findAll('ContractDefinition', sourceUnit)) {
15+
const baseContracts = contract.linearizedBaseContracts.map(i => resolver.resolveContract(i));
1816

19-
if (match) {
20-
yield {
21-
start: bounds.start + match.start + match.captureLengths[0],
22-
length: match.captureLengths[1],
23-
kind: 'rename-inheritdoc',
24-
transform: source => source.replace(/[a-zA-Z0-9$_]+$/, renameContract),
25-
};
17+
for (const doc of findAll('StructuredDocumentation', contract)) {
18+
const bounds = getNodeBounds(doc);
19+
const re = /(@inheritdoc\s+)([a-zA-Z0-9$_]+)/;
20+
const match = matchBuffer(readOriginal(doc, 'buffer'), re);
21+
22+
if (match) {
23+
yield {
24+
start: bounds.start + match.start + match.captureLengths[0],
25+
length: match.captureLengths[1],
26+
kind: 'rename-inheritdoc',
27+
transform: source => {
28+
const ref = baseContracts.find(c => c?.canonicalName == source);
29+
return ref !== undefined && getData(ref).importFromPeer !== undefined
30+
? source
31+
: source.replace(/[a-zA-Z0-9$_]+$/, renameContract);
32+
},
33+
};
34+
}
2635
}
2736
}
2837
}

0 commit comments

Comments
 (0)