Skip to content

Commit

Permalink
Fixed jsep types hack
Browse files Browse the repository at this point in the history
  • Loading branch information
luvies committed Aug 2, 2020
1 parent d924cc5 commit cba5bef
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 135 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
"test:ci": "jest --ci --expand",
"test:watch": "jest --watch",
"lint": "./node_modules/@luvies/config/scripts/lint.sh",
"fix": "./node_modules/@luvies/config/scripts/lint.sh fix",
"postinstall": "rm -rf ./node_modules/jsep/typings/*"
"fix": "./node_modules/@luvies/config/scripts/lint.sh fix"
},
"devDependencies": {
"@luvies/config": "^3.0.2",
Expand Down
29 changes: 16 additions & 13 deletions src/analyzer/expression-analyzer.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,4 @@
import {
EvaluatorOptions,
ExpressionError,
ExpressionReturnType,
SimpleType,
canAccessMember,
} from "../evaluator";
import { ExpressionAnalysis } from "./expression-analysis";
import { FunctionCall } from "./function-call";
import { RuntimeValue } from "./runtime-value";
import jsep, {
ArrayExpression,
BinaryExpression,
CallExpression,
Expand All @@ -19,7 +9,18 @@ import jsep, {
LogicalExpression,
MemberExpression,
UnaryExpression,
} from "jsep";
} from "../jsep-types";
import {
EvaluatorOptions,
ExpressionError,
ExpressionReturnType,
SimpleType,
canAccessMember,
} from "../evaluator";
import { ExpressionAnalysis } from "./expression-analysis";
import { FunctionCall } from "./function-call";
import { RuntimeValue } from "./runtime-value";
import jsep, { Expression as JsepExpression } from "jsep";

export class ExpressionAnalyzer {
private readonly _options?: EvaluatorOptions;
Expand All @@ -43,11 +44,13 @@ export class ExpressionAnalyzer {
this._valueFormatter = valueFormatter ?? evalOpts?.valueFormatter ?? String;
}

public analyze(expression: Expression | string): ExpressionAnalysis {
public analyze(expression: JsepExpression | string): ExpressionAnalysis {
this._visited = new WeakSet();

return this._analyzeExpression(
typeof expression === "string" ? jsep(expression) : expression,
(typeof expression === "string"
? jsep(expression)
: expression) as Expression,
);
}

Expand Down
31 changes: 17 additions & 14 deletions src/evaluator/expression-evaluator.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,4 @@
import {
ArrayType,
EvaluatorOptions,
ExpressionResult,
ExpressionReturnType,
MemberCheckFn,
SimpleType,
TypeMap,
canAccessMember,
} from "./utils";
import { ExpressionError } from "./expression-error";
import jsep, {
ArrayExpression,
BinaryExpression,
CallExpression,
Expand All @@ -21,7 +10,19 @@ import jsep, {
LogicalExpression,
MemberExpression,
UnaryExpression,
} from "jsep";
} from "../jsep-types";
import {
ArrayType,
EvaluatorOptions,
ExpressionResult,
ExpressionReturnType,
MemberCheckFn,
SimpleType,
TypeMap,
canAccessMember,
} from "./utils";
import { ExpressionError } from "./expression-error";
import jsep, { Expression as JsepExpression } from "jsep";

export class ExpressionEvaluator {
private readonly _context: TypeMap;
Expand All @@ -35,10 +36,12 @@ export class ExpressionEvaluator {
}

public async eval(
expression: string | Expression,
expression: string | JsepExpression,
): Promise<ExpressionResult> {
return this._evalExpression(
typeof expression === "string" ? jsep(expression) : expression,
(typeof expression === "string"
? jsep(expression)
: expression) as Expression,
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/evaluator/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ export type FunctionType = (

export interface ArrayType extends Array<ExpressionReturnType> {}

// eslint-disable-next-line @typescript-eslint/ban-types
export type ExpressionReturnType =
| SimpleType
| ArrayType
| FunctionType
| TypeMap
// eslint-disable-next-line @typescript-eslint/ban-types
| object;

export interface ExpressionResult<T = ExpressionReturnType> {
Expand Down
87 changes: 87 additions & 0 deletions src/jsep-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
export interface ArrayExpression {
type: "ArrayExpression";
elements: Expression[];
}

export interface BinaryExpression {
type: "BinaryExpression";
operator: string;
left: Expression;
right: Expression;
}

export interface CallExpression {
type: "CallExpression";
arguments: Expression[];
callee: Expression;
}

export interface Compound {
type: "Compound";
body: Expression[];
}

export interface ConditionalExpression {
type: "ConditionalExpression";
test: Expression;
consequent: Expression;
alternate: Expression;
}

export interface Identifier {
type: "Identifier";
name: string;
}

export interface Literal {
type: "Literal";
value: boolean | number | string;
raw: string;
}

export interface LogicalExpression {
type: "LogicalExpression";
operator: string;
left: Expression;
right: Expression;
}

interface BaseMemberExpression<T, U> {
type: "MemberExpression";
computed: T;
object: Expression;
property: U;
}

interface DirectMemberExpression
extends BaseMemberExpression<false, Identifier> {}
interface ComputedMemberExpression
extends BaseMemberExpression<true, Expression> {}

export type MemberExpression =
| DirectMemberExpression
| ComputedMemberExpression;

export interface ThisExpression {
type: "ThisExpression";
}

export interface UnaryExpression {
type: "UnaryExpression";
operator: string;
argument: Expression;
prefix: boolean;
}

export type Expression =
| ArrayExpression
| BinaryExpression
| CallExpression
| Compound
| ConditionalExpression
| Identifier
| Literal
| LogicalExpression
| MemberExpression
| ThisExpression
| UnaryExpression;
105 changes: 0 additions & 105 deletions types/jsep.d.ts

This file was deleted.

0 comments on commit cba5bef

Please sign in to comment.