comment-variables 0.6.0 → 0.6.1
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/comments.config.js
CHANGED
|
@@ -92,7 +92,7 @@ const data = {
|
|
|
92
92
|
findAllImports:
|
|
93
93
|
"The complete set of strings of import paths recursively related to the given file path in a success object (`success: true`). Errors are bubbled up during failures in a failure object (`success: false`).", // $COMMENT#JSDOC#RETURNS#FINDALLIMPORTS
|
|
94
94
|
processImport:
|
|
95
|
-
"
|
|
95
|
+
"The results of the embedded round of `findAllImports`, since `findAllImports`'s recursion happens within `processImport`.", // $COMMENT#JSDOC#RETURNS#PROCESSIMPORT
|
|
96
96
|
flattenConfigData:
|
|
97
97
|
"Both the flattened config data and its reversed version to ensure the strict reversibility of the `resolve` and `compress` commands in a success object (`success: true`). Errors are bubbled up during failures so they can be reused differently on the CLI and the VS Code Extension in a failure object (`success: false`).", // $COMMENT#JSDOC#RETURNS#FLATTENCONFIGDATA
|
|
98
98
|
resolveConfig:
|
package/library/index.js
CHANGED
|
@@ -18,7 +18,7 @@ import {
|
|
|
18
18
|
|
|
19
19
|
import { exitDueToFailure, logError } from "./_commons/utilities/helpers.js";
|
|
20
20
|
import { resolveConfig } from "./_commons/utilities/resolve-config.js"; // shared
|
|
21
|
-
import { findAllImports } from "
|
|
21
|
+
import { findAllImports } from "find-all-js-imports"; // own package
|
|
22
22
|
import {
|
|
23
23
|
resolveCommentsFlow,
|
|
24
24
|
compressCommentsFlow,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "comment-variables",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.1",
|
|
4
4
|
"description": "A CLI tool for configuring, managing and maintaining JavaScript comments as JavaScript variables.",
|
|
5
5
|
"bin": {
|
|
6
6
|
"jscomments": "./library/index.js",
|
|
@@ -27,8 +27,7 @@
|
|
|
27
27
|
"dependencies": {
|
|
28
28
|
"@eslint/markdown": "^6.5.0",
|
|
29
29
|
"eslint": "^9.29.0",
|
|
30
|
-
"
|
|
31
|
-
"resolve-importing-path": "^1.0.3",
|
|
30
|
+
"find-all-js-imports": "^1.0.0",
|
|
32
31
|
"tsconfig-paths": "^4.2.0",
|
|
33
32
|
"typescript-eslint": "^8.34.1",
|
|
34
33
|
"zod": "^3.25.67"
|
|
@@ -1,163 +0,0 @@
|
|
|
1
|
-
import fs from "fs";
|
|
2
|
-
import path from "path";
|
|
3
|
-
|
|
4
|
-
import { resolveImportingPath } from "resolve-importing-path";
|
|
5
|
-
import { getSourceCodeFromFilePath } from "get-sourcecode-from-file-path";
|
|
6
|
-
|
|
7
|
-
import { successFalse, successTrue, typeWarning } from "../constants/bases.js";
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* @typedef {{
|
|
11
|
-
* success: false;
|
|
12
|
-
* errors: Array<{ message: string; type: "warning";}>;
|
|
13
|
-
* } | {
|
|
14
|
-
* success: true;
|
|
15
|
-
* visitedSet: Set<string>;
|
|
16
|
-
* }} FindAllImportsResults
|
|
17
|
-
*/
|
|
18
|
-
|
|
19
|
-
// IMPORTANT. findAllImports needs to be able to take a callback function that it can play at every recursion to find the corresponding value for go-to-definitions. But that's on the roadmap, not in the first release. The first implementation of this pinpoint go-to-definition mechanism will be made by analyzing each path obtained rather than by doing so as the paths are being obtained.
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Processes recursively and resolves a single import path. (Unlike `findAllImports`, here `currentDir`, `cwd`, `visitedSet`, `depth`, and `maxDepth` aren't options because they are mandatory and not pre-parameterized.)
|
|
23
|
-
* @param {string} importPath The import path currently being addressed.
|
|
24
|
-
* @param {Object} settings The required settings as follows:
|
|
25
|
-
* @param {string} settings.currentDir The directory containing the import path currently being addressed.
|
|
26
|
-
* @param {string} settings.cwd The current working directory.
|
|
27
|
-
* @param {Set<string>} settings.visitedSet The set of strings tracking the import paths that have already been visited.
|
|
28
|
-
* @param {number} settings.depth The current depth of the recursion.
|
|
29
|
-
* @param {number} settings.maxDepth The maximum depth allowed for the recursion.
|
|
30
|
-
* @returns `true` to continue to the next operation, `false` to stop the whole `findAllImports` process. //
|
|
31
|
-
*/
|
|
32
|
-
const processImport = (
|
|
33
|
-
importPath,
|
|
34
|
-
{ currentDir, cwd, visitedSet, depth, maxDepth }
|
|
35
|
-
) => {
|
|
36
|
-
// Resolves the provided import path.
|
|
37
|
-
const resolvedPath = resolveImportingPath(currentDir, importPath, cwd);
|
|
38
|
-
// Returns true early to skip processing on unresolved paths.
|
|
39
|
-
if (!resolvedPath) return { ...successTrue, visitedSet };
|
|
40
|
-
|
|
41
|
-
// Establishes the options for the next round of findAllImports.
|
|
42
|
-
const findAllImportsOptions = {
|
|
43
|
-
cwd,
|
|
44
|
-
visitedSet,
|
|
45
|
-
depth: depth + 1,
|
|
46
|
-
maxDepth,
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
// Runs findAllImports on the imported path resolved, thus recursively.
|
|
50
|
-
const findAllImportsResults = /** @type {FindAllImportsResults} */ (
|
|
51
|
-
findAllImports(resolvedPath, findAllImportsOptions)
|
|
52
|
-
);
|
|
53
|
-
// Returns true if the round of findAllImports succeeded, false if it failed.
|
|
54
|
-
return findAllImportsResults;
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Finds all import paths recursively related to a given file path.
|
|
59
|
-
* @param {string} filePath The absolute path of the file whose imports are being recursively found, such as that of a project's `comments.config.js` file.
|
|
60
|
-
* @param {Object} options The additional options as follows:
|
|
61
|
-
* @param {string} [options.cwd] The current working directory, set as `process.cwd()` by default.
|
|
62
|
-
* @param {Set<string>} [options.visitedSet] The current working directory, set as `process.cwd()` by default.
|
|
63
|
-
* @param {number} [options.depth] The current depth of the recursion, instantiated at `0` by default.
|
|
64
|
-
* @param {number} [options.maxDepth] The maximum depth allowed for the recursion, instantiated at `100` by default.
|
|
65
|
-
* @returns The complete set of strings of import paths recursively related to the given file path in a success object (`success: true`). Errors are bubbled up during failures in a failure object (`success: false`).
|
|
66
|
-
*/
|
|
67
|
-
export const findAllImports = (
|
|
68
|
-
filePath,
|
|
69
|
-
{
|
|
70
|
-
cwd = process.cwd(),
|
|
71
|
-
visitedSet = new Set(),
|
|
72
|
-
depth = 0,
|
|
73
|
-
maxDepth = 100,
|
|
74
|
-
} = {}
|
|
75
|
-
) => {
|
|
76
|
-
// Fails early if max depth is recursively reached.
|
|
77
|
-
if (depth > maxDepth) {
|
|
78
|
-
return {
|
|
79
|
-
...successFalse,
|
|
80
|
-
errors: [
|
|
81
|
-
{
|
|
82
|
-
...typeWarning,
|
|
83
|
-
message: `WARNING. Max depth ${maxDepth} reached at ${filePath}.`,
|
|
84
|
-
},
|
|
85
|
-
],
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
|
-
// Fails early if no file is found.
|
|
89
|
-
if (!fs.existsSync(filePath)) {
|
|
90
|
-
return {
|
|
91
|
-
...successFalse,
|
|
92
|
-
errors: [
|
|
93
|
-
{
|
|
94
|
-
...typeWarning,
|
|
95
|
-
message: `WARNING. File not found at ${filePath}.`,
|
|
96
|
-
},
|
|
97
|
-
],
|
|
98
|
-
};
|
|
99
|
-
}
|
|
100
|
-
// Returns the existing set directly if a path has already been visited.
|
|
101
|
-
if (visitedSet.has(filePath)) {
|
|
102
|
-
return { ...successTrue, visitedSet };
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
// Updates the visited set.
|
|
106
|
-
visitedSet.add(filePath);
|
|
107
|
-
|
|
108
|
-
// Parses the file's source code AST.
|
|
109
|
-
const sourceCode = getSourceCodeFromFilePath(filePath);
|
|
110
|
-
// Fails early there is no AST.
|
|
111
|
-
if (!sourceCode?.ast) {
|
|
112
|
-
return {
|
|
113
|
-
...successFalse,
|
|
114
|
-
errors: [
|
|
115
|
-
{
|
|
116
|
-
...typeWarning,
|
|
117
|
-
message: `WARNING. Failed to parse AST for ${filePath}.`,
|
|
118
|
-
},
|
|
119
|
-
],
|
|
120
|
-
};
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
// Makes the joint settings for the conditional calls of processImport.
|
|
124
|
-
const processImportSettings = {
|
|
125
|
-
currentDir: path.dirname(filePath),
|
|
126
|
-
cwd,
|
|
127
|
-
visitedSet,
|
|
128
|
-
depth,
|
|
129
|
-
maxDepth,
|
|
130
|
-
};
|
|
131
|
-
|
|
132
|
-
// Processes all imports.
|
|
133
|
-
for (const node of sourceCode.ast.body) {
|
|
134
|
-
// ES Modules (import x from 'y')
|
|
135
|
-
if (node.type === "ImportDeclaration") {
|
|
136
|
-
const processImportResults = processImport(
|
|
137
|
-
node.source.value,
|
|
138
|
-
processImportSettings
|
|
139
|
-
);
|
|
140
|
-
if (!processImportResults) {
|
|
141
|
-
return processImportResults;
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
// CommonJS (require('x'))
|
|
146
|
-
if (
|
|
147
|
-
node.type === "ExpressionStatement" &&
|
|
148
|
-
node.expression.type === "CallExpression" &&
|
|
149
|
-
node.expression.callee.name === "require" &&
|
|
150
|
-
node.expression.arguments[0]?.type === "Literal"
|
|
151
|
-
) {
|
|
152
|
-
const processImportResults = processImport(
|
|
153
|
-
node.expression.arguments[0].value,
|
|
154
|
-
processImportSettings
|
|
155
|
-
);
|
|
156
|
-
if (!processImportResults) {
|
|
157
|
-
return processImportResults;
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
return { ...successTrue, visitedSet };
|
|
163
|
-
};
|