-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Importing consts from external files
- Loading branch information
1 parent
b13fbd1
commit d7f0fd5
Showing
22 changed files
with
1,934 additions
and
701 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import ts, { SymbolFlags } from 'typescript' | ||
import type { Constant } from '../awst/nodes' | ||
import type { SourceLocation } from '../awst/source-location' | ||
import { CodeError } from '../errors' | ||
import { logger } from '../logger' | ||
import { hasFlags, invariant } from '../util' | ||
|
||
export class ConstantStore { | ||
#constants = new Map<ts.Symbol, Constant>() | ||
#typeChecker: ts.TypeChecker | ||
constructor(program: ts.Program) { | ||
this.#typeChecker = program.getTypeChecker() | ||
} | ||
|
||
tryResolveConstant(node: ts.Identifier) { | ||
let symbol = this.#typeChecker.resolveName(node.text, node, SymbolFlags.All, true) | ||
if (!symbol) return undefined | ||
if (hasFlags(symbol.flags, ts.SymbolFlags.Alias)) { | ||
symbol = this.#typeChecker.getAliasedSymbol(symbol) | ||
} | ||
return this.#constants.get(symbol) | ||
} | ||
|
||
addConstant(identifier: ts.Identifier, value: Constant, constantLocation: SourceLocation) { | ||
const symbol = this.#typeChecker.resolveName(identifier.text, identifier, ts.SymbolFlags.All, false) | ||
invariant(symbol, 'Constant identifier must resolve to a symbol') | ||
|
||
if (this.#constants.has(symbol)) { | ||
logger.error(new CodeError(`Duplicate definitions found for constant ${identifier.text}`, { sourceLocation: constantLocation })) | ||
return | ||
} | ||
const exportSymbol = this.#typeChecker.getExportSymbolOfSymbol(symbol) | ||
this.#constants.set(symbol, value) | ||
if (exportSymbol !== symbol) this.#constants.set(exportSymbol, value) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,3 +18,5 @@ export abstract class Arc4Contract extends Contract { | |
return a + b | ||
} | ||
} | ||
|
||
export const VERY_IMPORTANT_VALUE = '42' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,18 @@ | ||
import type { uint64 } from '@algorandfoundation/algorand-typescript' | ||
import { op, Txn } from '@algorandfoundation/algorand-typescript' | ||
import { Arc4Contract, SimpleContract } from './inheritance-a.algo' | ||
import { log, op, Txn } from '@algorandfoundation/algorand-typescript' | ||
import { Arc4Contract, SimpleContract, VERY_IMPORTANT_VALUE } from './inheritance-a.algo' | ||
|
||
export class ConcreteSimpleContract extends SimpleContract { | ||
public approvalProgram(): uint64 { | ||
const a = op.btoi(Txn.applicationArgs(0)) | ||
const b = op.btoi(Txn.applicationArgs(1)) | ||
return this.simpleMethod(a, b) | ||
log(this.simpleMethod(a, b)) | ||
return 1 | ||
} | ||
} | ||
|
||
export class ConcreteArc4Contract extends Arc4Contract {} | ||
export class ConcreteArc4Contract extends Arc4Contract { | ||
public getVeryImportantValue() { | ||
return VERY_IMPORTANT_VALUE | ||
} | ||
} |
152 changes: 0 additions & 152 deletions
152
tests/approvals/out/arc4-types/Arc4TypesTestContract.approval.teal
This file was deleted.
Oops, something went wrong.
5 changes: 0 additions & 5 deletions
5
tests/approvals/out/arc4-types/Arc4TypesTestContract.clear.teal
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.