diff --git a/.changeset/mighty-beans-occur.md b/.changeset/mighty-beans-occur.md
new file mode 100644
index 0000000..9dd0c66
--- /dev/null
+++ b/.changeset/mighty-beans-occur.md
@@ -0,0 +1,5 @@
+---
+'@modular-rocks/slimfast-node': patch
+---
+
+Added types for constraints
diff --git a/packages/slimfast-node/src/slimfast/pipeline/build/builder/combine-imports/index.test.ts b/packages/slimfast-node/src/slimfast/pipeline/build/builder/combine-imports/index.test.ts
index 3ac37c3..101c55f 100644
--- a/packages/slimfast-node/src/slimfast/pipeline/build/builder/combine-imports/index.test.ts
+++ b/packages/slimfast-node/src/slimfast/pipeline/build/builder/combine-imports/index.test.ts
@@ -8,7 +8,7 @@ import { combineImports } from '.';
import { extractIdentifiers } from '../../../../visitors/utils/extract-identifiers';
import { parser } from '../../../../visitors/utils/parser';
-import type { RandomObject, SlimFastOpts } from '../../../../../types';
+import type { SlimFastOpts } from '../../../../../types';
import type { Binding, NodePath } from '@babel/traverse';
const files: [string, string][] = [[`/path`, '']];
@@ -42,7 +42,10 @@ describe('Combine imports', () => {
});
if (rootPath !== null) {
- const data: RandomObject = {};
+ const data = {
+ toImport: [],
+ toInject: [],
+ };
extractIdentifiers(rootPath, data);
// TODO: Verify 'data.toImport' content and ensure it provides valid 'Binding[]' for 'combineImports'.
const imports = unique(data.toImport) as Binding[];
@@ -74,7 +77,10 @@ describe('Combine imports', () => {
});
if (rootPath !== null) {
- const data: RandomObject = {};
+ const data = {
+ toImport: [],
+ toInject: [],
+ };
extractIdentifiers(rootPath, data);
// TODO: Verify 'data.toImport' content and ensure it provides valid 'Binding[]' for 'combineImports'.
const imports = unique(data.toImport) as Binding[];
diff --git a/packages/slimfast-node/src/slimfast/pipeline/build/builder/replace/index.test.ts b/packages/slimfast-node/src/slimfast/pipeline/build/builder/replace/index.test.ts
index d1a19e0..81dda2c 100644
--- a/packages/slimfast-node/src/slimfast/pipeline/build/builder/replace/index.test.ts
+++ b/packages/slimfast-node/src/slimfast/pipeline/build/builder/replace/index.test.ts
@@ -39,7 +39,10 @@ describe('Generate JSX', () => {
});
if (rootPath !== null) {
- const data = {};
+ const data = {
+ toImport: [],
+ toInject: [],
+ };
extractIdentifiers(rootPath, data);
expect(rootPath.isJSXElement()).toBe(false);
const el = replace('myFunction', rootPath, data, opts);
@@ -65,7 +68,10 @@ describe('Generate JSX', () => {
});
if (rootPath !== null) {
- const data = {};
+ const data = {
+ toImport: [],
+ toInject: [],
+ };
extractIdentifiers(rootPath, data);
expect(rootPath.isJSXElement()).toBe(false);
const el = replace('myFunction', rootPath, data, opts);
@@ -94,7 +100,10 @@ describe('Generate JSX', () => {
});
if (rootPath !== null) {
- const data = {};
+ const data = {
+ toImport: [],
+ toInject: [],
+ };
extractIdentifiers(rootPath, data);
expect(rootPath.isJSXElement()).toBe(true);
const el = replace('MyComponent', rootPath, data, opts);
@@ -124,7 +133,10 @@ describe('Generate JSX', () => {
});
if (rootPath) {
- const data = {};
+ const data = {
+ toImport: [],
+ toInject: [],
+ };
extractIdentifiers(rootPath, data);
expect(rootPath.isJSXElement()).toBe(true);
const el = replace('MyComponent', rootPath, data, opts);
diff --git a/packages/slimfast-node/src/slimfast/pipeline/build/builder/replace/jsx-function/index.test.ts b/packages/slimfast-node/src/slimfast/pipeline/build/builder/replace/jsx-function/index.test.ts
index 4403d6d..9562436 100644
--- a/packages/slimfast-node/src/slimfast/pipeline/build/builder/replace/jsx-function/index.test.ts
+++ b/packages/slimfast-node/src/slimfast/pipeline/build/builder/replace/jsx-function/index.test.ts
@@ -44,7 +44,10 @@ describe('Generate JSX', () => {
});
if (rootPath !== null) {
- const data = {};
+ const data = {
+ toImport: [],
+ toInject: [],
+ };
extractIdentifiers(rootPath, data);
const el = generateJSXElement('MyComponent', data);
expect(file.astToCode(el)).toBe(``);
@@ -73,7 +76,10 @@ describe('Generate JSX', () => {
});
if (rootPath !== null) {
- const data = {};
+ const data = {
+ toImport: [],
+ toInject: [],
+ };
extractIdentifiers(rootPath, data);
const el = generateJSXElement('MyComponent', data);
expect(file.astToCode(el)).toBe(``);
diff --git a/packages/slimfast-node/src/slimfast/pipeline/build/builder/replace/normal-function/index.test.ts b/packages/slimfast-node/src/slimfast/pipeline/build/builder/replace/normal-function/index.test.ts
index ba7794d..11079c9 100644
--- a/packages/slimfast-node/src/slimfast/pipeline/build/builder/replace/normal-function/index.test.ts
+++ b/packages/slimfast-node/src/slimfast/pipeline/build/builder/replace/normal-function/index.test.ts
@@ -40,7 +40,10 @@ describe('Generate JSX', () => {
});
if (rootPath !== null) {
- const data = {};
+ const data = {
+ toImport: [],
+ toInject: [],
+ };
extractIdentifiers(rootPath, data);
const el = generateFunction('myFunction', data);
expect(file.astToCode(el)).toBe(`myFunction()`);
@@ -65,7 +68,10 @@ describe('Generate JSX', () => {
});
if (rootPath !== null) {
- const data = {};
+ const data = {
+ toImport: [],
+ toInject: [],
+ };
extractIdentifiers(rootPath, data);
const el = generateFunction('myFunction', data);
expect(file.astToCode(el)).toBe(`myFunction(name)`);
diff --git a/packages/slimfast-node/src/slimfast/pipeline/build/builder/wrap/index.test.ts b/packages/slimfast-node/src/slimfast/pipeline/build/builder/wrap/index.test.ts
index 6fc9fa7..32eca59 100644
--- a/packages/slimfast-node/src/slimfast/pipeline/build/builder/wrap/index.test.ts
+++ b/packages/slimfast-node/src/slimfast/pipeline/build/builder/wrap/index.test.ts
@@ -40,7 +40,10 @@ describe('Generate JSX', () => {
});
if (rootPath !== null) {
- const data = {};
+ const data = {
+ toImport: [],
+ toInject: [],
+ };
extractIdentifiers(rootPath, data);
const el = wrap(rootPath, data, opts);
expect(file.astToCode(el)).toBe(`export default function() {
@@ -67,7 +70,10 @@ describe('Generate JSX', () => {
});
if (rootPath !== null) {
- const data = {};
+ const data = {
+ toImport: [],
+ toInject: [],
+ };
extractIdentifiers(rootPath, data);
const el = wrap(rootPath, data, opts);
expect(file.astToCode(el)).toBe(`export default function(name) {
@@ -97,7 +103,10 @@ describe('Generate JSX', () => {
});
if (rootPath !== null) {
- const data = {};
+ const data = {
+ toImport: [],
+ toInject: [],
+ };
extractIdentifiers(rootPath, data);
const el = wrap(rootPath, data, opts);
expect(file.astToCode(el)).toBe(`export default function(props) {
@@ -133,7 +142,10 @@ describe('Generate JSX', () => {
});
if (rootPath !== null) {
- const data = {};
+ const data = {
+ toImport: [],
+ toInject: [],
+ };
extractIdentifiers(rootPath, data);
const el = wrap(rootPath, data, opts);
expect(file.astToCode(el)).toBe(`export default function(props) {
diff --git a/packages/slimfast-node/src/slimfast/pipeline/build/builder/wrap/jsx-function/index.test.ts b/packages/slimfast-node/src/slimfast/pipeline/build/builder/wrap/jsx-function/index.test.ts
index 9c99f39..e2ea0da 100644
--- a/packages/slimfast-node/src/slimfast/pipeline/build/builder/wrap/jsx-function/index.test.ts
+++ b/packages/slimfast-node/src/slimfast/pipeline/build/builder/wrap/jsx-function/index.test.ts
@@ -44,7 +44,10 @@ describe('Generate JSX', () => {
});
if (rootPath !== null) {
- const data = {};
+ const data = {
+ toImport: [],
+ toInject: [],
+ };
extractIdentifiers(rootPath, data);
const el = generateExportedJSXComponent(rootPath, data);
expect(file.astToCode(el)).toBe(`export default function(props) {
@@ -79,7 +82,10 @@ describe('Generate JSX', () => {
});
if (rootPath !== null) {
- const data = {};
+ const data = {
+ toImport: [],
+ toInject: [],
+ };
extractIdentifiers(rootPath, data);
const el = generateExportedJSXComponent(rootPath, data);
expect(file.astToCode(el)).toBe(`export default function(props) {
diff --git a/packages/slimfast-node/src/slimfast/pipeline/build/builder/wrap/normal-function/index.test.ts b/packages/slimfast-node/src/slimfast/pipeline/build/builder/wrap/normal-function/index.test.ts
index 1ea81f8..ced3891 100644
--- a/packages/slimfast-node/src/slimfast/pipeline/build/builder/wrap/normal-function/index.test.ts
+++ b/packages/slimfast-node/src/slimfast/pipeline/build/builder/wrap/normal-function/index.test.ts
@@ -40,7 +40,10 @@ describe('Generate JSX', () => {
});
if (rootPath !== null) {
- const data = {};
+ const data = {
+ toImport: [],
+ toInject: [],
+ };
extractIdentifiers(rootPath, data);
const el = generateExportedFunction(rootPath, data);
expect(file.astToCode(el)).toBe(`export default function() {
@@ -71,7 +74,10 @@ describe('Generate JSX', () => {
});
if (rootPath !== null) {
- const data = {};
+ const data = {
+ toImport: [],
+ toInject: [],
+ };
extractIdentifiers(rootPath, data);
const el = generateExportedFunction(rootPath, data);
expect(file.astToCode(el)).toBe(`export default function(props) {
diff --git a/packages/slimfast-node/src/slimfast/visitors/expression/index.ts b/packages/slimfast-node/src/slimfast/visitors/expression/index.ts
index cd85ff9..2db92e6 100644
--- a/packages/slimfast-node/src/slimfast/visitors/expression/index.ts
+++ b/packages/slimfast-node/src/slimfast/visitors/expression/index.ts
@@ -9,7 +9,7 @@ import { shouldIgnore } from '../utils/contraints/should-ignore';
import { tooSmall } from '../utils/contraints/too-small';
import { Visitor } from '../visitor';
-import type { RandomObject } from '../../../types';
+import type { Constraints, RandomObject } from '../../../types';
/**
* A `Visitor` that traverses AST expression nodes, evaluating them against specific constraints.
@@ -20,7 +20,7 @@ export class ExpressionVisitor extends Visitor {
*
* @returns An array of constraint functions specific to expressions.
*/
- constraints(): Function[] {
+ constraints(): Constraints {
return [
removesTooMuch(2),
shouldIgnore,
diff --git a/packages/slimfast-node/src/slimfast/visitors/utils/contraints/contains-identifiers-in-other-scopes/index.test.ts b/packages/slimfast-node/src/slimfast/visitors/utils/contraints/contains-identifiers-in-other-scopes/index.test.ts
index 0ba7b84..27d676b 100644
--- a/packages/slimfast-node/src/slimfast/visitors/utils/contraints/contains-identifiers-in-other-scopes/index.test.ts
+++ b/packages/slimfast-node/src/slimfast/visitors/utils/contraints/contains-identifiers-in-other-scopes/index.test.ts
@@ -15,7 +15,6 @@ describe('Contains variables in other scopes', () => {
}`;
const ast = parser(code);
let rootPath: NodePath | null = null;
- const data = {};
traverse(ast, {
VariableDeclaration(path) {
@@ -24,7 +23,7 @@ describe('Contains variables in other scopes', () => {
},
});
if (rootPath !== null) {
- const result = containsIdentifiersInOtherScopes(rootPath, data, {}, ast);
+ const result = containsIdentifiersInOtherScopes(rootPath);
expect(result).toBe(true);
}
});
@@ -33,7 +32,6 @@ describe('Contains variables in other scopes', () => {
const code = `let yes = 'yes'`;
const ast = parser(code);
let rootPath: NodePath | null = null;
- const data = {};
traverse(ast, {
VariableDeclarator(path) {
@@ -42,7 +40,7 @@ describe('Contains variables in other scopes', () => {
},
});
if (rootPath !== null) {
- const result = containsIdentifiersInOtherScopes(rootPath, data, {}, ast);
+ const result = containsIdentifiersInOtherScopes(rootPath);
expect(result).toBe(false);
}
});
diff --git a/packages/slimfast-node/src/slimfast/visitors/utils/contraints/contains-identifiers-in-other-scopes/index.ts b/packages/slimfast-node/src/slimfast/visitors/utils/contraints/contains-identifiers-in-other-scopes/index.ts
index 4d62795..0fe97a7 100644
--- a/packages/slimfast-node/src/slimfast/visitors/utils/contraints/contains-identifiers-in-other-scopes/index.ts
+++ b/packages/slimfast-node/src/slimfast/visitors/utils/contraints/contains-identifiers-in-other-scopes/index.ts
@@ -1,5 +1,5 @@
-import type { RandomObject } from '../../../../../types';
-import type { NodePath, Node } from '@babel/traverse';
+import type { Constraint, RandomObject } from '../../../../../types';
+import type { NodePath } from '@babel/traverse';
function isInsidePath(innerPath: NodePath, outerPath: NodePath): boolean {
let currentPath: NodePath | null = innerPath;
@@ -21,23 +21,15 @@ function isInsidePath(innerPath: NodePath, outerPath: NodePath): boolean {
* This can be particularly useful for understanding dependencies or potential side-effects associated with variables.
*
* @param path - The AST node path of the variable declaration to be examined.
- * @param data - Additional information or context related to the node.
- * @param opts - Configuration options influencing the check.
- * @param ast - The complete Abstract Syntax Tree.
* @returns `true` if any of the declared variables within the node path are referenced or manipulated outside their declaring scope, otherwise `false`.
*
* @example
- * const hasExternalReferences = containsIdentifiersInOtherScopes(nodePath, data, opts, ast);
+ * const hasExternalReferences = containsIdentifiersInOtherScopes(nodePath);
* if (hasExternalReferences) {
* // Handle or analyze the variables that are used externally.
* }
*/
-export const containsIdentifiersInOtherScopes = (
- path: NodePath,
- data: RandomObject,
- opts: RandomObject,
- ast: Node
-) => {
+export const containsIdentifiersInOtherScopes: Constraint = (path) => {
let usedInOtherScopes = false;
// TODO: double check this condition
diff --git a/packages/slimfast-node/src/slimfast/visitors/utils/contraints/has-assignment-expression/index.test.ts b/packages/slimfast-node/src/slimfast/visitors/utils/contraints/has-assignment-expression/index.test.ts
index f300bdc..d36543b 100644
--- a/packages/slimfast-node/src/slimfast/visitors/utils/contraints/has-assignment-expression/index.test.ts
+++ b/packages/slimfast-node/src/slimfast/visitors/utils/contraints/has-assignment-expression/index.test.ts
@@ -11,7 +11,6 @@ describe('Has assignment expression', () => {
const code = `yes = 'no'`;
const ast = parser(code);
let rootPath: NodePath | null = null;
- const data = {};
traverse(ast, {
AssignmentExpression(path) {
@@ -20,7 +19,7 @@ describe('Has assignment expression', () => {
},
});
if (rootPath !== null) {
- const result = hasAssignmentExpression(rootPath, data, {}, ast);
+ const result = hasAssignmentExpression(rootPath);
expect(result).toBe(true);
}
});
@@ -29,7 +28,6 @@ describe('Has assignment expression', () => {
const code = `yes`;
const ast = parser(code);
let rootPath: NodePath | null = null;
- const data = {};
traverse(ast, {
AssignmentExpression(path) {
@@ -38,7 +36,7 @@ describe('Has assignment expression', () => {
},
});
if (rootPath !== null) {
- const result = hasAssignmentExpression(rootPath, data, {}, ast);
+ const result = hasAssignmentExpression(rootPath);
expect(result).toBe(false);
}
});
diff --git a/packages/slimfast-node/src/slimfast/visitors/utils/contraints/has-assignment-expression/index.ts b/packages/slimfast-node/src/slimfast/visitors/utils/contraints/has-assignment-expression/index.ts
index a19428f..a237437 100644
--- a/packages/slimfast-node/src/slimfast/visitors/utils/contraints/has-assignment-expression/index.ts
+++ b/packages/slimfast-node/src/slimfast/visitors/utils/contraints/has-assignment-expression/index.ts
@@ -1,5 +1,5 @@
-import type { RandomObject } from '../../../../../types';
-import type { NodePath, Node } from '@babel/traverse';
+import type { Constraint, RandomObject } from '../../../../../types';
+import type { NodePath } from '@babel/traverse';
function isInsidePath(
innerPath: NodePath,
@@ -40,23 +40,15 @@ const isUsedInPath = (
* The node path is examined for direct representations of an assignment expression. Additionally, nested nodes are inspected for assignment patterns, to determine if the assigned variables are referenced outside their original context.
*
* @param path - The AST node path to be checked.
- * @param data - Information or context related to the node.
- * @param opts - Configuration options.
- * @param ast - The complete Abstract Syntax Tree.
* @returns `true` if the node path contains an assignment expression and the assigned variables are used in other scopes, otherwise `false`.
*
* @example
- * const containsAssignment = hasAssignmentExpression(nodePath, data, opts, ast);
+ * const containsAssignment = hasAssignmentExpression(nodePath);
* if (containsAssignment) {
* // Handle or flag the assignment for further analysis.
* }
*/
-export const hasAssignmentExpression = (
- path: NodePath,
- data: RandomObject,
- opts: RandomObject,
- ast: Node
-) => {
+export const hasAssignmentExpression: Constraint = (path) => {
let usedInOtherScopes = false;
if (path.isAssignmentExpression()) {
diff --git a/packages/slimfast-node/src/slimfast/visitors/utils/contraints/has-blocklisted-identifiers/index.test.ts b/packages/slimfast-node/src/slimfast/visitors/utils/contraints/has-blocklisted-identifiers/index.test.ts
index ada9f1b..e20c82b 100644
--- a/packages/slimfast-node/src/slimfast/visitors/utils/contraints/has-blocklisted-identifiers/index.test.ts
+++ b/packages/slimfast-node/src/slimfast/visitors/utils/contraints/has-blocklisted-identifiers/index.test.ts
@@ -11,7 +11,6 @@ describe('Has blocklisted identifiers', () => {
const code = `x * y`;
const ast = parser(code);
let rootPath: NodePath | null = null;
- const data = {};
traverse(ast, {
ArrayExpression(path) {
@@ -20,12 +19,7 @@ describe('Has blocklisted identifiers', () => {
},
});
if (rootPath !== null) {
- const result = hasBlocklistedIdentifiers(['x', 'y'])(
- rootPath,
- data,
- {},
- ast
- );
+ const result = hasBlocklistedIdentifiers(['x', 'y'])(rootPath);
expect(result).toBe(true);
}
});
@@ -34,7 +28,6 @@ describe('Has blocklisted identifiers', () => {
const code = `true`;
const ast = parser(code);
let rootPath: NodePath | null = null;
- const data = {};
traverse(ast, {
ArrayExpression(path) {
@@ -43,12 +36,7 @@ describe('Has blocklisted identifiers', () => {
},
});
if (rootPath !== null) {
- const result = hasBlocklistedIdentifiers(['x', 'y'])(
- rootPath,
- data,
- {},
- ast
- );
+ const result = hasBlocklistedIdentifiers(['x', 'y'])(rootPath);
expect(result).toBe(false);
}
});
diff --git a/packages/slimfast-node/src/slimfast/visitors/utils/contraints/has-blocklisted-identifiers/index.ts b/packages/slimfast-node/src/slimfast/visitors/utils/contraints/has-blocklisted-identifiers/index.ts
index 718863e..1123a8d 100644
--- a/packages/slimfast-node/src/slimfast/visitors/utils/contraints/has-blocklisted-identifiers/index.ts
+++ b/packages/slimfast-node/src/slimfast/visitors/utils/contraints/has-blocklisted-identifiers/index.ts
@@ -1,6 +1,4 @@
-import type { RandomObject } from '../../../../../types';
-import type { NodePath } from '@babel/traverse';
-import type { File } from '@babel/types';
+import type { Constraint, RandomObject } from '../../../../../types';
/**
* Generates a function to check if a given AST node path contains any identifiers that are part of a specified blocklist.
@@ -11,23 +9,20 @@ import type { File } from '@babel/types';
* @returns A function that evaluates if a node path contains any of the blocklisted identifiers.
* @example
* const isIdentifierBlocklisted = hasBlocklistedIdentifiers(['useEffect', 'useMemo']);
- * const isBlocklisted = isIdentifierBlocklisted(nodePath, data, opts, ast);
+ * const isBlocklisted = isIdentifierBlocklisted(nodePath);
* if (isBlocklisted) {
* // Handle the blocklisted identifier.
* }
*/
-export const hasBlocklistedIdentifiers =
- (blocklisted: string[]) =>
+export const hasBlocklistedIdentifiers: (blocklisted: string[]) => Constraint =
+ (blocklisted) =>
/**
* Determines if the provided AST node path contains any blocklisted identifiers.
*
* @param path - The AST node path to be examined.
- * @param data - Information or context related to the node.
- * @param opts - Configuration options.
- * @param ast - The complete Abstract Syntax Tree.
* @returns `true` if any of the identifiers within the node path are blocklisted, otherwise `false`.
*/
- (path: NodePath, data: RandomObject, opts: RandomObject, ast: File) => {
+ (path) => {
let itHasBlocklistedIdentifiers = false;
path.traverse({
diff --git a/packages/slimfast-node/src/slimfast/visitors/utils/contraints/has-return-statement/index.test.ts b/packages/slimfast-node/src/slimfast/visitors/utils/contraints/has-return-statement/index.test.ts
index 06abd2d..5695ac1 100644
--- a/packages/slimfast-node/src/slimfast/visitors/utils/contraints/has-return-statement/index.test.ts
+++ b/packages/slimfast-node/src/slimfast/visitors/utils/contraints/has-return-statement/index.test.ts
@@ -13,7 +13,6 @@ describe('Has return statement', () => {
}`;
const ast = parser(code);
let rootPath: NodePath | null = null;
- const data = {};
traverse(ast, {
ArrayExpression(path) {
@@ -22,7 +21,7 @@ describe('Has return statement', () => {
},
});
if (rootPath !== null) {
- const result = hasReturnStatement(rootPath, data, {}, ast);
+ const result = hasReturnStatement(rootPath);
expect(result).toBe(true);
}
});
@@ -31,7 +30,6 @@ describe('Has return statement', () => {
const code = `true`;
const ast = parser(code);
let rootPath: NodePath | null = null;
- const data = {};
traverse(ast, {
ArrayExpression(path) {
@@ -40,7 +38,7 @@ describe('Has return statement', () => {
},
});
if (rootPath !== null) {
- const result = hasReturnStatement(rootPath, data, {}, ast);
+ const result = hasReturnStatement(rootPath);
expect(result).toBe(false);
}
});
diff --git a/packages/slimfast-node/src/slimfast/visitors/utils/contraints/has-return-statement/index.ts b/packages/slimfast-node/src/slimfast/visitors/utils/contraints/has-return-statement/index.ts
index 0285be0..de98692 100644
--- a/packages/slimfast-node/src/slimfast/visitors/utils/contraints/has-return-statement/index.ts
+++ b/packages/slimfast-node/src/slimfast/visitors/utils/contraints/has-return-statement/index.ts
@@ -1,7 +1,7 @@
import { isAFunction } from '../is-a-function';
-import type { RandomObject } from '../../../../../types';
-import type { NodePath, Node } from '@babel/traverse';
+import type { Constraint } from '../../../../../types';
+import type { NodePath } from '@babel/traverse';
/**
* Determines if a given AST node path represents a return statement or contains a return statement not within a nested function.
@@ -11,23 +11,15 @@ import type { NodePath, Node } from '@babel/traverse';
* 2. The path contains a return statement that isn't nested within any type of function.
*
* @param path - The AST node path to be checked.
- * @param data - Information or context related to the node.
- * @param opts - Configuration options.
- * @param ast - The complete Abstract Syntax Tree.
* @returns `true` if either condition is met, otherwise `false`.
*
* @example
- * const hasReturn = hasReturnStatement(nodePath, data, opts, ast);
+ * const hasReturn = hasReturnStatement(nodePath);
* if (hasReturn) {
* // Handle the problematic return statement.
* }
*/
-export const hasReturnStatement = (
- path: NodePath,
- data: RandomObject,
- opts: RandomObject,
- ast: Node
-) => {
+export const hasReturnStatement: Constraint = (path) => {
let problematic = false;
if (path.isReturnStatement()) {
@@ -39,7 +31,7 @@ export const hasReturnStatement = (
let parent: NodePath | null = innerPath;
let isWrapped = false;
while (!isWrapped && parent) {
- if (isAFunction(path, data, opts, ast)) {
+ if (isAFunction(path)) {
isWrapped = true;
}
parent = parent === path ? null : parent.parentPath;
diff --git a/packages/slimfast-node/src/slimfast/visitors/utils/contraints/has-variable-declarator/index.test.ts b/packages/slimfast-node/src/slimfast/visitors/utils/contraints/has-variable-declarator/index.test.ts
index a249d72..41f685e 100644
--- a/packages/slimfast-node/src/slimfast/visitors/utils/contraints/has-variable-declarator/index.test.ts
+++ b/packages/slimfast-node/src/slimfast/visitors/utils/contraints/has-variable-declarator/index.test.ts
@@ -11,7 +11,6 @@ describe('Has variable declarator', () => {
const code = `yes = 'no'`;
const ast = parser(code);
let rootPath: NodePath | null = null;
- const data = {};
traverse(ast, {
VariableDeclarator(path) {
@@ -20,7 +19,7 @@ describe('Has variable declarator', () => {
},
});
if (rootPath !== null) {
- const result = hasVariableDeclarator(rootPath, data, {}, ast);
+ const result = hasVariableDeclarator(rootPath);
expect(result).toBe(true);
}
});
@@ -29,7 +28,6 @@ describe('Has variable declarator', () => {
const code = `yes`;
const ast = parser(code);
let rootPath: NodePath | null = null;
- const data = {};
traverse(ast, {
VariableDeclarator(path) {
@@ -38,7 +36,7 @@ describe('Has variable declarator', () => {
},
});
if (rootPath !== null) {
- const result = hasVariableDeclarator(rootPath, data, {}, ast);
+ const result = hasVariableDeclarator(rootPath);
expect(result).toBe(false);
}
});
diff --git a/packages/slimfast-node/src/slimfast/visitors/utils/contraints/has-variable-declarator/index.ts b/packages/slimfast-node/src/slimfast/visitors/utils/contraints/has-variable-declarator/index.ts
index e5dfe21..7338298 100644
--- a/packages/slimfast-node/src/slimfast/visitors/utils/contraints/has-variable-declarator/index.ts
+++ b/packages/slimfast-node/src/slimfast/visitors/utils/contraints/has-variable-declarator/index.ts
@@ -1,5 +1,4 @@
-import type { RandomObject } from '../../../../../types';
-import type { NodePath, Node } from '@babel/traverse';
+import type { Constraint } from '../../../../../types';
/**
* Determines if a given AST node path represents a variable declarator.
@@ -9,23 +8,15 @@ import type { NodePath, Node } from '@babel/traverse';
* in the declaration `const hello = 'world';`, the variable declarator represents `hello = 'world'`.
*
* @param path - The AST node path to be checked. This can either be a proper NodePath or an object that behaves similarly.
- * @param data - Information or context related to the node.
- * @param opts - Configuration options influencing the check.
- * @param ast - The complete Abstract Syntax Tree.
* @returns `true` if the node represents a variable declarator, otherwise `false`.
*
* @example
- * const isVarDeclarator = hasVariableDeclarator(nodePath, data, opts, ast);
+ * const isVarDeclarator = hasVariableDeclarator(nodePath);
* if (isVarDeclarator) {
* // Handle the variable declarator node.
* }
*/
-export const hasVariableDeclarator = (
- path: NodePath | RandomObject,
- data: RandomObject,
- opts: RandomObject,
- ast: Node
-) => {
+export const hasVariableDeclarator: Constraint = (path) => {
let itHasVariableDeclarator = false;
if (path.isVariableDeclarator()) {
diff --git a/packages/slimfast-node/src/slimfast/visitors/utils/contraints/identifiers-not-within-range/index.ts b/packages/slimfast-node/src/slimfast/visitors/utils/contraints/identifiers-not-within-range/index.ts
index 1721b8e..6c62432 100644
--- a/packages/slimfast-node/src/slimfast/visitors/utils/contraints/identifiers-not-within-range/index.ts
+++ b/packages/slimfast-node/src/slimfast/visitors/utils/contraints/identifiers-not-within-range/index.ts
@@ -1,7 +1,6 @@
import { identifiersWithinRange } from '../identifiers-within-range';
-import type { RandomObject } from '../../../../../types';
-import type { NodePath, Node } from '@babel/traverse';
+import type { ConstraintWithData } from '../../../../../types';
/**
* Generates a function to determine if the number of identifiers within an AST node path does not fall within a specified range.
@@ -13,20 +12,21 @@ import type { NodePath, Node } from '@babel/traverse';
* @returns A function that evaluates if a node path contains identifiers outside the specified range.
* @example
* const isOutsideRange = identifiersNotWithinRange(2, 4);
- * const result = isOutsideRange(nodePath, data, opts, ast);
+ * const result = isOutsideRange(nodePath, data);
* // Returns true if nodePath contains less than 2 or more than 4 identifiers.
*/
-export const identifiersNotWithinRange =
- (min: number, max: number) =>
+export const identifiersNotWithinRange: (
+ min: number,
+ max: number
+) => ConstraintWithData<'toImport' | 'toInject'> =
+ (min, max) =>
/**
* Determines if the number of identifiers within a given AST node path is outside the specified range.
*
* @param path - The AST node path to be examined.
- * @param data - Information or context related to the node.
- * @param opts - Configuration options influencing the check.
- * @param ast - The complete Abstract Syntax Tree.
+ * @param data - Information or context related to the node. The data should contain keys 'toImport' and 'toInject'.
* @returns `true` if the number of identifiers lies outside the specified range, otherwise `false`.
*/
- (path: NodePath, data: RandomObject, opts: RandomObject, ast: Node) => {
- return !identifiersWithinRange(min, max)(path, data, opts, ast);
+ (path, data) => {
+ return !identifiersWithinRange(min, max)(path, data);
};
diff --git a/packages/slimfast-node/src/slimfast/visitors/utils/contraints/identifiers-within-range/index.test.ts b/packages/slimfast-node/src/slimfast/visitors/utils/contraints/identifiers-within-range/index.test.ts
index bb92821..02d40b1 100644
--- a/packages/slimfast-node/src/slimfast/visitors/utils/contraints/identifiers-within-range/index.test.ts
+++ b/packages/slimfast-node/src/slimfast/visitors/utils/contraints/identifiers-within-range/index.test.ts
@@ -11,7 +11,10 @@ describe('Identifiers within range', () => {
const code = `() => x * y * z * a * b * c`;
const ast = parser(code);
let rootPath: NodePath | null = null;
- const data = {};
+ const data = {
+ toInject: [],
+ toImport: [],
+ };
traverse(ast, {
ArrayExpression(path) {
@@ -20,7 +23,7 @@ describe('Identifiers within range', () => {
},
});
if (rootPath !== null) {
- const result = identifiersWithinRange(2, 4)(rootPath, data, {}, ast);
+ const result = identifiersWithinRange(2, 4)(rootPath, data);
expect(result).toBe(false);
}
});
@@ -29,7 +32,10 @@ describe('Identifiers within range', () => {
const code = `() => x * y`;
const ast = parser(code);
let rootPath: NodePath | null = null;
- const data = {};
+ const data = {
+ toInject: [],
+ toImport: [],
+ };
traverse(ast, {
ArrayExpression(path) {
@@ -38,7 +44,7 @@ describe('Identifiers within range', () => {
},
});
if (rootPath !== null) {
- const result = identifiersWithinRange(2, 4)(rootPath, data, {}, ast);
+ const result = identifiersWithinRange(2, 4)(rootPath, data);
expect(result).toBe(true);
}
});
diff --git a/packages/slimfast-node/src/slimfast/visitors/utils/contraints/identifiers-within-range/index.ts b/packages/slimfast-node/src/slimfast/visitors/utils/contraints/identifiers-within-range/index.ts
index 4ffec4c..6987724 100644
--- a/packages/slimfast-node/src/slimfast/visitors/utils/contraints/identifiers-within-range/index.ts
+++ b/packages/slimfast-node/src/slimfast/visitors/utils/contraints/identifiers-within-range/index.ts
@@ -1,7 +1,6 @@
import { extractIdentifiers } from '../../extract-identifiers';
-import type { RandomObject } from '../../../../../types';
-import type { NodePath, Node } from '@babel/traverse';
+import type { ConstraintWithData } from '../../../../../types';
/**
* Generates a function to determine if the number of identifiers within an AST node path falls within a specified range.
@@ -13,22 +12,23 @@ import type { NodePath, Node } from '@babel/traverse';
* @returns A function that evaluates if a node path contains identifiers within the specified range.
* @example
* const isWithinRange = identifiersWithinRange(2, 4);
- * const result = isWithinRange(nodePath, data, opts, ast);
+ * const result = isWithinRange(nodePath, data);
* // Returns true if nodePath contains between 2 and 4 identifiers, inclusive.
*/
-export const identifiersWithinRange =
- (min: number, max: number) =>
+export const identifiersWithinRange: (
+ min: number,
+ max: number
+) => ConstraintWithData<'toInject' | 'toImport'> =
+ (min, max) =>
/**
* Determines if the number of identifiers within a given AST node path is within the specified range.
*
* @param path - The AST node path to be examined.
- * @param data - Information or context related to the node.
- * @param opts - Configuration options influencing the check.
- * @param ast - The complete Abstract Syntax Tree.
+ * @param data - Information or context related to the node. The data should contain keys 'toImport' and 'toInject'.
* @returns `true` if the number of identifiers lies within the specified range, otherwise `false`.
*/
- (path: NodePath, data: RandomObject, opts: RandomObject, ast: Node) => {
- extractIdentifiers(path, data, opts, ast);
+ (path, data) => {
+ extractIdentifiers(path, data);
const minIdentifiers = min || 2;
const maxIdentifiers = max || 4;
diff --git a/packages/slimfast-node/src/slimfast/visitors/utils/contraints/is-a-function/index.test.ts b/packages/slimfast-node/src/slimfast/visitors/utils/contraints/is-a-function/index.test.ts
index 99caae7..93fa028 100644
--- a/packages/slimfast-node/src/slimfast/visitors/utils/contraints/is-a-function/index.test.ts
+++ b/packages/slimfast-node/src/slimfast/visitors/utils/contraints/is-a-function/index.test.ts
@@ -11,7 +11,6 @@ describe('Is a function', () => {
const code = `() => 3 * 7`;
const ast = parser(code);
let rootPath: NodePath | null = null;
- const data = {};
traverse(ast, {
ArrayExpression(path) {
@@ -20,7 +19,7 @@ describe('Is a function', () => {
},
});
if (rootPath !== null) {
- const result = isAFunction(rootPath, data, {}, ast);
+ const result = isAFunction(rootPath);
expect(result).toBe(true);
}
});
@@ -28,7 +27,6 @@ describe('Is a function', () => {
const code = `3 * 7`;
const ast = parser(code);
let rootPath: NodePath | null = null;
- const data = {};
traverse(ast, {
ArrayExpression(path) {
@@ -37,7 +35,7 @@ describe('Is a function', () => {
},
});
if (rootPath !== null) {
- const result = isAFunction(rootPath, data, {}, ast);
+ const result = isAFunction(rootPath);
expect(result).toBe(false);
}
});
diff --git a/packages/slimfast-node/src/slimfast/visitors/utils/contraints/is-a-function/index.ts b/packages/slimfast-node/src/slimfast/visitors/utils/contraints/is-a-function/index.ts
index bda343c..17338ba 100644
--- a/packages/slimfast-node/src/slimfast/visitors/utils/contraints/is-a-function/index.ts
+++ b/packages/slimfast-node/src/slimfast/visitors/utils/contraints/is-a-function/index.ts
@@ -1,5 +1,4 @@
-import type { RandomObject } from '../../../../../types';
-import type { NodePath, Node } from '@babel/traverse';
+import type { Constraint } from '../../../../../types';
/**
* Determines if a given AST node path represents any kind of function.
@@ -8,23 +7,15 @@ import type { NodePath, Node } from '@babel/traverse';
* standard JavaScript function types (e.g., regular functions, arrow functions, class methods).
*
* @param path - The AST node path to be checked.
- * @param data - Information or context related to the node.
- * @param opts - Configuration options.
- * @param ast - The complete Abstract Syntax Tree.
* @returns `true` if the node represents any kind of function, otherwise `false`.
*
* @example
- * const isFunctionNode = isAFunction(nodePath, data, opts, ast);
+ * const isFunctionNode = isAFunction(nodePath);
* if (isFunctionNode) {
* // Handle the function node.
* }
*/
-export const isAFunction = (
- path: NodePath,
- data: RandomObject,
- opts: RandomObject,
- ast: Node
-) => {
+export const isAFunction: Constraint = (path) => {
return [
'FunctionDeclaration',
'FunctionExpression',
diff --git a/packages/slimfast-node/src/slimfast/visitors/utils/contraints/is-call-expression/index.test.ts b/packages/slimfast-node/src/slimfast/visitors/utils/contraints/is-call-expression/index.test.ts
index 392f624..ee751bc 100644
--- a/packages/slimfast-node/src/slimfast/visitors/utils/contraints/is-call-expression/index.test.ts
+++ b/packages/slimfast-node/src/slimfast/visitors/utils/contraints/is-call-expression/index.test.ts
@@ -22,7 +22,7 @@ describe('Is call expression', () => {
});
if (rootPath !== null) {
- const result = isCallExpression(rootPath, {}, {}, ast);
+ const result = isCallExpression(rootPath);
const expected = true;
expect(result).toBe(expected);
}
diff --git a/packages/slimfast-node/src/slimfast/visitors/utils/contraints/is-call-expression/index.ts b/packages/slimfast-node/src/slimfast/visitors/utils/contraints/is-call-expression/index.ts
index 296d6fc..ac4a591 100644
--- a/packages/slimfast-node/src/slimfast/visitors/utils/contraints/is-call-expression/index.ts
+++ b/packages/slimfast-node/src/slimfast/visitors/utils/contraints/is-call-expression/index.ts
@@ -1,5 +1,4 @@
-import type { RandomObject } from '../../../../../types';
-import type { NodePath, Node } from '@babel/traverse';
+import type { Constraint } from '../../../../../types';
/**
* Determines if a given AST node path represents a call expression within an expression statement.
@@ -8,23 +7,15 @@ import type { NodePath, Node } from '@babel/traverse';
* of its contained expression is 'CallExpression'.
*
* @param path - The AST node path to be checked.
- * @param data - Information or context related to the node
- * @param opts - Configuration options
- * @param ast - The complete Abstract Syntax Tree
* @returns `true` if the node represents a call expression within an expression statement, otherwise `false`.
*
* @example
- * const isItACallExpression = isCallExpression(nodePath, data, opts, ast);
+ * const isItACallExpression = isCallExpression(nodePath);
* if (isItACallExpression) {
* // Handle the call expression.
* }
*/
-export const isCallExpression = (
- path: NodePath,
- data: RandomObject,
- opts: RandomObject,
- ast: Node
-) => {
+export const isCallExpression: Constraint = (path) => {
const { node } = path;
return (
diff --git a/packages/slimfast-node/src/slimfast/visitors/utils/contraints/removes-too-much/index.test.ts b/packages/slimfast-node/src/slimfast/visitors/utils/contraints/removes-too-much/index.test.ts
index dc1110d..c3b2cae 100644
--- a/packages/slimfast-node/src/slimfast/visitors/utils/contraints/removes-too-much/index.test.ts
+++ b/packages/slimfast-node/src/slimfast/visitors/utils/contraints/removes-too-much/index.test.ts
@@ -10,7 +10,6 @@ describe('Removes too much', () => {
const code = `() => 3 * 7`;
const ast = parser(code);
let rootPath: NodePath | null = null;
- const data = {};
traverse(ast, {
Expression(path) {
@@ -20,7 +19,7 @@ describe('Removes too much', () => {
});
test('', () => {
if (rootPath !== null) {
- const result = removesTooMuch(3)(rootPath, data, {}, ast);
+ const result = removesTooMuch(3)(rootPath, { ast });
expect(result).toBe(true);
}
});
diff --git a/packages/slimfast-node/src/slimfast/visitors/utils/contraints/removes-too-much/index.ts b/packages/slimfast-node/src/slimfast/visitors/utils/contraints/removes-too-much/index.ts
index a8021b2..2cb5d1b 100644
--- a/packages/slimfast-node/src/slimfast/visitors/utils/contraints/removes-too-much/index.ts
+++ b/packages/slimfast-node/src/slimfast/visitors/utils/contraints/removes-too-much/index.ts
@@ -1,5 +1,5 @@
-import type { RandomObject } from '../../../../../types';
-import type { NodePath, Node } from '@babel/traverse';
+import type { ConstraintWithData } from '../../../../../types';
+import type { Node } from '@babel/traverse';
const notANumber = (num: number | null | undefined): boolean =>
num === null || num === undefined || Number.isNaN(num);
@@ -20,22 +20,22 @@ const getSize = (node: Node): number => {
* @returns A function that checks if the removal of a node path would reduce the AST size too significantly.
* @example
* const wouldRemoveTooMuch = removesTooMuch(2);
- * const significantReduction = wouldRemoveTooMuch(nodePath, data, opts, ast);
+ * const significantReduction = wouldRemoveTooMuch(nodePath, data);
* // Returns true if the removal would be too significant.
*/
-export const removesTooMuch =
- (multiplier: number) =>
+export const removesTooMuch: (
+ multiplier: number
+) => ConstraintWithData<'ast'> =
+ (multiplier) =>
/**
* Determines if the removal of a specific AST node would lead to a disproportionate reduction in the overall size of the AST.
*
* @param path - The AST node path under evaluation.
- * @param data - Information or context related to the node.
- * @param opts - Configuration options influencing the check.
- * @param ast - The complete Abstract Syntax Tree.
+ * @param data - Information or context related to the node. Specifically, contains the entire AST to which the node belongs in the `ast` key.
* @returns `true` if the removal of the node would cause a significant reduction in the AST's size, otherwise `false`.
*/
- (path: NodePath, data: RandomObject, opts: RandomObject, ast: Node) => {
- const astSize = getSize(ast);
+ (path, data) => {
+ const astSize = getSize(data.ast);
const size = getSize(path.node);
return size * multiplier > astSize;
diff --git a/packages/slimfast-node/src/slimfast/visitors/utils/contraints/should-ignore/index.test.ts b/packages/slimfast-node/src/slimfast/visitors/utils/contraints/should-ignore/index.test.ts
index d48727f..45097d0 100644
--- a/packages/slimfast-node/src/slimfast/visitors/utils/contraints/should-ignore/index.test.ts
+++ b/packages/slimfast-node/src/slimfast/visitors/utils/contraints/should-ignore/index.test.ts
@@ -16,7 +16,6 @@ describe('Should Ignore', () => {
}`;
const ast = parser(code);
let rootPath: NodePath | null = null;
- const data = {};
traverse(ast, {
Program(path) {
@@ -25,7 +24,7 @@ describe('Should Ignore', () => {
},
});
if (rootPath !== null) {
- const result = shouldIgnore(rootPath, data, {}, ast);
+ const result = shouldIgnore(rootPath);
expect(result).toBe(true);
}
});
@@ -39,7 +38,6 @@ describe('Should Ignore', () => {
}`;
const ast = parser(code);
let rootPath: NodePath | null = null;
- const data = {};
traverse(ast, {
Program(path) {
@@ -48,7 +46,7 @@ describe('Should Ignore', () => {
},
});
if (rootPath !== null) {
- const result = shouldIgnore(rootPath, data, {}, ast);
+ const result = shouldIgnore(rootPath);
expect(result).toBe(true);
}
});
@@ -58,7 +56,6 @@ describe('Should Ignore', () => {
4 * 7`;
const ast = parser(code);
let rootPath: NodePath | null = null;
- const data = {};
traverse(ast, {
Program(path) {
@@ -67,7 +64,7 @@ describe('Should Ignore', () => {
},
});
if (rootPath !== null) {
- const result = shouldIgnore(rootPath, data, {}, ast);
+ const result = shouldIgnore(rootPath);
expect(result).toBe(false);
}
});
diff --git a/packages/slimfast-node/src/slimfast/visitors/utils/contraints/should-ignore/index.ts b/packages/slimfast-node/src/slimfast/visitors/utils/contraints/should-ignore/index.ts
index 274d251..329c0fd 100644
--- a/packages/slimfast-node/src/slimfast/visitors/utils/contraints/should-ignore/index.ts
+++ b/packages/slimfast-node/src/slimfast/visitors/utils/contraints/should-ignore/index.ts
@@ -1,7 +1,6 @@
import Traverse from '@babel/traverse';
-import type { RandomObject } from '../../../../../types';
-import type { NodePath, Node } from '@babel/traverse';
+import type { Constraint } from '../../../../../types';
/**
* Determines if a given AST node path contains either a `Super` or a `YieldExpression` node.
@@ -11,23 +10,15 @@ import type { NodePath, Node } from '@babel/traverse';
* that the original node should be ignored in any subsequent operations or analyses.
*
* @param path - The AST node path to be examined.
- * @param data - Information or context related to the node.
- * @param opts - Configuration options influencing the check.
- * @param ast - The complete Abstract Syntax Tree.
* @returns `true` if the node contains either a `Super` or `YieldExpression` node, otherwise `false`.
*
* @example
- * const nodeShouldBeIgnored = shouldIgnore(nodePath, data, opts, ast);
+ * const nodeShouldBeIgnored = shouldIgnore(nodePath);
* if (nodeShouldBeIgnored) {
* // Skip processing or analysis for this node
* }
*/
-export function shouldIgnore(
- path: NodePath,
- data: RandomObject,
- opts: RandomObject,
- ast: Node
-) {
+export const shouldIgnore: Constraint = (path) => {
let shouldNot = false;
const visitor = {
@@ -42,4 +33,4 @@ export function shouldIgnore(
Traverse(path.node, visitor, path.scope, path.parentPath);
return shouldNot;
-}
+};
diff --git a/packages/slimfast-node/src/slimfast/visitors/utils/contraints/too-small/index.test.ts b/packages/slimfast-node/src/slimfast/visitors/utils/contraints/too-small/index.test.ts
index 0c953e9..4c2f1c8 100644
--- a/packages/slimfast-node/src/slimfast/visitors/utils/contraints/too-small/index.test.ts
+++ b/packages/slimfast-node/src/slimfast/visitors/utils/contraints/too-small/index.test.ts
@@ -11,7 +11,10 @@ describe('Too Small', () => {
const code = `let result = ((2 + 3) * 4 - Math.sqrt(9)) / (6 % 2) + Math.pow(2, 5) - parseFloat('10.5') + parseInt('100', 2);`;
const ast = parser(code);
let rootPath: NodePath | null = null;
- const data = {};
+ const data = {
+ toInject: [],
+ toImport: [],
+ };
traverse(ast, {
Expression(path) {
@@ -22,21 +25,21 @@ describe('Too Small', () => {
test('', () => {
if (rootPath !== null) {
extractIdentifiers(rootPath, data);
- const result = tooSmall(2, 50, true)(rootPath, data, {}, ast);
+ const result = tooSmall(2, 50, true)(rootPath, data);
expect(result).toBe(false);
}
});
test('', () => {
if (rootPath !== null) {
extractIdentifiers(rootPath, data);
- const result = tooSmall(4, 50, true)(rootPath, data, {}, ast);
+ const result = tooSmall(4, 50, true)(rootPath, data);
expect(result).toBe(false);
}
});
test('', () => {
if (rootPath !== null) {
extractIdentifiers(rootPath, data);
- const result = tooSmall(1, 400, true)(rootPath, data, {}, ast);
+ const result = tooSmall(1, 400, true)(rootPath, data);
expect(result).toBe(true);
}
});
diff --git a/packages/slimfast-node/src/slimfast/visitors/utils/contraints/too-small/index.ts b/packages/slimfast-node/src/slimfast/visitors/utils/contraints/too-small/index.ts
index c367356..afa8461 100644
--- a/packages/slimfast-node/src/slimfast/visitors/utils/contraints/too-small/index.ts
+++ b/packages/slimfast-node/src/slimfast/visitors/utils/contraints/too-small/index.ts
@@ -1,5 +1,5 @@
-import type { RandomObject } from '../../../../../types';
-import type { NodePath, Node } from '@babel/traverse';
+import type { ConstraintWithData, RandomObject } from '../../../../../types';
+import type { Node } from '@babel/traverse';
const notANumber = (num: number | null | undefined): boolean =>
num === null || num === undefined || Number.isNaN(num);
@@ -20,21 +20,23 @@ const getSize = (node: Node): number => {
* @returns A function that evaluates if a node path is "too small".
* @example
* const isNodeTooSmall = tooSmall(1.5, 50, true);
- * const small = isNodeTooSmall(nodePath, data, opts, ast);
+ * const result = isNodeTooSmall(nodePath, data);
* // Returns true if node is too small.
*/
-export const tooSmall =
- (multiplier: number, minLength: number, measureIdentifiers?: Boolean) =>
+export const tooSmall: (
+ multiplier: number,
+ minLength: number,
+ measureIdentifiers?: Boolean
+) => ConstraintWithData<'toInject'> =
+ (multiplier, minLength, measureIdentifiers) =>
/**
* Evaluates if an AST node path is "too small" based on its size and associated identifiers.
*
* @param path - The AST node path under evaluation.
- * @param data - Information about the node, especially `toInject` which lists associated identifiers.
- * @param opts - Configuration options influencing the check.
- * @param ast - The complete Abstract Syntax Tree.
+ * @param data - Information or context related to the node. Specifically, contains the identifiers to be injected in the `toInject` key.
* @returns `true` if the node path is "too small", otherwise `false`.
*/
- (path: NodePath, data: RandomObject, opts: RandomObject, ast: Node) => {
+ (path, data) => {
multiplier = multiplier || 1.5;
minLength = minLength || 50;
measureIdentifiers = measureIdentifiers || true;
diff --git a/packages/slimfast-node/src/slimfast/visitors/utils/extract-identifiers/index.test.ts b/packages/slimfast-node/src/slimfast/visitors/utils/extract-identifiers/index.test.ts
index a4442b2..141d26e 100644
--- a/packages/slimfast-node/src/slimfast/visitors/utils/extract-identifiers/index.test.ts
+++ b/packages/slimfast-node/src/slimfast/visitors/utils/extract-identifiers/index.test.ts
@@ -4,7 +4,6 @@ import { describe, expect, test } from 'vitest';
import { extractIdentifiers } from './index';
import { parser } from '../parser';
-import type { RandomObject } from '../../../../types';
import type { NodePath, Binding } from '@babel/traverse';
const str = JSON.stringify;
@@ -23,7 +22,10 @@ describe('Extract Identifiers', () => {
},
});
if (rootPath !== null) {
- const data: RandomObject = {};
+ const data = {
+ toInject: [],
+ toImport: [],
+ };
extractIdentifiers(rootPath, data);
const variables = data.toInject.map((x: Binding) => x.identifier.name);
expect(str(variables)).toBe(str(['x', 'e', 'o']));
@@ -41,7 +43,10 @@ describe('Extract Identifiers', () => {
},
});
if (rootPath !== null) {
- const data: RandomObject = {};
+ const data = {
+ toInject: [],
+ toImport: [],
+ };
extractIdentifiers(rootPath, data);
const imports = data.toImport.map((x: Binding) => x.identifier.name);
expect(str(imports)).toBe(str(['x']));
diff --git a/packages/slimfast-node/src/slimfast/visitors/utils/extract-identifiers/index.ts b/packages/slimfast-node/src/slimfast/visitors/utils/extract-identifiers/index.ts
index a77e29e..46837e5 100644
--- a/packages/slimfast-node/src/slimfast/visitors/utils/extract-identifiers/index.ts
+++ b/packages/slimfast-node/src/slimfast/visitors/utils/extract-identifiers/index.ts
@@ -1,7 +1,7 @@
import unique from 'array-unique';
-import type { RandomObject } from '../../../../types';
-import type { NodePath, Node, Binding } from '@babel/traverse';
+import type { ConstraintData, RandomObject } from '../../../../types';
+import type { NodePath, Binding } from '@babel/traverse';
const importTypes = ['ImportDefaultSpecifier', 'ImportSpecifier'];
const isImportStatement = (x: Binding) =>
@@ -33,15 +33,16 @@ const buildBinding = (name: string, binding: Binding): Binding => {
* These categorized bindings are added to the provided `data` object under the respective keys.
*
* @param path - The starting AST node path to be traversed.
- * @param data - An object to which the categorized bindings will be added.
- * @param opts - Optional configuration options.
- * @param ast - The entire AST, if available.
+ * @param data - An object to which the categorized bindings will be added. The data should contain keys 'toImport' and 'toInject'.
* @returns This function modifies the `data` object in-place and doesn't return a value.
*
* @example
* const code = `import React from 'react'; const greet = (name) => "Hello, " + name;`;
* const ast = parser(code);
- * const data = {};
+ * const data = {
+ * toImport: [],
+ * toInject: [],
+ * };
* traverse(ast, {
* Program(path) {
* extractIdentifiers(path, data);
@@ -51,9 +52,7 @@ const buildBinding = (name: string, binding: Binding): Binding => {
*/
export const extractIdentifiers = (
path: NodePath,
- data: RandomObject,
- opts?: RandomObject,
- ast?: Node
+ data: ConstraintData<'toImport' | 'toInject'>
) => {
const identifiers: Binding[] = [];
@@ -70,6 +69,7 @@ export const extractIdentifiers = (
const toImport: Binding[] = identifiers.filter(isImportStatement);
const toInject: Binding[] = identifiers.filter(isVariableDeclaration);
+ // TODO: are these used?
data.toImport = unique(toImport);
data.toInject = unique(toInject);
diff --git a/packages/slimfast-node/src/slimfast/visitors/visitor/index.ts b/packages/slimfast-node/src/slimfast/visitors/visitor/index.ts
index 3ea003f..b936775 100644
--- a/packages/slimfast-node/src/slimfast/visitors/visitor/index.ts
+++ b/packages/slimfast-node/src/slimfast/visitors/visitor/index.ts
@@ -3,7 +3,7 @@ import traverse from '@babel/traverse';
import { notInExtracted } from '../utils/not-in-extracted';
import { rejectParentsWithTypes } from '../utils/reject-parents-with-types';
-import type { RandomObject } from '../../../types';
+import type { Constraints, ConstraintData, RandomObject } from '../../../types';
import type { NodePath, Node } from '@babel/traverse';
/**
@@ -19,7 +19,7 @@ export class Visitor {
/**
* The abstract syntax tree that the Visitor will traverse.
*/
- ast: Node | undefined;
+ ast: Node;
/**
* Options passed to the Visitor.
@@ -44,7 +44,7 @@ export class Visitor {
* const visitor = new Visitor(ast, options, state, extractedNodes);
*/
constructor(
- ast: Node | undefined,
+ ast: Node,
opts: RandomObject,
state: RandomObject,
extracted: Map
@@ -78,7 +78,7 @@ export class Visitor {
* return [isIdentifier];
* }
*/
- constraints(): Function[] {
+ constraints(): Constraints {
return [];
}
@@ -110,10 +110,11 @@ export class Visitor {
* @returns Returns `true` if the node passes all constraints, otherwise `false`.
*
*/
- passesContraints(path: NodePath, data: RandomObject): Boolean {
+ passesContraints(path: NodePath, data: ConstraintData): Boolean {
if (
- this.constraints().some((constraint: Function) =>
- constraint(path, data, this.opts, this.ast)
+ this.constraints().some((constraint) =>
+ // constraint(path, data, this.opts, this.ast)
+ constraint(path, data)
)
) {
return false;
@@ -159,7 +160,11 @@ export class Visitor {
let parent: NodePath | null = path;
while (parent) {
- const data: RandomObject = {};
+ const data: ConstraintData = {
+ toImport: this.opts.toImport,
+ toInject: this.opts.toInject,
+ ast: this.ast,
+ };
if (this.passesContraints(parent, data)) {
this.extracted.set(parent, data);
return;
diff --git a/packages/slimfast-node/src/types.ts b/packages/slimfast-node/src/types.ts
index 3141d89..3fd767d 100644
--- a/packages/slimfast-node/src/types.ts
+++ b/packages/slimfast-node/src/types.ts
@@ -1,8 +1,98 @@
-import type { Statement } from '@babel/types';
+import type { Binding, NodePath } from '@babel/traverse';
+import type { Node, Statement } from '@babel/types';
import type { CodebaseOpts } from '@modular-rocks/workspace-node/dist/types/types';
export type RandomObject = Record;
+/**
+ * Type that ensures that all properties K of T are present.
+ *
+ * @template T - The base type from which properties are extracted.
+ * @template K - A subset of the keys from T.
+ *
+ * @example
+ * type Data = { toInject: Binding[], ast: Node };
+ * type RequiredData = MandatoryKeys;
+ * // { toInject: Binding[] }
+ */
+type MandatoryKeys = {
+ [P in K]: T[P];
+};
+
+/**
+ * Type that checks if a given type T is a key of {@link ConstraintData}. If it's not, it resolves to never.
+ *
+ * @template T - The type to check against ConstraintData.
+ *
+ * @example
+ * type MyKey = 'toInject';
+ * type CheckedKey = IsAnyKey; // 'toInject'
+ */
+type IsAnyKey = T extends keyof ConstraintData ? T : never;
+
+/**
+ * Represents a constraint function that takes an AST node path and returns a boolean.
+ *
+ * @param path - The Abstract Syntax Tree (AST) node path.
+ * @returns A boolean indicating if the provided path meets the constraint.
+ */
+export type Constraint = (path: NodePath) => boolean;
+
+/**
+ * Represents a constraint function that takes an AST node path and additional constraint data.
+ * The constraint data keys can be customized but must be a subset of {@link ConstraintData}.
+ *
+ * By default, it considers all keys of {@link ConstraintData}.
+ *
+ * @template K - A subset of the keys from ConstraintData. Default is all keys from ConstraintData.
+ * @param path - The Abstract Syntax Tree (AST) node path.
+ * @param data - The additional constraint data associated with the node path.
+ * @returns A boolean indicating if the provided path and data meet the constraint.
+ *
+ * @example
+ * const constraintFunction: ConstraintWithData<'toInject'> = (path, data) => {
+ * const bindings = data.toInject;
+ * // Perform checks using the `bindings` and return a boolean.
+ * return true;
+ * };
+ */
+export type ConstraintWithData<
+ K extends keyof ConstraintData = keyof ConstraintData,
+> = (
+ path: NodePath,
+ data: MandatoryKeys>
+) => boolean;
+
+/**
+ * Default data structure that contains properties commonly used in constraints.
+ */
+type DefaultConstraintData = {
+ toInject: Binding[];
+ toImport: Binding[];
+ ast: Node;
+};
+
+/**
+ * Represents constraint data with a custom subset of {@link DefaultConstraintData} keys.
+ *
+ * By default, it includes all keys of {@link DefaultConstraintData}.
+ *
+ * @template K - A subset of the keys from DefaultConstraintData. Default is all keys from DefaultConstraintData.
+ *
+ * @example
+ * type MyConstraintData = ConstraintData<'toInject' | 'ast'>;
+ * // { toInject: Binding[], ast: Node }
+ */
+export type ConstraintData<
+ K extends keyof DefaultConstraintData = keyof DefaultConstraintData,
+> = MandatoryKeys;
+
+/**
+ * Represents an array of constraint functions, with or without options,
+ * used to validate and process AST node paths in a specific context.
+ */
+export type Constraints = (Constraint | ConstraintWithData)[];
+
export type SlimFastOpts = CodebaseOpts & {
visitors?: any[];
namer?: Function;