Skip to content

Commit fdb169e

Browse files
committed
fix type errors
1 parent b947137 commit fdb169e

10 files changed

+37
-42
lines changed

package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"lodash": "^4.17.20",
4343
"minimatch": "^6.0.0",
4444
"minimist": "^1.2.5",
45-
"solidity-ast": "^0.4.48"
45+
"solidity-ast": "^0.4.49"
4646
},
4747
"devDependencies": {
4848
"@types/lodash": "^4.14.165",

src/ast-resolver.ts

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ContractDefinition } from 'solidity-ast';
2-
import { findAll, astDereferencer, ASTDereferencer } from 'solidity-ast/utils';
2+
import { findAll, astDereferencer, ASTDereferencer, ASTDereferencerError } from 'solidity-ast/utils';
33
import { NodeType, NodeTypeMap } from 'solidity-ast/node';
44

55
import { SolcOutput } from './solc/input-output';
@@ -12,26 +12,10 @@ export class ASTResolver {
1212
}
1313

1414
resolveContract(id: number): ContractDefinition | undefined {
15-
try {
16-
return this.resolveNode('ContractDefinition', id);
17-
} catch (e) {
18-
if (e instanceof ASTResolverError) {
19-
return undefined;
20-
} else {
21-
throw e;
22-
}
23-
}
15+
return this.tryResolveNode('ContractDefinition', id);
2416
}
2517

2618
resolveNode<T extends NodeType>(nodeType: T, id: number): NodeTypeMap[T] {
27-
const node = this.tryResolveNode(nodeType, id);
28-
if (node === undefined) {
29-
throw new ASTResolverError(nodeType);
30-
}
31-
return node;
32-
}
33-
34-
tryResolveNode<T extends NodeType>(nodeType: T, id: number): NodeTypeMap[T] | undefined {
3519
const { node, sourceUnit } = this.deref.withSourceUnit(nodeType, id);
3620
const source = sourceUnit.absolutePath;
3721
if (this.exclude?.(source)) {
@@ -40,10 +24,18 @@ export class ASTResolver {
4024
return node;
4125
}
4226
}
43-
}
4427

45-
export class ASTResolverError extends Error {
46-
constructor(nodeType: NodeType) {
47-
super(`Can't find required ${nodeType}`);
28+
tryResolveNode<T extends NodeType>(nodeType: T, id: number): NodeTypeMap[T] | undefined {
29+
try {
30+
return this.resolveNode(nodeType, id);
31+
} catch (e) {
32+
if (e instanceof ASTDereferencerError) {
33+
return undefined;
34+
} else {
35+
throw e;
36+
}
37+
}
4838
}
4939
}
40+
41+
export const ASTResolverError = ASTDereferencerError;

src/transformations/add-storage-gaps.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@ export function* addStorageGaps(
6161
}
6262
}
6363

64-
function isStorageVariable(varDecl: VariableDeclaration): boolean {
64+
function isStorageVariable(varDecl: VariableDeclaration, resolver: ASTResolver): boolean {
6565
switch (varDecl.mutability) {
6666
case 'constant':
6767
return false;
6868
case 'immutable':
69-
return !hasOverride(varDecl, 'state-variable-immutable');
69+
return !hasOverride(varDecl, 'state-variable-immutable', resolver);
7070
default:
7171
return true;
7272
}
@@ -114,7 +114,7 @@ function getContractSlotCount(
114114

115115
// don't use `findAll` here, we don't want to go recursive
116116
for (const varDecl of contractNode.nodes.filter(isNodeType('VariableDeclaration'))) {
117-
if (isStorageVariable(varDecl)) {
117+
if (isStorageVariable(varDecl, resolver)) {
118118
// try get type details
119119
const typeIdentifier = decodeTypeIdentifier(varDecl.typeDescriptions.typeIdentifier ?? '');
120120

src/transformations/purge-var-inits.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
import { SourceUnit } from 'solidity-ast';
22
import { findAll } from 'solidity-ast/utils';
33
import { getNodeBounds } from '../solc/ast-utils';
4+
import { TransformerTools } from '../transform';
45
import { hasConstructorOverride, hasOverride } from '../utils/upgrades-overrides';
56

67
import { Transformation } from './type';
78

8-
export function* removeStateVarInits(sourceUnit: SourceUnit): Generator<Transformation> {
9+
export function* removeStateVarInits(sourceUnit: SourceUnit, { resolver }: TransformerTools): Generator<Transformation> {
910
for (const contractNode of findAll('ContractDefinition', sourceUnit)) {
1011
if (hasConstructorOverride(contractNode)) {
1112
continue;
1213
}
1314

1415
for (const varDecl of findAll('VariableDeclaration', contractNode)) {
1516
if (varDecl.stateVariable && varDecl.value && !varDecl.constant) {
16-
if (hasOverride(varDecl, 'state-variable-assignment')) {
17+
if (hasOverride(varDecl, 'state-variable-assignment', resolver)) {
1718
continue;
1819
}
1920

src/transformations/remove-immutable.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import { SourceUnit } from 'solidity-ast';
22
import { findAll } from 'solidity-ast/utils';
33
import { getNodeBounds } from '../solc/ast-utils';
4+
import { TransformerTools } from '../transform';
45
import { hasOverride } from '../utils/upgrades-overrides';
56

67
import { Transformation } from './type';
78

8-
export function* removeImmutable(sourceUnit: SourceUnit): Generator<Transformation> {
9+
export function* removeImmutable(sourceUnit: SourceUnit, { resolver }: TransformerTools): Generator<Transformation> {
910
for (const varDecl of findAll('VariableDeclaration', sourceUnit)) {
1011
if (varDecl.mutability === 'immutable') {
11-
if (hasOverride(varDecl, 'state-variable-immutable')) {
12+
if (hasOverride(varDecl, 'state-variable-immutable', resolver)) {
1213
continue;
1314
}
1415

src/transformations/transform-constructor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export function* transformConstructor(
9595
varInitNodes,
9696
modifiers,
9797
emptyUnchained: emptyConstructor,
98-
} = getInitializerItems(contractNode);
98+
} = getInitializerItems(contractNode, resolver);
9999

100100
const initializer = (
101101
helper: TransformHelper,

src/transformations/utils/build-super-calls-for-chain.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ export function buildSuperCallsForChain(
203203

204204
const args = parentArgsValues.get(parentNode) ?? [];
205205

206-
if (args.length || !getInitializerItems(parentNode).emptyUnchained) {
206+
if (args.length || !getInitializerItems(parentNode, resolver).emptyUnchained) {
207207
// TODO: we have to use the name in the lexical context and not necessarily
208208
// the original contract name
209209
linearizedCtorCalls.push(buildSuperCall(args, parentNode.name, helper));

src/transformations/utils/get-initializer-items.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { ContractDefinition } from 'solidity-ast';
22
import { findAll } from 'solidity-ast/utils';
3+
import { ASTResolver } from '../../ast-resolver';
34
import { getConstructor } from '../../solc/ast-utils';
45
import { hasOverride } from '../../utils/upgrades-overrides';
56

6-
export function getInitializerItems(contract: ContractDefinition) {
7+
export function getInitializerItems(contract: ContractDefinition, resolver: ASTResolver) {
78
const constructorNode = getConstructor(contract);
89

910
const varInitNodes = [...findAll('VariableDeclaration', contract)].filter(
10-
v => v.stateVariable && v.value && !v.constant && !hasOverride(v, 'state-variable-assignment'),
11+
v => v.stateVariable && v.value && !v.constant && !hasOverride(v, 'state-variable-assignment', resolver),
1112
);
1213

1314
const modifiers =

src/utils/upgrades-overrides.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,5 @@ function getOwnOverrides(node: Node): ValidationErrorKind[] {
6262

6363
export function hasConstructorOverride(contract: ContractDefinition): boolean {
6464
const ctor = getConstructor(contract);
65-
return ctor ? hasOverride(ctor, 'constructor') : false;
65+
return ctor ? getOwnOverrides(ctor).includes('constructor') : false;
6666
}

0 commit comments

Comments
 (0)