diff --git a/README.md b/README.md index b880f27..45f60f0 100644 --- a/README.md +++ b/README.md @@ -162,6 +162,11 @@ else { In this example, the referenced bindings within the lexical environment of the Node will be discovered and evaluated before producing a final value. This means that you don't have to evaluate the entire program to produce a value which may potentially be a much faster operation. +### Behavior without a typechecker + +If you do not have access to a typechecker, for example if you don't have a TypeScript _Program_ to work with, you can avoid passing in a typechecker as an option. +This will work for evaluating literal values, but `ts-evaluator` won't be able to resolve and dealias symbols and identifiers. + ### Setting up an environment You can define the kind of environment that `evaluate()` assumes when evaluating the given Node. By default, a CommonJS-based `Node` environment is assumed, to align with what you would get simply by running `node` with no arguments. diff --git a/src/interpreter/evaluate-options.ts b/src/interpreter/evaluate-options.ts index a1e9bc4..c85aabe 100644 --- a/src/interpreter/evaluate-options.ts +++ b/src/interpreter/evaluate-options.ts @@ -6,7 +6,7 @@ import {TS} from "../type/ts.js"; export interface EvaluateOptions { node: TS.Statement | TS.Declaration | TS.Expression; - typeChecker: TS.TypeChecker; + typeChecker?: TS.TypeChecker; typescript?: typeof TS; environment?: Partial; logLevel?: LogLevelKind; diff --git a/src/interpreter/evaluator/evaluate-binary-expression.ts b/src/interpreter/evaluator/evaluate-binary-expression.ts index eccdccc..e791d43 100644 --- a/src/interpreter/evaluator/evaluate-binary-expression.ts +++ b/src/interpreter/evaluator/evaluate-binary-expression.ts @@ -148,7 +148,7 @@ export function evaluateBinaryExpression(options: EvaluatorOptions, parent: IndexLiteral): void { - const constantValue = typeChecker.getConstantValue(node) as number | string; + const constantValue = typeChecker?.getConstantValue(node); const propertyName = evaluate.nodeWithValue(node.name, environment, statementTraversalStack) as IndexLiteralKey; // If it is a String enum, all keys will be initialized to strings if (typeof constantValue === "string") { parent[propertyName] = constantValue; } else { - parent[(parent[propertyName] = constantValue)] = propertyName; + parent[(parent[propertyName] = constantValue ?? 0)] = propertyName; } } diff --git a/src/interpreter/evaluator/evaluate-identifier.ts b/src/interpreter/evaluator/evaluate-identifier.ts index 60b5125..4aedc22 100644 --- a/src/interpreter/evaluator/evaluate-identifier.ts +++ b/src/interpreter/evaluator/evaluate-identifier.ts @@ -24,14 +24,14 @@ export function evaluateIdentifier(options: EvaluatorOptions> { node: T; - typeChecker: TS.TypeChecker; + typeChecker?: TS.TypeChecker; typescript: typeof TS; evaluate: NodeEvaluator; environment: LexicalEnvironment; diff --git a/src/interpreter/evaluator/node-evaluator/i-create-node-evaluator-options.ts b/src/interpreter/evaluator/node-evaluator/i-create-node-evaluator-options.ts index 2c2a4ec..d6cf152 100644 --- a/src/interpreter/evaluator/node-evaluator/i-create-node-evaluator-options.ts +++ b/src/interpreter/evaluator/node-evaluator/i-create-node-evaluator-options.ts @@ -5,7 +5,7 @@ import {ReportingOptionsSanitized} from "../../reporting/i-reporting-options.js" import {TS} from "../../../type/ts.js"; export interface ICreateNodeEvaluatorOptions { - typeChecker: TS.TypeChecker; + typeChecker?: TS.TypeChecker; typescript: typeof TS; policy: EvaluatePolicySanitized; reporting: ReportingOptionsSanitized;