Correct AX and FX regex

This commit is contained in:
Jeroen Engels 2022-03-24 20:12:17 +01:00
parent 0f777a4f74
commit f8d322fd92
4 changed files with 42 additions and 40 deletions

View File

@ -1,4 +1,5 @@
import ts from 'typescript';
import {parseAXFunction, parseFXFunction} from "./utils/ElmWrappers";
/*
@ -54,10 +55,6 @@ export type FuncSplit = {
const deriveRawLambdaName = (wrappedName: string): string =>
wrappedName + '_fn';
const wrapperRegex = /^F(?<arity>[1-9]+[0-9]*)$/;
const invocationRegex = /^A(?<arity>[1-9]+[0-9]*)$/;
function reportInlinining(split: FuncSplit, { inlined }: InlineContext) {
switch (split.type) {
case 'alias': {
@ -181,14 +178,12 @@ const createSplitterVisitor = (
// detects "var a = f(..)"
if (ts.isIdentifier(callExpression)) {
// detects "var a = F123(..)"
const wrapperMatch = callExpression.text.match(wrapperRegex);
if (wrapperMatch && wrapperMatch.groups) {
const arity = parseFXFunction(callExpression.text);
if (arity) {
const args = node.initializer.arguments;
// checks that it should be called with only one argument
if (args.length === 1) {
const [maybeFuncExpression] = args;
const arity = Number(wrapperMatch.groups.arity);
const originalName = node.name.text;
// detects var a = F123(innerFunc)
@ -266,8 +261,7 @@ const createSplitterVisitor = (
let funcName = callExpression.text;
let isWrappedWithA = false;
// but it can be also A2(func, 1,2) with larger number of args.
const maybeMatch = callExpression.text.match(invocationRegex);
if (maybeMatch && maybeMatch.groups) {
if (parseAXFunction(callExpression.text)) {
const invocationArgs = node.initializer.arguments;
const [funcIdentifier, ...restOfArgs] = invocationArgs;
@ -411,11 +405,9 @@ const createInlinerVisitor = (
const expression = node.expression;
// detects f(..)
if (ts.isIdentifier(expression)) {
const maybeMatch = expression.text.match(invocationRegex);
// detects A123(...)
if (maybeMatch && maybeMatch.groups) {
const arity = Number(maybeMatch.groups.arity);
const arity = parseAXFunction(expression.text);
if (arity) {
const allArgs = node.arguments;
const [funcName, ...args] = allArgs;
@ -522,12 +514,11 @@ function checkIfFunctionReturnsWrappedFunction(
ts.isCallExpression(returnExpression) &&
ts.isIdentifier(returnExpression.expression)
) {
const maybeWrapper = returnExpression.expression.text.match(wrapperRegex);
if (maybeWrapper && maybeWrapper.groups) {
const resultArity = parseFXFunction(returnExpression.expression.text);
if (resultArity) {
return {
arity,
resultArity: Number(maybeWrapper.groups.arity),
resultArity: resultArity,
};
}
}

View File

@ -1,10 +1,8 @@
import ts from 'typescript';
import {parseAXFunction, parseFXFunction} from "./utils/ElmWrappers";
export type Pattern<T> = (node: ts.Node) => T | undefined;
const invocationRegex = /A(?<arity>[1-9]+[0-9]*)/;
const wrapperRegex = /F(?<arity>[1-9]+[0-9]*)/;
type WrappedInvocation = {
args: ts.Expression[];
calleeName: ts.Identifier;
@ -17,11 +15,9 @@ export const matchWrappedInvocation: Pattern<WrappedInvocation> = node => {
const expression = node.expression;
// detects f(..)
if (ts.isIdentifier(expression)) {
const maybeMatch = expression.text.match(invocationRegex);
// detects A123(...)
if (maybeMatch && maybeMatch.groups) {
const arity = Number(maybeMatch.groups.arity);
const arity = parseAXFunction(expression.text);
if (arity) {
const allArgs = node.arguments;
const [funcName, ...args] = allArgs;
@ -52,12 +48,12 @@ type Wrapping = {
};
export const matchWrapping: Pattern<Wrapping> = node => {
// Detects FX(...)
if (ts.isCallExpression(node) && ts.isIdentifier(node.expression)) {
const maybeMatch = node.expression.text.match(wrapperRegex);
if (maybeMatch && maybeMatch.groups) {
const arity = parseFXFunction(node.expression.text);
if (arity) {
return {
arity: Number(maybeMatch.groups.arity),
arity: arity,
wrappedExpression: node.arguments[0],
};
}

View File

@ -0,0 +1,22 @@
const invocationRegex = /^A(?<arity>[1-9]+[0-9]*)$/;
const wrapperRegex = /^F(?<arity>[1-9]+[0-9]*)$/;
/* Checks whether the function is a A2/A3/A4/... function and if so returns the arity.
*/
export function parseAXFunction(str: string) : number | null {
const match = str.match(invocationRegex);
if (match && match.groups) {
return Number(match.groups.arity);
}
return null;
}
/* Checks whether the function is a F2/F3/F4/... function and if so returns the arity.
*/
export function parseFXFunction(str: string) : number | null {
const match = str.match(wrapperRegex);
if (match && match.groups) {
return Number(match.groups.arity);
}
return null;
}

View File

@ -1,10 +1,7 @@
import * as ts from "typescript";
import {parseAXFunction, parseFXFunction} from "./ElmWrappers";
const functionsToIgnore: string[] = []; // optionally ['require', 'parseInt', 'exec', 'reject', 'resolve'];
const invocationRegex = /A(?<arity>[1-9]+[0-9]*)/;
const wrapperRegex = /F(?<arity>[1-9]+[0-9]*)/;
export type CallGraph = {
all: string[],
@ -80,8 +77,7 @@ function extractFunctionCalls(node: ts.Node, sourceFile: ts.SourceFile, indentLe
contextFn = node.name.text;
// match F{n} wrapper
const fnWrapper = node.initializer.expression.text.match(wrapperRegex);
if (fnWrapper && fnWrapper.groups) {
if (parseFXFunction(node.initializer.expression.text)) {
graph.all.push(fn_var_name);
node.initializer.forEachChild((child) => {
if (ts.isFunctionExpression(child)) {
@ -128,10 +124,7 @@ function extractFunctionCalls(node: ts.Node, sourceFile: ts.SourceFile, indentLe
if (ts.isCallExpression(node) && !already_inspected) {
if (ts.isIdentifier(node.expression)) {
const maybeMatch = node.expression.text.match(invocationRegex);
if (maybeMatch && maybeMatch.groups) {
if (parseAXFunction(node.expression.text)) {
let found_index = 0
node.forEachChild((child) => {
if (found_index == 1 && ts.isIdentifier(child) && contextFn ){