Skip to content

Commit

Permalink
chore: lint
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Feb 15, 2023
1 parent e5bb15f commit b4d944c
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 64 deletions.
4 changes: 2 additions & 2 deletions src/module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { proxify } from "./proxy/_utils";
import type { Proxified } from './proxy/types'
import type { Proxified } from "./proxy/types";
import type { ParsedFileNode } from "./types";

export class ModuleNode {
constructor(public ast: ParsedFileNode) { }
constructor(public ast: ParsedFileNode) {}

get exports(): Record<string, Proxified> {
const _exports: Record<string, Proxified> = {};
Expand Down
23 changes: 14 additions & 9 deletions src/proxy/_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,18 @@ export function createProxy<T extends object>(
handler: ProxyHandler<T>
): Proxified<T> {
const utils = makeProxyUtils(node, extend);
return new Proxy({}, {
...handler,
get(target: T, key: string | symbol, receiver: any) {
if (key in utils) {
return (utils as any)[key];
}
return handler.get!(target, key, receiver);
},
}) as Proxified<T>
return new Proxy(
{},
{
...handler,
get(target: T, key: string | symbol, receiver: any) {
if (key in utils) {
return (utils as any)[key];
}
if (handler.get) {
return handler.get(target, key, receiver);
}
},
}
) as Proxified<T>;
}
96 changes: 50 additions & 46 deletions src/proxy/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,53 +14,57 @@ export function proxifyArrayElements<T extends object>(
elements[key] = value as any;
};

return createProxy<T>(node, {
$type: "array",
push(value: any) {
elements.push(literalToAst(value) as any);
return createProxy(
node,
{
$type: "array",
push(value: any) {
elements.push(literalToAst(value) as any);
},
pop() {
return proxify(elements.pop() as any);
},
unshift(value: any) {
elements.unshift(literalToAst(value) as any);
},
shift() {
return proxify(elements.shift() as any);
},
toJSON() {
return elements.map((n) => proxify(n as any));
},
},
pop() {
return proxify(elements.pop() as any);
},
unshift(value: any) {
elements.unshift(literalToAst(value) as any);
},
shift() {
return proxify(elements.shift() as any);
},
toJSON() {
return elements.map((n) => proxify(n as any));
},
}, {
get(_, key) {
if (key === "length") {
return elements.length;
}
if (typeof key === "symbol") {
return;
}
const prop = getItem(+key);
if (prop) {
return proxify(prop);
}
},
set(_, key, value) {
if (typeof key === "symbol") {
return false;
}
replaceItem(+key, literalToAst(value));
return true;
},
ownKeys() {
return ["length", ...elements.map((_, i) => i.toString())];
},
getOwnPropertyDescriptor() {
return {
enumerable: true,
configurable: true,
};
},
});
{
get(_, key) {
if (key === "length") {
return elements.length;
}
if (typeof key === "symbol") {
return;
}
const prop = getItem(+key);
if (prop) {
return proxify(prop);
}
},
set(_, key, value) {
if (typeof key === "symbol") {
return false;
}
replaceItem(+key, literalToAst(value));
return true;
},
ownKeys() {
return ["length", ...elements.map((_, i) => i.toString())];
},
getOwnPropertyDescriptor() {
return {
enumerable: true,
configurable: true,
};
},
}
);
}

export function proxifyArray<T extends object>(node: ESNode): Proxified<T> {
Expand Down
14 changes: 9 additions & 5 deletions src/proxy/function-call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@ export function proxifyFunctionCall<T>(node: ESNode): Proxified<T> {

const argumentsProxy = proxifyArrayElements(node, node.arguments as any);

return createProxy(node, {
$type: "function-call",
name: stringifyExpression(node.callee as any),
arguments: argumentsProxy,
}, {}) as any;
return createProxy(
node,
{
$type: "function-call",
name: stringifyExpression(node.callee as any),
arguments: argumentsProxy,
},
{}
) as any;
}
2 changes: 1 addition & 1 deletion src/proxy/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,5 @@ export function proxifyObject<T extends object>(node: ESNode): Proxified<T> {
};
},
}
)
);
}
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as recast from "recast";

import type { Node as ESNode } from "estree";

export * from './proxy/types'
export * from "./proxy/types";
export type { Node as ESNode } from "estree";

export interface Loc {
Expand Down

0 comments on commit b4d944c

Please sign in to comment.