Skip to content
This repository has been archived by the owner on Sep 4, 2023. It is now read-only.

Commit

Permalink
refactor: always return a BoxNodeList from extractCallExpressionValues
Browse files Browse the repository at this point in the history
feat: functions.matchArg

rename: extractCallExpressionValues -> extractCallExpressionArguments
  • Loading branch information
astahmer committed Mar 2, 2023
1 parent 8cac8be commit 21e61bf
Show file tree
Hide file tree
Showing 15 changed files with 784 additions and 94 deletions.
9 changes: 9 additions & 0 deletions .changeset/rotten-shrimps-suffer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@box-extractor/core": patch
---

refactor: always return a BoxNodeList from extractCallExpressionValues

feat: functions.matchArg

rename: extractCallExpressionValues -> extractCallExpressionArguments
28 changes: 8 additions & 20 deletions packages/box-extractor/src/extractor/extract.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { createLogger } from "@box-extractor/logger";
import { tsquery } from "@phenomnomnominal/tsquery";
import { castAsArray } from "pastable";
import { JsxOpeningElement, JsxSelfClosingElement, Node } from "ts-morph";

import { extractCallExpressionValues } from "./extractCallExpressionValues";
import { extractJsxAttribute } from "./extractJsxAttributeIdentifierValue";
import { extractCallExpressionArguments } from "./extractCallExpressionArguments";
import { extractJsxAttribute } from "./extractJsxAttribute";
import { extractJsxSpreadAttributeValues } from "./extractJsxSpreadAttributeValues";
import { box, BoxNode, BoxNodeMap, BoxNodeObject, castObjectLikeAsMapValue, MapTypeValue } from "./type-factory";
import type {
Expand All @@ -17,7 +16,6 @@ import type {
MatchFnPropArgs,
MatchPropArgs,
} from "./types";
import { isNotNullish } from "./utils";

const logger = createLogger("box-ex:extractor:extract");
type QueryComponentMap = Map<JsxOpeningElement | JsxSelfClosingElement, { name: string; props: MapTypeValue }>;
Expand Down Expand Up @@ -235,8 +233,6 @@ export const extract = ({ ast, components, functions, extractMap = new Map() }:

const matchProp = ({ propName, propNode }: MatchFnPropArgs) =>
functions.matchProp({ fnNode: node, fnName, propName, propNode });
const maybeBox = extractCallExpressionValues(node, matchProp);
if (!maybeBox) return;
// console.log({ objectOrMapType });

if (!localExtraction.has(fnName)) {
Expand All @@ -254,17 +250,15 @@ export const extract = ({ ast, components, functions, extractMap = new Map() }:
const localList = localFnMap.queryList;
// console.log(componentName, componentMap);

// TODO rm
const fromNode = () => node;
const boxList = castAsArray(maybeBox)
.map((boxNode) => {
const nodeList = extractCallExpressionArguments(node, matchProp, functions.matchArg).value.map(
(boxNode) => {
if (boxNode.isObject() || boxNode.isMap()) {
const map = castObjectLikeAsMapValue(boxNode, node);
const entries = mergeSpreadEntries({
map,
// if the boxNode is an object
// that means it was evaluated so we need to filter its props
// otherwise, it was already filtered in extractCallExpressionValues
// otherwise, it was already filtered in extractCallExpressionArguments
matchProp: boxNode.isObject() ? (matchProp as any) : undefined,
});

Expand All @@ -288,16 +282,11 @@ export const extract = ({ ast, components, functions, extractMap = new Map() }:
}

return boxNode;
})
.filter(isNotNullish);
// console.log({ boxList, maybeBox });
}
);

// TODO box.function
const query = {
name: fnName,
fromNode,
box: box.list(boxList, node, []),
} as ExtractedFunctionInstance;
const query = { name: fnName, box: box.list(nodeList, node, []) } as ExtractedFunctionInstance;
fnMap.queryList.push(query);
localList.push(query);
}
Expand All @@ -311,7 +300,6 @@ export const extract = ({ ast, components, functions, extractMap = new Map() }:
// TODO box.component
const query = {
name: parentRef.name,
fromNode: () => componentNode, // TODO rm
box: box.map(parentRef.props, componentNode, []),
} as ExtractedComponentInstance;
queryComponentMap;
Expand Down
8 changes: 4 additions & 4 deletions packages/box-extractor/src/extractor/extractAtRange.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { createLogger } from "@box-extractor/logger";
import { JsxOpeningElement, JsxSelfClosingElement, Node, SourceFile, ts } from "ts-morph";

import { extractCallExpressionValues } from "./extractCallExpressionValues";
import { extractJsxAttribute } from "./extractJsxAttributeIdentifierValue";
import { extractCallExpressionArguments } from "./extractCallExpressionArguments";
import { extractJsxAttribute } from "./extractJsxAttribute";
import { extractJsxSpreadAttributeValues } from "./extractJsxSpreadAttributeValues";
import { box, BoxNode } from "./type-factory";
import type { ComponentMatchers, FunctionMatchers } from "./types";
Expand All @@ -26,7 +26,7 @@ export const extractAtRange = (

if (Node.isCallExpression(node)) {
// TODO box.function(node) ?
return extractCallExpressionValues(node, (prop) =>
return extractCallExpressionArguments(node, (prop) =>
(matchProp as FunctionMatchers["matchProp"])({
...prop,
fnNode: node,
Expand Down Expand Up @@ -54,7 +54,7 @@ export const extractAtRange = (

if (Node.isCallExpression(parent)) {
// TODO box.function(node) ?
return extractCallExpressionValues(parent, (prop) =>
return extractCallExpressionArguments(parent, (prop) =>
(matchProp as FunctionMatchers["matchProp"])({
...prop,
fnNode: parent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,26 @@ import { maybeBoxNode } from "./maybeBoxNode";

import { maybeObjectLikeBox } from "./maybeObjectLikeBox";
import { box } from "./type-factory";
import type { MatchFnPropArgs } from "./types";
import { isNotNullish, unwrapExpression } from "./utils";
import type { MatchFnArgs, MatchFnArguments, MatchFnPropArgs } from "./types";
import { unwrapExpression } from "./utils";

const logger = createLogger("box-ex:extractor:call-expr");

export const extractCallExpressionValues = (node: CallExpression, matchProp: (prop: MatchFnPropArgs) => boolean) => {
export const extractCallExpressionArguments = (
node: CallExpression,
matchProp: (prop: MatchFnPropArgs) => boolean = () => true,
matchArg: (prop: MatchFnArgs & MatchFnArguments) => boolean = () => true
) => {
const argList = node.getArguments();
const fnName = node.getExpression().getText();

if (argList.length === 0) return box.list([], node, []);

const boxes = argList
.map((arg) => {
return box.list(
argList.map((arg, index) => {
const argNode = unwrapExpression(arg);
if (!argNode) return;
if (!argNode) return box.unresolvable(argNode, []);
if (!matchArg({ fnNode: node, fnName, argNode, index })) return box.unresolvable(argNode, []);

const stack = [node, argNode] as Node[];

Expand All @@ -29,12 +36,10 @@ export const extractCallExpressionValues = (node: CallExpression, matchProp: (pr
const maybeObject = maybeObjectLikeBox(argNode, stack, matchProp);
logger({ maybeObject });
if (maybeObject) return maybeObject;
})
.filter(isNotNullish);

// TODO box.function
if (boxes.length === 0) return;
if (boxes.length === 1) return boxes[0]!;

return boxes;
return box.unresolvable(argNode, stack);
}),
node,
[]
);
};
6 changes: 3 additions & 3 deletions packages/box-extractor/src/extractor/extractFunctionFrom.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BindingName, Identifier, Node, SourceFile, ts } from "ts-morph";
import { BindingName, CallExpression, Identifier, Node, SourceFile, ts } from "ts-morph";

import { createLogger } from "@box-extractor/logger";
import { extract } from "./extract";
Expand Down Expand Up @@ -40,7 +40,7 @@ export const extractFunctionFrom = <Result>(
const resultByName = new Map<string, { result: Result; queryBox: BoxNodeList; nameNode: () => BindingName }>();
const extractedTheme = extract({
ast: sourceFile,
functions: { matchFn: ({ fnName }) => fnName === functionName, matchProp: () => true },
functions: { matchFn: ({ fnName }) => fnName === functionName, matchProp: () => true, matchArg: () => true },
});
const fnExtraction = extractedTheme.get(functionName) as ExtractedFunctionResult;
if (!fnExtraction) return resultByName;
Expand All @@ -50,7 +50,7 @@ export const extractFunctionFrom = <Result>(
logger({ from, queryList: queryList.length });

queryList.forEach((query) => {
const fromNode = query.fromNode();
const fromNode = query.box.getNode() as CallExpression;
const declaration = fromNode.getParentIfKind(ts.SyntaxKind.VariableDeclaration);
if (!declaration) return;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { unwrapExpression } from "./utils";

const logger = createLogger("box-ex:extractor:jsx-attr");

// TODO rename file to extractJsxAttribute
export const extractJsxAttribute = (jsxAttribute: JsxAttribute) => {
// <ColorBox color="red.200" backgroundColor="blackAlpha.100" />
// ^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
6 changes: 6 additions & 0 deletions packages/box-extractor/src/extractor/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type {
JsxAttribute,
JsxOpeningElement,
JsxSelfClosingElement,
Node,
PropertyAssignment,
ShorthandPropertyAssignment,
SourceFile,
Expand Down Expand Up @@ -50,13 +51,18 @@ export type MatchFnArgs = {
fnName: string;
fnNode: CallExpression;
};
export type MatchFnArguments = {
argNode: Node;
index: number;
};
export type MatchFnPropArgs = {
propName: string;
propNode: PropertyAssignment | ShorthandPropertyAssignment;
};
export type MatchPropFn = (prop: MatchPropArgs) => boolean;
export type FunctionMatchers = {
matchFn: (element: MatchFnArgs) => boolean;
matchArg: (arg: Pick<MatchFnArgs, "fnName" | "fnNode"> & MatchFnArguments) => boolean;
matchProp: (prop: Pick<MatchFnArgs, "fnName" | "fnNode"> & MatchFnPropArgs) => boolean;
};

Expand Down
4 changes: 2 additions & 2 deletions packages/box-extractor/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
export { ensureAbsolute } from "./extensions-helpers";
export { extract, query } from "./extractor/extract";
export { extractAtRange, extractJsxElementProps } from "./extractor/extractAtRange";
export { extractCallExpressionValues } from "./extractor/extractCallExpressionValues";
export { extractCallExpressionArguments } from "./extractor/extractCallExpressionArguments";
export { extractFunctionFrom, isImportedFrom } from "./extractor/extractFunctionFrom";
export { extractJsxAttribute } from "./extractor/extractJsxAttributeIdentifierValue";
export { extractJsxAttribute } from "./extractor/extractJsxAttribute";
export { extractJsxSpreadAttributeValues } from "./extractor/extractJsxSpreadAttributeValues";
export type {
FindAllTransitiveComponentsOptions,
Expand Down
1 change: 1 addition & 0 deletions packages/box-extractor/tests/createProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export const getTestExtract = (
? {
matchFn: ({ fnName }) => functionNameList.includes(fnName),
matchProp: () => true,
matchArg: () => true,
}
: options.functions,
});
Expand Down
9 changes: 1 addition & 8 deletions packages/box-extractor/tests/extract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10397,14 +10397,7 @@ it("extract CallExpression > no args", () => {
stack: [],
type: "list",
node: "CallExpression",
value: [
{
stack: [],
type: "list",
node: "CallExpression",
value: [],
},
],
value: [],
},
},
],
Expand Down
29 changes: 18 additions & 11 deletions packages/box-extractor/tests/extractAtRange.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,25 @@ test("extractAtRange", () => {
extracted = extractAtRange(sourceFile, 15, 19);
expect(extracted).toMatchInlineSnapshot(`
{
stack: ["CallExpression", "ObjectLiteralExpression"],
type: "map",
node: "ObjectLiteralExpression",
value: {
prop: {
stack: ["CallExpression", "ObjectLiteralExpression", "PropertyAssignment", "NumericLiteral"],
type: "literal",
node: "NumericLiteral",
value: 123,
kind: "number",
stack: [],
type: "list",
node: "CallExpression",
value: [
{
stack: ["CallExpression", "ObjectLiteralExpression"],
type: "map",
node: "ObjectLiteralExpression",
value: {
prop: {
stack: ["CallExpression", "ObjectLiteralExpression", "PropertyAssignment", "NumericLiteral"],
type: "literal",
node: "NumericLiteral",
value: 123,
kind: "number",
},
},
},
},
],
}
`);

Expand Down
Loading

0 comments on commit 21e61bf

Please sign in to comment.