mirror of
https://github.com/mdgriffith/elm-optimize-level-2.git
synced 2024-09-11 12:15:24 +03:00
Correct AX and FX regex
This commit is contained in:
parent
0f777a4f74
commit
f8d322fd92
@ -1,4 +1,5 @@
|
|||||||
import ts from 'typescript';
|
import ts from 'typescript';
|
||||||
|
import {parseAXFunction, parseFXFunction} from "./utils/ElmWrappers";
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
||||||
@ -54,10 +55,6 @@ export type FuncSplit = {
|
|||||||
const deriveRawLambdaName = (wrappedName: string): string =>
|
const deriveRawLambdaName = (wrappedName: string): string =>
|
||||||
wrappedName + '_fn';
|
wrappedName + '_fn';
|
||||||
|
|
||||||
const wrapperRegex = /^F(?<arity>[1-9]+[0-9]*)$/;
|
|
||||||
|
|
||||||
const invocationRegex = /^A(?<arity>[1-9]+[0-9]*)$/;
|
|
||||||
|
|
||||||
function reportInlinining(split: FuncSplit, { inlined }: InlineContext) {
|
function reportInlinining(split: FuncSplit, { inlined }: InlineContext) {
|
||||||
switch (split.type) {
|
switch (split.type) {
|
||||||
case 'alias': {
|
case 'alias': {
|
||||||
@ -181,14 +178,12 @@ const createSplitterVisitor = (
|
|||||||
// detects "var a = f(..)"
|
// detects "var a = f(..)"
|
||||||
if (ts.isIdentifier(callExpression)) {
|
if (ts.isIdentifier(callExpression)) {
|
||||||
// detects "var a = F123(..)"
|
// detects "var a = F123(..)"
|
||||||
const wrapperMatch = callExpression.text.match(wrapperRegex);
|
const arity = parseFXFunction(callExpression.text);
|
||||||
if (wrapperMatch && wrapperMatch.groups) {
|
if (arity) {
|
||||||
const args = node.initializer.arguments;
|
const args = node.initializer.arguments;
|
||||||
// checks that it should be called with only one argument
|
// checks that it should be called with only one argument
|
||||||
if (args.length === 1) {
|
if (args.length === 1) {
|
||||||
const [maybeFuncExpression] = args;
|
const [maybeFuncExpression] = args;
|
||||||
|
|
||||||
const arity = Number(wrapperMatch.groups.arity);
|
|
||||||
const originalName = node.name.text;
|
const originalName = node.name.text;
|
||||||
|
|
||||||
// detects var a = F123(innerFunc)
|
// detects var a = F123(innerFunc)
|
||||||
@ -266,8 +261,7 @@ const createSplitterVisitor = (
|
|||||||
let funcName = callExpression.text;
|
let funcName = callExpression.text;
|
||||||
let isWrappedWithA = false;
|
let isWrappedWithA = false;
|
||||||
// but it can be also A2(func, 1,2) with larger number of args.
|
// but it can be also A2(func, 1,2) with larger number of args.
|
||||||
const maybeMatch = callExpression.text.match(invocationRegex);
|
if (parseAXFunction(callExpression.text)) {
|
||||||
if (maybeMatch && maybeMatch.groups) {
|
|
||||||
const invocationArgs = node.initializer.arguments;
|
const invocationArgs = node.initializer.arguments;
|
||||||
const [funcIdentifier, ...restOfArgs] = invocationArgs;
|
const [funcIdentifier, ...restOfArgs] = invocationArgs;
|
||||||
|
|
||||||
@ -411,11 +405,9 @@ const createInlinerVisitor = (
|
|||||||
const expression = node.expression;
|
const expression = node.expression;
|
||||||
// detects f(..)
|
// detects f(..)
|
||||||
if (ts.isIdentifier(expression)) {
|
if (ts.isIdentifier(expression)) {
|
||||||
const maybeMatch = expression.text.match(invocationRegex);
|
|
||||||
// detects A123(...)
|
// detects A123(...)
|
||||||
if (maybeMatch && maybeMatch.groups) {
|
const arity = parseAXFunction(expression.text);
|
||||||
const arity = Number(maybeMatch.groups.arity);
|
if (arity) {
|
||||||
|
|
||||||
const allArgs = node.arguments;
|
const allArgs = node.arguments;
|
||||||
const [funcName, ...args] = allArgs;
|
const [funcName, ...args] = allArgs;
|
||||||
|
|
||||||
@ -522,12 +514,11 @@ function checkIfFunctionReturnsWrappedFunction(
|
|||||||
ts.isCallExpression(returnExpression) &&
|
ts.isCallExpression(returnExpression) &&
|
||||||
ts.isIdentifier(returnExpression.expression)
|
ts.isIdentifier(returnExpression.expression)
|
||||||
) {
|
) {
|
||||||
const maybeWrapper = returnExpression.expression.text.match(wrapperRegex);
|
const resultArity = parseFXFunction(returnExpression.expression.text);
|
||||||
|
if (resultArity) {
|
||||||
if (maybeWrapper && maybeWrapper.groups) {
|
|
||||||
return {
|
return {
|
||||||
arity,
|
arity,
|
||||||
resultArity: Number(maybeWrapper.groups.arity),
|
resultArity: resultArity,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,8 @@
|
|||||||
import ts from 'typescript';
|
import ts from 'typescript';
|
||||||
|
import {parseAXFunction, parseFXFunction} from "./utils/ElmWrappers";
|
||||||
|
|
||||||
export type Pattern<T> = (node: ts.Node) => T | undefined;
|
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 = {
|
type WrappedInvocation = {
|
||||||
args: ts.Expression[];
|
args: ts.Expression[];
|
||||||
calleeName: ts.Identifier;
|
calleeName: ts.Identifier;
|
||||||
@ -17,11 +15,9 @@ export const matchWrappedInvocation: Pattern<WrappedInvocation> = node => {
|
|||||||
const expression = node.expression;
|
const expression = node.expression;
|
||||||
// detects f(..)
|
// detects f(..)
|
||||||
if (ts.isIdentifier(expression)) {
|
if (ts.isIdentifier(expression)) {
|
||||||
const maybeMatch = expression.text.match(invocationRegex);
|
|
||||||
// detects A123(...)
|
// detects A123(...)
|
||||||
if (maybeMatch && maybeMatch.groups) {
|
const arity = parseAXFunction(expression.text);
|
||||||
const arity = Number(maybeMatch.groups.arity);
|
if (arity) {
|
||||||
|
|
||||||
const allArgs = node.arguments;
|
const allArgs = node.arguments;
|
||||||
const [funcName, ...args] = allArgs;
|
const [funcName, ...args] = allArgs;
|
||||||
|
|
||||||
@ -52,12 +48,12 @@ type Wrapping = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const matchWrapping: Pattern<Wrapping> = node => {
|
export const matchWrapping: Pattern<Wrapping> = node => {
|
||||||
|
// Detects FX(...)
|
||||||
if (ts.isCallExpression(node) && ts.isIdentifier(node.expression)) {
|
if (ts.isCallExpression(node) && ts.isIdentifier(node.expression)) {
|
||||||
const maybeMatch = node.expression.text.match(wrapperRegex);
|
const arity = parseFXFunction(node.expression.text);
|
||||||
|
if (arity) {
|
||||||
if (maybeMatch && maybeMatch.groups) {
|
|
||||||
return {
|
return {
|
||||||
arity: Number(maybeMatch.groups.arity),
|
arity: arity,
|
||||||
wrappedExpression: node.arguments[0],
|
wrappedExpression: node.arguments[0],
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
22
src/transforms/utils/ElmWrappers.ts
Normal file
22
src/transforms/utils/ElmWrappers.ts
Normal 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;
|
||||||
|
}
|
@ -1,10 +1,7 @@
|
|||||||
import * as ts from "typescript";
|
import * as ts from "typescript";
|
||||||
|
import {parseAXFunction, parseFXFunction} from "./ElmWrappers";
|
||||||
|
|
||||||
const functionsToIgnore: string[] = []; // optionally ['require', 'parseInt', 'exec', 'reject', 'resolve'];
|
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 = {
|
export type CallGraph = {
|
||||||
all: string[],
|
all: string[],
|
||||||
@ -80,8 +77,7 @@ function extractFunctionCalls(node: ts.Node, sourceFile: ts.SourceFile, indentLe
|
|||||||
contextFn = node.name.text;
|
contextFn = node.name.text;
|
||||||
|
|
||||||
// match F{n} wrapper
|
// match F{n} wrapper
|
||||||
const fnWrapper = node.initializer.expression.text.match(wrapperRegex);
|
if (parseFXFunction(node.initializer.expression.text)) {
|
||||||
if (fnWrapper && fnWrapper.groups) {
|
|
||||||
graph.all.push(fn_var_name);
|
graph.all.push(fn_var_name);
|
||||||
node.initializer.forEachChild((child) => {
|
node.initializer.forEachChild((child) => {
|
||||||
if (ts.isFunctionExpression(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.isCallExpression(node) && !already_inspected) {
|
||||||
|
|
||||||
if (ts.isIdentifier(node.expression)) {
|
if (ts.isIdentifier(node.expression)) {
|
||||||
|
if (parseAXFunction(node.expression.text)) {
|
||||||
const maybeMatch = node.expression.text.match(invocationRegex);
|
|
||||||
if (maybeMatch && maybeMatch.groups) {
|
|
||||||
|
|
||||||
let found_index = 0
|
let found_index = 0
|
||||||
node.forEachChild((child) => {
|
node.forEachChild((child) => {
|
||||||
if (found_index == 1 && ts.isIdentifier(child) && contextFn ){
|
if (found_index == 1 && ts.isIdentifier(child) && contextFn ){
|
||||||
|
Loading…
Reference in New Issue
Block a user