From f8d322fd92ecc47f2b6508e0a3e035d3a538f900 Mon Sep 17 00:00:00 2001 From: Jeroen Engels Date: Thu, 24 Mar 2022 20:12:17 +0100 Subject: [PATCH] Correct AX and FX regex --- src/transforms/inlineWrappedFunctions.ts | 27 ++++++++---------------- src/transforms/patterns.ts | 20 +++++++----------- src/transforms/utils/ElmWrappers.ts | 22 +++++++++++++++++++ src/transforms/utils/callgraph.ts | 13 +++--------- 4 files changed, 42 insertions(+), 40 deletions(-) create mode 100644 src/transforms/utils/ElmWrappers.ts diff --git a/src/transforms/inlineWrappedFunctions.ts b/src/transforms/inlineWrappedFunctions.ts index 3dc0431..377962e 100644 --- a/src/transforms/inlineWrappedFunctions.ts +++ b/src/transforms/inlineWrappedFunctions.ts @@ -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(?[1-9]+[0-9]*)$/; - -const invocationRegex = /^A(?[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, }; } } diff --git a/src/transforms/patterns.ts b/src/transforms/patterns.ts index 0bf987d..282f607 100644 --- a/src/transforms/patterns.ts +++ b/src/transforms/patterns.ts @@ -1,10 +1,8 @@ import ts from 'typescript'; +import {parseAXFunction, parseFXFunction} from "./utils/ElmWrappers"; + export type Pattern = (node: ts.Node) => T | undefined; -const invocationRegex = /A(?[1-9]+[0-9]*)/; - -const wrapperRegex = /F(?[1-9]+[0-9]*)/; - type WrappedInvocation = { args: ts.Expression[]; calleeName: ts.Identifier; @@ -17,11 +15,9 @@ export const matchWrappedInvocation: Pattern = 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 = 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], }; } diff --git a/src/transforms/utils/ElmWrappers.ts b/src/transforms/utils/ElmWrappers.ts new file mode 100644 index 0000000..8e8f4ca --- /dev/null +++ b/src/transforms/utils/ElmWrappers.ts @@ -0,0 +1,22 @@ +const invocationRegex = /^A(?[1-9]+[0-9]*)$/; +const wrapperRegex = /^F(?[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; +} diff --git a/src/transforms/utils/callgraph.ts b/src/transforms/utils/callgraph.ts index b1a8afd..af06097 100644 --- a/src/transforms/utils/callgraph.ts +++ b/src/transforms/utils/callgraph.ts @@ -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(?[1-9]+[0-9]*)/; -const wrapperRegex = /F(?[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 ){