casper-context 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +253 -0
- package/dist/index.js +9 -0
- package/dist/lifecycle/post.js +239 -0
- package/dist/lifecycle/pre.js +113 -0
- package/dist/plugin.js +57 -0
- package/dist/transforms/autoContextTransform.js +1 -0
- package/dist/transforms/contextTransform.js +1 -0
- package/dist/transforms/stateTransform.js +1 -0
- package/dist/types/plugin.d.js +1 -0
- package/dist/utils/astHelpers.js +644 -0
- package/dist/utils/constants.js +111 -0
- package/dist/utils/names.js +1 -0
- package/dist/utils/scope.js +1 -0
- package/dist/utils/utilityHelpers.js +606 -0
- package/dist/visitors/AssignmentExpression.js +104 -0
- package/dist/visitors/CallExpression.js +1 -0
- package/dist/visitors/FunctionDeclaration.js +116 -0
- package/dist/visitors/Identifier.js +123 -0
- package/dist/visitors/JSXElement.js +1 -0
- package/dist/visitors/Program.js +278 -0
- package/dist/visitors/ReturnStatement.js +81 -0
- package/dist/visitors/VariableDeclaration.js +209 -0
- package/package.json +60 -0
- package/src/index.js +2 -0
- package/src/lifecycle/post.js +237 -0
- package/src/lifecycle/pre.js +103 -0
- package/src/plugin.js +51 -0
- package/src/transforms/autoContextTransform.js +0 -0
- package/src/transforms/contextTransform.js +0 -0
- package/src/transforms/stateTransform.js +0 -0
- package/src/types/plugin.d.ts +0 -0
- package/src/utils/astHelpers.js +767 -0
- package/src/utils/constants.js +102 -0
- package/src/utils/names.js +0 -0
- package/src/utils/scope.js +0 -0
- package/src/utils/utilityHelpers.js +636 -0
- package/src/visitors/AssignmentExpression.js +100 -0
- package/src/visitors/CallExpression.js +0 -0
- package/src/visitors/FunctionDeclaration.js +114 -0
- package/src/visitors/Identifier.js +142 -0
- package/src/visitors/JSXElement.js +0 -0
- package/src/visitors/Program.js +280 -0
- package/src/visitors/ReturnStatement.js +75 -0
- package/src/visitors/VariableDeclaration.js +216 -0
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview File System Orchestration for Casper Context.
|
|
3
|
+
* This module manages the lifecycle of the generated context files, handling
|
|
4
|
+
* the reading, writing, and directory verification required to sync the
|
|
5
|
+
* Babel transformation with the physical project structure.
|
|
6
|
+
*/
|
|
7
|
+
import fs from 'fs';
|
|
8
|
+
import path from 'path';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Path & State Dependencies
|
|
12
|
+
* @description
|
|
13
|
+
* - CONTEXT_FILE_PATH: The absolute resolved destination for the generated React Context.
|
|
14
|
+
* - _CCTX_EMPTY: Used as a safe fallback or initializer for file content buffers.
|
|
15
|
+
*/
|
|
16
|
+
import { CONTEXT_FILE_PATH } from '../utils/utilityHelpers';
|
|
17
|
+
import { _CCTX_EMPTY } from '../utils/constants';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* @module Logger
|
|
21
|
+
* @description Provides a controlled logging interface for the Babel transformation process.
|
|
22
|
+
*/
|
|
23
|
+
import { log } from '../utils/utilityHelpers';
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @important
|
|
27
|
+
* All file operations in this module should prioritize synchronous methods (`fs.writeFileSync`, etc.)
|
|
28
|
+
* during the Babel build-step to ensure the file system is in sync before the
|
|
29
|
+
* bundling process (Webpack/Vite) begins.
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Initializes the scope context file and directory for Casper context tracking.
|
|
34
|
+
*
|
|
35
|
+
* This function ensures that the directory containing the context file exists,
|
|
36
|
+
* and then creates an empty context file at `CONTEXT_FILE_PATH` if it does not
|
|
37
|
+
* already exist. It is intended to prepare the filesystem for storing generated
|
|
38
|
+
* React context instances and related metadata.
|
|
39
|
+
*
|
|
40
|
+
* @returns {void}
|
|
41
|
+
* This function does not return a value; its effect is purely filesystem-based.
|
|
42
|
+
*
|
|
43
|
+
* @important
|
|
44
|
+
* - Uses `fs.mkdirSync` with `{ recursive: true }` to create parent directories as needed.
|
|
45
|
+
* - Writes an empty file at `CONTEXT_FILE_PATH` with the `{ flag: 'wx' }` option,
|
|
46
|
+
* which means the file is only created if it does not already exist.
|
|
47
|
+
* - Errors are silently caught, so existing files or directories will not cause failures.
|
|
48
|
+
* - Intended to be called before processing files (`preProcess`) or generating context content.
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```js
|
|
52
|
+
* createScopeContext();
|
|
53
|
+
* // Ensures the context directory and empty context file exist
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
function createScopeContext() {
|
|
57
|
+
try {
|
|
58
|
+
const dirPath = path.dirname(CONTEXT_FILE_PATH);
|
|
59
|
+
fs.mkdirSync(dirPath, { recursive: true });
|
|
60
|
+
fs.writeFileSync(CONTEXT_FILE_PATH, _CCTX_EMPTY, { flag: 'wx' });
|
|
61
|
+
} catch (e) {
|
|
62
|
+
log('error', '[::createScopeContext::]', e.message);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Performs pre-processing steps before AST transformations on a file.
|
|
68
|
+
*
|
|
69
|
+
* This function sets up the necessary runtime or plugin context before
|
|
70
|
+
* any file transformations occur. Currently, it calls `createScopeContext()`
|
|
71
|
+
* to initialize the global scope or virtual registry for tracking component
|
|
72
|
+
* variables and contexts.
|
|
73
|
+
*
|
|
74
|
+
* @param {string} file - The path of the file that will be processed. Currently unused,
|
|
75
|
+
* but provided for future enhancements or logging purposes.
|
|
76
|
+
* @param {Object} t - Babel types helper (`@babel/types`) used for AST operations.
|
|
77
|
+
*
|
|
78
|
+
* @returns {void}
|
|
79
|
+
* This function does not return a value; its effect is to initialize internal
|
|
80
|
+
* context structures for later processing.
|
|
81
|
+
*
|
|
82
|
+
* @important
|
|
83
|
+
* - Errors are silently caught; consider adding logging for debugging.
|
|
84
|
+
* - Currently, the function only calls `createScopeContext()`, but can be
|
|
85
|
+
* extended for additional pre-processing tasks.
|
|
86
|
+
* - The `file` parameter is not used internally but may be relevant in
|
|
87
|
+
* future versions for file-specific setup.
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* ```js
|
|
91
|
+
* import preProcess from './preProcess';
|
|
92
|
+
*
|
|
93
|
+
* preProcess('src/App.js', t);
|
|
94
|
+
* // Initializes context for virtual registry and component tracking
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
97
|
+
export default function preProcess(file, t) {
|
|
98
|
+
try {
|
|
99
|
+
createScopeContext()
|
|
100
|
+
} catch (e) {
|
|
101
|
+
log('error', '[::preProcess::]', e.message);
|
|
102
|
+
}
|
|
103
|
+
}
|
package/src/plugin.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { PLUGIN_NAME } from './utils/constants';
|
|
2
|
+
import preProcess from './lifecycle/pre';
|
|
3
|
+
import postProcess from './lifecycle/post';
|
|
4
|
+
import variableDeclarationVisitor from './visitors/VariableDeclaration';
|
|
5
|
+
import assignmentExpressionVisitor from './visitors/AssignmentExpression';
|
|
6
|
+
import identifierVisitor from './visitors/Identifier';
|
|
7
|
+
import { programExit, programEnter } from './visitors/Program';
|
|
8
|
+
import { functionDeclarationExit } from './visitors/FunctionDeclaration';
|
|
9
|
+
import { readCasperConfig } from './utils/utilityHelpers';
|
|
10
|
+
|
|
11
|
+
const virtualRegistry = {};
|
|
12
|
+
const prevVirtualRegistrySnap = {}
|
|
13
|
+
|
|
14
|
+
export default function ({ types: t }) {
|
|
15
|
+
const config = readCasperConfig();
|
|
16
|
+
const seen = new WeakSet();
|
|
17
|
+
return {
|
|
18
|
+
name: PLUGIN_NAME,
|
|
19
|
+
pre(file) {
|
|
20
|
+
preProcess.call(this, file, t);
|
|
21
|
+
},
|
|
22
|
+
visitor: {
|
|
23
|
+
Program: {
|
|
24
|
+
enter(path, state) {
|
|
25
|
+
programEnter.call(this, path, state, t, virtualRegistry, config);
|
|
26
|
+
},
|
|
27
|
+
exit(path, state) {
|
|
28
|
+
programExit.call(this, path, state, t);
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
VariableDeclarator (path, state) {
|
|
32
|
+
variableDeclarationVisitor.call(this, path, state, t, virtualRegistry);
|
|
33
|
+
},
|
|
34
|
+
AssignmentExpression (path, state) {
|
|
35
|
+
assignmentExpressionVisitor.call(this, path, state, t, virtualRegistry);
|
|
36
|
+
},
|
|
37
|
+
Identifier (path, state) {
|
|
38
|
+
identifierVisitor.call(this, path, state, t, seen, virtualRegistry);
|
|
39
|
+
},
|
|
40
|
+
FunctionDeclaration: {
|
|
41
|
+
exit (path, state) {
|
|
42
|
+
|
|
43
|
+
functionDeclarationExit.call(this, path, state, t, virtualRegistry);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
post(file) {
|
|
48
|
+
postProcess.call(this, file, t, virtualRegistry, prevVirtualRegistrySnap);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|